]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiMathMatrix.cpp
GuiMathMatrix.cpp: fix bug #7049
[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 #include "support/docstring.h"
23
24 #include <QLineEdit>
25 #include <QPushButton>
26 #include <QSpinBox>
27
28 using namespace std;
29
30 namespace lyx {
31 namespace frontend {
32
33 static char const * const DecoChars[] = {
34         N_("None"),
35         N_("[x]"),
36         N_("(x)"),
37         N_("{x}"),
38         N_("|x|"),
39         N_("||x||"),
40         ""
41 };
42
43 static char const * const DecoNames[] = {
44         N_("bmatrix"),
45         N_("pmatrix"),
46         N_("Bmatrix"),
47         N_("vmatrix"),
48         N_("Vmatrix"),
49         ""
50 };
51
52 static char const * const VertAligns[] = {
53         N_("Top"),
54         N_("Middle"),
55         N_("Bottom"),
56         ""
57 };
58
59 static char const v_align_c[] = "tcb";
60
61
62 GuiMathMatrix::GuiMathMatrix(GuiView & lv)
63         : GuiDialog(lv, "mathmatrix", qt_("Math Matrix"))
64 {
65         setupUi(this);
66
67         table->setMinimumSize(100, 100);
68         rowsSB->setValue(5);
69         columnsSB->setValue(5);
70         valignCO->setCurrentIndex(1);
71         decorationCO->setCurrentIndex(0);
72
73         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
74         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
75
76         connect(table, SIGNAL(rowsChanged(int)),
77                 rowsSB, SLOT(setValue(int)));
78         connect(table, SIGNAL(colsChanged(int)),
79                 columnsSB, SLOT(setValue(int)));
80         connect(rowsSB, SIGNAL(valueChanged(int)),
81                 table, SLOT(setNumberRows(int)));
82         connect(columnsSB, SIGNAL(valueChanged(int)),
83                 table, SLOT(setNumberColumns(int)));
84         connect(rowsSB, SIGNAL(valueChanged(int)),
85                 this, SLOT(change_adaptor()));
86         connect(columnsSB, SIGNAL(valueChanged(int)),
87                 this, SLOT(columnsChanged(int)) );
88         connect(valignCO, SIGNAL(highlighted(QString)),
89                 this, SLOT(change_adaptor()));
90         connect(halignED, SIGNAL(textChanged(QString)),
91                 this, SLOT(change_adaptor()));
92         connect(decorationCO, SIGNAL(activated(int)),
93                 this, SLOT(decorationChanged(int)));
94
95         for (int i = 0; *VertAligns[i]; ++i)
96                 valignCO->addItem(qt_(VertAligns[i]));
97         for (int i = 0; *DecoChars[i]; ++i)
98                 decorationCO->addItem(qt_(DecoChars[i]));
99
100         bc().setPolicy(ButtonPolicy::IgnorantPolicy);
101 }
102
103
104 void GuiMathMatrix::columnsChanged(int)
105 {
106         int const nx = int(columnsSB->value());
107         halignED->setText(QString(nx, 'c'));
108 }
109
110
111 void GuiMathMatrix::decorationChanged(int deco)
112 {
113         // a matrix with a decoration cannot have a vertical alignment
114         if (deco != 0) {
115                 valignCO->setEnabled(false);
116                 valignCO->setCurrentIndex(1);
117         } else
118                 valignCO->setEnabled(true);
119 }
120
121
122 void GuiMathMatrix::change_adaptor()
123 {
124         // FIXME: We need a filter for the halign input
125 }
126
127
128 void GuiMathMatrix::slotOK()
129 {
130         int const nx = columnsSB->value();
131         int const ny = rowsSB->value();
132         // a matrix without a decoration is an array,
133         // otherwise it is an AMS matrix
134         // decorated matrices cannot have a vertical alignment
135         
136         char const c = v_align_c[valignCO->currentIndex()];
137         QString const sh = halignED->text();
138         string const str = fromqstr(
139                 QString("%1 %2 %3 %4").arg(nx).arg(ny).arg(c).arg(sh));
140
141         if (decorationCO->currentIndex() != 0) {
142                 int const deco = decorationCO->currentIndex();
143                 QString deco_name = DecoNames[deco - 1];
144                 // only if a special alignment is set create a 1x1 AMS array in which
145                 // a normal array will be created, otherwise create just a normal AMS array
146                 if (sh.contains('l') || sh.contains('r')) {
147                         string const str_ams = fromqstr(
148                                 QString("%1 %2 %3").arg(int(1)).arg(int(1)).arg(deco_name));
149                         dispatch(FuncRequest(LFUN_MATH_AMS_MATRIX, str_ams));
150                 } else {
151                         string const str_ams = fromqstr(
152                                 QString("%1 %2 %3").arg(nx).arg(ny).arg(deco_name));
153                         dispatch(FuncRequest(LFUN_MATH_AMS_MATRIX, str_ams));
154                         close();
155                         return;
156                 }
157         }
158         // create the normal array
159                 dispatch(FuncRequest(LFUN_MATH_MATRIX, str));
160         close();
161 }
162
163
164 void GuiMathMatrix::slotClose()
165 {
166         close();
167 }
168
169
170 Dialog * createGuiMathMatrix(GuiView & lv) { return new GuiMathMatrix(lv); }
171
172
173 } // namespace frontend
174 } // namespace lyx
175
176 #include "moc_GuiMathMatrix.cpp"