]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QMathDialog.C
* qt_helpers.h:
[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
16 #include <QPixmap>
17 #include <QScrollArea>
18 #include <QMenu>
19 #include <QPushButton>
20 #include <QListWidget>
21 #include <QListWidgetItem>
22 #include <QIcon>
23
24 #include "iconpalette.h"
25 #include "qt_helpers.h"
26 #include "controllers/ControlMath.h"
27
28 #include "support/filetools.h"
29
30 using std::string;
31
32 namespace lyx {
33
34 using support::FileName;
35 using support::libFileSearch;
36
37 namespace frontend {
38
39
40 namespace {
41
42 char const ** panels[] = {
43         latex_bop, latex_varsz, latex_brel, latex_greek, latex_arrow,
44         latex_dots, latex_deco, latex_misc, latex_ams_ops,
45         latex_ams_rel, latex_ams_nrel, latex_ams_arrows,
46         latex_ams_misc
47 };
48
49 int const nr_panels = sizeof(panels)/sizeof(panels[0]);
50
51 int  panel_index[nr_panels];
52
53 } // namespace anon
54
55
56 QMathDialog::QMathDialog(QMath * form)
57         : form_(form)
58 {
59         setupUi(this);
60
61         // enlarge the symbols ComboBox (no scrollbar)
62         //symbolsCO->setSizeLimit(13);
63
64         connect( tearoffPB, SIGNAL( clicked() ), this, SLOT( expandClicked() ) );
65         connect( closePB, SIGNAL( clicked() ), this, SLOT( accept() ) );
66         connect( fracPB, SIGNAL( clicked() ), this, SLOT( fracClicked() ) );
67         connect( superscriptPB, SIGNAL( clicked() ), this, SLOT( superscriptClicked() ) );
68         connect( subscriptPB, SIGNAL( clicked() ), this, SLOT( subscriptClicked() ) );
69         connect( delimitersPB, SIGNAL( clicked() ), this, SLOT( delimiterClicked() ) );
70         connect( matrixPB, SIGNAL( clicked() ), this, SLOT( matrixClicked() ) );
71         connect( functionsLW, SIGNAL( itemActivated(QListWidgetItem *)  ), this, SLOT( functionSelected(QListWidgetItem *) ) );
72         connect( equationPB, SIGNAL( clicked() ), this, SLOT( equationClicked() ) );
73         connect( symbolsCO, SIGNAL(activated(int)), this, SLOT(showingPanel(int)));
74
75         FileName icon_path = libFileSearch("images/math", "sqrt-square", "xpm");
76         sqrtPB->setIcon(QIcon(toqstr(icon_path.absFilename())));
77         icon_path = libFileSearch("images/math", "space", "xpm");
78         spacePB->setIcon(QIcon(toqstr(icon_path.absFilename())));
79         icon_path = libFileSearch("images/math", "style", "xpm");
80         stylePB->setIcon(QIcon(toqstr(icon_path.absFilename())));
81         icon_path = libFileSearch("images/math", "font", "xpm");
82         fontPB->setIcon(QIcon(toqstr(icon_path.absFilename())));
83         icon_path = libFileSearch("images/math", "equation", "xpm");
84         equationPB->setIcon(QIcon(toqstr(icon_path.absFilename())));
85         icon_path = libFileSearch("images/math", "frac-square", "xpm");
86         fracPB->setIcon(QIcon(toqstr(icon_path.absFilename())));
87         icon_path = libFileSearch("images/math", "sub", "xpm");
88         subscriptPB->setIcon(QIcon(toqstr(icon_path.absFilename())));
89         icon_path = libFileSearch("images/math", "super", "xpm");
90         superscriptPB->setIcon(QIcon(toqstr(icon_path.absFilename())));
91         icon_path = libFileSearch("images/math", "matrix", "xpm");
92         matrixPB->setIcon(QIcon(toqstr(icon_path.absFilename())));
93         icon_path = libFileSearch("images/math", "delim", "xpm");
94         delimitersPB->setIcon(QIcon(toqstr(icon_path.absFilename())));
95
96         // function list
97         for (int i = 0; *function_names[i]; ++i) {
98                 functionsLW->addItem(function_names[i]);
99         }
100         //functionsLW->setFixedWidth(functionsLW->sizeHint().width());
101
102         // show first symbol panel
103         showingPanel(0);
104
105         // add menu's to the buttons
106         QMenu * m = new QMenu(spacePB);
107         m->setTitle(qt_("LyX: Math Spacing"));
108         m->setTearOffEnabled(true);
109         addMenuItem(m, qt_("Thin space  \\,"), ",");
110         addMenuItem(m, qt_("Medium space        \\:"), ":");
111         addMenuItem(m, qt_("Thick space \\;"), ";");
112         addMenuItem(m, qt_("Quadratin space     \\quad"), "quad");
113         addMenuItem(m, qt_("Double quadratin space      \\qquad"), "qquad");
114         addMenuItem(m, qt_("Negative space      \\!"), "!");
115         spacePB->setMenu(m);
116
117         m = new QMenu(sqrtPB);
118         m->setTitle(qt_("LyX: Math Roots"));
119         m->setTearOffEnabled(true);
120         addMenuItem(m, qt_("Square root \\sqrt"), "sqrt");
121         QAction * ma = new QAction(qt_("Cube root       \\root"), this);
122         connect(ma, SIGNAL(triggered()), this, SLOT(insertCubeRoot()));
123         m->addAction(ma);
124         addMenuItem(m, qt_("Other root  \\root"), "root");
125         sqrtPB->setMenu(m);
126
127         m = new QMenu(stylePB);
128         m->setTitle(qt_("LyX: Math Styles"));
129         m->setTearOffEnabled(true);
130         addMenuItem(m, qt_("Display style       \\displaystyle"), "displaystyle");
131         addMenuItem(m, qt_("Normal text style   \\textstyle"), "textstyle");
132         addMenuItem(m, qt_("Script (small) style        \\scriptstyle"), "scriptstyle");
133         addMenuItem(m, qt_("Scriptscript (smaller) style        \\scriptscriptstyle"), "scriptscriptstyle");
134         stylePB->setMenu(m);
135
136         m = new QMenu(fracPB);
137         m->setTitle(qt_("LyX: Fractions"));
138         m->setTearOffEnabled(true);
139         addMenuItem(m, qt_("Standard    \\frac"), "frac");
140         addMenuItem(m, qt_("No hor. line        \\atop"), "atop");
141         addMenuItem(m, qt_("Nice        \\nicefrac"), "nicefrac");
142         addMenuItem(m, qt_("Text frac (amsmath) \\tfrac"), "tfrac");
143         addMenuItem(m, qt_("Display frac (amsmath)      \\dfrac"), "dfrac");
144         addMenuItem(m, qt_("Binomial    \\choose"),  "choose");
145         fracPB->setMenu(m);
146
147         m = new QMenu(fontPB);
148         m->setTitle(qt_("LyX: Math Fonts"));
149         m->setTearOffEnabled(true);
150         addMenuItem(m, qt_("Roman       \\mathrm"), "mathrm");
151         addMenuItem(m, qt_("Bold        \\mathbf"), "mathbf");
152         addMenuItem(m, qt_("Bold symbol \\boldsymbol"), "boldsymbol");
153         addMenuItem(m, qt_("Sans serif  \\mathsf"), "mathsf");
154         addMenuItem(m, qt_("Italic      \\mathit"), "mathit");
155         addMenuItem(m, qt_("Typewriter  \\mathtt"), "mathtt");
156         addMenuItem(m, qt_("Blackboard  \\mathbb"), "mathbb");
157         addMenuItem(m, qt_("Fraktur     \\mathfrak"), "mathfrak");
158         addMenuItem(m, qt_("Calligraphic        \\mathcal"), "mathcal");
159         addMenuItem(m, qt_("Normal text mode    \\textrm"), "textrm");
160         fontPB->setMenu(m);
161 }
162
163 void QMathDialog::addMenuItem(QMenu * menu, const QString & label, const std::string & action)
164 {
165         QMAction * ma = new QMAction(label,action, this);
166         connect(ma, SIGNAL(action(const std::string &)), this, SLOT(symbol_clicked(const std::string &)));
167         menu->addAction(ma);
168 }
169
170 void QMathDialog::showingPanel(int num)
171 {
172         if (!panel_index[num])
173                 addPanel(num);
174
175         symbolWS->setCurrentIndex(panel_index[num]);
176 }
177
178
179 IconPalette * QMathDialog::makePanel(QWidget * parent, char const ** entries)
180 {
181         IconPalette * p = new IconPalette(parent, entries);
182         // Leave these std:: qualifications alone !
183         connect(p, SIGNAL(button_clicked(const std::string &)),
184                 this, SLOT(symbol_clicked(const std::string &)));
185
186         return p;
187 }
188
189
190 void QMathDialog::addPanel(int num)
191 {
192         QScrollArea * sc = new QScrollArea(symbolWS);
193         IconPalette * p = makePanel(this, panels[num]);
194         sc->setWidget(p);
195         panel_index[num] = symbolWS->addWidget(sc);
196 }
197
198
199 void QMathDialog::symbol_clicked(const string & str)
200 {
201         form_->controller().dispatchInsert(str);
202 }
203
204
205 void QMathDialog::fracClicked()
206 {
207         form_->controller().dispatchInsert("frac");
208 }
209
210
211 void QMathDialog::delimiterClicked()
212 {
213         form_->controller().showDialog("mathdelimiter");
214 }
215
216
217 void QMathDialog::expandClicked()
218 {
219         int const id = symbolsCO->currentIndex();
220         IconPalette * p = makePanel(this, panels[id]);
221         string s = "LyX: ";
222         s += fromqstr(symbolsCO->currentText());
223         p->setWindowTitle(toqstr(s));
224         p->setWindowFlags(Qt::Dialog);
225         p->show();
226 }
227
228
229 void QMathDialog::functionSelected(QListWidgetItem * item)
230 {
231         form_->controller().dispatchInsert(fromqstr(item->text()));
232 }
233
234
235 void QMathDialog::matrixClicked()
236 {
237         form_->controller().showDialog("mathmatrix");
238 }
239
240
241 void QMathDialog::equationClicked()
242 {
243         form_->controller().dispatchToggleDisplay();
244 }
245
246
247 void QMathDialog::subscriptClicked()
248 {
249         form_->controller().dispatchSubscript();
250 }
251
252
253 void QMathDialog::superscriptClicked()
254 {
255         form_->controller().dispatchSuperscript();
256 }
257
258
259 void QMathDialog::insertCubeRoot()
260 {
261         form_->controller().dispatchCubeRoot();
262 }
263
264
265 } // namespace frontend
266 } // namespace lyx
267
268 #include "QMathDialog_moc.cpp"