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