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