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