]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QMathDialog.C
Fix symbol insert.
[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                 string xpm_name = LibFileSearch("images/math/", entries[i], "xpm");
184                 p->add(QPixmap(xpm_name.c_str()), entries[i], string("\\") + entries[i]);
185         }
186         connect(p, SIGNAL(button_clicked(string const &)), this, SLOT(symbol_clicked(string const &)));
187
188         return p;
189 }
190
191
192 void QMathDialog::addPanel(int num)
193 {
194         QScrollViewSingle * view = static_cast<QScrollViewSingle*>(symbolsWS->visibleWidget());
195         IconPalette * p = makePanel(view->viewport(), panels[num]);
196         view->setChild(p);
197 }
198
199
200 void QMathDialog::symbol_clicked(string const & str)
201 {
202         form_->insert(str);
203 }
204
205
206 void QMathDialog::fracClicked()
207 {
208         form_->insert("frac");
209 }
210
211
212 void QMathDialog::delimiterClicked()
213 {
214         // FIXME: leak
215         QDelimiterDialog * d = new QDelimiterDialog(form_);
216         d->show();
217 }
218
219
220 void QMathDialog::expandClicked()
221 {
222         int const id = symbolsWS->id(symbolsWS->visibleWidget());
223         IconPalette * p = makePanel(0, panels[id]);
224         string s = "LyX: ";
225         s += symbolsCO->text(id).latin1();
226         p->setCaption(s.c_str());
227         p->resize(40 * 5, p->height());
228         p->show();
229         p->setMaximumSize(p->width(), p->height());
230 }
231
232
233 void QMathDialog::functionSelected(const QString & str)
234 {
235         form_->insert(str.latin1());
236 }
237
238
239 void QMathDialog::matrixClicked()
240 {
241         // FIXME: leak?
242         QMathMatrixDialog * d = new QMathMatrixDialog(form_);
243         d->show();
244 }
245
246
247 void QMathDialog::equationClicked()
248 {
249         form_->toggleDisplay();
250 }
251
252
253 void QMathDialog::subscriptClicked()
254 {
255         form_->subscript();
256 }
257
258
259 void QMathDialog::superscriptClicked()
260 {
261         form_->superscript();
262 }
263
264
265 void QMathDialog::insertSpace(int id)
266 {
267         string str;
268         switch (id) {
269                 case 1: str = ","; break;
270                 case 2: str = ":"; break;
271                 case 3: str = ";"; break;
272                 case 4: str = "quad"; break;
273                 case 5: str = "qquad"; break;
274                 case 6: str = "!"; break;
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         }
305         form_->insert(str);
306 }
307
308
309 void QMathDialog::insertFont(int id)
310 {
311         string str;
312         switch (id) {
313                 case 1: str = "mathrm"; break;
314                 case 2: str = "mathbf"; break;
315                 case 3: str = "mathsf"; break;
316                 case 4: str = "mathit"; break;
317                 case 5: str = "mathtt"; break;
318                 case 6: str = "mathbb"; break;
319                 case 7: str = "mathfrak"; break;
320                 case 8: str = "mathcal"; break;
321                 case 9: str = "textrm"; break;
322         }
323         form_->insert(str);
324 }