]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDelimiter.cpp
7182870f2feacd9295fbbd15bffa6ce032de50d8
[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 "GuiView.h"
17
18 #include "qt_helpers.h"
19 #include "gettext.h"
20
21 #include <QPixmap>
22 #include <QCheckBox>
23 #include <QListWidgetItem>
24
25 // Set to zero if unicode symbols are preferred.
26 #define USE_PIXMAP 1
27
28 using std::string;
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")
74 {
75         setupUi(this);
76         setViewTitle(_("Math Delimiter"));
77
78         connect(closePB, SIGNAL(clicked()), this, SLOT(accept()));
79
80         setFocusProxy(leftLW);
81
82         leftLW->setViewMode(QListView::IconMode);
83         rightLW->setViewMode(QListView::IconMode);
84
85         typedef std::map<char_type, QListWidgetItem *> ListItems;
86         ListItems list_items;
87         // The last element is the empty one.
88         int const end = nr_latex_delimiters - 1;
89         for (int i = 0; i < end; ++i) {
90                 string const delim = latex_delimiters[i];
91                 MathSymbol const & ms = mathSymbol(delim);
92                 QString symbol(ms.fontcode?
93                         QChar(ms.fontcode) : toqstr(docstring(1, ms.unicode)));
94                 QListWidgetItem * lwi = new QListWidgetItem(symbol);
95                 lwi->setToolTip(toqstr(delim));
96                 FontInfo lyxfont;
97                 lyxfont.setFamily(ms.fontfamily);
98                 QFont const & symbol_font = guiApp->guiFontLoader().get(lyxfont);
99                 lwi->setFont(symbol_font);
100                 list_items[ms.unicode] = lwi;
101                 leftLW->addItem(lwi);
102         }
103
104         for (int i = 0; i != leftLW->count(); ++i) {
105                 MathSymbol const & ms = mathSymbol(
106                         fromqstr(leftLW->item(i)->toolTip()));
107                 rightLW->addItem(list_items[doMatch(ms.unicode)]->clone());
108         }
109
110         // The last element is the empty one.
111         leftLW->addItem(qt_("(None)"));
112         rightLW->addItem(qt_("(None)"));
113
114         sizeCO->addItem(qt_("Variable"));
115
116         for (int i = 0; *biggui[i]; ++i)
117                 sizeCO->addItem(qt_(biggui[i]));
118
119         on_leftLW_currentRowChanged(0);
120         bc().setPolicy(ButtonPolicy::IgnorantPolicy);
121 }
122
123
124 char_type GuiDelimiter::doMatch(char_type const symbol)
125 {
126         string const & str = texName(symbol);
127         string match;
128         if (str == "(") match = ")";
129         else if (str == ")") match = "(";
130         else if (str == "[") match = "]";
131         else if (str == "]") match = "[";
132         else if (str == "{") match = "}";
133         else if (str == "}") match = "{";
134         else if (str == "l") match = "r";
135         else if (str == "rceil") match = "lceil";
136         else if (str == "lceil") match = "rceil";
137         else if (str == "rfloor") match = "lfloor";
138         else if (str == "lfloor") match = "rfloor";
139         else if (str == "rangle") match = "langle";
140         else if (str == "langle") match = "rangle";
141         else if (str == "backslash") match = "/";
142         else if (str == "/") match = "backslash";
143         else return symbol;
144
145         return mathSymbol(match).unicode;
146 }
147
148
149 void GuiDelimiter::updateTeXCode(int size)
150 {
151         bool const bigsize = size != 0;
152
153         QString left_str = fix_name(leftLW->currentItem()->toolTip(), bigsize);
154         QString right_str = fix_name(rightLW->currentItem()->toolTip(), bigsize);
155
156         if (!bigsize)
157                 tex_code_ = left_str + ' ' + right_str;
158         else {
159                 tex_code_ = bigleft[size] + ' '
160                         + left_str + ' '
161                         + bigright[size] + ' '
162                         + right_str;
163         }
164
165         // Generate TeX-code for GUI display.
166         // FIXME: Instead of reconstructing the TeX code it would be nice to
167         // FIXME: retrieve the LateX code directly from mathed.
168         // In all cases, we want the '\' prefix if needed, so we pass 'true'
169         // to fix_name.
170         left_str = fix_name(leftLW->currentItem()->toolTip(), true);
171         right_str = fix_name(rightLW->currentItem()->toolTip(), true);
172         QString code_str;
173         if (!bigsize)
174                 code_str = "\\left" + left_str + " \\right" + right_str;
175         else {
176                 // There should be nothing in the TeX-code when the delimiter is "None".
177                 if (left_str != ".")
178                         code_str = "\\" + bigleft[size] + left_str + ' ';
179                 if (right_str != ".")
180                         code_str += "\\" + bigright[size] + right_str;
181         }
182
183         texCodeL->setText(qt_("TeX Code: ") + code_str);
184 }
185
186
187 void GuiDelimiter::on_insertPB_clicked()
188 {
189         if (sizeCO->currentIndex() == 0)
190                 dispatchDelim(fromqstr(tex_code_));
191         else {
192                 QString command = '"' + tex_code_ + '"';
193                 command.replace(' ', "\" \"");
194                 dispatchBigDelim(fromqstr(command));
195         }
196  }
197
198
199 void GuiDelimiter::on_sizeCO_activated(int index)
200 {
201         updateTeXCode(index);
202 }
203
204
205 void GuiDelimiter::on_leftLW_itemActivated(QListWidgetItem *)
206 {
207         on_insertPB_clicked();
208         accept();
209 }
210
211
212 void GuiDelimiter::on_rightLW_itemActivated(QListWidgetItem *)
213 {
214         on_insertPB_clicked();
215         accept();
216 }
217
218
219 void GuiDelimiter::on_leftLW_currentRowChanged(int item)
220 {
221         if (matchCB->isChecked())
222                 rightLW->setCurrentRow(item);
223
224         updateTeXCode(sizeCO->currentIndex());
225 }
226
227
228 void GuiDelimiter::on_rightLW_currentRowChanged(int item)
229 {
230         if (matchCB->isChecked())
231                 leftLW->setCurrentRow(item);
232
233         updateTeXCode(sizeCO->currentIndex());
234 }
235
236
237 void GuiDelimiter::on_matchCB_stateChanged(int state)
238 {
239         if (state == Qt::Checked)
240                 on_leftLW_currentRowChanged(leftLW->currentRow());
241
242         updateTeXCode(sizeCO->currentIndex());
243 }
244
245
246 Dialog * createGuiDelimiter(GuiView & lv) { return new GuiDelimiter(lv); }
247
248
249 } // namespace frontend
250 } // namespace lyx
251
252 #include "GuiDelimiter_moc.cpp"