]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiHSpace.cpp
cosmetics
[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)
39         : GuiDialog(lv, "space", qt_("Horizontal Space Settings"))
40 {
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         // remove the %-items from the unit choice
79         unitCO->noPercents();
80 }
81
82
83 void GuiHSpace::change_adaptor()
84 {
85         changed();
86 }
87
88
89 void GuiHSpace::enableWidgets(int selection)
90 {
91         valueLE->setEnabled(selection == 7);
92         unitCO->setEnabled(selection == 7);
93         fillPatternCO->setEnabled(selection == 6);
94         int pattern = fillPatternCO->currentIndex();
95         bool const enable_keep =
96                 selection == 0 || selection == 3  ||
97                 (selection == 6 && pattern == 0) || selection == 7;
98         keepCB->setEnabled(enable_keep);
99         if (selection == 3)
100                 keepCB->setToolTip(qt_("Insert the spacing even after a line break.\n"
101                                        "Note that a protected Half Quad will be turned into\n"
102                                        "a vertical space if used at the beginning of a paragraph!"));
103         else
104                 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
105         changed();
106 }
107
108
109 void GuiHSpace::patternChanged()
110 {
111         enableWidgets(spacingCO->currentIndex());
112         changed();
113 }
114
115
116 static void setWidgetsFromHSpace(InsetSpaceParams const & params,
117                           QComboBox * spacing,
118                           QLineEdit * value,
119                           LengthCombo * unit,
120                           QCheckBox * keep,
121                           QComboBox * fillPattern)
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 = true;
133                         break;
134                 case InsetSpaceParams::THIN:
135                         item = 1;
136                         break;
137                 case InsetSpaceParams::NEGTHIN:
138                         item = 2;
139                         break;
140                 case InsetSpaceParams::ENSKIP:
141                         item = 3;
142                         break;
143                 case InsetSpaceParams::ENSPACE:
144                         item = 3;
145                         protect = true;
146                         break;
147                 case InsetSpaceParams::QUAD:
148                         item = 4;
149                         break;
150                 case InsetSpaceParams::QQUAD:
151                         item = 5;
152                         break;
153                 case InsetSpaceParams::HFILL:
154                         item = 6;
155                         break;
156                 case InsetSpaceParams::HFILL_PROTECTED:
157                         item = 6;
158                         protect = true;
159                         break;
160                 case InsetSpaceParams::DOTFILL:
161                         item = 6;
162                         pattern = 1;
163                         break;
164                 case InsetSpaceParams::HRULEFILL:
165                         item = 6;
166                         pattern = 2;
167                         break;
168                 case InsetSpaceParams::CUSTOM:
169                         item = 7;
170                         break;
171                 case InsetSpaceParams::CUSTOM_PROTECTED:
172                         item = 7;
173                         protect = true;
174                         break;
175         }
176         spacing->setCurrentIndex(item);
177         fillPattern->setCurrentIndex(pattern);
178         keep->setChecked(protect);
179
180         Length::UNIT default_unit =
181                         (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
182         if (item == 7)
183                 lengthToWidgets(value, unit, params.length, default_unit);
184         else
185                 lengthToWidgets(value, unit, "", default_unit);
186 }
187
188
189 static InsetSpaceParams setHSpaceFromWidgets(int spacing,
190         QLineEdit * value, LengthCombo * unit, bool keep, int fill)
191 {
192         InsetSpaceParams params;
193         switch (spacing) {
194                 case 0:
195                         if (keep)
196                                 params.kind = InsetSpaceParams::PROTECTED;
197                         else
198                                 params.kind = InsetSpaceParams::NORMAL;
199                         break;
200                 case 1:
201                         params.kind = InsetSpaceParams::THIN;
202                         break;
203                 case 2:
204                         params.kind = InsetSpaceParams::NEGTHIN;
205                         break;
206                 case 3:
207                         if (keep)
208                                 params.kind = InsetSpaceParams::ENSPACE;
209                         else
210                                 params.kind = InsetSpaceParams::ENSKIP;
211                         break;
212                 case 4:
213                         params.kind = InsetSpaceParams::QUAD;
214                         break;
215                 case 5:
216                         params.kind = InsetSpaceParams::QQUAD;
217                         break;
218                 case 6:
219                         if (fill == 1)
220                                 params.kind = InsetSpaceParams::DOTFILL;
221                         else if (fill == 2)
222                                 params.kind = InsetSpaceParams::HRULEFILL;
223                         else if (keep)
224                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
225                         else
226                                 params.kind = InsetSpaceParams::HFILL;
227                         break;
228                 case 7:
229                         if (keep)
230                                 params.kind = InsetSpaceParams::CUSTOM_PROTECTED;
231                         else
232                                 params.kind = InsetSpaceParams::CUSTOM;
233                         params.length = Length(widgetsToLength(value, unit));
234                         break;
235         }
236         return params;
237 }
238
239
240 void GuiHSpace::applyView()
241 {
242         params_ = setHSpaceFromWidgets(spacingCO->currentIndex(),
243                         valueLE, unitCO, keepCB->isChecked(),
244                         fillPatternCO->currentIndex());
245 }
246
247
248 void GuiHSpace::updateContents()
249 {
250         setWidgetsFromHSpace(params_, spacingCO, valueLE, unitCO, keepCB,
251                 fillPatternCO);
252         enableWidgets(spacingCO->currentIndex());
253 }
254
255
256 bool GuiHSpace::initialiseParams(string const & data)
257 {
258         InsetSpace::string2params(data, params_);
259         setButtonsValid(true);
260         return true;
261 }
262
263
264 void GuiHSpace::clearParams()
265 {
266         params_ = InsetSpaceParams();
267 }
268
269
270 void GuiHSpace::dispatchParams()
271 {
272         dispatch(FuncRequest(getLfun(), InsetSpace::params2string(params_)));
273 }
274
275
276 bool GuiHSpace::isValid()
277 {
278         return spacingCO->currentIndex() != 7 || !valueLE->text().isEmpty();
279 }
280
281
282 Dialog * createGuiHSpace(GuiView & lv) { return new GuiHSpace(lv); }
283
284
285 } // namespace frontend
286 } // namespace lyx
287
288
289 #include "GuiHSpace_moc.cpp"