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