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