]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QMathDialog.C
Joao latest bits
[lyx.git] / src / frontends / qt2 / QMathDialog.C
1 /**
2  * \file QMathDialog.C
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
14 #include "qt_helpers.h"
15
16 #include "ControlMath.h"
17
18 #include "QMathDialog.h"
19 #include "QMath.h"
20
21 #include "iconpalette.h"
22
23 #include <qwidgetstack.h>
24 #include <qcombobox.h>
25 #include <qpushbutton.h>
26 #include <qlistbox.h>
27 #include <qpopupmenu.h>
28
29 using std::string;
30
31
32 class QScrollViewSingle : public QScrollView {
33 public:
34         QScrollViewSingle(QWidget * p)
35                 : QScrollView(p), w_(0) {
36                 setResizePolicy(Manual);
37                 setHScrollBarMode(AlwaysOff);
38                 setVScrollBarMode(AlwaysOn);
39                 setBackgroundMode(PaletteBackground);
40                 viewport()->setBackgroundMode(PaletteBackground);
41         }
42
43         void setChild(QWidget * w) {
44                 w_ = w;
45                 setMinimumWidth(verticalScrollBar()->width() + w_->width() + 4);
46                 addChild(w_);
47         }
48 protected:
49         virtual void resizeEvent(QResizeEvent * e) {
50                 QScrollView::resizeEvent(e);
51                 if (!w_)
52                         return;
53
54                 w_->resize(viewport()->width(), w_->height());
55                 // force the resize to get accurate scrollbars
56                 qApp->processEvents();
57                 resizeContents(w_->width(), w_->height());
58         }
59 private:
60         QWidget * w_;
61 };
62
63 namespace {
64
65 char const ** panels[] = {
66         latex_bop, latex_varsz, latex_brel, latex_greek, latex_arrow,
67         latex_dots, latex_deco, latex_misc, latex_ams_ops,
68         latex_ams_rel, latex_ams_nrel, latex_ams_arrows,
69         latex_ams_misc
70 };
71
72 int const nr_panels = sizeof(panels)/sizeof(panels[0]);
73
74 bool panel_initialised[nr_panels];
75
76 } // namespace anon
77
78
79 QMathDialog::QMathDialog(QMath * form)
80         : QMathDialogBase(0, 0, false, 0),
81         form_(form)
82 {
83         // enlarge the symbols ComboBox (no scrollbar)
84         symbolsCO->setSizeLimit(13);
85
86         connect(symbolsCO, SIGNAL(activated(int)), symbolsWS, SLOT(raiseWidget(int)));
87
88         for (int i = 0; *function_names[i]; ++i) {
89                 functionsLB->insertItem(function_names[i]);
90         }
91
92         for (int i = 0; i < nr_panels; ++i) {
93                 QScrollViewSingle * view = new QScrollViewSingle(symbolsWS);
94                 symbolsWS->addWidget(view, i);
95         }
96
97         // aboutToShow() only fires when != 0 in Qt 2 !
98         symbolsWS->raiseWidget(0);
99         addPanel(0);
100         panel_initialised[0] = true;
101
102         connect(symbolsWS, SIGNAL(aboutToShow(int)), this, SLOT(showingPanel(int)));
103
104         QPopupMenu * m = new QPopupMenu(spacePB);
105         m->setCaption(qt_("LyX: Insert space"));
106         m->insertTearOffHandle();
107         m->insertItem(qt_("Thin space   \\,"), 1);
108         m->insertItem(qt_("Medium space \\:"), 2);
109         m->insertItem(qt_("Thick space  \\;"), 3);
110         m->insertItem(qt_("Quadratin space      \\quad"), 4);
111         m->insertItem(qt_("Double quadratin space       \\qquad"), 5);
112         m->insertItem(qt_("Negative space       \\!"), 6);
113         connect(m, SIGNAL(activated(int)), this, SLOT(insertSpace(int)));
114         spacePB->setPopup(m);
115
116         m = new QPopupMenu(sqrtPB);
117         m->setCaption(qt_("LyX: Insert root"));
118         m->insertTearOffHandle();
119         m->insertItem(qt_("Square root  \\sqrt"), 1);
120         m->insertItem(qt_("Cube root    \\root"), 2);
121         m->insertItem(qt_("Other root   \\root"), 3);
122         connect(m, SIGNAL(activated(int)), this, SLOT(insertRoot(int)));
123         sqrtPB->setPopup(m);
124
125         m = new QPopupMenu(stylePB);
126         m->setCaption(qt_("LyX: Set math style"));
127         m->insertTearOffHandle();
128         m->insertItem(qt_("Display style        \\displaystyle"), 1);
129         m->insertItem(qt_("Normal text style    \\textstyle"), 2);
130         m->insertItem(qt_("Script (small) style \\scriptstyle"), 3);
131         m->insertItem(qt_("Scriptscript (smaller) style \\scriptscriptstyle"), 4);
132         connect(m, SIGNAL(activated(int)), this, SLOT(insertStyle(int)));
133         stylePB->setPopup(m);
134
135         m = new QPopupMenu(fontPB);
136         m->setCaption(qt_("LyX: Set math font"));
137         m->insertTearOffHandle();
138         m->insertItem(qt_("Roman        \\mathrm"), 1);
139         m->insertItem(qt_("Bold \\mathbf"), 2);
140         m->insertItem(qt_("Sans serif   \\mathsf"), 3);
141         m->insertItem(qt_("Italic       \\mathit"), 4);
142         m->insertItem(qt_("Typewriter   \\mathtt"), 5);
143         m->insertItem(qt_("Blackboard   \\mathbb"), 6);
144         m->insertItem(qt_("Fraktur      \\mathfrak"), 7);
145         m->insertItem(qt_("Calligraphic \\mathcal"), 8);
146         m->insertItem(qt_("Normal text mode     \\textrm"), 9);
147         connect(m, SIGNAL(activated(int)), this, SLOT(insertFont(int)));
148         fontPB->setPopup(m);
149 }
150
151
152 void QMathDialog::showingPanel(int num)
153 {
154         if (panel_initialised[num])
155                 return;
156
157         addPanel(num);
158
159         // Qt needs to catch up. Dunno why.
160         qApp->processEvents();
161
162         panel_initialised[num] = true;
163 }
164
165
166 IconPalette * QMathDialog::makePanel(QWidget * parent, char const ** entries)
167 {
168         IconPalette * p = new IconPalette(parent);
169         for (int i = 0; *entries[i]; ++i) {
170                 p->add(QPixmap(toqstr(find_xpm(entries[i]))), entries[i], string("\\") + entries[i]);
171         }
172         connect(p, SIGNAL(button_clicked(const string &)), this, SLOT(symbol_clicked(const string &)));
173
174         return p;
175 }
176
177
178 void QMathDialog::addPanel(int num)
179 {
180         QScrollViewSingle * view = static_cast<QScrollViewSingle*>(symbolsWS->widget(num));
181         IconPalette * p = makePanel(view->viewport(), panels[num]);
182         view->setChild(p);
183 }
184
185
186 void QMathDialog::symbol_clicked(const string & str)
187 {
188         form_->controller().dispatchInsert(str);
189 }
190
191
192 void QMathDialog::fracClicked()
193 {
194         form_->controller().dispatchInsert("frac");
195 }
196
197
198 void QMathDialog::delimiterClicked()
199 {
200         form_->controller().showDialog("mathdelimiter");
201 }
202
203
204 void QMathDialog::expandClicked()
205 {
206         int const id = symbolsWS->id(symbolsWS->visibleWidget());
207         IconPalette * p = makePanel(0, panels[id]);
208         string s = "LyX: ";
209         s += fromqstr(symbolsCO->text(id));
210         p->setCaption(toqstr(s));
211         p->resize(40 * 5, p->height());
212         p->show();
213         p->setMaximumSize(p->width(), p->height());
214 }
215
216
217 void QMathDialog::functionSelected(const QString & str)
218 {
219         form_->controller().dispatchInsert(fromqstr(str));
220 }
221
222
223 void QMathDialog::matrixClicked()
224 {
225         form_->controller().showDialog("mathmatrix");
226 }
227
228
229 void QMathDialog::equationClicked()
230 {
231         form_->controller().dispatchToggleDisplay();
232 }
233
234
235 void QMathDialog::subscriptClicked()
236 {
237         form_->controller().dispatchSubscript();
238 }
239
240
241 void QMathDialog::superscriptClicked()
242 {
243         form_->controller().dispatchSuperscript();
244 }
245
246
247 void QMathDialog::insertSpace(int id)
248 {
249         string str;
250         switch (id) {
251                 case 1: str = ","; break;
252                 case 2: str = ":"; break;
253                 case 3: str = ";"; break;
254                 case 4: str = "quad"; break;
255                 case 5: str = "qquad"; break;
256                 case 6: str = "!"; break;
257                 default: return;
258         }
259         form_->controller().dispatchInsert(str);
260 }
261
262
263 void QMathDialog::insertRoot(int id)
264 {
265         switch (id) {
266                 case 1:
267                         form_->controller().dispatchInsert("sqrt");
268                         break;
269                 case 2:
270                         form_->controller().dispatchCubeRoot();
271                         break;
272                 case 3:
273                         form_->controller().dispatchInsert("root");
274                         break;
275         }
276 }
277
278
279 void QMathDialog::insertStyle(int id)
280 {
281         string str;
282         switch (id) {
283                 case 1: str = "displaystyle"; break;
284                 case 2: str = "textstyle"; break;
285                 case 3: str = "scriptstyle"; break;
286                 case 4: str = "scriptscriptstyle"; break;
287                 default: return;
288         }
289         form_->controller().dispatchInsert(str);
290 }
291
292
293 void QMathDialog::insertFont(int id)
294 {
295         string str;
296         switch (id) {
297                 case 1: str = "mathrm"; break;
298                 case 2: str = "mathbf"; break;
299                 case 3: str = "mathsf"; break;
300                 case 4: str = "mathit"; break;
301                 case 5: str = "mathtt"; break;
302                 case 6: str = "mathbb"; break;
303                 case 7: str = "mathfrak"; break;
304                 case 8: str = "mathcal"; break;
305                 case 9: str = "textrm"; break;
306                 default: return;
307         }
308         form_->controller().dispatchInsert(str);
309 }