]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiHSpace.cpp
Unify Inter-word and Interword space strings.
[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 #include "FuncRequest.h"
21
22 #include "insets/InsetSpace.h"
23
24 #include "mathed/InsetMathSpace.h"
25
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28
29 #include <QCheckBox>
30 #include <QLineEdit>
31 #include <QPushButton>
32 #include <QValidator>
33
34 using namespace std;
35
36 namespace lyx {
37 namespace frontend {
38
39 GuiHSpace::GuiHSpace(bool math_mode, QWidget * parent)
40         : InsetParamsWidget(parent), math_mode_(math_mode)
41 {
42         setupUi(this);
43
44         spacingCO->clear();
45         if (math_mode_) {
46                 spacingCO->addItem(qt_("Thin space"));
47                 spacingCO->addItem(qt_("Medium space"));
48                 spacingCO->addItem(qt_("Thick space"));
49                 spacingCO->addItem(qt_("Negative thin space"));
50                 spacingCO->addItem(qt_("Negative medium space"));
51                 spacingCO->addItem(qt_("Negative thick space"));
52                 spacingCO->addItem(qt_("Half Quad (0.5 em)"));
53                 spacingCO->addItem(qt_("Quad (1 em)"));
54                 spacingCO->addItem(qt_("Double Quad (2 em)"));
55                 spacingCO->addItem(qt_("Custom"));
56         } else {
57                 spacingCO->addItem(qt_("Interword space"));
58                 spacingCO->addItem(qt_("Thin space"));
59                 spacingCO->addItem(qt_("Negative thin space"));
60                 spacingCO->addItem(qt_("Half Quad (0.5 em)"));
61                 spacingCO->addItem(qt_("Quad (1 em)"));
62                 spacingCO->addItem(qt_("Double Quad (2 em)"));
63                 spacingCO->addItem(qt_("Horizontal Fill"));
64                 spacingCO->addItem(qt_("Custom"));
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 = (selection == spacingCO->count() - 1);
101         valueLE->setEnabled(custom);
102         unitCO->setEnabled(custom);
103         if (math_mode_) {
104                 fillPatternCO->setEnabled(false);
105                 keepCB->setEnabled(false);
106                 return;
107         }
108         fillPatternCO->setEnabled(selection == 6);
109         bool const no_pattern = fillPatternCO->currentIndex() == 0;
110         bool const enable_keep =
111                 selection == 0 || selection == 3  ||
112                 (selection == 6 && no_pattern) || custom;
113         keepCB->setEnabled(enable_keep);
114 }
115
116
117 void GuiHSpace::paramsToDialog(Inset const * inset)
118 {
119         InsetSpaceParams const params = math_mode_
120                 ? static_cast<InsetMathSpace const *>(inset)->params()
121                 : static_cast<InsetSpace const *>(inset)->params();
122
123         int item = 0;
124         int pattern = 0;
125         bool protect = false;
126         switch (params.kind) {
127                 case InsetSpaceParams::NORMAL:
128                         item = 0;
129                         break;
130                 case InsetSpaceParams::PROTECTED:
131                         item = 0;
132                         protect = !params.math;
133                         break;
134                 case InsetSpaceParams::THIN:
135                         item = params.math ? 0 : 1;
136                         break;
137                 case InsetSpaceParams::MEDIUM:
138                         item = 1;
139                         break;
140                 case InsetSpaceParams::THICK:
141                         item = params.math ? 2 : 1;
142                         break;
143                 case InsetSpaceParams::NEGTHIN:
144                         item = params.math ? 3 : 2;
145                         break;
146                 case InsetSpaceParams::NEGMEDIUM:
147                         item = params.math ? 4 : 2;
148                         break;
149                 case InsetSpaceParams::NEGTHICK:
150                         item = params.math ? 5 : 2;
151                         break;
152                 case InsetSpaceParams::ENSKIP:
153                         item = params.math ? 6 : 3;
154                         break;
155                 case InsetSpaceParams::ENSPACE:
156                         item = params.math ? 6 : 3;
157                         protect = !params.math;
158                         break;
159                 case InsetSpaceParams::QUAD:
160                         item = params.math ? 7 : 4;
161                         break;
162                 case InsetSpaceParams::QQUAD:
163                         item = params.math ? 8 : 5;
164                         break;
165                 case InsetSpaceParams::HFILL:
166                         item = params.math ? 3 : 6;
167                         break;
168                 case InsetSpaceParams::HFILL_PROTECTED:
169                         item = params.math ? 3 : 6;
170                         protect = !params.math;
171                         break;
172                 case InsetSpaceParams::DOTFILL:
173                         item = params.math ? 3 : 6;
174                         pattern = 1;
175                         break;
176                 case InsetSpaceParams::HRULEFILL:
177                         item = params.math ? 3 : 6;
178                         pattern = 2;
179                         break;
180                 case InsetSpaceParams::LEFTARROWFILL:
181                         item = params.math ? 3 : 6;
182                         pattern = 3;
183                         break;
184                 case InsetSpaceParams::RIGHTARROWFILL:
185                         item = params.math ? 3 : 6;
186                         pattern = 4;
187                         break;
188                 case InsetSpaceParams::UPBRACEFILL:
189                         item = params.math ? 3 : 6;
190                         pattern = 5;
191                         break;
192                 case InsetSpaceParams::DOWNBRACEFILL:
193                         item = params.math ? 3 : 6;
194                         pattern = 6;
195                         break;
196                 case InsetSpaceParams::CUSTOM:
197                         item = params.math ? 9 : 7;
198                         break;
199                 case InsetSpaceParams::CUSTOM_PROTECTED:
200                         item = params.math ? 9 : 7;
201                         protect = !params.math;
202                         break;
203         }
204         spacingCO->setCurrentIndex(item);
205         fillPatternCO->setCurrentIndex(pattern);
206         keepCB->setChecked(protect);
207         if (math_mode_) {
208                 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
209         } else if (item == 3) {
210                 keepCB->setToolTip(qt_("Insert the spacing even after a line break.\n"
211                                        "Note that a protected Half Quad will be turned into\n"
212                                        "a vertical space if used at the beginning of a paragraph!"));
213         } else {
214                 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
215         }
216         Length::UNIT const default_unit = Length::defaultUnit();
217         if (item == (params.math ? 9 : 7)) {
218                 string length = params.length.asString();
219                 lengthToWidgets(valueLE, unitCO, length, default_unit);
220         } else
221                 lengthToWidgets(valueLE, unitCO, "", default_unit);
222
223         enableWidgets();
224 }
225
226
227 docstring GuiHSpace::dialogToMathParams() const
228 {
229         InsetSpaceParams params(true);
230         switch (spacingCO->currentIndex()) {
231         case 0: params.kind = InsetSpaceParams::THIN;      break;
232         case 1: params.kind = InsetSpaceParams::MEDIUM;    break;
233         case 2: params.kind = InsetSpaceParams::THICK;     break;
234         case 3: params.kind = InsetSpaceParams::NEGTHIN;   break;
235         case 4: params.kind = InsetSpaceParams::NEGMEDIUM; break;
236         case 5: params.kind = InsetSpaceParams::NEGTHICK;  break;
237         case 6: params.kind = InsetSpaceParams::ENSKIP;    break;
238         case 7: params.kind = InsetSpaceParams::QUAD;      break;
239         case 8: params.kind = InsetSpaceParams::QQUAD;     break;
240         case 9:
241                 params.kind = InsetSpaceParams::CUSTOM;
242                 params.length = GlueLength(widgetsToLength(valueLE, unitCO));
243                 break;
244         }
245         return from_ascii(InsetSpace::params2string(params));
246 }
247
248
249 docstring GuiHSpace::dialogToParams() const
250 {
251         if (math_mode_)
252                 return dialogToMathParams();
253
254         InsetSpaceParams params(false);
255
256         switch (spacingCO->currentIndex()) {
257                 case 0:
258                         if (keepCB->isChecked())
259                                 params.kind = InsetSpaceParams::PROTECTED;
260                         else
261                                 params.kind = InsetSpaceParams::NORMAL;
262                         break;
263                 case 1:
264                         params.kind = InsetSpaceParams::THIN;
265                         break;
266                 case 2:
267                         params.kind = InsetSpaceParams::NEGTHIN;
268                         break;
269                 case 3:
270                         if (keepCB->isChecked())
271                                 params.kind = InsetSpaceParams::ENSPACE;
272                         else
273                                 params.kind = InsetSpaceParams::ENSKIP;
274                         break;
275                 case 4:
276                         params.kind = InsetSpaceParams::QUAD;
277                         break;
278                 case 5:
279                         params.kind = InsetSpaceParams::QQUAD;
280                         break;
281                 case 6:
282                         switch (fillPatternCO->currentIndex()) {
283                         case 1:
284                                 params.kind = InsetSpaceParams::DOTFILL;
285                                 break;
286                         case 2:
287                                 params.kind = InsetSpaceParams::HRULEFILL;
288                                 break;
289                         case 3:
290                                 params.kind = InsetSpaceParams::LEFTARROWFILL;
291                                 break;
292                         case 4:
293                                 params.kind = InsetSpaceParams::RIGHTARROWFILL;
294                                 break;
295                         case 5:
296                                 params.kind = InsetSpaceParams::UPBRACEFILL;
297                                 break;
298                         case 6:
299                                 params.kind = InsetSpaceParams::DOWNBRACEFILL;
300                                 break;
301                         default:
302                                 if (keepCB->isChecked())
303                                         params.kind = InsetSpaceParams::HFILL_PROTECTED;
304                                 else
305                                         params.kind = InsetSpaceParams::HFILL;
306                                 break;
307                         }
308                         break;
309                 case 7:
310                         if (keepCB->isChecked())
311                                 params.kind = InsetSpaceParams::CUSTOM_PROTECTED;
312                         else
313                                 params.kind = InsetSpaceParams::CUSTOM;
314                         params.length = GlueLength(widgetsToLength(valueLE, unitCO));
315                         break;
316         }
317         return from_ascii(InsetSpace::params2string(params));
318 }
319
320
321 bool GuiHSpace::checkWidgets() const
322 {
323         if (!InsetParamsWidget::checkWidgets())
324                 return false;
325         return spacingCO->currentIndex() != (math_mode_ ? 9 : 7)
326                 || !valueLE->text().isEmpty();
327 }
328
329 } // namespace frontend
330 } // namespace lyx
331
332
333 #include "moc_GuiHSpace.cpp"