]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDelimiter.cpp
Pimpl stuff in GuiApplication.
[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 "FontInfo.h"
21
22 #include "support/gettext.h"
23
24 #include <QPixmap>
25 #include <QCheckBox>
26 #include <QListWidgetItem>
27
28 using namespace std;
29
30 static char const *  latex_delimiters[] = {
31         "(", ")", "{", "}", "[", "]",
32         "lceil", "rceil", "lfloor", "rfloor", "langle", "rangle",
33         "uparrow", "updownarrow", "Uparrow", "Updownarrow", "downarrow", "Downarrow",
34         "|", "Vert", "/", "backslash", ""
35 };
36
37
38 static int const nr_latex_delimiters =
39         sizeof(latex_delimiters) / sizeof(char const *);
40
41 static QString const bigleft[]  = {"", "bigl", "Bigl", "biggl", "Biggl"};
42
43 static QString const bigright[] = {"", "bigr", "Bigr", "biggr", "Biggr"};
44
45 static char const * const biggui[] = {
46         N_("big[[delimiter size]]"),
47         N_("Big[[delimiter size]]"),
48         N_("bigg[[delimiter size]]"),
49         N_("Bigg[[delimiter size]]"),
50         ""
51 };
52
53
54 // FIXME: It might be better to fix the big delim LFUN to not require
55 // additional '\' prefix.
56 static QString fix_name(QString const & str, bool big)
57 {
58         if (str.isEmpty())
59                 return ".";
60         if (!big || str == "(" || str == ")" || str == "[" || str == "]"
61             || str == "|" || str == "/")
62                 return str;
63
64         return "\\" + str;
65 }
66
67
68 namespace lyx {
69 namespace frontend {
70
71
72 GuiDelimiter::GuiDelimiter(GuiView & lv)
73         : GuiMath(lv, "mathdelimiter", qt_("Math Delimiter"))
74 {
75         setupUi(this);
76
77         connect(closePB, SIGNAL(clicked()), this, SLOT(accept()));
78
79         setFocusProxy(leftLW);
80
81         leftLW->setViewMode(QListView::IconMode);
82         rightLW->setViewMode(QListView::IconMode);
83
84         typedef map<char_type, QListWidgetItem *> ListItems;
85         ListItems list_items;
86         // The last element is the empty one.
87         int const end = nr_latex_delimiters - 1;
88         for (int i = 0; i < end; ++i) {
89                 string const delim = latex_delimiters[i];
90                 MathSymbol const & ms = mathSymbol(delim);
91                 QString symbol(ms.fontcode?
92                         QChar(ms.fontcode) : toqstr(docstring(1, ms.unicode)));
93                 QListWidgetItem * lwi = new QListWidgetItem(symbol);
94                 lwi->setToolTip(toqstr(delim));
95                 FontInfo lyxfont;
96                 lyxfont.setFamily(ms.fontfamily);
97                 lwi->setFont(frontend::getFont(lyxfont));
98                 list_items[ms.unicode] = lwi;
99                 leftLW->addItem(lwi);
100         }
101
102         for (int i = 0; i != leftLW->count(); ++i) {
103                 MathSymbol const & ms = mathSymbol(
104                         fromqstr(leftLW->item(i)->toolTip()));
105                 rightLW->addItem(list_items[doMatch(ms.unicode)]->clone());
106         }
107
108         // The last element is the empty one.
109         leftLW->addItem(qt_("(None)"));
110         rightLW->addItem(qt_("(None)"));
111
112         sizeCO->addItem(qt_("Variable"));
113
114         for (int i = 0; *biggui[i]; ++i)
115                 sizeCO->addItem(qt_(biggui[i]));
116
117         on_leftLW_currentRowChanged(0);
118         bc().setPolicy(ButtonPolicy::IgnorantPolicy);
119 }
120
121
122 char_type GuiDelimiter::doMatch(char_type const symbol)
123 {
124         string const & str = texName(symbol);
125         string match;
126         if (str == "(") match = ")";
127         else if (str == ")") match = "(";
128         else if (str == "[") match = "]";
129         else if (str == "]") match = "[";
130         else if (str == "{") match = "}";
131         else if (str == "}") match = "{";
132         else if (str == "l") match = "r";
133         else if (str == "rceil") match = "lceil";
134         else if (str == "lceil") match = "rceil";
135         else if (str == "rfloor") match = "lfloor";
136         else if (str == "lfloor") match = "rfloor";
137         else if (str == "rangle") match = "langle";
138         else if (str == "langle") match = "rangle";
139         else if (str == "backslash") match = "/";
140         else if (str == "/") match = "backslash";
141         else return symbol;
142
143         return mathSymbol(match).unicode;
144 }
145
146
147 void GuiDelimiter::updateTeXCode(int size)
148 {
149         bool const bigsize = size != 0;
150
151         QString left_str = fix_name(leftLW->currentItem()->toolTip(), bigsize);
152         QString right_str = fix_name(rightLW->currentItem()->toolTip(), bigsize);
153
154         if (!bigsize)
155                 tex_code_ = left_str + ' ' + right_str;
156         else {
157                 tex_code_ = bigleft[size] + ' '
158                         + left_str + ' '
159                         + bigright[size] + ' '
160                         + right_str;
161         }
162
163         // Generate TeX-code for GUI display.
164         // FIXME: Instead of reconstructing the TeX code it would be nice to
165         // FIXME: retrieve the LateX code directly from mathed.
166         // In all cases, we want the '\' prefix if needed, so we pass 'true'
167         // to fix_name.
168         left_str = fix_name(leftLW->currentItem()->toolTip(), true);
169         right_str = fix_name(rightLW->currentItem()->toolTip(), true);
170         QString code_str;
171         if (!bigsize)
172                 code_str = "\\left" + left_str + " \\right" + right_str;
173         else {
174                 // There should be nothing in the TeX-code when the delimiter is "None".
175                 if (left_str != ".")
176                         code_str = "\\" + bigleft[size] + left_str + ' ';
177                 if (right_str != ".")
178                         code_str += "\\" + bigright[size] + right_str;
179         }
180
181         texCodeL->setText(qt_("TeX Code: ") + code_str);
182 }
183
184
185 void GuiDelimiter::on_insertPB_clicked()
186 {
187         if (sizeCO->currentIndex() == 0)
188                 dispatchDelim(fromqstr(tex_code_));
189         else {
190                 QString command = '"' + tex_code_ + '"';
191                 command.replace(' ', "\" \"");
192                 dispatchBigDelim(fromqstr(command));
193         }
194  }
195
196
197 void GuiDelimiter::on_sizeCO_activated(int index)
198 {
199         updateTeXCode(index);
200 }
201
202
203 void GuiDelimiter::on_leftLW_itemActivated(QListWidgetItem *)
204 {
205         on_insertPB_clicked();
206         accept();
207 }
208
209
210 void GuiDelimiter::on_rightLW_itemActivated(QListWidgetItem *)
211 {
212         on_insertPB_clicked();
213         accept();
214 }
215
216
217 void GuiDelimiter::on_leftLW_currentRowChanged(int item)
218 {
219         if (matchCB->isChecked())
220                 rightLW->setCurrentRow(item);
221
222         updateTeXCode(sizeCO->currentIndex());
223 }
224
225
226 void GuiDelimiter::on_rightLW_currentRowChanged(int item)
227 {
228         if (matchCB->isChecked())
229                 leftLW->setCurrentRow(item);
230
231         updateTeXCode(sizeCO->currentIndex());
232 }
233
234
235 void GuiDelimiter::on_matchCB_stateChanged(int state)
236 {
237         if (state == Qt::Checked)
238                 on_leftLW_currentRowChanged(leftLW->currentRow());
239
240         updateTeXCode(sizeCO->currentIndex());
241 }
242
243
244 Dialog * createGuiDelimiter(GuiView & lv) { return new GuiDelimiter(lv); }
245
246
247 } // namespace frontend
248 } // namespace lyx
249
250 #include "GuiDelimiter_moc.cpp"