3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jürgen Spitzmüller
8 * Full author contact details are available in file CREDITS.
13 #include "GuiSymbols.h"
15 #include "GuiApplication.h"
17 #include "qt_helpers.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
24 #include "FuncRequest.h"
26 #include "support/debug.h"
27 #include "support/gettext.h"
30 #include <QDialogButtonBox>
31 #include <QPushButton>
44 /// name of unicode block, start and end code point
45 struct UnicodeBlocks {
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 }
151 const int no_blocks = sizeof(unicode_blocks) / sizeof(UnicodeBlocks);
154 QString getBlock(char_type c)
156 // store an educated guess for the next search
157 // 0 <= lastBlock < no_blocks
159 static int lastBlock = 0;
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;
170 // c falls into an uncovered area, but we can guess which
171 if (c > unicode_blocks[lastBlock].end
172 && (lastBlock == no_blocks-1 || c < unicode_blocks[lastBlock + 1].start))
175 // guessing was wrong so far. do a real search.
177 while (i < no_blocks && c > unicode_blocks[i].end)
183 if (c < unicode_blocks[i].start) {
185 // cache the previous block for guessing next time
190 // 0 <= i < no_blocks
191 // cache the last block for guessing next time
193 return unicode_blocks[lastBlock].qname;
200 /////////////////////////////////////////////////////////////////////
204 /////////////////////////////////////////////////////////////////////
206 class GuiSymbols::Model : public QAbstractListModel
209 Model(GuiSymbols * parent)
210 : QAbstractListModel(parent), encoding_(nullptr)
213 QModelIndex index(int row, int column, QModelIndex const &) const override
215 return createIndex(row, column);
218 QModelIndex parent(QModelIndex const &) const override
220 return QModelIndex();
223 int rowCount(QModelIndex const &) const override
225 return symbols_.count();
228 QVariant data(QModelIndex const & index, int role) const override
230 if (!index.isValid())
233 char_type c = symbols_.at(index.row());
236 case Qt::TextAlignmentRole:
237 return QVariant(Qt::AlignCenter);
238 case Qt::DisplayRole:
240 case Qt::ToolTipRole: {
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));
249 return formatToolTip(QString("<p align=center><span "
250 "style=\"font-size: xx-large;\">%1"
251 "</span><br>U+%2</p>%3")
253 .arg(QString("%1").arg(c, 0, 16).toUpper())
256 case Qt::SizeHintRole:
257 // Fix many symbols not displaying in combination with
258 // setUniformItemSizes
259 return QSize(1000,1000);
265 void setSymbols(QList<char_type> const & symbols, Encoding const * encoding)
269 encoding_ = encoding;
274 friend class GuiSymbols;
276 QList<char_type> symbols_;
277 Encoding const * encoding_;
281 /////////////////////////////////////////////////////////////////////
285 /////////////////////////////////////////////////////////////////////
287 GuiSymbols::GuiSymbols(GuiView & lv)
288 : DialogView(lv, "symbols", qt_("Symbols")), encoding_("ascii"),
289 model_(new Model(this))
294 for (int i = 0 ; i < no_blocks; ++i)
295 unicode_blocks[i].qname = qt_(unicode_blocks[i].name);
297 setFocusProxy(symbolsLW);
299 symbolsLW->setViewMode(QListView::IconMode);
300 symbolsLW->setLayoutMode(QListView::Batched);
301 symbolsLW->setBatchSize(1000);
302 symbolsLW->setUniformItemSizes(true);
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_);
319 void GuiSymbols::updateView()
323 string new_encoding = bufferview()->cursor().getEncoding()->name();
324 if (buffer().params().inputenc != "auto-legacy" &&
325 buffer().params().inputenc != "auto-legacy-plain")
326 new_encoding = buffer().params().encoding().name();
327 if (new_encoding == encoding_)
328 // everything up to date
330 if (!new_encoding.empty())
331 encoding_ = new_encoding;
332 bool const utf8 = toqstr(encoding_).startsWith("utf8");
334 categoryFilterCB->setChecked(false);
335 //categoryFilterCB->setEnabled(!utf8);
340 void GuiSymbols::enableView(bool enable)
342 chosenLE->setEnabled(enable);
343 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enable);
344 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
346 buttonBox->button(QDialogButtonBox::Close)->setText(qt_("Cancel"));
348 buttonBox->button(QDialogButtonBox::Close)->setText(qt_("Close"));
352 void GuiSymbols::on_buttonBox_clicked(QAbstractButton * button)
354 QDialogButtonBox * bbox = qobject_cast<QDialogButtonBox*>(sender());
355 switch (bbox->standardButton(button)) {
356 case QDialogButtonBox::Ok:
359 case QDialogButtonBox::Apply:
362 case QDialogButtonBox::Close:
371 void GuiSymbols::slotOK()
378 void GuiSymbols::on_symbolsLW_activated(QModelIndex const &)
384 void GuiSymbols::on_chosenLE_textChanged(QString const & text)
386 bool const empty_sel = text.isEmpty();
387 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!empty_sel);
388 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!empty_sel);
392 void GuiSymbols::on_chosenLE_returnPressed()
398 void GuiSymbols::on_symbolsLW_clicked(QModelIndex const & index)
400 QString const text = model_->data(index, Qt::DisplayRole).toString();
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));
412 void GuiSymbols::on_categoryCO_activated(int)
414 if (!categoryFilterCB->isChecked())
415 updateSymbolList(false);
417 scrollToItem(categoryCO->currentText());
421 void GuiSymbols::on_categoryFilterCB_toggled(bool on)
423 updateSymbolList(on);
425 scrollToItem(categoryCO->currentText());
429 void GuiSymbols::scrollToItem(QString const & category)
431 if (used_blocks.find(category) == used_blocks.end())
433 int row = used_blocks[category];
434 QModelIndex index = symbolsLW->model()->index(row, 0, QModelIndex());
435 symbolsLW->scrollTo(index, QAbstractItemView::PositionAtTop);
439 void GuiSymbols::updateSymbolList(bool update_combo)
441 QString const category = categoryCO->currentText();
442 char_type range_start = 0x0000;
443 char_type range_end = 0x110000;
449 bool const show_all = categoryFilterCB->isChecked();
451 Encoding const * const enc = encodings.fromLyXName(encoding_);
453 if (symbols_.empty() || update_combo)
454 symbols_ = enc->symbolsList();
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;
466 for (char_type c : symbols_) {
467 if (!update_combo && !show_all && (c < range_start || c > range_end))
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)
474 if (show_all || (c >= range_start && c <= range_end))
477 used_blocks.insert({getBlock(c), numItem});
479 model_->setSymbols(s, enc);
482 // update category combo
483 for (UsedBlocks::iterator it = used_blocks.begin();
484 it != used_blocks.end(); ++it) {
485 categoryCO->addItem(it->first);
489 int old = categoryCO->findText(category);
491 categoryCO->setCurrentIndex(old);
492 else if (update_combo) {
493 // restart with a non-empty block
494 // this happens when the encoding changes when moving the cursor
495 categoryCO->setCurrentIndex(0);
496 updateSymbolList(false);
501 void GuiSymbols::dispatchParams()
503 dispatch(FuncRequest(getLfun(), fromqstr(chosenLE->text())));
507 } // namespace frontend
510 #include "moc_GuiSymbols.cpp"