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