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
11 * Full author contact details are available in file CREDITS.
18 #include "LengthCombo.h"
20 #include "qt_helpers.h"
21 #include "Validator.h"
23 #include "insets/InsetBox.h"
25 #include "support/gettext.h"
26 #include "support/foreach.h"
27 #include "support/lstrings.h"
30 #include <QPushButton>
42 static QStringList boxGuiIds()
45 << "Frameless" << "Boxed"
46 << "ovalbox" << "Ovalbox"
47 << "Shadowbox" << "Shaded"
52 static QStringList boxGuiNames()
55 << qt_("No frame") << qt_("Simple rectangular frame")
56 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
57 << qt_("Drop shadow") << qt_("Shaded background")
58 << qt_("Double rectangular frame");
62 static QStringList boxGuiSpecialLengthIds()
64 return QStringList() << "height" << "depth"
65 << "totalheight" << "width";
69 static QStringList boxGuiSpecialLengthNames()
71 return QStringList() << qt_("Height") << qt_("Depth")
72 << qt_("Total Height") << qt_("Width");
76 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
80 // fill the box type choice
82 gui_names_ = boxGuiNames();
83 for (int i = 0; i != ids_.size(); ++i)
84 typeCO->addItem(gui_names_[i], ids_[i]);
86 // add the special units to the height choice
87 // width needs different handling
88 ids_spec_ = boxGuiSpecialLengthIds();
89 gui_names_spec_ = boxGuiSpecialLengthNames();
90 for (int i = 0; i != ids_spec_.size(); ++i)
91 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
93 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
94 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
95 this, SIGNAL(changed()));
96 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
97 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
98 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
99 this, SIGNAL(changed()));
100 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
101 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
102 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
103 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
104 this, SIGNAL(changed()));
105 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
106 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
107 this, SIGNAL(changed()));
108 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
109 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
110 this, SIGNAL(changed()));
112 heightED->setValidator(unsignedLengthValidator(heightED));
113 widthED->setValidator(unsignedLengthValidator(widthED));
114 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
115 separationED->setValidator(unsignedLengthValidator(separationED));
116 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
118 // initialize the length validator
119 addCheckedWidget(widthED, widthCB);
120 addCheckedWidget(heightED, heightCB);
121 addCheckedWidget(thicknessED, thicknessLA);
122 addCheckedWidget(separationED, separationLA);
123 addCheckedWidget(shadowsizeED, shadowsizeLA);
129 void GuiBox::on_innerBoxCO_activated(int /* index */)
132 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
133 // handle parbox and minipage the same way
134 bool const ibox = (itype != "none" && itype != "makebox");
135 if (heightCB->isChecked() && !ibox)
136 heightCB->setChecked(false);
137 widthCB->setChecked(!widthED->text().isEmpty());
143 void GuiBox::on_typeCO_activated(int index)
146 typeCO->itemData(index).toString();
147 bool const frameless = (type == "Frameless");
149 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
150 setInnerType(frameless, itype);
151 // refresh itype because it might have been changed in setInnerType
153 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
154 // handle parbox and minipage the same way
155 bool const ibox = (itype != "none" && itype != "makebox");
156 if (frameless && itype != "makebox") {
157 if (heightCB->isChecked() && !ibox)
158 heightCB->setChecked(false);
161 if (type != "Boxed") {
162 if (type != "Frameless")
163 widthCB->setChecked(itype != "none");
164 pagebreakCB->setChecked(false);
170 void GuiBox::initDialog()
172 setInnerType(true, toqstr("minipage"));
173 widthED->setText("100");
174 widthCB->setChecked(true);
175 widthCB->setEnabled(false);
176 widthUnitsLC->setCurrentItem(Length::PCW);
177 heightED->setText("1");
178 heightUnitsLC->setCurrentItem("totalheight");
179 // LaTeX's default for \fboxrule is 0.4 pt
180 thicknessED->setText("0.4");
181 thicknessUnitsLC->setCurrentItem(Length::PT);
182 // LaTeX's default for \fboxsep is 3 pt
183 separationED->setText("3");
184 separationUnitsLC->setCurrentItem(Length::PT);
185 // LaTeX's default for \shadowsize is 4 pt
186 shadowsizeED->setText("4");
187 shadowsizeUnitsLC->setCurrentItem(Length::PT);
191 void GuiBox::on_widthCB_stateChanged(int)
197 void GuiBox::on_heightCB_stateChanged(int /*state*/)
203 void GuiBox::on_pagebreakCB_stateChanged()
205 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
207 widthCB->setChecked(!pbreak);
209 on_typeCO_activated(typeCO->currentIndex());
217 void GuiBox::paramsToDialog(Inset const * inset)
219 InsetBox const * box = static_cast<InsetBox const *>(inset);
220 InsetBoxParams const & params = box->params();
221 QString type = toqstr(params.type);
222 if (type == "Framed") {
223 pagebreakCB->setChecked(true);
226 pagebreakCB->setChecked(false);
229 typeCO->setCurrentIndex(typeCO->findData(type));
232 QString inner_type = "minipage";
233 if (!params.inner_box)
235 if (params.use_parbox)
236 inner_type = "parbox";
237 if (params.use_makebox)
238 inner_type = "makebox";
239 bool const frameless = (params.type == "Frameless");
240 setInnerType(frameless, inner_type);
243 valignCO->setCurrentIndex(string("tcb").find(c, 0));
244 c = params.inner_pos;
245 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
247 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
249 bool ibox = (params.inner_box && !params.use_makebox);
250 valignCO->setEnabled(ibox);
251 ialignCO->setEnabled(ibox);
254 // halign is only allowed if a width is used
255 halignCO->setEnabled(widthCB->isChecked());
256 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
257 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
258 && halignCO->count() < 4)
259 halignCO->addItem(toqstr("Stretch"));
260 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
261 halignCO->removeItem(3);
262 // pagebreak is only allowed for Boxed without inner box
263 pagebreakCB->setEnabled(!ibox && type == "Boxed");
265 Length::UNIT const default_unit = Length::defaultUnit();
267 // the width can only be selected for makebox or framebox
268 widthCB->setEnabled(inner_type == "makebox"
270 && !ibox && !pagebreakCB->isChecked()));
271 if (params.width.empty()) {
272 widthCB->setChecked(false);
273 lengthToWidgets(widthED, widthUnitsLC,
274 params.width, default_unit);
276 widthCB->setChecked(true);
277 lengthToWidgets(widthED, widthUnitsLC,
278 params.width, default_unit);
279 QString const special = toqstr(params.special);
280 if (!special.isEmpty() && special != "none")
281 widthUnitsLC->setCurrentItem(special);
284 widthED->setEnabled(widthCB->isChecked());
285 widthUnitsLC->setEnabled(widthCB->isChecked());
287 lengthToWidgets(heightED, heightUnitsLC,
288 (params.height).asString(), default_unit);
290 QString const height_special = toqstr(params.height_special);
291 if (!height_special.isEmpty() && height_special != "none")
292 heightUnitsLC->setCurrentItem(height_special);
293 // set no optional height if the value is the default "1\height"
294 // (special units like \height are handled as "in",
295 // FIXME: this is a very bad UI, this check box should be disabled in
296 // this case, not forced to 'unchecked' state.
297 if (height_special == "totalheight" && params.height == Length("1in"))
298 heightCB->setCheckState(Qt::Unchecked);
300 heightCB->setCheckState(Qt::Checked);
302 heightCB->setEnabled(ibox);
304 // enable line thickness only for the rectangular frame types and drop shadow
305 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
306 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
307 lengthToWidgets(thicknessED, thicknessUnitsLC,
308 (params.thickness).asString(), default_unit);
309 // enable line separation for the allowed frame types
310 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
311 || type == "Doublebox" || type == "Shadowbox");
312 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
313 || type == "Doublebox" || type == "Shadowbox");
314 lengthToWidgets(separationED, separationUnitsLC,
315 (params.separation).asString(), default_unit);
316 // enable shadow size for drop shadow
317 shadowsizeED->setEnabled(type == "Shadowbox");
318 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
319 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
320 (params.shadowsize).asString(), default_unit);
324 docstring GuiBox::dialogToParams() const
326 bool const pagebreak =
327 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
332 box_type = fromqstr(typeCO->itemData(
333 typeCO->currentIndex()).toString());
335 InsetBoxParams params(box_type);
337 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
339 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
341 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
343 params.pos = "tcb"[valignCO->currentIndex()];
344 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
345 params.hor_pos = "lcrs"[halignCO->currentIndex()];
348 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
349 QString value = widthED->text();
351 if (widthED->isEnabled()) {
352 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
353 params.special = fromqstr(unit);
354 // Note: the unit is simply ignored in this case
355 params.width = Length(value.toDouble(), Length::IN);
357 params.special = "none";
358 // we must specify a valid length in this case
360 widthED->setText("0");
361 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
364 params.special = "none";
365 params.width = Length();
368 // the height parameter is omitted if the value
369 // is "1in" and "Total Height" is used as unit.
370 // 1in + "Total Height" means "1\height" which is the LaTeX default
371 // if no height is given
372 if (heightCB->checkState() == Qt::Unchecked) {
373 params.height = Length("1in");
374 params.height_special = "totalheight";
376 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
377 value = heightED->text();
378 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
379 params.height_special = fromqstr(unit);
380 // Note: the unit is simply ignored in this case
381 params.height = Length(value.toDouble(), Length::IN);
383 params.height_special = "none";
385 Length(widgetsToLength(heightED, heightUnitsLC));
389 // handle the line thickness, line separation and shadow size
390 if (thicknessED->isEnabled())
391 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
393 params.thickness = Length("0.4pt");
394 if (separationED->isEnabled())
395 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
397 params.separation = Length("3pt");
398 if (separationED->isEnabled())
399 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
401 params.shadowsize = Length("4pt");
403 return from_ascii(InsetBox::params2string(params));
407 bool GuiBox::checkWidgets(bool readonly) const
409 typeCO->setEnabled(!readonly);
412 pagebreakCB->setEnabled(false);
413 innerBoxCO->setEnabled(false);
414 valignCO->setEnabled(false);
415 ialignCO->setEnabled(false);
416 halignCO->setEnabled(false);
417 widthCB->setEnabled(false);
418 widthED->setEnabled(false);
419 widthUnitsLC->setEnabled(false);
420 heightED->setEnabled(false);
421 heightUnitsLC->setEnabled(false);
422 heightCB->setEnabled(false);
423 thicknessED->setEnabled(false);
424 thicknessUnitsLC->setEnabled(false);
425 separationED->setEnabled(false);
426 separationUnitsLC->setEnabled(false);
427 shadowsizeED->setEnabled(false);
428 shadowsizeUnitsLC->setEnabled(false);
430 QString const outer =
431 typeCO->itemData(typeCO->currentIndex()).toString();
432 QString const itype =
433 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
434 bool const ibox = (itype != "none" && itype != "makebox");
435 valignCO->setEnabled(ibox);
436 ialignCO->setEnabled(ibox);
437 if (heightCB->isChecked() && !ibox)
438 heightCB->setChecked(false);
439 heightCB->setEnabled(ibox);
440 // the width can only be selected for makebox or framebox
441 widthCB->setEnabled(itype == "makebox"
442 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
443 // except for Frameless and Boxed, the width cannot be specified if
444 // there is no inner box
445 bool const width_enabled =
446 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
447 // enable if width_enabled
448 widthED->setEnabled(width_enabled);
449 widthUnitsLC->setEnabled(width_enabled);
450 if (!widthCB->isChecked() && widthCB->isEnabled()) {
451 widthED->setEnabled(false);
452 widthUnitsLC->setEnabled(false);
454 // halign is only allowed if a width is used
455 halignCO->setEnabled(widthCB->isChecked());
456 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
457 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
458 && halignCO->count() < 4)
459 halignCO->addItem(toqstr("Stretch"));
460 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
461 halignCO->removeItem(3);
462 // pagebreak is only allowed for Boxed without inner box
463 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
465 heightED->setEnabled(itype != "none" && heightCB->isChecked());
466 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
467 heightCB->setEnabled(ibox);
469 // enable line thickness for the rectangular frame types and drop shadow
470 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
471 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
472 // set default values if empty
473 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
474 thicknessED->setText("0.4");
475 thicknessUnitsLC->setCurrentItem(Length::PT);
477 // enable line separation for the allowed frame types
478 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
479 || outer == "Doublebox" || outer == "Shadowbox");
480 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
481 || outer == "Doublebox" || outer == "Shadowbox");
482 // set default values if empty
483 if (separationED->text().isEmpty() && separationED->isEnabled()) {
484 separationED->setText("3");
485 separationUnitsLC->setCurrentItem(Length::PT);
487 // enable shadow size for drop shadow
488 shadowsizeED->setEnabled(outer == "Shadowbox");
489 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
490 // set default values if empty
491 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
492 shadowsizeED->setText("4");
493 shadowsizeUnitsLC->setCurrentItem(Length::PT);
497 return InsetParamsWidget::checkWidgets();
501 void GuiBox::setSpecial(bool ibox)
503 QString const last_item =
504 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
506 // check if the widget contains the special units
507 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
508 // insert 'em if needed...
509 if (!ibox && !has_special) {
510 for (int i = 1; i < ids_spec_.size(); ++i)
511 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
512 // ... or remove 'em if needed
513 } else if (ibox && has_special) {
514 for (int i = 1; i < ids_spec_.size(); ++i) {
515 int n = widthUnitsLC->findData(ids_spec_[i]);
517 widthUnitsLC->removeItem(n);
520 // restore selected text, if possible
521 widthUnitsLC->setCurrentItem(last_item);
525 void GuiBox::setInnerType(bool frameless, QString const & type)
527 // with "frameless" boxes, inner box is mandatory
528 // (i.e. is the actual box)
529 // we have to remove "none" then and adjust the combo
532 innerBoxCO->addItem(qt_("None"), toqstr("none"));
534 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
535 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
536 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
537 int i = (innerBoxCO->findData(type) != -1)
538 ? innerBoxCO->findData(type) : 0;
539 innerBoxCO->setCurrentIndex(i);
542 } // namespace frontend
546 #include "moc_GuiBox.cpp"