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