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()));
61 valueLE->setValidator(unsignedLengthValidator(valueLE));
63 valueLE->setValidator(unsignedGlueLengthValidator(valueLE));
65 // Manage the ok, apply, restore and cancel/close buttons
66 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
68 bc().setApply(applyPB);
69 bc().setCancel(closePB);
71 // disable for read-only documents
72 bc().addReadOnly(spacingCO);
73 bc().addReadOnly(valueLE);
74 bc().addReadOnly(unitCO);
75 bc().addReadOnly(keepCB);
76 bc().addReadOnly(fillPatternCO);
78 // initialize the length validator
79 bc().addCheckedLineEdit(valueLE, valueL);
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 string length = params.length.asString();
253 lengthToWidgets(value, unit, length, default_unit);
255 lengthToWidgets(value, unit, "", default_unit);
259 static InsetSpaceParams setHSpaceFromWidgets(int spacing,
260 QLineEdit * value, LengthCombo * unit, bool keep, int fill, bool math)
262 InsetSpaceParams params(math);
265 case 0: params.kind = InsetSpaceParams::THIN; break;
266 case 1: params.kind = InsetSpaceParams::MEDIUM; break;
267 case 2: params.kind = InsetSpaceParams::THICK; break;
268 case 3: params.kind = InsetSpaceParams::NEGTHIN; break;
269 case 4: params.kind = InsetSpaceParams::NEGMEDIUM; break;
270 case 5: params.kind = InsetSpaceParams::NEGTHICK; break;
271 case 6: params.kind = InsetSpaceParams::ENSKIP; break;
272 case 7: params.kind = InsetSpaceParams::QUAD; break;
273 case 8: params.kind = InsetSpaceParams::QQUAD; break;
275 params.kind = InsetSpaceParams::CUSTOM;
276 params.length = GlueLength(widgetsToLength(value, unit));
285 params.kind = InsetSpaceParams::PROTECTED;
287 params.kind = InsetSpaceParams::NORMAL;
290 params.kind = InsetSpaceParams::THIN;
293 params.kind = InsetSpaceParams::NEGTHIN;
297 params.kind = InsetSpaceParams::ENSPACE;
299 params.kind = InsetSpaceParams::ENSKIP;
302 params.kind = InsetSpaceParams::QUAD;
305 params.kind = InsetSpaceParams::QQUAD;
309 params.kind = InsetSpaceParams::DOTFILL;
311 params.kind = InsetSpaceParams::HRULEFILL;
313 params.kind = InsetSpaceParams::LEFTARROWFILL;
315 params.kind = InsetSpaceParams::RIGHTARROWFILL;
317 params.kind = InsetSpaceParams::UPBRACEFILL;
319 params.kind = InsetSpaceParams::DOWNBRACEFILL;
321 params.kind = InsetSpaceParams::HFILL_PROTECTED;
323 params.kind = InsetSpaceParams::HFILL;
327 params.kind = InsetSpaceParams::CUSTOM_PROTECTED;
329 params.kind = InsetSpaceParams::CUSTOM;
330 params.length = GlueLength(widgetsToLength(value, unit));
337 void GuiHSpace::applyView()
339 params_ = setHSpaceFromWidgets(spacingCO->currentIndex(),
340 valueLE, unitCO, keepCB->isChecked(),
341 fillPatternCO->currentIndex(), params_.math);
345 void GuiHSpace::updateContents()
347 setWidgetsFromHSpace(params_, spacingCO, valueLE, unitCO, keepCB,
349 enableWidgets(spacingCO->currentIndex());
353 bool GuiHSpace::initialiseParams(string const & data)
355 bool const math = params_.math;
356 InsetSpace::string2params(data, params_);
359 setMath(params_.kind == InsetSpaceParams::CUSTOM);
360 setButtonsValid(true);
365 void GuiHSpace::clearParams()
367 params_ = InsetSpaceParams(params_.math);
371 void GuiHSpace::dispatchParams()
373 dispatch(FuncRequest(getLfun(), InsetSpace::params2string(params_)));
377 bool GuiHSpace::isValid()
379 return spacingCO->currentIndex() != (params_.math ? 9 : 7)
380 || !valueLE->text().isEmpty();
384 Dialog * createGuiMathHSpace(GuiView & lv) { return new GuiHSpace(lv, true); }
387 Dialog * createGuiTextHSpace(GuiView & lv) { return new GuiHSpace(lv, false); }
390 } // namespace frontend
394 #include "moc_GuiHSpace.cpp"