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