3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
14 #include "support/filetools.h"
15 #include "qt_helpers.h"
18 #include "ControlMath.h"
20 #include "QMathDialog.h"
23 #include "iconpalette.h"
24 #include "QDelimiterDialog.h"
25 #include "QMathMatrixDialog.h"
27 #include <qapplication.h>
28 #include <qwidgetstack.h>
29 #include <qcombobox.h>
30 #include <qpushbutton.h>
33 #include <qscrollview.h>
35 #include <qpopupmenu.h>
43 class QScrollViewSingle : public QScrollView {
45 QScrollViewSingle(QWidget * p)
46 : QScrollView(p), w_(0) {
47 setResizePolicy(Manual);
48 setHScrollBarMode(AlwaysOff);
49 setVScrollBarMode(AlwaysOn);
50 setBackgroundMode(PaletteBackground);
51 viewport()->setBackgroundMode(PaletteBackground);
54 void setChild(QWidget * w) {
56 setMinimumWidth(verticalScrollBar()->width() + w_->width() + 4);
60 virtual void resizeEvent(QResizeEvent * e) {
61 QScrollView::resizeEvent(e);
65 w_->resize(viewport()->width(), w_->height());
66 // force the resize to get accurate scrollbars
67 qApp->processEvents();
68 resizeContents(w_->width(), w_->height());
76 char const ** panels[] = {
77 latex_bop, latex_varsz, latex_brel, latex_greek, latex_arrow,
78 latex_dots, latex_deco, latex_misc, latex_ams_ops,
79 latex_ams_rel, latex_ams_nrel, latex_ams_arrows,
83 int const nr_panels = sizeof(panels)/sizeof(panels[0]);
85 bool panel_initialised[nr_panels];
90 QMathDialog::QMathDialog(QMath * form)
91 : QMathDialogBase(0, 0, false, 0),
94 // enlarge the symbols ComboBox (no scrollbar)
95 symbolsCO->setSizeLimit(13);
97 connect(symbolsCO, SIGNAL(activated(int)), symbolsWS, SLOT(raiseWidget(int)));
99 for (int i = 0; *function_names[i]; ++i) {
100 functionsLB->insertItem(function_names[i]);
103 for (int i = 0; i < nr_panels; ++i) {
104 QScrollViewSingle * view = new QScrollViewSingle(symbolsWS);
105 symbolsWS->addWidget(view, i);
108 // aboutToShow() only fires when != 0 in Qt 2 !
109 symbolsWS->raiseWidget(0);
111 panel_initialised[0] = true;
113 connect(symbolsWS, SIGNAL(aboutToShow(int)), this, SLOT(showingPanel(int)));
115 QPopupMenu * m = new QPopupMenu(spacePB);
116 m->setCaption(qt_("LyX: Insert space"));
117 m->insertTearOffHandle();
118 m->insertItem(qt_("Thin space \\,"), 1);
119 m->insertItem(qt_("Medium space \\:"), 2);
120 m->insertItem(qt_("Thick space \\;"), 3);
121 m->insertItem(qt_("Quadratin space \\quad"), 4);
122 m->insertItem(qt_("Double quadratin space \\qquad"), 5);
123 m->insertItem(qt_("Negative space \\!"), 6);
124 connect(m, SIGNAL(activated(int)), this, SLOT(insertSpace(int)));
125 spacePB->setPopup(m);
127 m = new QPopupMenu(sqrtPB);
128 m->setCaption(qt_("LyX: Insert root"));
129 m->insertTearOffHandle();
130 m->insertItem(qt_("Square root \\sqrt"), 1);
131 m->insertItem(qt_("Cube root \\root"), 2);
132 m->insertItem(qt_("Other root \\root"), 3);
133 connect(m, SIGNAL(activated(int)), this, SLOT(insertRoot(int)));
136 m = new QPopupMenu(stylePB);
137 m->setCaption(qt_("LyX: Set math style"));
138 m->insertTearOffHandle();
139 m->insertItem(qt_("Display style \\displaystyle"), 1);
140 m->insertItem(qt_("Normal text style \\textstyle"), 2);
141 m->insertItem(qt_("Script (small) style \\scriptstyle"), 3);
142 m->insertItem(qt_("Scriptscript (smaller) style \\scriptscriptstyle"), 4);
143 connect(m, SIGNAL(activated(int)), this, SLOT(insertStyle(int)));
144 stylePB->setPopup(m);
146 m = new QPopupMenu(fontPB);
147 m->setCaption(qt_("LyX: Set math font"));
148 m->insertTearOffHandle();
149 m->insertItem(qt_("Roman \\mathrm"), 1);
150 m->insertItem(qt_("Bold \\mathbf"), 2);
151 m->insertItem(qt_("Sans serif \\mathsf"), 3);
152 m->insertItem(qt_("Italic \\mathit"), 4);
153 m->insertItem(qt_("Typewriter \\mathtt"), 5);
154 m->insertItem(qt_("Blackboard \\mathbb"), 6);
155 m->insertItem(qt_("Fraktur \\mathfrak"), 7);
156 m->insertItem(qt_("Calligraphic \\mathcal"), 8);
157 m->insertItem(qt_("Normal text mode \\textrm"), 9);
158 connect(m, SIGNAL(activated(int)), this, SLOT(insertFont(int)));
163 void QMathDialog::showingPanel(int num)
165 if (panel_initialised[num])
170 // Qt needs to catch up. Dunno why.
171 qApp->processEvents();
173 panel_initialised[num] = true;
177 IconPalette * QMathDialog::makePanel(QWidget * parent, char const ** entries)
179 IconPalette * p = new IconPalette(parent);
180 for (int i = 0; *entries[i]; ++i) {
181 p->add(QPixmap(toqstr(find_xpm(entries[i]))), entries[i], string("\\") + entries[i]);
183 connect(p, SIGNAL(button_clicked(const string &)), this, SLOT(symbol_clicked(const string &)));
189 void QMathDialog::addPanel(int num)
191 QScrollViewSingle * view = static_cast<QScrollViewSingle*>(symbolsWS->visibleWidget());
192 IconPalette * p = makePanel(view->viewport(), panels[num]);
197 void QMathDialog::symbol_clicked(string const & str)
203 void QMathDialog::fracClicked()
205 form_->insert("frac");
209 void QMathDialog::delimiterClicked()
212 QDelimiterDialog * d = new QDelimiterDialog(form_);
217 void QMathDialog::expandClicked()
219 int const id = symbolsWS->id(symbolsWS->visibleWidget());
220 IconPalette * p = makePanel(0, panels[id]);
222 s += fromqstr(symbolsCO->text(id));
223 p->setCaption(toqstr(s));
224 p->resize(40 * 5, p->height());
226 p->setMaximumSize(p->width(), p->height());
230 void QMathDialog::functionSelected(const QString & str)
232 form_->insert(fromqstr(str));
236 void QMathDialog::matrixClicked()
239 QMathMatrixDialog * d = new QMathMatrixDialog(form_);
244 void QMathDialog::equationClicked()
246 form_->toggleDisplay();
250 void QMathDialog::subscriptClicked()
256 void QMathDialog::superscriptClicked()
258 form_->superscript();
262 void QMathDialog::insertSpace(int 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;
278 void QMathDialog::insertRoot(int id)
282 form_->insert("sqrt");
285 form_->insertCubeRoot();
288 form_->insert("root");
294 void QMathDialog::insertStyle(int 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;
308 void QMathDialog::insertFont(int id)
312 case 1: str = "mathrm"; break;
313 case 2: str = "mathbf"; break;
314 case 3: str = "mathsf"; break;
315 case 4: str = "mathit"; break;
316 case 5: str = "mathtt"; break;
317 case 6: str = "mathbb"; break;
318 case 7: str = "mathfrak"; break;
319 case 8: str = "mathcal"; break;
320 case 9: str = "textrm"; break;