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