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