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