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