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