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
GuiMathMatrix(GuiView & lv)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(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
78 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
79
80 connect(table, SIGNAL(rowsChanged(int)),
81 rowsSB, SLOT(setValue(int)));
82 connect(table, SIGNAL(colsChanged(int)),
83 columnsSB, SLOT(setValue(int)));
84 connect(rowsSB, SIGNAL(valueChanged(int)),
85 table, SLOT(setNumberRows(int)));
86 connect(columnsSB, SIGNAL(valueChanged(int)),
87 table, SLOT(setNumberColumns(int)));
88 connect(rowsSB, SIGNAL(valueChanged(int)),
89 this, SLOT(change_adaptor()));
90 connect(columnsSB, SIGNAL(valueChanged(int)),
91 this, SLOT(columnsChanged(int)) );
92 connect(valignCO, SIGNAL(highlighted(QString)),
93 this, SLOT(change_adaptor()));
94 connect(halignED, SIGNAL(textChanged(QString)),
95 this, SLOT(change_adaptor()));
96 connect(decorationCO, SIGNAL(activated(int)),
97 this, SLOT(decorationChanged(int)));
98
99 bc().setPolicy(ButtonPolicy::IgnorantPolicy);
100 }
101
102
columnsChanged(int)103 void GuiMathMatrix::columnsChanged(int)
104 {
105 int const nx = int(columnsSB->value());
106 halignED->setText(QString(nx, 'c'));
107 }
108
109
decorationChanged(int deco)110 void GuiMathMatrix::decorationChanged(int deco)
111 {
112 // a matrix with a decoration cannot have a vertical alignment
113 if (deco != 0) {
114 valignCO->setEnabled(false);
115 valignCO->setCurrentIndex(1);
116 } else
117 valignCO->setEnabled(true);
118 }
119
120
change_adaptor()121 void GuiMathMatrix::change_adaptor()
122 {
123 // FIXME: We need a filter for the halign input
124 }
125
126
slotOK()127 void GuiMathMatrix::slotOK()
128 {
129 int const nx = columnsSB->value();
130 int const ny = rowsSB->value();
131 // a matrix without a decoration is an array,
132 // otherwise it is an AMS matrix
133 // decorated matrices cannot have a vertical alignment
134
135 char const c = v_align_c[valignCO->currentIndex()];
136 QString const sh = halignED->text();
137 string const str = fromqstr(
138 QString("%1 %2 %3 %4").arg(nx).arg(ny).arg(c).arg(sh));
139
140 if (decorationCO->currentIndex() != 0) {
141 int const deco = decorationCO->currentIndex();
142 QString deco_name = DecoNames[deco - 1];
143 // only if a special alignment is set create a 1x1 AMS array in which
144 // a normal array will be created, otherwise create just a normal AMS array
145 if (sh.contains('l') || sh.contains('r')) {
146 string const str_ams = fromqstr(
147 QString("%1 %2 %3").arg(int(1)).arg(int(1)).arg(deco_name));
148 dispatch(FuncRequest(LFUN_MATH_AMS_MATRIX, str_ams));
149 } else {
150 string const str_ams = fromqstr(
151 QString("%1 %2 %3").arg(nx).arg(ny).arg(deco_name));
152 dispatch(FuncRequest(LFUN_MATH_AMS_MATRIX, str_ams));
153 close();
154 return;
155 }
156 }
157 // create the normal array
158 dispatch(FuncRequest(LFUN_MATH_MATRIX, str));
159 close();
160 }
161
162
slotClose()163 void GuiMathMatrix::slotClose()
164 {
165 close();
166 }
167
168
createGuiMathMatrix(GuiView & lv)169 Dialog * createGuiMathMatrix(GuiView & lv) { return new GuiMathMatrix(lv); }
170
171
172 } // namespace frontend
173 } // namespace lyx
174
175 #include "moc_GuiMathMatrix.cpp"
176