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