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