]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiHSpace.cpp
cleanup/compile fix
[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 #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)
38         : GuiDialog(lv, "space", qt_("Horizontal Space Settings"))
39 {
40         setupUi(this);
41
42         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
43         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
44         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
45
46         connect(spacingCO, SIGNAL(highlighted(QString)),
47                 this, SLOT(change_adaptor()));
48         connect(valueLE, SIGNAL(textChanged(QString)),
49                 this, SLOT(change_adaptor()));
50         connect(spacingCO, SIGNAL(activated(int)),
51                 this, SLOT(enableWidgets(int)));
52         connect(keepCB, SIGNAL(clicked()),
53                 this, SLOT(change_adaptor()));
54         connect(unitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
55                 this, SLOT(change_adaptor()));
56         connect(fillPatternCO, SIGNAL(activated(int)),
57                 this, SLOT(patternChanged()));
58
59         valueLE->setValidator(unsignedLengthValidator(valueLE));
60
61         // Manage the ok, apply, restore and cancel/close buttons
62         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
63         bc().setOK(okPB);
64         bc().setApply(applyPB);
65         bc().setCancel(closePB);
66
67         // disable for read-only documents
68         bc().addReadOnly(spacingCO);
69         bc().addReadOnly(valueLE);
70         bc().addReadOnly(unitCO);
71         bc().addReadOnly(keepCB);
72         bc().addReadOnly(fillPatternCO);
73
74         // initialize the length validator
75         bc().addCheckedLineEdit(valueLE, valueL);
76
77         // remove the %-items from the unit choice
78         unitCO->noPercents();
79 }
80
81
82 void GuiHSpace::change_adaptor()
83 {
84         changed();
85 }
86
87
88 void GuiHSpace::enableWidgets(int selection)
89 {
90         valueLE->setEnabled(selection == 7);
91         unitCO->setEnabled(selection == 7);
92         fillPatternCO->setEnabled(selection == 6);
93         int pattern = fillPatternCO->currentIndex();
94         bool const enable_keep =
95                 selection == 0 || selection == 3  ||
96                 (selection == 6 && pattern == 0) || selection == 7;
97         keepCB->setEnabled(enable_keep);
98         changed();
99 }
100
101
102 void GuiHSpace::patternChanged()
103 {
104         enableWidgets(spacingCO->currentIndex());
105         changed();
106 }
107
108
109 static void setWidgetsFromHSpace(InsetSpaceParams const & params,
110                           QComboBox * spacing,
111                           QLineEdit * value,
112                           LengthCombo * unit,
113                           QCheckBox * keep,
114                           QComboBox * fillPattern)
115 {
116         int item = 0;
117         int pattern = 0;
118         bool protect = false;
119         switch (params.kind) {
120                 case InsetSpaceParams::NORMAL:
121                         item = 0;
122                         break;
123                 case InsetSpaceParams::PROTECTED:
124                         item = 0;
125                         protect = true;
126                         break;
127                 case InsetSpaceParams::THIN:
128                         item = 1;
129                         break;
130                 case InsetSpaceParams::NEGTHIN:
131                         item = 2;
132                         break;
133                 case InsetSpaceParams::ENSKIP:
134                         item = 3;
135                         break;
136                 case InsetSpaceParams::ENSPACE:
137                         item = 3;
138                         protect = true;
139                         break;
140                 case InsetSpaceParams::QUAD:
141                         item = 4;
142                         break;
143                 case InsetSpaceParams::QQUAD:
144                         item = 5;
145                         break;
146                 case InsetSpaceParams::HFILL:
147                         item = 6;
148                         break;
149                 case InsetSpaceParams::HFILL_PROTECTED:
150                         item = 6;
151                         protect = true;
152                         break;
153                 case InsetSpaceParams::DOTFILL:
154                         item = 6;
155                         pattern = 1;
156                         break;
157                 case InsetSpaceParams::HRULEFILL:
158                         item = 6;
159                         pattern = 2;
160                         break;
161                 case InsetSpaceParams::CUSTOM:
162                         item = 7;
163                         break;
164                 case InsetSpaceParams::CUSTOM_PROTECTED:
165                         item = 7;
166                         protect = true;
167                         break;
168         }
169         spacing->setCurrentIndex(item);
170         fillPattern->setCurrentIndex(pattern);
171         keep->setChecked(protect);
172
173         Length::UNIT default_unit =
174                         (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
175         if (item == 7)
176                 lengthToWidgets(value, unit, params.length, default_unit);
177         else
178                 lengthToWidgets(value, unit, "", default_unit);
179 }
180
181
182 static InsetSpaceParams setHSpaceFromWidgets(int spacing,
183         QLineEdit * value, LengthCombo * unit, bool keep, int fill)
184 {
185         InsetSpaceParams params;
186         switch (spacing) {
187                 case 0:
188                         if (keep)
189                                 params.kind = InsetSpaceParams::PROTECTED;
190                         else
191                                 params.kind = InsetSpaceParams::NORMAL;
192                         break;
193                 case 1:
194                         params.kind = InsetSpaceParams::THIN;
195                         break;
196                 case 2:
197                         params.kind = InsetSpaceParams::NEGTHIN;
198                         break;
199                 case 3:
200                         if (keep)
201                                 params.kind = InsetSpaceParams::ENSPACE;
202                         else
203                                 params.kind = InsetSpaceParams::ENSKIP;
204                         break;
205                 case 4:
206                         params.kind = InsetSpaceParams::QUAD;
207                         break;
208                 case 5:
209                         params.kind = InsetSpaceParams::QQUAD;
210                         break;
211                 case 6:
212                         if (fill == 1)
213                                 params.kind = InsetSpaceParams::DOTFILL;
214                         else if (fill == 2)
215                                 params.kind = InsetSpaceParams::HRULEFILL;
216                         else if (keep)
217                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
218                         else
219                                 params.kind = InsetSpaceParams::HFILL;
220                         break;
221                 case 7:
222                         if (keep)
223                                 params.kind = InsetSpaceParams::CUSTOM_PROTECTED;
224                         else
225                                 params.kind = InsetSpaceParams::CUSTOM;
226                         params.length = Length(widgetsToLength(value, unit));
227                         break;
228         }
229         return params;
230 }
231
232
233 void GuiHSpace::applyView()
234 {
235         params_ = setHSpaceFromWidgets(spacingCO->currentIndex(),
236                         valueLE, unitCO, keepCB->isChecked(),
237                         fillPatternCO->currentIndex());
238 }
239
240
241 void GuiHSpace::updateContents()
242 {
243         setWidgetsFromHSpace(params_, spacingCO, valueLE, unitCO, keepCB,
244                 fillPatternCO);
245         enableWidgets(spacingCO->currentIndex());
246 }
247
248
249 bool GuiHSpace::initialiseParams(string const & data)
250 {
251         InsetSpaceMailer::string2params(data, params_);
252         setButtonsValid(true);
253
254         return true;
255 }
256
257
258 void GuiHSpace::clearParams()
259 {
260         params_ = InsetSpaceParams();
261 }
262
263
264 void GuiHSpace::dispatchParams()
265 {
266         dispatch(FuncRequest(getLfun(), InsetSpaceMailer::params2string(params_)));
267 }
268
269
270 bool GuiHSpace::isValid()
271 {
272         return (spacingCO->currentIndex() != 7 || !valueLE->text().isEmpty());
273 }
274
275
276 Dialog * createGuiHSpace(GuiView & lv) { return new GuiHSpace(lv); }
277
278
279 } // namespace frontend
280 } // namespace lyx
281
282
283 #include "GuiHSpace_moc.cpp"