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