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