]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiHSpace.cpp
* fix spelling in comments to please John.
[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 "support/gettext.h"
25 #include "support/lstrings.h"
26
27 #include <QCheckBox>
28 #include <QLineEdit>
29 #include <QPushButton>
30 #include <QValidator>
31
32 using namespace std;
33
34 namespace lyx {
35 namespace frontend {
36
37 GuiHSpace::GuiHSpace(GuiView & lv, bool math)
38         : GuiDialog(lv, math ? "mathspace" : "space", qt_("Horizontal Space Settings"))
39 {
40         params_.math = math;
41         setupUi(this);
42
43         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
44         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
45         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
46
47         connect(spacingCO, SIGNAL(highlighted(QString)),
48                 this, SLOT(change_adaptor()));
49         connect(valueLE, SIGNAL(textChanged(QString)),
50                 this, SLOT(change_adaptor()));
51         connect(spacingCO, SIGNAL(activated(int)),
52                 this, SLOT(enableWidgets(int)));
53         connect(keepCB, SIGNAL(clicked()),
54                 this, SLOT(change_adaptor()));
55         connect(unitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
56                 this, SLOT(change_adaptor()));
57         connect(fillPatternCO, SIGNAL(activated(int)),
58                 this, SLOT(patternChanged()));
59
60         if (params_.math)
61                 valueLE->setValidator(unsignedLengthValidator(valueLE));
62         else
63                 valueLE->setValidator(unsignedGlueLengthValidator(valueLE));
64
65         // Manage the ok, apply, restore and cancel/close buttons
66         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
67         bc().setOK(okPB);
68         bc().setApply(applyPB);
69         bc().setCancel(closePB);
70
71         // disable for read-only documents
72         bc().addReadOnly(spacingCO);
73         bc().addReadOnly(valueLE);
74         bc().addReadOnly(unitCO);
75         bc().addReadOnly(keepCB);
76         bc().addReadOnly(fillPatternCO);
77
78         // initialize the length validator
79         bc().addCheckedLineEdit(valueLE, valueL);
80 }
81
82
83 void GuiHSpace::change_adaptor()
84 {
85         changed();
86 }
87
88
89 void GuiHSpace::setMath(bool custom)
90 {
91         valueLE->setEnabled(custom);
92         unitCO->setEnabled(custom);
93         fillPatternCO->setEnabled(false);
94         keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
95         keepCB->setEnabled(false);
96 }
97
98
99 void GuiHSpace::enableWidgets(int selection)
100 {
101         if (params_.math) {
102                 setMath(selection == 9);
103                 changed();
104                 return;
105         }
106         valueLE->setEnabled(selection == 7);
107         unitCO->setEnabled(selection == 7);
108         fillPatternCO->setEnabled(selection == 6);
109         int pattern = fillPatternCO->currentIndex();
110         bool const enable_keep =
111                 selection == 0 || selection == 3  ||
112                 (selection == 6 && pattern == 0) || selection == 7;
113         keepCB->setEnabled(enable_keep);
114         if (selection == 3)
115                 keepCB->setToolTip(qt_("Insert the spacing even after a line break.\n"
116                                        "Note that a protected Half Quad will be turned into\n"
117                                        "a vertical space if used at the beginning of a paragraph!"));
118         else
119                 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
120         changed();
121 }
122
123
124 void GuiHSpace::patternChanged()
125 {
126         enableWidgets(spacingCO->currentIndex());
127         changed();
128 }
129
130
131 static void setWidgetsFromHSpace(InsetSpaceParams const & params,
132                           QComboBox * spacing,
133                           QLineEdit * value,
134                           LengthCombo * unit,
135                           QCheckBox * keep,
136                           QComboBox * fillPattern)
137 {
138         spacing->clear();
139         if (params.math) {
140                 spacing->addItem(qt_("Thin space"));
141                 spacing->addItem(qt_("Medium space"));
142                 spacing->addItem(qt_("Thick space"));
143                 spacing->addItem(qt_("Negative thin space"));
144                 spacing->addItem(qt_("Negative medium space"));
145                 spacing->addItem(qt_("Negative thick space"));
146                 spacing->addItem(qt_("Half Quad (0.5 em)"));
147                 spacing->addItem(qt_("Quad (1 em)"));
148                 spacing->addItem(qt_("Double Quad (2 em)"));
149                 spacing->addItem(qt_("Custom"));
150                 keep->setEnabled(false);
151                 fillPattern->setEnabled(false);
152         } else {
153                 spacing->addItem(qt_("Inter-word space"));
154                 spacing->addItem(qt_("Thin space"));
155                 spacing->addItem(qt_("Negative thin space"));
156                 spacing->addItem(qt_("Half Quad (0.5 em)"));
157                 spacing->addItem(qt_("Quad (1 em)"));
158                 spacing->addItem(qt_("Double Quad (2 em)"));
159                 spacing->addItem(qt_("Horizontal Fill"));
160                 spacing->addItem(qt_("Custom"));
161                 keep->setEnabled(true);
162                 fillPattern->setEnabled(true);
163         }
164
165         int item = 0;
166         int pattern = 0;
167         bool protect = false;
168         switch (params.kind) {
169                 case InsetSpaceParams::NORMAL:
170                         item = 0;
171                         break;
172                 case InsetSpaceParams::PROTECTED:
173                         item = 0;
174                         protect = !params.math;
175                         break;
176                 case InsetSpaceParams::THIN:
177                         item = params.math ? 0 : 1;
178                         break;
179                 case InsetSpaceParams::MEDIUM:
180                         item = 1;
181                         break;
182                 case InsetSpaceParams::THICK:
183                         item = params.math ? 2 : 1;
184                         break;
185                 case InsetSpaceParams::NEGTHIN:
186                         item = params.math ? 3 : 2;
187                         break;
188                 case InsetSpaceParams::NEGMEDIUM:
189                         item = params.math ? 4 : 2;
190                         break;
191                 case InsetSpaceParams::NEGTHICK:
192                         item = params.math ? 5 : 2;
193                         break;
194                 case InsetSpaceParams::ENSKIP:
195                         item = params.math ? 6 : 3;
196                         break;
197                 case InsetSpaceParams::ENSPACE:
198                         item = params.math ? 6 : 3;
199                         protect = !params.math;
200                         break;
201                 case InsetSpaceParams::QUAD:
202                         item = params.math ? 7 : 4;
203                         break;
204                 case InsetSpaceParams::QQUAD:
205                         item = params.math ? 8 : 5;
206                         break;
207                 case InsetSpaceParams::HFILL:
208                         item = params.math ? 3 : 6;
209                         break;
210                 case InsetSpaceParams::HFILL_PROTECTED:
211                         item = params.math ? 3 : 6;
212                         protect = !params.math;
213                         break;
214                 case InsetSpaceParams::DOTFILL:
215                         item = params.math ? 3 : 6;
216                         pattern = 1;
217                         break;
218                 case InsetSpaceParams::HRULEFILL:
219                         item = params.math ? 3 : 6;
220                         pattern = 2;
221                         break;
222                 case InsetSpaceParams::LEFTARROWFILL:
223                         item = params.math ? 3 : 6;
224                         pattern = 3;
225                         break;
226                 case InsetSpaceParams::RIGHTARROWFILL:
227                         item = params.math ? 3 : 6;
228                         pattern = 4;
229                         break;
230                 case InsetSpaceParams::UPBRACEFILL:
231                         item = params.math ? 3 : 6;
232                         pattern = 5;
233                         break;
234                 case InsetSpaceParams::DOWNBRACEFILL:
235                         item = params.math ? 3 : 6;
236                         pattern = 6;
237                         break;
238                 case InsetSpaceParams::CUSTOM:
239                         item = params.math ? 9 : 7;
240                         break;
241                 case InsetSpaceParams::CUSTOM_PROTECTED:
242                         item = params.math ? 9 : 7;
243                         protect = !params.math;
244                         break;
245         }
246         spacing->setCurrentIndex(item);
247         fillPattern->setCurrentIndex(pattern);
248         keep->setChecked(protect);
249
250         Length::UNIT const default_unit = Length::defaultUnit();
251         if (item == (params.math ? 9 : 7)) {
252                 string length = params.length.asString();
253                 lengthToWidgets(value, unit, length, default_unit);
254         } else
255                 lengthToWidgets(value, unit, "", default_unit);
256 }
257
258
259 static InsetSpaceParams setHSpaceFromWidgets(int spacing,
260         QLineEdit * value, LengthCombo * unit, bool keep, int fill, bool math)
261 {
262         InsetSpaceParams params(math);
263         if (math) {
264                 switch (spacing) {
265                 case 0: params.kind = InsetSpaceParams::THIN;      break;
266                 case 1: params.kind = InsetSpaceParams::MEDIUM;    break;
267                 case 2: params.kind = InsetSpaceParams::THICK;     break;
268                 case 3: params.kind = InsetSpaceParams::NEGTHIN;   break;
269                 case 4: params.kind = InsetSpaceParams::NEGMEDIUM; break;
270                 case 5: params.kind = InsetSpaceParams::NEGTHICK;  break;
271                 case 6: params.kind = InsetSpaceParams::ENSKIP;    break;
272                 case 7: params.kind = InsetSpaceParams::QUAD;      break;
273                 case 8: params.kind = InsetSpaceParams::QQUAD;     break;
274                 case 9:
275                         params.kind = InsetSpaceParams::CUSTOM;
276                         params.length = GlueLength(widgetsToLength(value, unit));
277                         break;
278                 }
279                 return params;
280         }
281
282         switch (spacing) {
283                 case 0:
284                         if (keep)
285                                 params.kind = InsetSpaceParams::PROTECTED;
286                         else
287                                 params.kind = InsetSpaceParams::NORMAL;
288                         break;
289                 case 1:
290                         params.kind = InsetSpaceParams::THIN;
291                         break;
292                 case 2:
293                         params.kind = InsetSpaceParams::NEGTHIN;
294                         break;
295                 case 3:
296                         if (keep)
297                                 params.kind = InsetSpaceParams::ENSPACE;
298                         else
299                                 params.kind = InsetSpaceParams::ENSKIP;
300                         break;
301                 case 4:
302                         params.kind = InsetSpaceParams::QUAD;
303                         break;
304                 case 5:
305                         params.kind = InsetSpaceParams::QQUAD;
306                         break;
307                 case 6:
308                         if (fill == 1)
309                                 params.kind = InsetSpaceParams::DOTFILL;
310                         else if (fill == 2)
311                                 params.kind = InsetSpaceParams::HRULEFILL;
312                         else if (fill == 3)
313                                 params.kind = InsetSpaceParams::LEFTARROWFILL;
314                         else if (fill == 4)
315                                 params.kind = InsetSpaceParams::RIGHTARROWFILL;
316                         else if (fill == 5)
317                                 params.kind = InsetSpaceParams::UPBRACEFILL;
318                         else if (fill == 6)
319                                 params.kind = InsetSpaceParams::DOWNBRACEFILL;
320                         else if (keep)
321                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
322                         else
323                                 params.kind = InsetSpaceParams::HFILL;
324                         break;
325                 case 7:
326                         if (keep)
327                                 params.kind = InsetSpaceParams::CUSTOM_PROTECTED;
328                         else
329                                 params.kind = InsetSpaceParams::CUSTOM;
330                         params.length = GlueLength(widgetsToLength(value, unit));
331                         break;
332         }
333         return params;
334 }
335
336
337 void GuiHSpace::applyView()
338 {
339         params_ = setHSpaceFromWidgets(spacingCO->currentIndex(),
340                         valueLE, unitCO, keepCB->isChecked(),
341                         fillPatternCO->currentIndex(), params_.math);
342 }
343
344
345 void GuiHSpace::updateContents()
346 {
347         setWidgetsFromHSpace(params_, spacingCO, valueLE, unitCO, keepCB,
348                 fillPatternCO);
349         enableWidgets(spacingCO->currentIndex());
350 }
351
352
353 bool GuiHSpace::initialiseParams(string const & data)
354 {
355         bool const math = params_.math;
356         InsetSpace::string2params(data, params_);
357         params_.math = math;
358         if (params_.math)
359                 setMath(params_.kind == InsetSpaceParams::CUSTOM);
360         setButtonsValid(true);
361         return true;
362 }
363
364
365 void GuiHSpace::clearParams()
366 {
367         params_ = InsetSpaceParams(params_.math);
368 }
369
370
371 void GuiHSpace::dispatchParams()
372 {
373         dispatch(FuncRequest(getLfun(), InsetSpace::params2string(params_)));
374 }
375
376
377 bool GuiHSpace::isValid()
378 {
379         return spacingCO->currentIndex() != (params_.math ? 9 : 7)
380                 || !valueLE->text().isEmpty();
381 }
382
383
384 Dialog * createGuiMathHSpace(GuiView & lv) { return new GuiHSpace(lv, true); }
385
386
387 Dialog * createGuiTextHSpace(GuiView & lv) { return new GuiHSpace(lv, false); }
388
389
390 } // namespace frontend
391 } // namespace lyx
392
393
394 #include "moc_GuiHSpace.cpp"