3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jürgen Spitzmüller
8 * Full author contact details are available in file CREDITS.
13 #include "GuiHSpace.h"
15 #include "LengthCombo.h"
16 #include "qt_helpers.h"
17 #include "Validator.h"
20 #include "FuncRequest.h"
22 #include "insets/InsetSpace.h"
24 #include "support/gettext.h"
25 #include "support/lstrings.h"
29 #include <QPushButton>
37 GuiHSpace::GuiHSpace(GuiView & lv, bool math)
38 : GuiDialog(lv, math ? "mathspace" : "space", qt_("Horizontal Space Settings"))
43 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
44 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
45 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
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()));
60 valueLE->setValidator(unsignedLengthValidator(valueLE));
62 // Manage the ok, apply, restore and cancel/close buttons
63 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
65 bc().setApply(applyPB);
66 bc().setCancel(closePB);
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);
75 // initialize the length validator
76 bc().addCheckedLineEdit(valueLE, valueL);
78 // remove the %-items from the unit choice
83 void GuiHSpace::change_adaptor()
89 void GuiHSpace::setMath(bool custom)
91 valueLE->setEnabled(custom);
92 unitCO->setEnabled(custom);
93 fillPatternCO->setEnabled(false);
94 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
95 keepCB->setEnabled(false);
99 void GuiHSpace::enableWidgets(int selection)
102 setMath(selection == 9);
106 valueLE->setEnabled(selection == 7);
107 unitCO->setEnabled(selection == 7);
108 fillPatternCO->setEnabled(selection == 6);
109 int pattern = fillPatternCO->currentIndex();
110 bool const enable_keep =
111 selection == 0 || selection == 3 ||
112 (selection == 6 && pattern == 0) || selection == 7;
113 keepCB->setEnabled(enable_keep);
115 keepCB->setToolTip(qt_("Insert the spacing even after a line break.\n"
116 "Note that a protected Half Quad will be turned into\n"
117 "a vertical space if used at the beginning of a paragraph!"));
119 keepCB->setToolTip(qt_("Insert the spacing even after a line break"));
124 void GuiHSpace::patternChanged()
126 enableWidgets(spacingCO->currentIndex());
131 static void setWidgetsFromHSpace(InsetSpaceParams const & params,
136 QComboBox * fillPattern)
140 spacing->addItem(qt_("Thin space"));
141 spacing->addItem(qt_("Medium space"));
142 spacing->addItem(qt_("Thick space"));
143 spacing->addItem(qt_("Negative thin space"));
144 spacing->addItem(qt_("Negative medium space"));
145 spacing->addItem(qt_("Negative thick space"));
146 spacing->addItem(qt_("Half Quad (0.5 em)"));
147 spacing->addItem(qt_("Quad (1 em)"));
148 spacing->addItem(qt_("Double Quad (2 em)"));
149 spacing->addItem(qt_("Custom"));
150 keep->setEnabled(false);
151 fillPattern->setEnabled(false);
153 spacing->addItem(qt_("Inter-word space"));
154 spacing->addItem(qt_("Thin space"));
155 spacing->addItem(qt_("Negative thin space"));
156 spacing->addItem(qt_("Half Quad (0.5 em)"));
157 spacing->addItem(qt_("Quad (1 em)"));
158 spacing->addItem(qt_("Double Quad (2 em)"));
159 spacing->addItem(qt_("Horizontal Fill"));
160 spacing->addItem(qt_("Custom"));
161 keep->setEnabled(true);
162 fillPattern->setEnabled(true);
167 bool protect = false;
168 switch (params.kind) {
169 case InsetSpaceParams::NORMAL:
172 case InsetSpaceParams::PROTECTED:
174 protect = !params.math;
176 case InsetSpaceParams::THIN:
177 item = params.math ? 0 : 1;
179 case InsetSpaceParams::MEDIUM:
182 case InsetSpaceParams::THICK:
183 item = params.math ? 2 : 1;
185 case InsetSpaceParams::NEGTHIN:
186 item = params.math ? 3 : 2;
188 case InsetSpaceParams::NEGMEDIUM:
189 item = params.math ? 4 : 2;
191 case InsetSpaceParams::NEGTHICK:
192 item = params.math ? 5 : 2;
194 case InsetSpaceParams::ENSKIP:
195 item = params.math ? 6 : 3;
197 case InsetSpaceParams::ENSPACE:
198 item = params.math ? 6 : 3;
199 protect = !params.math;
201 case InsetSpaceParams::QUAD:
202 item = params.math ? 7 : 4;
204 case InsetSpaceParams::QQUAD:
205 item = params.math ? 8 : 5;
207 case InsetSpaceParams::HFILL:
208 item = params.math ? 3 : 6;
210 case InsetSpaceParams::HFILL_PROTECTED:
211 item = params.math ? 3 : 6;
212 protect = !params.math;
214 case InsetSpaceParams::DOTFILL:
215 item = params.math ? 3 : 6;
218 case InsetSpaceParams::HRULEFILL:
219 item = params.math ? 3 : 6;
222 case InsetSpaceParams::LEFTARROWFILL:
223 item = params.math ? 3 : 6;
226 case InsetSpaceParams::RIGHTARROWFILL:
227 item = params.math ? 3 : 6;
230 case InsetSpaceParams::UPBRACEFILL:
231 item = params.math ? 3 : 6;
234 case InsetSpaceParams::DOWNBRACEFILL:
235 item = params.math ? 3 : 6;
238 case InsetSpaceParams::CUSTOM:
239 item = params.math ? 9 : 7;
241 case InsetSpaceParams::CUSTOM_PROTECTED:
242 item = params.math ? 9 : 7;
243 protect = !params.math;
246 spacing->setCurrentIndex(item);
247 fillPattern->setCurrentIndex(pattern);
248 keep->setChecked(protect);
250 Length::UNIT const default_unit = Length::defaultUnit();
251 if (item == (params.math ? 9 : 7))
252 lengthToWidgets(value, unit, params.length, default_unit);
254 lengthToWidgets(value, unit, "", default_unit);
258 static InsetSpaceParams setHSpaceFromWidgets(int spacing,
259 QLineEdit * value, LengthCombo * unit, bool keep, int fill, bool math)
261 InsetSpaceParams params(math);
264 case 0: params.kind = InsetSpaceParams::THIN; break;
265 case 1: params.kind = InsetSpaceParams::MEDIUM; break;
266 case 2: params.kind = InsetSpaceParams::THICK; break;
267 case 3: params.kind = InsetSpaceParams::NEGTHIN; break;
268 case 4: params.kind = InsetSpaceParams::NEGMEDIUM; break;
269 case 5: params.kind = InsetSpaceParams::NEGTHICK; break;
270 case 6: params.kind = InsetSpaceParams::ENSKIP; break;
271 case 7: params.kind = InsetSpaceParams::QUAD; break;
272 case 8: params.kind = InsetSpaceParams::QQUAD; break;
274 params.kind = InsetSpaceParams::CUSTOM;
275 params.length = Length(widgetsToLength(value, unit));
284 params.kind = InsetSpaceParams::PROTECTED;
286 params.kind = InsetSpaceParams::NORMAL;
289 params.kind = InsetSpaceParams::THIN;
292 params.kind = InsetSpaceParams::NEGTHIN;
296 params.kind = InsetSpaceParams::ENSPACE;
298 params.kind = InsetSpaceParams::ENSKIP;
301 params.kind = InsetSpaceParams::QUAD;
304 params.kind = InsetSpaceParams::QQUAD;
308 params.kind = InsetSpaceParams::DOTFILL;
310 params.kind = InsetSpaceParams::HRULEFILL;
312 params.kind = InsetSpaceParams::LEFTARROWFILL;
314 params.kind = InsetSpaceParams::RIGHTARROWFILL;
316 params.kind = InsetSpaceParams::UPBRACEFILL;
318 params.kind = InsetSpaceParams::DOWNBRACEFILL;
320 params.kind = InsetSpaceParams::HFILL_PROTECTED;
322 params.kind = InsetSpaceParams::HFILL;
326 params.kind = InsetSpaceParams::CUSTOM_PROTECTED;
328 params.kind = InsetSpaceParams::CUSTOM;
329 params.length = Length(widgetsToLength(value, unit));
336 void GuiHSpace::applyView()
338 params_ = setHSpaceFromWidgets(spacingCO->currentIndex(),
339 valueLE, unitCO, keepCB->isChecked(),
340 fillPatternCO->currentIndex(), params_.math);
344 void GuiHSpace::updateContents()
346 setWidgetsFromHSpace(params_, spacingCO, valueLE, unitCO, keepCB,
348 enableWidgets(spacingCO->currentIndex());
352 bool GuiHSpace::initialiseParams(string const & data)
354 bool const math = params_.math;
355 InsetSpace::string2params(data, params_);
358 setMath(params_.kind == InsetSpaceParams::CUSTOM);
359 setButtonsValid(true);
364 void GuiHSpace::clearParams()
366 params_ = InsetSpaceParams(params_.math);
370 void GuiHSpace::dispatchParams()
372 dispatch(FuncRequest(getLfun(), InsetSpace::params2string(params_)));
376 bool GuiHSpace::isValid()
378 return spacingCO->currentIndex() != (params_.math ? 9 : 7)
379 || !valueLE->text().isEmpty();
383 Dialog * createGuiMathHSpace(GuiView & lv) { return new GuiHSpace(lv, true); }
386 Dialog * createGuiTextHSpace(GuiView & lv) { return new GuiHSpace(lv, false); }
389 } // namespace frontend
393 #include "moc_GuiHSpace.cpp"