]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiHSpace.cpp
remove remaining MailInsets.
[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         changed();
100 }
101
102
103 void GuiHSpace::patternChanged()
104 {
105         enableWidgets(spacingCO->currentIndex());
106         changed();
107 }
108
109
110 static void setWidgetsFromHSpace(InsetSpaceParams const & params,
111                           QComboBox * spacing,
112                           QLineEdit * value,
113                           LengthCombo * unit,
114                           QCheckBox * keep,
115                           QComboBox * fillPattern)
116 {
117         int item = 0;
118         int pattern = 0;
119         bool protect = false;
120         switch (params.kind) {
121                 case InsetSpaceParams::NORMAL:
122                         item = 0;
123                         break;
124                 case InsetSpaceParams::PROTECTED:
125                         item = 0;
126                         protect = true;
127                         break;
128                 case InsetSpaceParams::THIN:
129                         item = 1;
130                         break;
131                 case InsetSpaceParams::NEGTHIN:
132                         item = 2;
133                         break;
134                 case InsetSpaceParams::ENSKIP:
135                         item = 3;
136                         break;
137                 case InsetSpaceParams::ENSPACE:
138                         item = 3;
139                         protect = true;
140                         break;
141                 case InsetSpaceParams::QUAD:
142                         item = 4;
143                         break;
144                 case InsetSpaceParams::QQUAD:
145                         item = 5;
146                         break;
147                 case InsetSpaceParams::HFILL:
148                         item = 6;
149                         break;
150                 case InsetSpaceParams::HFILL_PROTECTED:
151                         item = 6;
152                         protect = true;
153                         break;
154                 case InsetSpaceParams::DOTFILL:
155                         item = 6;
156                         pattern = 1;
157                         break;
158                 case InsetSpaceParams::HRULEFILL:
159                         item = 6;
160                         pattern = 2;
161                         break;
162                 case InsetSpaceParams::CUSTOM:
163                         item = 7;
164                         break;
165                 case InsetSpaceParams::CUSTOM_PROTECTED:
166                         item = 7;
167                         protect = true;
168                         break;
169         }
170         spacing->setCurrentIndex(item);
171         fillPattern->setCurrentIndex(pattern);
172         keep->setChecked(protect);
173
174         Length::UNIT default_unit =
175                         (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
176         if (item == 7)
177                 lengthToWidgets(value, unit, params.length, default_unit);
178         else
179                 lengthToWidgets(value, unit, "", default_unit);
180 }
181
182
183 static InsetSpaceParams setHSpaceFromWidgets(int spacing,
184         QLineEdit * value, LengthCombo * unit, bool keep, int fill)
185 {
186         InsetSpaceParams params;
187         switch (spacing) {
188                 case 0:
189                         if (keep)
190                                 params.kind = InsetSpaceParams::PROTECTED;
191                         else
192                                 params.kind = InsetSpaceParams::NORMAL;
193                         break;
194                 case 1:
195                         params.kind = InsetSpaceParams::THIN;
196                         break;
197                 case 2:
198                         params.kind = InsetSpaceParams::NEGTHIN;
199                         break;
200                 case 3:
201                         if (keep)
202                                 params.kind = InsetSpaceParams::ENSPACE;
203                         else
204                                 params.kind = InsetSpaceParams::ENSKIP;
205                         break;
206                 case 4:
207                         params.kind = InsetSpaceParams::QUAD;
208                         break;
209                 case 5:
210                         params.kind = InsetSpaceParams::QQUAD;
211                         break;
212                 case 6:
213                         if (fill == 1)
214                                 params.kind = InsetSpaceParams::DOTFILL;
215                         else if (fill == 2)
216                                 params.kind = InsetSpaceParams::HRULEFILL;
217                         else if (keep)
218                                 params.kind = InsetSpaceParams::HFILL_PROTECTED;
219                         else
220                                 params.kind = InsetSpaceParams::HFILL;
221                         break;
222                 case 7:
223                         if (keep)
224                                 params.kind = InsetSpaceParams::CUSTOM_PROTECTED;
225                         else
226                                 params.kind = InsetSpaceParams::CUSTOM;
227                         params.length = Length(widgetsToLength(value, unit));
228                         break;
229         }
230         return params;
231 }
232
233
234 void GuiHSpace::applyView()
235 {
236         params_ = setHSpaceFromWidgets(spacingCO->currentIndex(),
237                         valueLE, unitCO, keepCB->isChecked(),
238                         fillPatternCO->currentIndex());
239 }
240
241
242 void GuiHSpace::updateContents()
243 {
244         setWidgetsFromHSpace(params_, spacingCO, valueLE, unitCO, keepCB,
245                 fillPatternCO);
246         enableWidgets(spacingCO->currentIndex());
247 }
248
249
250 bool GuiHSpace::initialiseParams(string const & data)
251 {
252         InsetSpace::string2params(data, params_);
253         setButtonsValid(true);
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(), InsetSpace::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"