]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiHSpace.cpp
Improve read-only mode of InsetParams derived dialogs
[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         if (custom)
108                 valueLE->setFocus();
109         valueL->setEnabled(custom);
110         unitCO->setEnabled(custom);
111         fillPatternCO->setEnabled(!math_mode_ && selection == "hfill");
112         fillPatternL->setEnabled(!math_mode_ && selection == "hfill");
113         bool const no_pattern = fillPatternCO->currentIndex() == 0 || math_mode_;
114         bool const enable_keep =
115                 selection == "normal" || selection == "halfquad"
116                 || (selection == "hfill" && no_pattern) || custom;
117         keepCB->setEnabled(enable_keep);
118         keepL->setEnabled(enable_keep);
119 }
120
121
122 void GuiHSpace::paramsToDialog(Inset const * inset)
123 {
124         InsetSpaceParams const params = math_mode_
125                 ? static_cast<InsetMathSpace const *>(inset)->params()
126                 : static_cast<InsetSpace const *>(inset)->params();
127
128         QString item;
129         int pattern = 0;
130         bool protect = false;
131         switch (params.kind) {
132                 case InsetSpaceParams::NORMAL:
133                         item = "normal";
134                         break;
135                 case InsetSpaceParams::PROTECTED:
136                         item = "normal";
137                         protect = true;
138                         break;
139                 case InsetSpaceParams::VISIBLE:
140                         item = "visible";
141                         protect = true;
142                         break;
143                 case InsetSpaceParams::THIN:
144                         item = "thinspace";
145                         break;
146                 case InsetSpaceParams::MEDIUM:
147                         item = "medspace";
148                         break;
149                 case InsetSpaceParams::THICK:
150                         item = "thickspace";
151                         break;
152                 case InsetSpaceParams::NEGTHIN:
153                         item = "negthinspace";
154                         break;
155                 case InsetSpaceParams::NEGMEDIUM:
156                         item = "negmedspace";
157                         break;
158                 case InsetSpaceParams::NEGTHICK:
159                         item = "negthickspace";
160                         break;
161                 case InsetSpaceParams::ENSKIP:
162                         item = "halfquad";
163                         break;
164                 case InsetSpaceParams::ENSPACE:
165                         item = "halfquad";
166                         protect = true;
167                         break;
168                 case InsetSpaceParams::QUAD:
169                         item = "quad";
170                         break;
171                 case InsetSpaceParams::QQUAD:
172                         item = "qquad";
173                         break;
174                 case InsetSpaceParams::HFILL:
175                         item = "hfill";
176                         break;
177                 case InsetSpaceParams::HFILL_PROTECTED:
178                         item = "hfill";
179                         protect = true;
180                         break;
181                 case InsetSpaceParams::DOTFILL:
182                         item = "hfill";
183                         pattern = 1;
184                         break;
185                 case InsetSpaceParams::HRULEFILL:
186                         item = "hfill";
187                         pattern = 2;
188                         break;
189                 case InsetSpaceParams::LEFTARROWFILL:
190                         item = "hfill";
191                         pattern = 3;
192                         break;
193                 case InsetSpaceParams::RIGHTARROWFILL:
194                         item = "hfill";
195                         pattern = 4;
196                         break;
197                 case InsetSpaceParams::UPBRACEFILL:
198                         item = "hfill";
199                         pattern = 5;
200                         break;
201                 case InsetSpaceParams::DOWNBRACEFILL:
202                         item = "hfill";
203                         pattern = 6;
204                         break;
205                 case InsetSpaceParams::CUSTOM:
206                         item = "custom";
207                         break;
208                 case InsetSpaceParams::CUSTOM_PROTECTED:
209                         item = "custom";
210                         protect = true;
211                         break;
212         }
213         spacingCO->setCurrentIndex(spacingCO->findData(item));
214         fillPatternCO->setCurrentIndex(pattern);
215         keepCB->setChecked(protect);
216         if (item == "halfquad") {
217                 keepCB->setToolTip(qt_("Insert the spacing even after a line break.\n"
218                                        "Note that a protected Half Quad will be turned into\n"
219                                        "a vertical space if used at the beginning of a paragraph!"));
220         } else {
221                 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
222         }
223         Length::UNIT const default_unit = Length::defaultUnit();
224         if (item == "custom") {
225                 string length = params.length.asString();
226                 lengthToWidgets(valueLE, unitCO, length, default_unit);
227         } else
228                 lengthToWidgets(valueLE, unitCO, "", default_unit);
229
230         enableWidgets();
231 }
232
233
234 docstring GuiHSpace::dialogToParams() const
235 {
236         InsetSpaceParams params = math_mode_ ?
237                 InsetSpaceParams(true) : InsetSpaceParams(false);
238
239         QString const item =
240                 spacingCO->itemData(spacingCO->currentIndex()).toString();
241
242         if (item == "normal")
243                 params.kind = keepCB->isChecked() ?
244                         InsetSpaceParams::PROTECTED : InsetSpaceParams::NORMAL;
245         else if (item == "thinspace")
246                 params.kind = InsetSpaceParams::THIN;
247         else if (item == "medspace")
248                 params.kind = InsetSpaceParams::MEDIUM;
249         else if (item == "thickspace")
250                 params.kind = InsetSpaceParams::THICK;
251         else if (item == "negthinspace")
252                 params.kind = InsetSpaceParams::NEGTHIN;
253         else if (item == "negmedspace")
254                 params.kind = InsetSpaceParams::NEGMEDIUM;
255         else if (item == "negthickspace")
256                 params.kind = InsetSpaceParams::NEGTHICK;
257         else if (item == "halfquad")
258                 params.kind = keepCB->isChecked() ?
259                         InsetSpaceParams::ENSPACE : InsetSpaceParams::ENSKIP;
260         else if (item == "quad")
261                         params.kind = InsetSpaceParams::QUAD;
262         else if (item == "qquad")
263                         params.kind = InsetSpaceParams::QQUAD;
264         else if (item == "hfill") {
265                 switch (fillPatternCO->currentIndex()) {
266                 case 1:
267                         params.kind = InsetSpaceParams::DOTFILL;
268                         break;
269                 case 2:
270                         params.kind = InsetSpaceParams::HRULEFILL;
271                         break;
272                 case 3:
273                         params.kind = InsetSpaceParams::LEFTARROWFILL;
274                         break;
275                 case 4:
276                         params.kind = InsetSpaceParams::RIGHTARROWFILL;
277                         break;
278                 case 5:
279                         params.kind = InsetSpaceParams::UPBRACEFILL;
280                         break;
281                 case 6:
282                         params.kind = InsetSpaceParams::DOWNBRACEFILL;
283                         break;
284                 default:
285                         if (keepCB->isChecked())
286                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
287                         else
288                                 params.kind = InsetSpaceParams::HFILL;
289                         break;
290                 }
291         } else if (item == "custom") {
292                         params.kind = keepCB->isChecked() ?
293                                 InsetSpaceParams::CUSTOM_PROTECTED : InsetSpaceParams::CUSTOM;
294                         params.length = GlueLength(widgetsToLength(valueLE, unitCO));
295         } else if (item == "visible") 
296                 params.kind = InsetSpaceParams::VISIBLE;
297
298         return from_ascii(InsetSpace::params2string(params));
299 }
300
301
302 bool GuiHSpace::checkWidgets(bool readonly) const
303 {
304         spacingCO->setEnabled(!readonly);
305         unitCO->setEnabled(!readonly);
306         fillPatternCO->setEnabled(!readonly);
307         keepCB->setEnabled(!readonly);
308         valueLE->setReadOnly(readonly);
309         if (!InsetParamsWidget::checkWidgets())
310                 return false;
311         return spacingCO->itemData(spacingCO->currentIndex()).toString() != "custom"
312                 || !valueLE->text().isEmpty();
313 }
314
315 } // namespace frontend
316 } // namespace lyx
317
318
319 #include "moc_GuiHSpace.cpp"