]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiHSpace.cpp
Fix the tab ordering of GuiDocument components.
[lyx.git] / src / frontends / qt4 / GuiHSpace.cpp
1 /**
2  * \file GuiHSpace.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiHSpace.h"
14
15 #include "LengthCombo.h"
16 #include "qt_helpers.h"
17 #include "Validator.h"
18
19 #include "Spacing.h"
20
21 #include "insets/InsetSpace.h"
22
23 #include "mathed/InsetMathSpace.h"
24
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27
28 #include <QCheckBox>
29 #include <QLineEdit>
30 #include <QPushButton>
31 #include <QValidator>
32
33 using namespace std;
34
35 namespace lyx {
36 namespace frontend {
37
38 GuiHSpace::GuiHSpace(bool math_mode, QWidget * parent)
39         : InsetParamsWidget(parent), math_mode_(math_mode)
40 {
41         setupUi(this);
42
43         spacingCO->clear();
44         if (math_mode_) {
45                 spacingCO->addItem(qt_("Thin Space"), toqstr("thinspace"));
46                 spacingCO->addItem(qt_("Medium Space"), toqstr("medspace"));
47                 spacingCO->addItem(qt_("Thick Space"), toqstr("thickspace"));
48                 spacingCO->addItem(qt_("Negative Thin Space"), toqstr("negthinspace"));
49                 spacingCO->addItem(qt_("Negative Medium Space"), toqstr("negmedspace"));
50                 spacingCO->addItem(qt_("Negative Thick Space"), toqstr("negthickspace"));
51                 spacingCO->addItem(qt_("Half Quad (0.5 em)"), toqstr("halfquad"));
52                 spacingCO->addItem(qt_("Quad (1 em)"), toqstr("quad"));
53                 spacingCO->addItem(qt_("Double Quad (2 em)"), toqstr("qquad"));
54                 spacingCO->addItem(qt_("Horizontal Fill"), toqstr("hfill"));
55                 spacingCO->addItem(qt_("Custom"), toqstr("custom"));
56         } else {
57                 spacingCO->addItem(qt_("Interword Space"), toqstr("normal"));
58                 spacingCO->addItem(qt_("Thin Space"), toqstr("thinspace"));
59                 spacingCO->addItem(qt_("Negative Thin Space"), toqstr("negthinspace"));
60                 spacingCO->addItem(qt_("Negative Medium Space"), toqstr("negmedspace"));
61                 spacingCO->addItem(qt_("Negative Thick Space"), toqstr("negthickspace"));
62                 spacingCO->addItem(qt_("Half Quad (0.5 em)"), toqstr("halfquad"));
63                 spacingCO->addItem(qt_("Quad (1 em)"), toqstr("quad"));
64                 spacingCO->addItem(qt_("Double Quad (2 em)"), toqstr("qquad"));
65                 spacingCO->addItem(qt_("Horizontal Fill"), toqstr("hfill"));
66                 spacingCO->addItem(qt_("Custom"), toqstr("custom"));
67                 spacingCO->addItem(qt_("Visible Space"), toqstr("visible"));
68         }
69
70         connect(spacingCO, SIGNAL(highlighted(QString)),
71                 this, SLOT(changedSlot()));
72         connect(valueLE, SIGNAL(textChanged(QString)),
73                 this, SLOT(changedSlot()));
74         connect(spacingCO, SIGNAL(activated(int)),
75                 this, SLOT(changedSlot()));
76         connect(keepCB, SIGNAL(clicked()),
77                 this, SLOT(changedSlot()));
78         connect(unitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
79                 this, SLOT(changedSlot()));
80         connect(fillPatternCO, SIGNAL(activated(int)),
81                 this, SLOT(changedSlot()));
82
83         if (math_mode_)
84                 valueLE->setValidator(unsignedLengthValidator(valueLE));
85         else
86                 valueLE->setValidator(unsignedGlueLengthValidator(valueLE));
87
88         // initialize the length validator
89         addCheckedWidget(valueLE, valueL);
90 }
91
92
93 void GuiHSpace::changedSlot()
94 {
95         enableWidgets();
96         changed();
97 }
98
99
100 void GuiHSpace::enableWidgets()
101 {
102         QString const selection = spacingCO->itemData(spacingCO->currentIndex()).toString();
103         bool const custom = selection == "custom";
104         valueLE->setEnabled(custom);
105         unitCO->setEnabled(custom);
106         fillPatternCO->setEnabled(!math_mode_ && selection == "hfill");
107         bool const no_pattern = fillPatternCO->currentIndex() == 0 || math_mode_;
108         bool const enable_keep =
109                 selection == "normal" || selection == "halfquad" || (selection == "hfill" && no_pattern) || custom;
110         keepCB->setEnabled(enable_keep);
111 }
112
113
114 void GuiHSpace::paramsToDialog(Inset const * inset)
115 {
116         InsetSpaceParams const params = math_mode_
117                 ? static_cast<InsetMathSpace const *>(inset)->params()
118                 : static_cast<InsetSpace const *>(inset)->params();
119
120         QString item;
121         int pattern = 0;
122         bool protect = false;
123         switch (params.kind) {
124                 case InsetSpaceParams::NORMAL:
125                         item = "normal";
126                         break;
127                 case InsetSpaceParams::PROTECTED:
128                         item = "normal";
129                         protect = true;
130                         break;
131                 case InsetSpaceParams::VISIBLE:
132                         item = "visible";
133                         protect = true;
134                         break;
135                 case InsetSpaceParams::THIN:
136                         item = "thinspace";
137                         break;
138                 case InsetSpaceParams::MEDIUM:
139                         item = "medspace";
140                         break;
141                 case InsetSpaceParams::THICK:
142                         item = "thickspace";
143                         break;
144                 case InsetSpaceParams::NEGTHIN:
145                         item = "negthinspace";
146                         break;
147                 case InsetSpaceParams::NEGMEDIUM:
148                         item = "negmedspace";
149                         break;
150                 case InsetSpaceParams::NEGTHICK:
151                         item = "negthickspace";
152                         break;
153                 case InsetSpaceParams::ENSKIP:
154                         item = "halfquad";
155                         break;
156                 case InsetSpaceParams::ENSPACE:
157                         item = "halfquad";
158                         protect = true;
159                         break;
160                 case InsetSpaceParams::QUAD:
161                         item = "quad";
162                         break;
163                 case InsetSpaceParams::QQUAD:
164                         item = "qquad";
165                         break;
166                 case InsetSpaceParams::HFILL:
167                         item = "hfill";
168                         break;
169                 case InsetSpaceParams::HFILL_PROTECTED:
170                         item = "hfill";
171                         protect = true;
172                         break;
173                 case InsetSpaceParams::DOTFILL:
174                         item = "hfill";
175                         pattern = 1;
176                         break;
177                 case InsetSpaceParams::HRULEFILL:
178                         item = "hfill";
179                         pattern = 2;
180                         break;
181                 case InsetSpaceParams::LEFTARROWFILL:
182                         item = "hfill";
183                         pattern = 3;
184                         break;
185                 case InsetSpaceParams::RIGHTARROWFILL:
186                         item = "hfill";
187                         pattern = 4;
188                         break;
189                 case InsetSpaceParams::UPBRACEFILL:
190                         item = "hfill";
191                         pattern = 5;
192                         break;
193                 case InsetSpaceParams::DOWNBRACEFILL:
194                         item = "hfill";
195                         pattern = 6;
196                         break;
197                 case InsetSpaceParams::CUSTOM:
198                         item = "custom";
199                         break;
200                 case InsetSpaceParams::CUSTOM_PROTECTED:
201                         item = "custom";
202                         protect = true;
203                         break;
204         }
205         spacingCO->setCurrentIndex(spacingCO->findData(item));
206         fillPatternCO->setCurrentIndex(pattern);
207         keepCB->setChecked(protect);
208         if (item == "halfquad") {
209                 keepCB->setToolTip(qt_("Insert the spacing even after a line break.\n"
210                                        "Note that a protected Half Quad will be turned into\n"
211                                        "a vertical space if used at the beginning of a paragraph!"));
212         } else {
213                 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
214         }
215         Length::UNIT const default_unit = Length::defaultUnit();
216         if (item == "custom") {
217                 string length = params.length.asString();
218                 lengthToWidgets(valueLE, unitCO, length, default_unit);
219         } else
220                 lengthToWidgets(valueLE, unitCO, "", default_unit);
221
222         enableWidgets();
223 }
224
225
226 docstring GuiHSpace::dialogToParams() const
227 {
228         InsetSpaceParams params = math_mode_ ?
229                 InsetSpaceParams(true) : InsetSpaceParams(false);
230
231         QString const item =
232                 spacingCO->itemData(spacingCO->currentIndex()).toString();
233
234         if (item == "normal")
235                 params.kind = keepCB->isChecked() ?
236                         InsetSpaceParams::PROTECTED : InsetSpaceParams::NORMAL;
237         else if (item == "thinspace")
238                 params.kind = InsetSpaceParams::THIN;
239         else if (item == "medspace")
240                 params.kind = InsetSpaceParams::MEDIUM;
241         else if (item == "thickspace")
242                 params.kind = InsetSpaceParams::THICK;
243         else if (item == "negthinspace")
244                 params.kind = InsetSpaceParams::NEGTHIN;
245         else if (item == "negmedspace")
246                 params.kind = InsetSpaceParams::NEGMEDIUM;
247         else if (item == "negthickspace")
248                 params.kind = InsetSpaceParams::NEGTHICK;
249         else if (item == "halfquad")
250                 params.kind = keepCB->isChecked() ?
251                         InsetSpaceParams::ENSPACE : InsetSpaceParams::ENSKIP;
252         else if (item == "quad")
253                         params.kind = InsetSpaceParams::QUAD;
254         else if (item == "qquad")
255                         params.kind = InsetSpaceParams::QQUAD;
256         else if (item == "hfill") {
257                 switch (fillPatternCO->currentIndex()) {
258                 case 1:
259                         params.kind = InsetSpaceParams::DOTFILL;
260                         break;
261                 case 2:
262                         params.kind = InsetSpaceParams::HRULEFILL;
263                         break;
264                 case 3:
265                         params.kind = InsetSpaceParams::LEFTARROWFILL;
266                         break;
267                 case 4:
268                         params.kind = InsetSpaceParams::RIGHTARROWFILL;
269                         break;
270                 case 5:
271                         params.kind = InsetSpaceParams::UPBRACEFILL;
272                         break;
273                 case 6:
274                         params.kind = InsetSpaceParams::DOWNBRACEFILL;
275                         break;
276                 default:
277                         if (keepCB->isChecked())
278                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
279                         else
280                                 params.kind = InsetSpaceParams::HFILL;
281                         break;
282                 }
283         } else if (item == "custom") {
284                         params.kind = keepCB->isChecked() ?
285                                 InsetSpaceParams::CUSTOM_PROTECTED : InsetSpaceParams::CUSTOM;
286                         params.length = GlueLength(widgetsToLength(valueLE, unitCO));
287         } else if (item == "visible") 
288                 params.kind = InsetSpaceParams::VISIBLE;
289
290         return from_ascii(InsetSpace::params2string(params));
291 }
292
293
294 bool GuiHSpace::checkWidgets() const
295 {
296         if (!InsetParamsWidget::checkWidgets())
297                 return false;
298         return spacingCO->itemData(spacingCO->currentIndex()).toString() != "custom"
299                 || !valueLE->text().isEmpty();
300 }
301
302 } // namespace frontend
303 } // namespace lyx
304
305
306 #include "moc_GuiHSpace.cpp"