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