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