]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiHSpace.cpp
Compil 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
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::LEFTARROWFILL:
169                         item = 6;
170                         pattern = 3;
171                         break;
172                 case InsetSpaceParams::RIGHTARROWFILL:
173                         item = 6;
174                         pattern = 4;
175                         break;
176                 case InsetSpaceParams::UPBRACEFILL:
177                         item = 6;
178                         pattern = 5;
179                         break;
180                 case InsetSpaceParams::DOWNBRACEFILL:
181                         item = 6;
182                         pattern = 6;
183                         break;
184                 case InsetSpaceParams::CUSTOM:
185                         item = 7;
186                         break;
187                 case InsetSpaceParams::CUSTOM_PROTECTED:
188                         item = 7;
189                         protect = true;
190                         break;
191         }
192         spacing->setCurrentIndex(item);
193         fillPattern->setCurrentIndex(pattern);
194         keep->setChecked(protect);
195
196         Length::UNIT default_unit =
197                         (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
198         if (item == 7)
199                 lengthToWidgets(value, unit, params.length, default_unit);
200         else
201                 lengthToWidgets(value, unit, "", default_unit);
202 }
203
204
205 static InsetSpaceParams setHSpaceFromWidgets(int spacing,
206         QLineEdit * value, LengthCombo * unit, bool keep, int fill)
207 {
208         InsetSpaceParams params;
209         switch (spacing) {
210                 case 0:
211                         if (keep)
212                                 params.kind = InsetSpaceParams::PROTECTED;
213                         else
214                                 params.kind = InsetSpaceParams::NORMAL;
215                         break;
216                 case 1:
217                         params.kind = InsetSpaceParams::THIN;
218                         break;
219                 case 2:
220                         params.kind = InsetSpaceParams::NEGTHIN;
221                         break;
222                 case 3:
223                         if (keep)
224                                 params.kind = InsetSpaceParams::ENSPACE;
225                         else
226                                 params.kind = InsetSpaceParams::ENSKIP;
227                         break;
228                 case 4:
229                         params.kind = InsetSpaceParams::QUAD;
230                         break;
231                 case 5:
232                         params.kind = InsetSpaceParams::QQUAD;
233                         break;
234                 case 6:
235                         if (fill == 1)
236                                 params.kind = InsetSpaceParams::DOTFILL;
237                         else if (fill == 2)
238                                 params.kind = InsetSpaceParams::HRULEFILL;
239                         else if (fill == 3)
240                                 params.kind = InsetSpaceParams::LEFTARROWFILL;
241                         else if (fill == 4)
242                                 params.kind = InsetSpaceParams::RIGHTARROWFILL;
243                         else if (fill == 5)
244                                 params.kind = InsetSpaceParams::UPBRACEFILL;
245                         else if (fill == 6)
246                                 params.kind = InsetSpaceParams::DOWNBRACEFILL;
247                         else if (keep)
248                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
249                         else
250                                 params.kind = InsetSpaceParams::HFILL;
251                         break;
252                 case 7:
253                         if (keep)
254                                 params.kind = InsetSpaceParams::CUSTOM_PROTECTED;
255                         else
256                                 params.kind = InsetSpaceParams::CUSTOM;
257                         params.length = Length(widgetsToLength(value, unit));
258                         break;
259         }
260         return params;
261 }
262
263
264 void GuiHSpace::applyView()
265 {
266         params_ = setHSpaceFromWidgets(spacingCO->currentIndex(),
267                         valueLE, unitCO, keepCB->isChecked(),
268                         fillPatternCO->currentIndex());
269 }
270
271
272 void GuiHSpace::updateContents()
273 {
274         setWidgetsFromHSpace(params_, spacingCO, valueLE, unitCO, keepCB,
275                 fillPatternCO);
276         enableWidgets(spacingCO->currentIndex());
277 }
278
279
280 bool GuiHSpace::initialiseParams(string const & data)
281 {
282         InsetSpace::string2params(data, params_);
283         setButtonsValid(true);
284         return true;
285 }
286
287
288 void GuiHSpace::clearParams()
289 {
290         params_ = InsetSpaceParams();
291 }
292
293
294 void GuiHSpace::dispatchParams()
295 {
296         dispatch(FuncRequest(getLfun(), InsetSpace::params2string(params_)));
297 }
298
299
300 bool GuiHSpace::isValid()
301 {
302         return spacingCO->currentIndex() != 7 || !valueLE->text().isEmpty();
303 }
304
305
306 Dialog * createGuiHSpace(GuiView & lv) { return new GuiHSpace(lv); }
307
308
309 } // namespace frontend
310 } // namespace lyx
311
312
313 #include "GuiHSpace_moc.cpp"