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