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