]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiSymbols.cpp
QDialogButtonBox for the remaining dialogs.
[lyx.git] / src / frontends / qt4 / GuiSymbols.cpp
1 /**
2  * \file GuiSymbols.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Spitzmüller
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiSymbols.h"
14
15 #include "GuiApplication.h"
16 #include "GuiView.h"
17 #include "qt_helpers.h"
18
19 #include "Buffer.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "Cursor.h"
23 #include "Encoding.h"
24 #include "FuncRequest.h"
25
26 #include "support/debug.h"
27 #include "support/gettext.h"
28
29 #include <QChar>
30 #include <QDialogButtonBox>
31 #include <QPushButton>
32 #include <QString>
33
34 #include <cstdio>
35
36 using namespace std;
37
38 namespace lyx {
39 namespace frontend {
40
41
42 namespace {
43
44 /// name of unicode block, start and end code point
45 struct UnicodeBlocks {
46         char const * name;
47         QString qname;
48         char_type start;
49         char_type end;
50 };
51
52
53 /// all unicode blocks with start and end code point
54 UnicodeBlocks unicode_blocks[] = {
55         { N_("Basic Latin"), QString(), 0x0000, 0x007f },
56         { N_("Latin-1 Supplement"), QString(), 0x0080, 0x00ff },
57         { N_("Latin Extended-A"), QString(), 0x0100, 0x017f },
58         { N_("Latin Extended-B"), QString(), 0x0180, 0x024f },
59         { N_("IPA Extensions"), QString(), 0x0250, 0x02af },
60         { N_("Spacing Modifier Letters"), QString(), 0x02b0, 0x02ff },
61         { N_("Combining Diacritical Marks"), QString(), 0x0300, 0x036f },
62         { N_("Greek"), QString(), 0x0370, 0x03ff },
63         { N_("Cyrillic"), QString(), 0x0400, 0x04ff },
64         { N_("Armenian"), QString(), 0x0530, 0x058f },
65         { N_("Hebrew"), QString(), 0x0590, 0x05ff },
66         { N_("Arabic"), QString(), 0x0600, 0x06ff },
67         { N_("Devanagari"), QString(), 0x0900, 0x097f },
68         { N_("Bengali"), QString(), 0x0980, 0x09ff },
69         { N_("Gurmukhi"), QString(), 0x0a00, 0x0a7f },
70         { N_("Gujarati"), QString(), 0x0a80, 0x0aff },
71         { N_("Oriya"), QString(), 0x0b00, 0x0b7f },
72         { N_("Tamil"), QString(), 0x0b80, 0x0bff },
73         { N_("Telugu"), QString(), 0x0c00, 0x0c7f },
74         { N_("Kannada"), QString(), 0x0c80, 0x0cff },
75         { N_("Malayalam"), QString(), 0x0d00, 0x0d7f },
76         { N_("Thai"), QString(), 0x0e00, 0x0e7f },
77         { N_("Lao"), QString(), 0x0e80, 0x0eff },
78         { N_("Tibetan"), QString(), 0x0f00, 0x0fbf },
79         { N_("Georgian"), QString(), 0x10a0, 0x10ff },
80         { N_("Hangul Jamo"), QString(), 0x1100, 0x11ff },
81         { N_("Phonetic Extensions"), QString(), 0x1d00, 0x1d7f },
82         { N_("Latin Extended Additional"), QString(), 0x1e00, 0x1eff },
83         { N_("Greek Extended"), QString(), 0x1f00, 0x1fff },
84         { N_("General Punctuation"), QString(), 0x2000, 0x206f },
85         { N_("Superscripts and Subscripts"), QString(), 0x2070, 0x209f },
86         { N_("Currency Symbols"), QString(), 0x20a0, 0x20cf },
87         { N_("Combining Diacritical Marks for Symbols"), QString(), 0x20d0, 0x20ff },
88         { N_("Letterlike Symbols"), QString(), 0x2100, 0x214f },
89         { N_("Number Forms"), QString(), 0x2150, 0x218f },
90         { N_("Arrows"), QString(), 0x2190, 0x21ff },
91         { N_("Mathematical Operators"), QString(), 0x2200, 0x22ff },
92         { N_("Miscellaneous Technical"), QString(), 0x2300, 0x23ff },
93         { N_("Control Pictures"), QString(), 0x2400, 0x243f },
94         { N_("Optical Character Recognition"), QString(), 0x2440, 0x245f },
95         { N_("Enclosed Alphanumerics"), QString(), 0x2460, 0x24ff },
96         { N_("Box Drawing"), QString(), 0x2500, 0x257f },
97         { N_("Block Elements"), QString(), 0x2580, 0x259f },
98         { N_("Geometric Shapes"), QString(), 0x25a0, 0x25ff },
99         { N_("Miscellaneous Symbols"), QString(), 0x2600, 0x26ff },
100         { N_("Dingbats"), QString(), 0x2700, 0x27bf },
101         { N_("Miscellaneous Mathematical Symbols-A"), QString(), 0x27c0, 0x27ef },
102         { N_("CJK Symbols and Punctuation"), QString(), 0x3000, 0x303f },
103         { N_("Hiragana"), QString(), 0x3040, 0x309f },
104         { N_("Katakana"), QString(), 0x30a0, 0x30ff },
105         { N_("Bopomofo"), QString(), 0x3100, 0x312f },
106         { N_("Hangul Compatibility Jamo"), QString(), 0x3130, 0x318f },
107         { N_("Kanbun"), QString(), 0x3190, 0x319f },
108         { N_("Enclosed CJK Letters and Months"), QString(), 0x3200, 0x32ff },
109         { N_("CJK Compatibility"), QString(), 0x3300, 0x33ff },
110         { N_("CJK Unified Ideographs"), QString(), 0x4e00, 0x9fa5 },
111         { N_("Hangul Syllables"), QString(), 0xac00, 0xd7a3 },
112         { N_("High Surrogates"), QString(), 0xd800, 0xdb7f },
113         { N_("Private Use High Surrogates"), QString(), 0xdb80, 0xdbff },
114         { N_("Low Surrogates"), QString(), 0xdc00, 0xdfff },
115         { N_("Private Use Area"), QString(), 0xe000, 0xf8ff },
116         { N_("CJK Compatibility Ideographs"), QString(), 0xf900, 0xfaff },
117         { N_("Alphabetic Presentation Forms"), QString(), 0xfb00, 0xfb4f },
118         { N_("Arabic Presentation Forms-A"), QString(), 0xfb50, 0xfdff },
119         { N_("Combining Half Marks"), QString(), 0xfe20, 0xfe2f },
120         { N_("CJK Compatibility Forms"), QString(), 0xfe30, 0xfe4f },
121         { N_("Small Form Variants"), QString(), 0xfe50, 0xfe6f },
122         { N_("Arabic Presentation Forms-B"), QString(), 0xfe70, 0xfeff },
123         { N_("Halfwidth and Fullwidth Forms"), QString(), 0xff00, 0xffef },
124         { N_("Specials"), QString(), 0xfff0, 0xffff },
125         { N_("Linear B Syllabary"), QString(), 0x10000, 0x1007f },
126         { N_("Linear B Ideograms"), QString(), 0x10080, 0x100ff },
127         { N_("Aegean Numbers"), QString(), 0x10100, 0x1013f },
128         { N_("Ancient Greek Numbers"), QString(), 0x10140, 0x1018f },
129         { N_("Old Italic"), QString(), 0x10300, 0x1032f },
130         { N_("Gothic"), QString(), 0x10330, 0x1034f },
131         { N_("Ugaritic"), QString(), 0x10380, 0x1039f },
132         { N_("Old Persian"), QString(), 0x103a0, 0x103df },
133         { N_("Deseret"), QString(), 0x10400, 0x1044f },
134         { N_("Shavian"), QString(), 0x10450, 0x1047f },
135         { N_("Osmanya"), QString(), 0x10480, 0x104af },
136         { N_("Cypriot Syllabary"), QString(), 0x10800, 0x1083f },
137         { N_("Kharoshthi"), QString(), 0x10a00, 0x10a5f },
138         { N_("Byzantine Musical Symbols"), QString(), 0x1d000, 0x1d0ff },
139         { N_("Musical Symbols"), QString(), 0x1d100, 0x1d1ff },
140         { N_("Ancient Greek Musical Notation"), QString(), 0x1d200, 0x1d24f },
141         { N_("Tai Xuan Jing Symbols"), QString(), 0x1d300, 0x1d35f },
142         { N_("Mathematical Alphanumeric Symbols"), QString(), 0x1d400, 0x1d7ff },
143         { N_("CJK Unified Ideographs Extension B"), QString(), 0x20000, 0x2a6d6 },
144         { N_("CJK Compatibility Ideographs Supplement"), QString(), 0x2f800, 0x2fa1f },
145         { N_("Tags"), QString(), 0xe0000, 0xe007f },
146         { N_("Variation Selectors Supplement"), QString(), 0xe0100, 0xe01ef },
147         { N_("Supplementary Private Use Area-A"), QString(), 0xf0000, 0xffffd },
148         { N_("Supplementary Private Use Area-B"), QString(), 0x100000, 0x10fffd }
149 };
150
151 const int no_blocks = sizeof(unicode_blocks) / sizeof(UnicodeBlocks);
152
153
154 QString getBlock(char_type c)
155 {
156         // store an educated guess for the next search
157         // 0 <= lastBlock < no_blocks
158         // FIXME THREAD
159         static int lastBlock = 0;
160
161         // "clever reset"
162         if (c < 0x7f)
163                 lastBlock = 0;
164
165         // c falls into a covered area, and we can guess which
166         if (c >= unicode_blocks[lastBlock].start
167             && c <= unicode_blocks[lastBlock].end)
168                 return unicode_blocks[lastBlock].qname;
169
170         // c falls into an uncovered area, but we can guess which
171         if (c > unicode_blocks[lastBlock].end
172             && c < unicode_blocks[lastBlock + 1].start)
173                 return QString();
174
175         // guessing was wrong so far. do a real search.
176         int i = 0;
177         while (i < no_blocks && c > unicode_blocks[i].end)
178                 ++i;
179
180         if (i == no_blocks)
181                 return QString();
182
183         if (c < unicode_blocks[i].start) {
184                 // 0 < i < no_blocks
185                 // cache the previous block for guessing next time
186                 lastBlock = i - 1;
187                 return QString();
188         }
189
190         // 0 <= i < no_blocks
191         // cache the last block for guessing next time
192         lastBlock = i;
193         return unicode_blocks[lastBlock].qname;
194 }
195
196
197 } // namespace
198
199
200 /////////////////////////////////////////////////////////////////////
201 //
202 // GuiSymbols::Model
203 //
204 /////////////////////////////////////////////////////////////////////
205
206 class GuiSymbols::Model : public QAbstractListModel
207 {
208 public:
209         Model(GuiSymbols * parent)
210                 : QAbstractListModel(parent), encoding_(0)
211         {}
212
213         QModelIndex index(int row, int column, QModelIndex const &) const
214         {
215                 return createIndex(row, column);
216         }
217
218         QModelIndex parent(QModelIndex const &) const
219         {
220                 return QModelIndex();
221         }
222
223         int rowCount(QModelIndex const &) const
224         {
225                 return symbols_.count();
226         }
227
228         QVariant data(QModelIndex const & index, int role) const
229         {
230                 if (!index.isValid())
231                         return QVariant();
232
233                 char_type c = symbols_.at(index.row());
234
235                 switch (role) {
236                 case Qt::TextAlignmentRole:
237                         return QVariant(Qt::AlignCenter);
238                 case Qt::DisplayRole:
239                         return toqstr(c);
240                 case Qt::ToolTipRole: {
241                         QString latex;
242                         if (encoding_) {
243                                 // how is the character output in the current encoding?
244                                 docstring const code = encoding_->latexChar(c).first;
245                                 // only show it when it is not coded by itself
246                                 if (code != docstring(1, c))
247                                         latex = qt_("<p>LaTeX code: %1</p>").arg(toqstr(code));
248                         }
249                         return formatToolTip(QString("<p align=center><span "
250                                                      "style=\"font-size: xx-large;\">%1"
251                                                      "</span><br>U+%2</p>%3")
252                                              .arg(toqstr(c))
253                                              .arg(QString("%1").arg(c, 0, 16).toUpper())
254                                              .arg(latex));
255                 }
256                 case Qt::SizeHintRole:
257                         // Fix many symbols not displaying in combination with
258                         // setUniformItemSizes
259                         return QSize(1000,1000);
260                 default:
261                         return QVariant();
262                 }
263         }
264
265         void setSymbols(QList<char_type> const & symbols, Encoding const * encoding)
266         {
267                 beginResetModel();
268                 symbols_ = symbols;
269                 encoding_ = encoding;
270                 endResetModel();
271         }
272
273 private:
274         friend class GuiSymbols;
275
276         QList<char_type> symbols_;
277         Encoding const * encoding_;
278 };
279
280
281 /////////////////////////////////////////////////////////////////////
282 //
283 // GuiSymbols
284 //
285 /////////////////////////////////////////////////////////////////////
286
287 GuiSymbols::GuiSymbols(GuiView & lv)
288         : DialogView(lv, "symbols", qt_("Symbols")), encoding_("ascii"),
289                 model_(new Model(this))
290 {
291         setupUi(this);
292
293         // Translate names
294         for (int i = 0 ; i < no_blocks; ++i)
295                 unicode_blocks[i].qname = qt_(unicode_blocks[i].name);
296
297         setFocusProxy(symbolsLW);
298
299         symbolsLW->setViewMode(QListView::IconMode);
300         symbolsLW->setLayoutMode(QListView::Batched);
301         symbolsLW->setBatchSize(1000);
302         symbolsLW->setUniformItemSizes(true);
303
304         // increase the display size of the symbols a bit
305         QFont font = symbolsLW->font();
306         const int size = font.pointSize() + 3;
307         font.setPointSize(size);
308         symbolsLW->setFont(font);
309         QFontMetrics fm(font);
310         const int cellHeight = fm.height() + 6;
311         // FIXME: using at least cellHeight because of
312         // QFontMetrics::maxWidth() is returning 0 with Qt/Cocoa on Mac OS
313         const int cellWidth = max(cellHeight - 2, fm.maxWidth() + 4);
314         symbolsLW->setGridSize(QSize(cellWidth, cellHeight));
315         symbolsLW->setModel(model_);
316 }
317
318
319 void GuiSymbols::updateView()
320 {
321         chosenLE->clear();
322
323         string new_encoding = bufferview()->cursor().getEncoding()->name();
324         if (buffer().params().inputenc != "auto" &&
325             buffer().params().inputenc != "default")
326                 new_encoding = buffer().params().encoding().name();
327         if (new_encoding == encoding_)
328                 // everything up to date
329                 return;
330         if (!new_encoding.empty())
331                 encoding_ = new_encoding;
332         bool const utf8 = toqstr(encoding_).startsWith("utf8");
333         if (utf8)
334                 categoryFilterCB->setChecked(false);
335         //categoryFilterCB->setEnabled(!utf8);
336         updateSymbolList();
337 }
338
339
340 void GuiSymbols::enableView(bool enable)
341 {
342         chosenLE->setEnabled(enable);
343         buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enable);
344         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
345         if (enable)
346                 buttonBox->button(QDialogButtonBox::Close)->setText(qt_("Cancel"));
347         else
348                 buttonBox->button(QDialogButtonBox::Close)->setText(qt_("Close"));
349 }
350
351
352 void GuiSymbols::on_buttonBox_clicked(QAbstractButton * button)
353 {
354         QDialogButtonBox * bbox = qobject_cast<QDialogButtonBox*>(sender());
355         switch (bbox->standardButton(button)) {
356         case QDialogButtonBox::Ok:
357                 slotOK();
358                 break;
359         case QDialogButtonBox::Apply:
360                 dispatchParams();
361                 break;
362         case QDialogButtonBox::Close:
363                 hide();
364                 break;
365         default:
366                 break;
367         }
368 }
369
370
371 void GuiSymbols::slotOK()
372 {
373         dispatchParams();
374         hide();
375 }
376
377
378 void GuiSymbols::on_symbolsLW_activated(QModelIndex const &)
379 {
380         slotOK();
381 }
382
383
384 void GuiSymbols::on_chosenLE_textChanged(QString const & text)
385 {
386         bool const empty_sel = text.isEmpty();
387         buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!empty_sel);
388         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!empty_sel);
389 }
390
391
392 void GuiSymbols::on_chosenLE_returnPressed()
393 {
394         slotOK();
395 }
396
397
398 void GuiSymbols::on_symbolsLW_clicked(QModelIndex const & index)
399 {
400         QString const text = model_->data(index, Qt::DisplayRole).toString();
401         if (text.isEmpty())
402                 return;
403         if (chosenLE->isEnabled())
404                 chosenLE->insert(text);
405         if (categoryFilterCB->isChecked()) {
406                 QString const category = getBlock(text.data()->unicode());
407                 categoryCO->setCurrentIndex(categoryCO->findText(category));
408         }
409 }
410
411
412 void GuiSymbols::on_categoryCO_activated(QString const & text)
413 {
414         if (!categoryFilterCB->isChecked())
415                 updateSymbolList(false);
416         else
417                 scrollToItem(text);
418 }
419
420
421 void GuiSymbols::on_categoryFilterCB_toggled(bool on)
422 {
423         updateSymbolList(on);
424         if (on)
425                 scrollToItem(categoryCO->currentText());
426 }
427
428
429 void GuiSymbols::scrollToItem(QString const & category)
430 {
431         if (used_blocks.find(category) == used_blocks.end())
432                 return;
433         int row = used_blocks[category];
434         QModelIndex index = symbolsLW->model()->index(row, 0, QModelIndex());
435         symbolsLW->scrollTo(index, QAbstractItemView::PositionAtTop);
436 }
437
438
439 void GuiSymbols::updateSymbolList(bool update_combo)
440 {
441         QString const category = categoryCO->currentText();
442         char_type range_start = 0x0000;
443         char_type range_end = 0x110000;
444         QList<char_type> s;
445         if (update_combo) {
446                 used_blocks.clear();
447                 categoryCO->clear();
448         }
449         bool const show_all = categoryFilterCB->isChecked();
450
451         Encoding const * const enc = encodings.fromLyXName(encoding_);
452
453         if (symbols_.empty() || update_combo)
454                 symbols_ = enc->symbolsList();
455
456         if (!show_all) {
457                 for (int i = 0 ; i < no_blocks; ++i)
458                         if (unicode_blocks[i].qname == category) {
459                                 range_start = unicode_blocks[i].start;
460                                 range_end = unicode_blocks[i].end;
461                                 break;
462                         }
463         }
464
465         int numItem = 0;
466         for (char_type c : symbols_) {
467                 if (!update_combo && !show_all && (c < range_start || c > range_end))
468                         continue;
469                 QChar::Category const cat = QChar::category(uint(c));
470                 // we do not want control or space characters
471                 if (cat == QChar::Other_Control || cat == QChar::Separator_Space)
472                         continue;
473                 ++numItem;
474                 if (show_all || (c >= range_start && c <= range_end))
475                         s.append(c);
476                 if (update_combo) {
477                         QString block = getBlock(c);
478                         if (used_blocks.find(block) == used_blocks.end())
479                                 used_blocks[block] = numItem;
480                 }
481         }
482         model_->setSymbols(s, enc);
483
484         if (update_combo) {
485                 // update category combo
486                 for (UsedBlocks::iterator it = used_blocks.begin();
487                      it != used_blocks.end(); ++it) {
488                         categoryCO->addItem(it->first);
489                 }
490         }
491
492         int old = categoryCO->findText(category);
493         if (old != -1)
494                 categoryCO->setCurrentIndex(old);
495         else if (update_combo) {
496                 // restart with a non-empty block
497                 // this happens when the encoding changes when moving the cursor
498                 categoryCO->setCurrentIndex(0);
499                 updateSymbolList(false);
500         }
501 }
502
503
504 void GuiSymbols::dispatchParams()
505 {
506         dispatch(FuncRequest(getLfun(), fromqstr(chosenLE->text())));
507 }
508
509
510 Dialog * createGuiSymbols(GuiView & lv)
511 {
512         return new GuiSymbols(lv);
513 }
514
515
516 } // namespace frontend
517 } // namespace lyx
518
519 #include "moc_GuiSymbols.cpp"