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