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