]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiMathMatrix.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiMathMatrix.cpp
1 /**
2  * \file GuiMathMatrix.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Spitzmüller
7  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiMathMatrix.h"
15
16 #include "EmptyTable.h"
17 #include "qt_helpers.h"
18
19 #include "FuncRequest.h"
20
21 #include "support/gettext.h"
22
23 #include <QLineEdit>
24 #include <QPushButton>
25 #include <QSpinBox>
26
27 using namespace std;
28
29 namespace lyx {
30 namespace frontend {
31
32 static char const * const DecoChars[] = {
33         N_("None"),
34         N_("[x]"),
35         N_("(x)"),
36         N_("{x}"),
37         N_("|x|"),
38         N_("||x||"),
39         ""
40 };
41
42 static char const * const DecoNames[] = {
43         N_("bmatrix"),
44         N_("pmatrix"),
45         N_("Bmatrix"),
46         N_("vmatrix"),
47         N_("Vmatrix"),
48         ""
49 };
50
51 static char const * const VertAligns[] = {
52         N_("Top"),
53         N_("Middle"),
54         N_("Bottom"),
55         ""
56 };
57
58 static char const v_align_c[] = "tcb";
59
60
61 GuiMathMatrix::GuiMathMatrix(GuiView & lv)
62         : GuiDialog(lv, "mathmatrix", qt_("Math Matrix"))
63 {
64         setupUi(this);
65
66         for (int i = 0; *VertAligns[i]; ++i)
67                 valignCO->addItem(qt_(VertAligns[i]));
68         for (int i = 0; *DecoChars[i]; ++i)
69                 decorationCO->addItem(qt_(DecoChars[i]));
70
71         table->setMinimumSize(100, 100);
72         rowsSB->setValue(5);
73         columnsSB->setValue(5);
74         valignCO->setCurrentIndex(1);
75         decorationCO->setCurrentIndex(0);
76
77         connect(table, SIGNAL(rowsChanged(int)),
78                 rowsSB, SLOT(setValue(int)));
79         connect(table, SIGNAL(colsChanged(int)),
80                 columnsSB, SLOT(setValue(int)));
81         connect(rowsSB, SIGNAL(valueChanged(int)),
82                 table, SLOT(setNumberRows(int)));
83         connect(columnsSB, SIGNAL(valueChanged(int)),
84                 table, SLOT(setNumberColumns(int)));
85         connect(rowsSB, SIGNAL(valueChanged(int)),
86                 this, SLOT(change_adaptor()));
87         connect(columnsSB, SIGNAL(valueChanged(int)),
88                 this, SLOT(columnsChanged(int)) );
89         connect(valignCO, SIGNAL(highlighted(QString)),
90                 this, SLOT(change_adaptor()));
91         connect(halignED, SIGNAL(textChanged(QString)),
92                 this, SLOT(change_adaptor()));
93         connect(decorationCO, SIGNAL(activated(int)),
94                 this, SLOT(decorationChanged(int)));
95
96         bc().setPolicy(ButtonPolicy::IgnorantPolicy);
97 }
98
99
100 void GuiMathMatrix::columnsChanged(int)
101 {
102         int const nx = int(columnsSB->value());
103         halignED->setText(QString(nx, 'c'));
104 }
105
106
107 void GuiMathMatrix::decorationChanged(int deco)
108 {
109         // a matrix with a decoration cannot have a vertical alignment
110         if (deco != 0) {
111                 valignCO->setEnabled(false);
112                 valignCO->setCurrentIndex(1);
113         } else
114                 valignCO->setEnabled(true);
115 }
116
117
118 void GuiMathMatrix::change_adaptor()
119 {
120         // FIXME: We need a filter for the halign input
121 }
122
123
124 void GuiMathMatrix::on_buttonBox_clicked(QAbstractButton * button)
125 {
126         switch (buttonBox->standardButton(button)) {
127         case QDialogButtonBox::Ok:
128                 slotOK();
129                 break;
130         case QDialogButtonBox::Cancel:
131                 slotClose();
132                 break;
133         default:
134                 break;
135         }
136 }
137
138
139 void GuiMathMatrix::slotOK()
140 {
141         int const nx = columnsSB->value();
142         int const ny = rowsSB->value();
143         // a matrix without a decoration is an array,
144         // otherwise it is an AMS matrix
145         // decorated matrices cannot have a vertical alignment
146
147         char const c = v_align_c[valignCO->currentIndex()];
148         QString const sh = halignED->text();
149         string const str = fromqstr(
150                 QString("%1 %2 %3 %4").arg(nx).arg(ny).arg(c).arg(sh));
151
152         if (decorationCO->currentIndex() != 0) {
153                 int const deco = decorationCO->currentIndex();
154                 QString deco_name = DecoNames[deco - 1];
155                 // only if a special alignment is set create a 1x1 AMS array in which
156                 // a normal array will be created, otherwise create just a normal AMS array
157                 if (sh.contains('l') || sh.contains('r')) {
158                         string const str_ams = fromqstr(
159                                 QString("%1 %2 %3").arg(int(1)).arg(int(1)).arg(deco_name));
160                         dispatch(FuncRequest(LFUN_MATH_AMS_MATRIX, str_ams));
161                 } else {
162                         string const str_ams = fromqstr(
163                                 QString("%1 %2 %3").arg(nx).arg(ny).arg(deco_name));
164                         dispatch(FuncRequest(LFUN_MATH_AMS_MATRIX, str_ams));
165                         close();
166                         return;
167                 }
168         }
169         // create the normal array
170                 dispatch(FuncRequest(LFUN_MATH_MATRIX, str));
171         close();
172 }
173
174
175 void GuiMathMatrix::slotClose()
176 {
177         close();
178 }
179
180
181 Dialog * createGuiMathMatrix(GuiView & lv) { return new GuiMathMatrix(lv); }
182
183
184 } // namespace frontend
185 } // namespace lyx
186
187 #include "moc_GuiMathMatrix.cpp"