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 "GuiApplication.h"
19 #include "ColorCache.h"
21 #include "LengthCombo.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
25 #include "insets/InsetBox.h"
27 #include "support/gettext.h"
28 #include "support/Length.h"
29 #include "support/lstrings.h"
33 #include <QPushButton>
47 static QStringList boxGuiIds()
50 << "Frameless" << "Boxed"
51 << "ovalbox" << "Ovalbox"
52 << "Shadowbox" << "Shaded"
57 static QStringList boxGuiNames()
60 << qt_("No frame") << qt_("Simple rectangular frame")
61 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
62 << qt_("Drop shadow") << qt_("Shaded background")
63 << qt_("Double rectangular frame");
67 static QStringList boxGuiSpecialLengthIds()
69 return QStringList() << "height" << "depth"
70 << "totalheight" << "width";
74 static QStringList boxGuiSpecialLengthNames()
76 return QStringList() << qt_("Height") << qt_("Depth")
77 << qt_("Total Height") << qt_("Width");
81 static QList<ColorCode> colors()
83 QList<ColorCode> colors;
84 colors << Color_black;
85 colors << Color_white;
87 colors << Color_brown;
89 colors << Color_darkgray;
91 colors << Color_green;
92 colors << Color_lightgray;
94 colors << Color_magenta;
95 colors << Color_olive;
96 colors << Color_orange;
98 colors << Color_purple;
100 colors << Color_teal;
101 colors << Color_violet;
102 colors << Color_yellow;
107 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
111 // fill the box type choice
113 gui_names_ = boxGuiNames();
114 for (int i = 0; i != ids_.size(); ++i)
115 typeCO->addItem(gui_names_[i], ids_[i]);
117 // add the special units to the height choice
118 // width needs different handling
119 ids_spec_ = boxGuiSpecialLengthIds();
120 gui_names_spec_ = boxGuiSpecialLengthNames();
121 for (int i = 0; i != ids_spec_.size(); ++i)
122 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
124 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
125 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
126 this, SIGNAL(changed()));
127 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
128 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
129 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
130 this, SIGNAL(changed()));
131 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
132 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
133 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
134 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
135 this, SIGNAL(changed()));
136 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
137 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
138 this, SIGNAL(changed()));
139 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
140 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
141 this, SIGNAL(changed()));
142 connect(backgroundColorCO, SIGNAL(currentIndexChanged(int)),
143 this, SIGNAL(changed()));
145 heightED->setValidator(unsignedLengthValidator(heightED));
146 widthED->setValidator(unsignedLengthValidator(widthED));
147 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
148 separationED->setValidator(unsignedLengthValidator(separationED));
149 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
151 // initialize the length validator
152 addCheckedWidget(widthED, widthCB);
153 addCheckedWidget(heightED, heightCB);
154 addCheckedWidget(thicknessED, thicknessLA);
155 addCheckedWidget(separationED, separationLA);
156 addCheckedWidget(shadowsizeED, shadowsizeLA);
158 // the background can be uncolored while the frame cannot
159 color_codes_ = colors();
160 sort(color_codes_.begin(), color_codes_.end(), ColorSorter);
161 fillComboColor(backgroundColorCO, true);
162 fillComboColor(frameColorCO, false);
168 void GuiBox::fillComboColor(QComboBox * combo, bool const is_none)
171 QPixmap coloritem(32, 32);
173 // frameColorCO cannot be uncolored
175 combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
176 toqstr(lcolor.getLaTeXName(Color_none)));
177 QList<ColorCode>::const_iterator cit = color_codes_.begin();
178 for (; cit != color_codes_.end(); ++cit) {
179 QString const latexname = toqstr(lcolor.getLaTeXName(*cit));
180 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
181 color = QColor(guiApp->colorCache().get(*cit, false));
182 coloritem.fill(color);
183 combo->addItem(QIcon(coloritem), guiname, latexname);
188 void GuiBox::on_innerBoxCO_activated(int index)
190 QString itype = innerBoxCO->itemData(index).toString();
191 // handle parbox and minipage the same way
192 bool const ibox = (itype != "none" && itype != "makebox");
193 if (heightCB->isChecked() && !ibox)
194 heightCB->setChecked(false);
195 widthCB->setChecked(!widthED->text().isEmpty());
201 void GuiBox::on_typeCO_activated(int index)
204 typeCO->itemData(index).toString();
205 bool const frameless = (type == "Frameless");
207 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
208 setInnerType(frameless, itype);
209 // refresh itype because it might have been changed in setInnerType
211 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
212 // handle parbox and minipage the same way
213 bool const ibox = (itype != "none" && itype != "makebox");
214 if (frameless && itype != "makebox") {
215 if (heightCB->isChecked() && !ibox)
216 heightCB->setChecked(false);
219 if (type != "Boxed") {
220 if (type != "Frameless")
221 widthCB->setChecked(itype != "none");
222 pagebreakCB->setChecked(false);
224 // assure that the frame color is black for frameless boxes to
225 // provide the color "none"
226 int const b = frameColorCO->findData("black");
227 if (frameless && frameColorCO->currentIndex() != b)
228 frameColorCO->setCurrentIndex(b);
233 void GuiBox::on_frameColorCO_currentIndexChanged(int index)
235 // if there is a non-black frame color the background cannot be uncolored
236 // therefore remove the entry "none" in this case
237 int const n = backgroundColorCO->findData("none");
238 if (index != frameColorCO->findData("black")) {
240 if (backgroundColorCO->currentIndex() == n)
241 backgroundColorCO->setCurrentIndex(
242 backgroundColorCO->findData("white"));
243 backgroundColorCO->removeItem(n);
247 backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
248 toqstr(lcolor.getLaTeXName(Color_none)));
254 void GuiBox::initDialog()
256 setInnerType(true, toqstr("minipage"));
257 widthED->setText("100");
258 widthCB->setChecked(true);
259 widthCB->setEnabled(false);
260 widthUnitsLC->setCurrentItem(Length::PCW);
261 heightED->setText("1");
262 heightUnitsLC->setCurrentItem("totalheight");
263 // LaTeX's default for \fboxrule is 0.4 pt
264 thicknessED->setText("0.4");
265 thicknessUnitsLC->setCurrentItem(Length::PT);
266 // LaTeX's default for \fboxsep is 3 pt
267 separationED->setText("3");
268 separationUnitsLC->setCurrentItem(Length::PT);
269 // LaTeX's default for \shadowsize is 4 pt
270 shadowsizeED->setText("4");
271 shadowsizeUnitsLC->setCurrentItem(Length::PT);
275 void GuiBox::on_widthCB_stateChanged(int)
281 void GuiBox::on_heightCB_stateChanged(int /*state*/)
287 void GuiBox::on_pagebreakCB_stateChanged()
289 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
291 widthCB->setChecked(false);
293 on_typeCO_activated(typeCO->currentIndex());
301 void GuiBox::paramsToDialog(Inset const * inset)
303 InsetBox const * box = static_cast<InsetBox const *>(inset);
304 InsetBoxParams const & params = box->params();
305 QString type = toqstr(params.type);
306 if (type == "Framed") {
307 pagebreakCB->setChecked(true);
310 pagebreakCB->setChecked(false);
313 typeCO->setCurrentIndex(typeCO->findData(type));
316 QString inner_type = "minipage";
317 if (!params.inner_box)
319 if (params.use_parbox)
320 inner_type = "parbox";
321 if (params.use_makebox)
322 inner_type = "makebox";
323 bool const frameless = (params.type == "Frameless");
324 setInnerType(frameless, inner_type);
327 valignCO->setCurrentIndex(string("tcb").find(c, 0));
328 c = params.inner_pos;
329 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
331 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
333 bool ibox = (params.inner_box && !params.use_makebox);
334 valignCO->setEnabled(ibox);
335 ialignCO->setEnabled(ibox);
338 // halign is only allowed without inner box and if a width is used and if
339 // pagebreak is not used
340 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
341 && ((!ibox && type == "Boxed") || inner_type == "makebox"));
342 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
343 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
344 && halignCO->count() < 4)
345 halignCO->addItem(qt_("Stretch"));
346 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
347 halignCO->removeItem(3);
348 // pagebreak is only allowed for Boxed without inner box
349 pagebreakCB->setEnabled(!ibox && type == "Boxed");
351 Length::UNIT const default_unit = Length::defaultUnit();
353 // the width can only be selected for makebox or framebox
354 widthCB->setEnabled(inner_type == "makebox"
356 && !ibox && !pagebreakCB->isChecked()));
357 if (params.width.empty()) {
358 widthCB->setChecked(false);
359 lengthToWidgets(widthED, widthUnitsLC,
360 params.width, default_unit);
362 widthCB->setChecked(true);
363 lengthToWidgets(widthED, widthUnitsLC,
364 params.width, default_unit);
365 QString const special = toqstr(params.special);
366 if (!special.isEmpty() && special != "none")
367 widthUnitsLC->setCurrentItem(special);
370 widthED->setEnabled(widthCB->isChecked());
371 widthUnitsLC->setEnabled(widthCB->isChecked());
373 lengthToWidgets(heightED, heightUnitsLC,
374 (params.height).asString(), default_unit);
376 QString const height_special = toqstr(params.height_special);
377 if (!height_special.isEmpty() && height_special != "none")
378 heightUnitsLC->setCurrentItem(height_special);
379 // set no optional height if the value is the default "1\height"
380 // (special units like \height are handled as "in",
381 // FIXME: this is a very bad UI, this check box should be disabled in
382 // this case, not forced to 'unchecked' state.
383 if (height_special == "totalheight" && params.height == Length("1in"))
384 heightCB->setCheckState(Qt::Unchecked);
386 heightCB->setCheckState(Qt::Checked);
388 heightCB->setEnabled(ibox);
390 // enable line thickness only for the rectangular frame types and drop shadow
391 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
392 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
393 lengthToWidgets(thicknessED, thicknessUnitsLC,
394 (params.thickness).asString(), default_unit);
395 // enable line separation for the allowed frame types
396 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
397 || type == "Doublebox" || type == "Shadowbox");
398 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
399 || type == "Doublebox" || type == "Shadowbox");
400 lengthToWidgets(separationED, separationUnitsLC,
401 (params.separation).asString(), default_unit);
402 // enable shadow size for drop shadow
403 shadowsizeED->setEnabled(type == "Shadowbox");
404 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
405 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
406 (params.shadowsize).asString(), default_unit);
408 frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
409 backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
413 docstring GuiBox::dialogToParams() const
415 bool const pagebreak =
416 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
421 box_type = fromqstr(typeCO->itemData(
422 typeCO->currentIndex()).toString());
424 InsetBoxParams params(box_type);
426 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
428 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
430 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
432 params.pos = "tcb"[valignCO->currentIndex()];
433 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
434 params.hor_pos = "lcrs"[halignCO->currentIndex()];
437 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
438 QString value = widthED->text();
440 if (widthED->isEnabled()) {
441 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
442 params.special = fromqstr(unit);
443 // Note: the unit is simply ignored in this case
444 params.width = Length(widgetToDouble(widthED), Length::IN);
446 params.special = "none";
447 // we must specify a valid length in this case
449 widthED->setText("0");
450 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
453 params.special = "none";
454 params.width = Length();
457 // the height parameter is omitted if the value
458 // is "1in" and "Total Height" is used as unit.
459 // 1in + "Total Height" means "1\height" which is the LaTeX default
460 // if no height is given
461 if (heightCB->checkState() == Qt::Unchecked) {
462 params.height = Length("1in");
463 params.height_special = "totalheight";
465 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
466 value = heightED->text();
467 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
468 params.height_special = fromqstr(unit);
469 // Note: the unit is simply ignored in this case
470 params.height = Length(widgetToDouble(heightED), Length::IN);
472 params.height_special = "none";
474 Length(widgetsToLength(heightED, heightUnitsLC));
478 // handle the line thickness, line separation and shadow size
479 if (thicknessED->isEnabled())
480 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
482 params.thickness = Length("0.4pt");
483 if (separationED->isEnabled())
484 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
486 params.separation = Length("3pt");
487 if (separationED->isEnabled())
488 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
490 params.shadowsize = Length("4pt");
491 if (frameColorCO->isEnabled())
493 fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
495 params.framecolor = "black";
496 if (backgroundColorCO->isEnabled())
497 params.backgroundcolor =
498 fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
500 params.backgroundcolor = "none";
502 return from_ascii(InsetBox::params2string(params));
506 bool GuiBox::checkWidgets(bool readonly) const
508 typeCO->setEnabled(!readonly);
511 pagebreakCB->setEnabled(false);
512 innerBoxCO->setEnabled(false);
513 valignCO->setEnabled(false);
514 ialignCO->setEnabled(false);
515 halignCO->setEnabled(false);
516 widthCB->setEnabled(false);
517 widthED->setEnabled(false);
518 widthUnitsLC->setEnabled(false);
519 heightED->setEnabled(false);
520 heightUnitsLC->setEnabled(false);
521 heightCB->setEnabled(false);
522 thicknessED->setEnabled(false);
523 thicknessUnitsLC->setEnabled(false);
524 separationED->setEnabled(false);
525 separationUnitsLC->setEnabled(false);
526 shadowsizeED->setEnabled(false);
527 shadowsizeUnitsLC->setEnabled(false);
529 QString const outer =
530 typeCO->itemData(typeCO->currentIndex()).toString();
531 QString const itype =
532 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
533 bool const ibox = (itype != "none" && itype != "makebox");
534 valignCO->setEnabled(ibox);
535 ialignCO->setEnabled(ibox);
536 if (heightCB->isChecked() && !ibox)
537 heightCB->setChecked(false);
538 heightCB->setEnabled(ibox);
539 // the width can only be selected for makebox or framebox
540 widthCB->setEnabled(itype == "makebox"
541 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
542 // except for Frameless and Boxed, the width cannot be specified if
543 // there is no inner box
544 bool const width_enabled =
545 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
546 // enable if width_enabled
547 widthED->setEnabled(width_enabled);
548 widthUnitsLC->setEnabled(width_enabled);
549 if (!widthCB->isChecked() && widthCB->isEnabled()) {
550 widthED->setEnabled(false);
551 widthUnitsLC->setEnabled(false);
553 // halign is only allowed without inner box and if a width is used and if
554 // pagebreak is not used
555 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
556 && ((!ibox && outer == "Boxed") || itype == "makebox"));
557 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
558 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
559 && halignCO->count() < 4)
560 halignCO->addItem(qt_("Stretch"));
561 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
562 halignCO->removeItem(3);
563 // pagebreak is only allowed for Boxed without inner box
564 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
566 heightED->setEnabled(itype != "none" && heightCB->isChecked());
567 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
568 heightCB->setEnabled(ibox);
570 // enable line thickness for the rectangular frame types and drop shadow
571 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
572 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
573 // set default values if empty
574 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
575 thicknessED->setText("0.4");
576 thicknessUnitsLC->setCurrentItem(Length::PT);
578 // enable line separation for the allowed frame types
579 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
580 || outer == "Doublebox" || outer == "Shadowbox");
581 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
582 || outer == "Doublebox" || outer == "Shadowbox");
583 // set default values if empty
584 if (separationED->text().isEmpty() && separationED->isEnabled()) {
585 separationED->setText("3");
586 separationUnitsLC->setCurrentItem(Length::PT);
588 // enable shadow size for drop shadow
589 shadowsizeED->setEnabled(outer == "Shadowbox");
590 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
591 // set default values if empty
592 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
593 shadowsizeED->setText("4");
594 shadowsizeUnitsLC->setCurrentItem(Length::PT);
596 // \fboxcolor and \colorbox cannot be used for fancybox boxes
597 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
598 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
601 return InsetParamsWidget::checkWidgets();
605 void GuiBox::setSpecial(bool ibox)
607 QString const last_item =
608 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
610 // check if the widget contains the special units
611 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
612 // insert 'em if needed...
613 if (!ibox && !has_special) {
614 for (int i = 1; i < ids_spec_.size(); ++i)
615 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
616 // ... or remove 'em if needed
617 } else if (ibox && has_special) {
618 for (int i = 1; i < ids_spec_.size(); ++i) {
619 int n = widthUnitsLC->findData(ids_spec_[i]);
621 widthUnitsLC->removeItem(n);
624 // restore selected text, if possible
625 widthUnitsLC->setCurrentItem(last_item);
629 void GuiBox::setInnerType(bool frameless, QString const & type)
631 // with "frameless" boxes, inner box is mandatory
632 // (i.e. is the actual box)
633 // we have to remove "none" then and adjust the combo
636 innerBoxCO->addItem(qt_("None"), toqstr("none"));
638 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
639 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
640 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
641 int i = (innerBoxCO->findData(type) != -1)
642 ? innerBoxCO->findData(type) : 0;
643 innerBoxCO->setCurrentIndex(i);
646 } // namespace frontend
650 #include "moc_GuiBox.cpp"