3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jürgen Vigna (Minipage stuff)
7 * \author Martin Vermeer
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
17 #include "LengthCombo.h"
19 #include "qt_helpers.h"
20 #include "Validator.h"
22 #include "insets/InsetBox.h"
24 #include "support/gettext.h"
25 #include "support/foreach.h"
26 #include "support/lstrings.h"
28 #include <QPushButton>
41 static QStringList boxGuiIds()
44 << "Frameless" << "Boxed"
45 << "ovalbox" << "Ovalbox"
46 << "Shadowbox" << "Shaded"
51 static QStringList boxGuiNames()
54 << qt_("No frame") << qt_("Simple rectangular frame")
55 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
56 << qt_("Drop shadow") << qt_("Shaded background")
57 << qt_("Double rectangular frame");
61 static QStringList boxGuiSpecialLengthIds()
63 return QStringList() << "height" << "depth"
64 << "totalheight" << "width";
68 static QStringList boxGuiSpecialLengthNames()
70 return QStringList() << qt_("Height") << qt_("Depth")
71 << qt_("Total Height") << qt_("Width");
75 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
79 // fill the box type choice
81 gui_names_ = boxGuiNames();
82 for (int i = 0; i != ids_.size(); ++i)
83 typeCO->addItem(gui_names_[i], ids_[i]);
85 // add the special units to the height choice
86 // width needs different handling
87 ids_spec_ = boxGuiSpecialLengthIds();
88 gui_names_spec_ = boxGuiSpecialLengthNames();
89 for (int i = 0; i != ids_spec_.size(); ++i)
90 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
92 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
93 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
94 this, SIGNAL(changed()));
95 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
96 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
97 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
98 this, SIGNAL(changed()));
99 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
100 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
102 heightED->setValidator(unsignedLengthValidator(heightED));
103 widthED->setValidator(unsignedLengthValidator(widthED));
105 // initialize the length validator
106 addCheckedWidget(widthED, widthCB);
107 addCheckedWidget(heightED, heightCB);
113 void GuiBox::on_innerBoxCO_activated(int /* index */)
116 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
117 // handle parbox and minipage the same way
120 && itype != "makebox");
121 QString const outer =
122 typeCO->itemData(typeCO->currentIndex()).toString();
123 valignCO->setEnabled(ibox);
124 ialignCO->setEnabled(ibox);
125 if (heightCB->isChecked() && !ibox)
126 heightCB->setChecked(false);
127 heightCB->setEnabled(ibox);
128 // the width can only be selected for makebox or framebox
129 widthCB->setEnabled(itype == "makebox"
130 || (outer == "Boxed" && itype == "none"));
131 widthCB->setChecked(itype != "none" && !widthCB->isEnabled());
132 // except for frameless and boxed, the width cannot be specified if
133 // there is no inner box
134 bool const width_enabled =
135 ibox || outer == "Frameless" || outer == "Boxed";
136 // enable if width_enabled, except if checkbaox is active but unset
137 widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
138 widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
139 if (!widthCB->isChecked() && widthCB->isEnabled()) {
140 widthED->setEnabled(false);
141 widthUnitsLC->setEnabled(false);
143 // halign is only allowed without inner box and if a width is used and if
144 // pagebreak is not used
145 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
146 && ((!ibox && outer == "Boxed") || itype == "makebox"));
147 // pagebreak is only allowed for Boxed without inner box
148 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
154 void GuiBox::on_typeCO_activated(int index)
157 typeCO->itemData(index).toString();
158 bool const frameless = (type == "Frameless");
160 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
161 setInnerType(frameless, itype);
162 // refresh itype because it might have been changed in setInnerType
164 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
165 // handle parbox and minipage the same way
168 && itype != "makebox");
169 if (frameless && itype != "makebox") {
170 valignCO->setEnabled(ibox);
171 ialignCO->setEnabled(ibox);
172 if (heightCB->isChecked() && !ibox)
173 heightCB->setChecked(false);
174 heightCB->setEnabled(ibox);
177 // the width can only be selected for makebox or framebox
178 widthCB->setEnabled(itype == "makebox"
179 || (type == "Boxed" && itype == "none"));
180 widthCB->setChecked(itype != "none" && !widthCB->isEnabled());
181 // except for frameless and boxed, the width cannot be specified if
182 // there is no inner box
183 bool const width_enabled =
184 itype != "none" || frameless || type == "Boxed";
185 // enable if width_enabled, except if checkbaox is active but unset
186 widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
187 widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
188 if (!widthCB->isChecked() && widthCB->isEnabled()) {
189 widthED->setEnabled(false);
190 widthUnitsLC->setEnabled(false);
192 // halign is only allowed without inner box and if a width is used and if
193 // pagebreak is not used
194 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
195 && ((itype == "none" && type == "Boxed") || itype == "makebox"));
196 // pagebreak is only allowed for Boxed without inner box
197 pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
199 pagebreakCB->setChecked(false);
204 void GuiBox::initDialog()
206 setInnerType(true, toqstr("minipage"));
207 widthED->setText("100");
208 widthCB->setChecked(true);
209 widthCB->setEnabled(false);
210 widthUnitsLC->setCurrentItem(Length::PCW);
211 heightED->setText("1");
212 heightUnitsLC->setCurrentItem("totalheight");
216 void GuiBox::on_widthCB_stateChanged(int)
218 if (widthCB->isEnabled()) {
219 widthED->setEnabled(widthCB->isChecked());
220 widthUnitsLC->setEnabled(widthCB->isChecked());
221 halignCO->setEnabled(widthCB->isChecked());
227 void GuiBox::on_heightCB_stateChanged(int state)
229 bool const enable = (innerBoxCO->currentText() != qt_("None"))
230 && (state == Qt::Checked);
231 heightED->setEnabled(enable);
232 heightUnitsLC->setEnabled(enable);
237 void GuiBox::on_pagebreakCB_stateChanged()
239 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
240 innerBoxCO->setEnabled(!pbreak);
241 widthCB->setEnabled(!pbreak);
243 widthCB->setChecked(!pbreak);
244 widthED->setEnabled(!pbreak);
245 widthUnitsLC->setEnabled(!pbreak);
247 on_typeCO_activated(typeCO->currentIndex());
250 valignCO->setEnabled(false);
251 ialignCO->setEnabled(false);
252 halignCO->setEnabled(false);
253 heightCB->setEnabled(false);
254 heightED->setEnabled(false);
255 heightUnitsLC->setEnabled(false);
261 void GuiBox::paramsToDialog(Inset const * inset)
263 InsetBox const * box = static_cast<InsetBox const *>(inset);
264 InsetBoxParams const & params = box->params();
265 QString type = toqstr(params.type);
266 if (type == "Framed") {
267 pagebreakCB->setChecked(true);
270 pagebreakCB->setChecked(false);
273 typeCO->setCurrentIndex(typeCO->findData(type));
276 QString inner_type = "minipage";
277 if (!params.inner_box)
279 if (params.use_parbox)
280 inner_type = "parbox";
281 if (params.use_makebox)
282 inner_type = "makebox";
283 bool const frameless = (params.type == "Frameless");
284 setInnerType(frameless, inner_type);
287 valignCO->setCurrentIndex(string("tcb").find(c, 0));
288 c = params.inner_pos;
289 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
291 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
293 bool ibox = (params.inner_box && !params.use_makebox);
294 valignCO->setEnabled(ibox);
295 ialignCO->setEnabled(ibox);
298 // halign is only allowed without inner box and if a width is used and if
299 // pagebreak is not used
300 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
301 && ((!ibox && type == "Boxed") || params.use_makebox));
302 // pagebreak is only allowed for Boxed without inner box
303 pagebreakCB->setEnabled(!ibox && type == "Boxed");
305 Length::UNIT const default_unit = Length::defaultUnit();
307 // the width can only be selected for makebox or framebox
308 widthCB->setEnabled(inner_type == "makebox"
309 || (type == "Boxed" && !ibox && !pagebreakCB->isChecked()));
310 // "-999col%" is the code for no width
311 if ((params.width).asString() == "-999col%")
312 widthCB->setCheckState(Qt::Unchecked);
314 if (widthCB->isEnabled())
315 widthCB->setChecked(true);
316 lengthToWidgets(widthED, widthUnitsLC,
317 (params.width).asString(), default_unit);
318 QString const special = toqstr(params.special);
319 if (!special.isEmpty() && special != "none")
320 widthUnitsLC->setCurrentItem(special);
323 widthED->setEnabled(widthCB->isChecked());
324 widthUnitsLC->setEnabled(widthCB->isChecked());
326 lengthToWidgets(heightED, heightUnitsLC,
327 (params.height).asString(), default_unit);
329 QString const height_special = toqstr(params.height_special);
330 if (!height_special.isEmpty() && height_special != "none")
331 heightUnitsLC->setCurrentItem(height_special);
332 // set no optional height if the value is the default "1\height"
333 // (special units like \height are handled as "in",
334 // FIXME: this is a very bad UI, this check box should be disabled in
335 // this case, not forced to 'unchecked' state.
336 if (height_special == "totalheight" && params.height == Length("1in"))
337 heightCB->setCheckState(Qt::Unchecked);
339 heightCB->setCheckState(Qt::Checked);
341 heightCB->setEnabled(ibox);
345 docstring GuiBox::dialogToParams() const
347 bool const pagebreak =
348 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
353 box_type = fromqstr(typeCO->itemData(
354 typeCO->currentIndex()).toString());
356 InsetBoxParams params(box_type);
358 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
360 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
362 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
364 params.pos = "tcb"[valignCO->currentIndex()];
365 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
366 params.hor_pos = "lcrs"[halignCO->currentIndex()];
369 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
370 QString value = widthED->text();
372 if (widthCB->isChecked()) {
373 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
374 params.special = fromqstr(unit);
375 // Note: the unit is simply ignored in this case
376 params.width = Length(value.toDouble(), Length::IN);
378 params.special = "none";
379 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
382 if (widthCB->isEnabled()) {
383 // use the code "-999col%" for the case that no width was selected
384 params.special = "none";
385 params.width = Length("-999col%");
389 // the height parameter is omitted if the value
390 // is "1in" and "Total Height" is used as unit.
391 // 1in + "Total Height" means "1\height" which is the LaTeX default
392 // if no height is given
393 if (heightCB->checkState() == Qt::Unchecked) {
394 params.height = Length("1in");
395 params.height_special = "totalheight";
397 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
398 value = heightED->text();
399 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
400 params.height_special = fromqstr(unit);
401 // Note: the unit is simply ignored in this case
402 params.height = Length(value.toDouble(), Length::IN);
404 params.height_special = "none";
406 Length(widgetsToLength(heightED, heightUnitsLC));
409 return from_ascii(InsetBox::params2string(params));
413 void GuiBox::setSpecial(bool ibox)
415 QString const last_item =
416 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
418 // check if the widget contains the special units
419 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
420 // insert 'em if needed...
421 if (!ibox && !has_special) {
422 for (int i = 1; i < ids_spec_.size(); ++i)
423 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
424 // ... or remove 'em if needed
425 } else if (ibox && has_special) {
426 for (int i = 1; i < ids_spec_.size(); ++i) {
427 int n = widthUnitsLC->findData(ids_spec_[i]);
429 widthUnitsLC->removeItem(n);
432 // restore selected text, if possible
433 widthUnitsLC->setCurrentItem(last_item);
437 void GuiBox::setInnerType(bool frameless, QString const & type)
439 // with "frameless" boxes, inner box is mandatory
440 // (i.e. is the actual box)
441 // we have to remove "none" then and adjust the combo
444 innerBoxCO->addItem(qt_("None"), toqstr("none"));
446 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
447 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
448 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
449 int i = (innerBoxCO->findData(type) != -1)
450 ? innerBoxCO->findData(type) : 0;
451 innerBoxCO->setCurrentIndex(i);
454 } // namespace frontend
458 #include "moc_GuiBox.cpp"