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