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