]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiDelimiter.cpp
c1a4f90bd0b21eae2e8623f105c656e240fb047b
[features.git] / src / frontends / qt4 / GuiDelimiter.cpp
1 /**
2  * \file GuiDelimiter.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiDelimiter.h"
14
15 #include "GuiApplication.h"
16 #include "GuiFontLoader.h"
17 #include "GuiView.h"
18 #include "qt_helpers.h"
19
20 #include "FontEnums.h"
21 #include "FontInfo.h"
22 #include "FuncRequest.h"
23
24 #include "support/gettext.h"
25 #include "support/docstring.h"
26
27 #include <QPixmap>
28 #include <QCheckBox>
29 #include <QListWidgetItem>
30
31 #include <map>
32 #include <string>
33
34 using namespace std;
35
36 namespace lyx {
37 namespace frontend {
38
39 namespace {
40
41 static char const *  latex_delimiters[] = {
42         "(", ")", "{", "}", "[", "]",
43         "lceil", "rceil", "lfloor", "rfloor", "langle", "rangle",
44         "llbracket", "rrbracket",
45         "uparrow", "updownarrow", "Uparrow", "Updownarrow", "downarrow", "Downarrow",
46         "|", "Vert", "/", "backslash", ""
47 };
48
49
50 static int const nr_latex_delimiters =
51         sizeof(latex_delimiters) / sizeof(char const *);
52
53 static QString const bigleft[]  = {"", "bigl", "Bigl", "biggl", "Biggl"};
54
55 static QString const bigright[] = {"", "bigr", "Bigr", "biggr", "Biggr"};
56
57 static char const * const biggui[] = {
58         N_("big[[delimiter size]]"),
59         N_("Big[[delimiter size]]"),
60         N_("bigg[[delimiter size]]"),
61         N_("Bigg[[delimiter size]]"),
62         ""
63 };
64
65
66 // FIXME: It might be better to fix the big delim LFUN to not require
67 // additional '\' prefix.
68 static QString fix_name(QString const & str, bool big)
69 {
70         if (str.isEmpty())
71                 return ".";
72         if (!big || str == "(" || str == ")" || str == "[" || str == "]"
73             || str == "|" || str == "/")
74                 return str;
75
76         return "\\" + str;
77 }
78
79 struct MathSymbol {
80         MathSymbol(char_type uc = '?', unsigned char fc = 0,
81                 FontFamily ff = SYMBOL_FAMILY)
82                 : unicode(uc), fontcode(fc), fontfamily(ff)
83         {}
84         char_type unicode;
85         unsigned char fontcode;
86         FontFamily fontfamily;
87 };
88
89 /// TeX-name / Math-symbol map.
90 static map<std::string, MathSymbol> math_symbols_;
91 /// Math-symbol / TeX-name map.
92 /// This one is for fast search, it contains the same data as
93 /// \c math_symbols_.
94 static map<char_type, string> tex_names_;
95
96 void initMathSymbols()
97 {
98         // FIXME: Ideally, those unicode codepoints would be defined
99         // in "lib/symbols". Unfortunately, some of those are already
100         // defined with non-unicode ids for use within mathed.
101         // FIXME 2: We should fill-in this map with the parsed "symbols"
102         // file done in MathFactory.cpp.
103         math_symbols_["("] = MathSymbol('(', 40, CMR_FAMILY);
104         math_symbols_[")"] = MathSymbol(')', 41, CMR_FAMILY);
105         math_symbols_["{"] = MathSymbol('{', 102, CMSY_FAMILY);
106         math_symbols_["}"] = MathSymbol('}', 103, CMSY_FAMILY);
107         math_symbols_["["] = MathSymbol('[', 91, CMR_FAMILY);
108         math_symbols_["]"] = MathSymbol(']', 93, CMR_FAMILY);
109         math_symbols_["|"] = MathSymbol('|', 106, CMSY_FAMILY);
110         math_symbols_["/"] = MathSymbol('/', 47, CMR_FAMILY);
111         math_symbols_["backslash"] = MathSymbol('\\', 110, CMSY_FAMILY);
112         math_symbols_["lceil"] = MathSymbol(0x2308, 100, CMSY_FAMILY);
113         math_symbols_["rceil"] = MathSymbol(0x2309, 101, CMSY_FAMILY);
114         math_symbols_["lfloor"] = MathSymbol(0x230A, 98, CMSY_FAMILY);
115         math_symbols_["rfloor"] = MathSymbol(0x230B, 99, CMSY_FAMILY);
116         math_symbols_["langle"] = MathSymbol(0x2329, 104, CMSY_FAMILY);
117         math_symbols_["rangle"] = MathSymbol(0x232A, 105, CMSY_FAMILY);
118         math_symbols_["llbracket"] = MathSymbol(0x27e6, 74, STMARY_FAMILY);
119         math_symbols_["rrbracket"] = MathSymbol(0x27e7, 75, STMARY_FAMILY);
120         math_symbols_["uparrow"] = MathSymbol(0x2191, 34, CMSY_FAMILY);
121         math_symbols_["Uparrow"] = MathSymbol(0x21D1, 42, CMSY_FAMILY);
122         math_symbols_["updownarrow"] = MathSymbol(0x2195, 108, CMSY_FAMILY);
123         math_symbols_["Updownarrow"] = MathSymbol(0x21D5, 109, CMSY_FAMILY);
124         math_symbols_["downarrow"] = MathSymbol(0x2193, 35, CMSY_FAMILY);
125         math_symbols_["Downarrow"] = MathSymbol(0x21D3, 43, CMSY_FAMILY);
126         math_symbols_["downdownarrows"] = MathSymbol(0x21CA, 184, MSA_FAMILY);
127         math_symbols_["downharpoonleft"] = MathSymbol(0x21C3, 188, MSA_FAMILY);
128         math_symbols_["downharpoonright"] = MathSymbol(0x21C2, 186, MSA_FAMILY);
129         math_symbols_["vert"] = MathSymbol(0x007C, 106, CMSY_FAMILY);
130         math_symbols_["Vert"] = MathSymbol(0x2016, 107, CMSY_FAMILY);
131
132         map<string, MathSymbol>::const_iterator it = math_symbols_.begin();
133         map<string, MathSymbol>::const_iterator end = math_symbols_.end();
134         for (; it != end; ++it)
135                 tex_names_[it->second.unicode] = it->first;
136 }
137
138 /// \return the math unicode symbol associated to a TeX name.
139 MathSymbol const & mathSymbol(string tex_name)
140 {
141         map<string, MathSymbol>::const_iterator it =
142                 math_symbols_.find(tex_name);
143
144         static MathSymbol const unknown_symbol;
145         if (it == math_symbols_.end())
146                 return unknown_symbol;
147
148         return it->second;
149 }
150
151 /// \return the TeX name associated to a math unicode symbol.
152 string const & texName(char_type math_symbol)
153 {
154         map<char_type, string>::const_iterator it =
155                 tex_names_.find(math_symbol);
156
157         static string const empty_string;
158         if (it == tex_names_.end())
159                 return empty_string;
160
161         return it->second;
162 }
163
164 } // anon namespace
165
166
167 GuiDelimiter::GuiDelimiter(GuiView & lv)
168         : GuiDialog(lv, "mathdelimiter", qt_("Math Delimiter"))
169 {
170         setupUi(this);
171
172         connect(closePB, SIGNAL(clicked()), this, SLOT(accept()));
173
174         setFocusProxy(leftLW);
175
176         leftLW->setViewMode(QListView::IconMode);
177         rightLW->setViewMode(QListView::IconMode);
178
179         leftLW->setDragDropMode(QAbstractItemView::NoDragDrop);
180         rightLW->setDragDropMode(QAbstractItemView::NoDragDrop);
181
182         initMathSymbols();
183
184         typedef map<char_type, QListWidgetItem *> ListItems;
185         ListItems list_items;
186         // The last element is the empty one.
187         int const end = nr_latex_delimiters - 1;
188         for (int i = 0; i < end; ++i) {
189                 string const delim = latex_delimiters[i];
190                 MathSymbol const & ms = mathSymbol(delim);
191 // Due to a bug in Qt 4 on Windows, we need to use our math symbol font
192 // on Windows (see #5760).
193 // FIXME: Re-check after Windows has settled to Qt 5.
194 //        ATM, this doesn't work also with Qt 5.4.1 because of still missing
195 //        glyphs for \llbracket and \rrbracket.
196 #if defined(Q_OS_WIN) || defined(Q_CYGWIN_WIN)
197                 QString symbol(ms.fontcode?
198                         QChar(ms.fontcode) : toqstr(docstring(1, ms.unicode)));
199                 QListWidgetItem * lwi = new QListWidgetItem(symbol);
200                 FontInfo lyxfont;
201                 lyxfont.setFamily(ms.fontfamily);
202                 QFont font = frontend::getFont(lyxfont);
203 #else
204                 QString symbol(QChar(ms.unicode));
205                 QListWidgetItem * lwi = new QListWidgetItem(symbol);
206                 QFont font = lwi->font();
207                 font.setPointSize(2 * font.pointSize());
208 #endif
209                 lwi->setFont(font);
210                 lwi->setToolTip(toqstr(delim));
211                 list_items[ms.unicode] = lwi;
212                 leftLW->addItem(lwi);
213         }
214
215         for (int i = 0; i != leftLW->count(); ++i) {
216                 MathSymbol const & ms = mathSymbol(
217                         fromqstr(leftLW->item(i)->toolTip()));
218                 rightLW->addItem(list_items[doMatch(ms.unicode)]->clone());
219         }
220
221         // The last element is the empty one.
222         QListWidgetItem * lwi = new QListWidgetItem(qt_("(None)"));
223         QListWidgetItem * rwi = new QListWidgetItem(qt_("(None)"));
224 // See above comment.
225 // FIXME: Re-check after Windows has settled to Qt 5.
226 #if !defined(Q_OS_WIN) && !defined(Q_CYGWIN_WIN)
227         QFont font = lwi->font();
228         font.setPointSize(2 * font.pointSize());
229         lwi->setFont(font);
230         rwi->setFont(font);
231 #endif
232         leftLW->addItem(lwi);
233         rightLW->addItem(rwi);
234
235         sizeCO->addItem(qt_("Variable"));
236
237         for (int i = 0; *biggui[i]; ++i)
238                 sizeCO->addItem(qt_(biggui[i]));
239
240         on_leftLW_currentRowChanged(0);
241         bc().setPolicy(ButtonPolicy::IgnorantPolicy);
242 }
243
244
245 char_type GuiDelimiter::doMatch(char_type const symbol)
246 {
247         string const & str = texName(symbol);
248         string match;
249         if (str == "(") match = ")";
250         else if (str == ")") match = "(";
251         else if (str == "[") match = "]";
252         else if (str == "]") match = "[";
253         else if (str == "{") match = "}";
254         else if (str == "}") match = "{";
255         else if (str == "l") match = "r";
256         else if (str == "rceil") match = "lceil";
257         else if (str == "lceil") match = "rceil";
258         else if (str == "rfloor") match = "lfloor";
259         else if (str == "lfloor") match = "rfloor";
260         else if (str == "rangle") match = "langle";
261         else if (str == "langle") match = "rangle";
262         else if (str == "llbracket") match = "rrbracket";
263         else if (str == "rrbracket") match = "llbracket";
264         else if (str == "backslash") match = "/";
265         else if (str == "/") match = "backslash";
266         else return symbol;
267
268         return mathSymbol(match).unicode;
269 }
270
271
272 void GuiDelimiter::updateTeXCode(int size)
273 {
274         bool const bigsize = size != 0;
275
276         QString left_str = fix_name(leftLW->currentItem()->toolTip(), bigsize);
277         QString right_str = fix_name(rightLW->currentItem()->toolTip(), bigsize);
278
279         if (!bigsize)
280                 tex_code_ = left_str + ' ' + right_str;
281         else {
282                 tex_code_ = bigleft[size] + ' '
283                         + left_str + ' '
284                         + bigright[size] + ' '
285                         + right_str;
286         }
287
288         // Generate TeX-code for GUI display.
289         // FIXME: Instead of reconstructing the TeX code it would be nice to
290         // FIXME: retrieve the LateX code directly from mathed.
291         // In all cases, we want the '\' prefix if needed, so we pass 'true'
292         // to fix_name.
293         left_str = fix_name(leftLW->currentItem()->toolTip(), true);
294         right_str = fix_name(rightLW->currentItem()->toolTip(), true);
295         QString code_str;
296         if (!bigsize)
297                 code_str = "\\left" + left_str + " \\right" + right_str;
298         else {
299                 // There should be nothing in the TeX-code when the delimiter is "None".
300                 if (left_str != ".")
301                         code_str = "\\" + bigleft[size] + left_str + ' ';
302                 if (right_str != ".")
303                         code_str += "\\" + bigright[size] + right_str;
304         }
305
306         texCodeL->setText(qt_("TeX Code: ") + code_str);
307 }
308
309
310 void GuiDelimiter::on_insertPB_clicked()
311 {
312         if (sizeCO->currentIndex() == 0)
313                 dispatch(FuncRequest(LFUN_MATH_DELIM, fromqstr(tex_code_)));
314         else {
315                 QString command = '"' + tex_code_ + '"';
316                 command.replace(' ', "\" \"");
317                 dispatch(FuncRequest(LFUN_MATH_BIGDELIM, fromqstr(command)));
318         }
319  }
320
321
322 void GuiDelimiter::on_sizeCO_activated(int index)
323 {
324         updateTeXCode(index);
325 }
326
327
328 void GuiDelimiter::on_leftLW_itemActivated(QListWidgetItem *)
329 {
330         // do not auto-apply if !matchCB->isChecked()
331         if (!matchCB->isChecked())
332                 return;
333         on_insertPB_clicked();
334         accept();
335 }
336
337
338 void GuiDelimiter::on_rightLW_itemActivated(QListWidgetItem *)
339 {
340         // do not auto-apply if !matchCB->isChecked()
341         if (!matchCB->isChecked())
342                 return;
343         on_insertPB_clicked();
344         accept();
345 }
346
347
348 void GuiDelimiter::on_leftLW_currentRowChanged(int item)
349 {
350         if (matchCB->isChecked())
351                 rightLW->setCurrentRow(item);
352
353         updateTeXCode(sizeCO->currentIndex());
354 }
355
356
357 void GuiDelimiter::on_rightLW_currentRowChanged(int item)
358 {
359         if (matchCB->isChecked())
360                 leftLW->setCurrentRow(item);
361
362         updateTeXCode(sizeCO->currentIndex());
363 }
364
365
366 void GuiDelimiter::on_matchCB_stateChanged(int state)
367 {
368         if (state == Qt::Checked)
369                 on_leftLW_currentRowChanged(leftLW->currentRow());
370
371         updateTeXCode(sizeCO->currentIndex());
372 }
373
374
375 Dialog * createGuiDelimiter(GuiView & lv) { return new GuiDelimiter(lv); }
376
377
378 } // namespace frontend
379 } // namespace lyx
380
381 #include "moc_GuiDelimiter.cpp"