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"
23 #include "qt_helpers.h"
24 #include "Validator.h"
26 #include "insets/InsetBox.h"
28 #include "support/gettext.h"
29 #include "support/foreach.h"
30 #include "support/lstrings.h"
34 #include <QPushButton>
46 static QStringList boxGuiIds()
49 << "Frameless" << "Boxed"
50 << "ovalbox" << "Ovalbox"
51 << "Shadowbox" << "Shaded"
56 static QStringList boxGuiNames()
59 << qt_("No frame") << qt_("Simple rectangular frame")
60 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
61 << qt_("Drop shadow") << qt_("Shaded background")
62 << qt_("Double rectangular frame");
66 static QStringList boxGuiSpecialLengthIds()
68 return QStringList() << "height" << "depth"
69 << "totalheight" << "width";
73 static QStringList boxGuiSpecialLengthNames()
75 return QStringList() << qt_("Height") << qt_("Depth")
76 << qt_("Total Height") << qt_("Width");
80 static QList<ColorCode> colors()
82 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(frameColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
143 connect(backgroundColorCO, SIGNAL(highlighted(QString)), 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 fillComboColor(frameColorCO, false);
161 fillComboColor(backgroundColorCO, true);
167 void GuiBox::fillComboColor(QComboBox * combo, bool const is_none)
169 QPixmap coloritem(32, 32);
171 // frameColorCO cannot be uncolored
173 combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
174 toqstr(lcolor.getLaTeXName(Color_none)));
175 QList<ColorCode>::const_iterator cit = color_codes_.begin() + 1;
176 for (; cit != color_codes_.end(); ++cit) {
177 QString const latexname = toqstr(lcolor.getLaTeXName(*cit));
178 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
179 color = QColor(guiApp->colorCache().get(*cit, false));
180 coloritem.fill(color);
181 combo->addItem(QIcon(coloritem), guiname, latexname);
186 void GuiBox::on_innerBoxCO_activated(int /* index */)
189 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
190 // handle parbox and minipage the same way
191 bool const ibox = (itype != "none" && itype != "makebox");
192 if (heightCB->isChecked() && !ibox)
193 heightCB->setChecked(false);
194 widthCB->setChecked(!widthED->text().isEmpty());
200 void GuiBox::on_typeCO_activated(int index)
203 typeCO->itemData(index).toString();
204 bool const frameless = (type == "Frameless");
206 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
207 setInnerType(frameless, itype);
208 // refresh itype because it might have been changed in setInnerType
210 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
211 // handle parbox and minipage the same way
212 bool const ibox = (itype != "none" && itype != "makebox");
213 if (frameless && itype != "makebox") {
214 if (heightCB->isChecked() && !ibox)
215 heightCB->setChecked(false);
218 if (type != "Boxed") {
219 if (type != "Frameless")
220 widthCB->setChecked(itype != "none");
221 pagebreakCB->setChecked(false);
223 // assure that the frame color is black for frameless boxes to
224 // provide the color "none"
225 int const b = frameColorCO->findData("black");
226 if (frameless && frameColorCO->currentIndex() != b)
227 frameColorCO->setCurrentIndex(b);
232 void GuiBox::on_frameColorCO_currentIndexChanged(int index)
234 // if there is a non-black frame color the background cannot be uncolored
235 // therefore remove the entry "none" in this case
236 if (index != frameColorCO->findData("black")) {
237 int const n = backgroundColorCO->findData("none");
239 if (backgroundColorCO->currentIndex() == n)
240 backgroundColorCO->setCurrentIndex(
241 backgroundColorCO->findData("white"));
242 backgroundColorCO->removeItem(n);
245 if (backgroundColorCO->findData("none") == -1)
246 backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
247 toqstr(lcolor.getLaTeXName(Color_none)));
253 void GuiBox::initDialog()
255 setInnerType(true, toqstr("minipage"));
256 widthED->setText("100");
257 widthCB->setChecked(true);
258 widthCB->setEnabled(false);
259 widthUnitsLC->setCurrentItem(Length::PCW);
260 heightED->setText("1");
261 heightUnitsLC->setCurrentItem("totalheight");
262 // LaTeX's default for \fboxrule is 0.4 pt
263 thicknessED->setText("0.4");
264 thicknessUnitsLC->setCurrentItem(Length::PT);
265 // LaTeX's default for \fboxsep is 3 pt
266 separationED->setText("3");
267 separationUnitsLC->setCurrentItem(Length::PT);
268 // LaTeX's default for \shadowsize is 4 pt
269 shadowsizeED->setText("4");
270 shadowsizeUnitsLC->setCurrentItem(Length::PT);
274 void GuiBox::on_widthCB_stateChanged(int)
280 void GuiBox::on_heightCB_stateChanged(int /*state*/)
286 void GuiBox::on_pagebreakCB_stateChanged()
288 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
290 widthCB->setChecked(!pbreak);
292 on_typeCO_activated(typeCO->currentIndex());
300 void GuiBox::paramsToDialog(Inset const * inset)
302 InsetBox const * box = static_cast<InsetBox const *>(inset);
303 InsetBoxParams const & params = box->params();
304 QString type = toqstr(params.type);
305 if (type == "Framed") {
306 pagebreakCB->setChecked(true);
309 pagebreakCB->setChecked(false);
312 typeCO->setCurrentIndex(typeCO->findData(type));
315 QString inner_type = "minipage";
316 if (!params.inner_box)
318 if (params.use_parbox)
319 inner_type = "parbox";
320 if (params.use_makebox)
321 inner_type = "makebox";
322 bool const frameless = (params.type == "Frameless");
323 setInnerType(frameless, inner_type);
326 valignCO->setCurrentIndex(string("tcb").find(c, 0));
327 c = params.inner_pos;
328 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
330 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
332 bool ibox = (params.inner_box && !params.use_makebox);
333 valignCO->setEnabled(ibox);
334 ialignCO->setEnabled(ibox);
337 // halign is only allowed if a width is used
338 halignCO->setEnabled(widthCB->isChecked());
339 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
340 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
341 && halignCO->count() < 4)
342 halignCO->addItem(toqstr("Stretch"));
343 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
344 halignCO->removeItem(3);
345 // pagebreak is only allowed for Boxed without inner box
346 pagebreakCB->setEnabled(!ibox && type == "Boxed");
348 Length::UNIT const default_unit = Length::defaultUnit();
350 // the width can only be selected for makebox or framebox
351 widthCB->setEnabled(inner_type == "makebox"
353 && !ibox && !pagebreakCB->isChecked()));
354 if (params.width.empty()) {
355 widthCB->setChecked(false);
356 lengthToWidgets(widthED, widthUnitsLC,
357 params.width, default_unit);
359 widthCB->setChecked(true);
360 lengthToWidgets(widthED, widthUnitsLC,
361 params.width, default_unit);
362 QString const special = toqstr(params.special);
363 if (!special.isEmpty() && special != "none")
364 widthUnitsLC->setCurrentItem(special);
367 widthED->setEnabled(widthCB->isChecked());
368 widthUnitsLC->setEnabled(widthCB->isChecked());
370 lengthToWidgets(heightED, heightUnitsLC,
371 (params.height).asString(), default_unit);
373 QString const height_special = toqstr(params.height_special);
374 if (!height_special.isEmpty() && height_special != "none")
375 heightUnitsLC->setCurrentItem(height_special);
376 // set no optional height if the value is the default "1\height"
377 // (special units like \height are handled as "in",
378 // FIXME: this is a very bad UI, this check box should be disabled in
379 // this case, not forced to 'unchecked' state.
380 if (height_special == "totalheight" && params.height == Length("1in"))
381 heightCB->setCheckState(Qt::Unchecked);
383 heightCB->setCheckState(Qt::Checked);
385 heightCB->setEnabled(ibox);
387 // enable line thickness only for the rectangular frame types and drop shadow
388 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
389 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
390 lengthToWidgets(thicknessED, thicknessUnitsLC,
391 (params.thickness).asString(), default_unit);
392 // enable line separation for the allowed frame types
393 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
394 || type == "Doublebox" || type == "Shadowbox");
395 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
396 || type == "Doublebox" || type == "Shadowbox");
397 lengthToWidgets(separationED, separationUnitsLC,
398 (params.separation).asString(), default_unit);
399 // enable shadow size for drop shadow
400 shadowsizeED->setEnabled(type == "Shadowbox");
401 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
402 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
403 (params.shadowsize).asString(), default_unit);
405 // FIXME: Why use LaTeXName here, not LyXName? (spitz)
406 frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
407 backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
411 docstring GuiBox::dialogToParams() const
413 bool const pagebreak =
414 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
419 box_type = fromqstr(typeCO->itemData(
420 typeCO->currentIndex()).toString());
422 InsetBoxParams params(box_type);
424 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
426 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
428 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
430 params.pos = "tcb"[valignCO->currentIndex()];
431 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
432 params.hor_pos = "lcrs"[halignCO->currentIndex()];
435 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
436 QString value = widthED->text();
438 if (widthED->isEnabled()) {
439 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
440 params.special = fromqstr(unit);
441 // Note: the unit is simply ignored in this case
442 params.width = Length(value.toDouble(), Length::IN);
444 params.special = "none";
445 // we must specify a valid length in this case
447 widthED->setText("0");
448 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
451 params.special = "none";
452 params.width = Length();
455 // the height parameter is omitted if the value
456 // is "1in" and "Total Height" is used as unit.
457 // 1in + "Total Height" means "1\height" which is the LaTeX default
458 // if no height is given
459 if (heightCB->checkState() == Qt::Unchecked) {
460 params.height = Length("1in");
461 params.height_special = "totalheight";
463 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
464 value = heightED->text();
465 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
466 params.height_special = fromqstr(unit);
467 // Note: the unit is simply ignored in this case
468 params.height = Length(value.toDouble(), Length::IN);
470 params.height_special = "none";
472 Length(widgetsToLength(heightED, heightUnitsLC));
476 // handle the line thickness, line separation and shadow size
477 if (thicknessED->isEnabled())
478 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
480 params.thickness = Length("0.4pt");
481 if (separationED->isEnabled())
482 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
484 params.separation = Length("3pt");
485 if (separationED->isEnabled())
486 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
488 params.shadowsize = Length("4pt");
489 // FIXME: Why use LaTeXName here, not LyXName? (spitz)
490 if (frameColorCO->isEnabled())
492 fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
494 params.framecolor = "black";
495 if (backgroundColorCO->isEnabled())
496 params.backgroundcolor =
497 fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
499 params.backgroundcolor = "none";
501 return from_ascii(InsetBox::params2string(params));
505 bool GuiBox::checkWidgets(bool readonly) const
507 typeCO->setEnabled(!readonly);
510 pagebreakCB->setEnabled(false);
511 innerBoxCO->setEnabled(false);
512 valignCO->setEnabled(false);
513 ialignCO->setEnabled(false);
514 halignCO->setEnabled(false);
515 widthCB->setEnabled(false);
516 widthED->setEnabled(false);
517 widthUnitsLC->setEnabled(false);
518 heightED->setEnabled(false);
519 heightUnitsLC->setEnabled(false);
520 heightCB->setEnabled(false);
521 thicknessED->setEnabled(false);
522 thicknessUnitsLC->setEnabled(false);
523 separationED->setEnabled(false);
524 separationUnitsLC->setEnabled(false);
525 shadowsizeED->setEnabled(false);
526 shadowsizeUnitsLC->setEnabled(false);
528 QString const outer =
529 typeCO->itemData(typeCO->currentIndex()).toString();
530 QString const itype =
531 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
532 bool const ibox = (itype != "none" && itype != "makebox");
533 valignCO->setEnabled(ibox);
534 ialignCO->setEnabled(ibox);
535 if (heightCB->isChecked() && !ibox)
536 heightCB->setChecked(false);
537 heightCB->setEnabled(ibox);
538 // the width can only be selected for makebox or framebox
539 widthCB->setEnabled(itype == "makebox"
540 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
541 // except for Frameless and Boxed, the width cannot be specified if
542 // there is no inner box
543 bool const width_enabled =
544 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
545 // enable if width_enabled
546 widthED->setEnabled(width_enabled);
547 widthUnitsLC->setEnabled(width_enabled);
548 if (!widthCB->isChecked() && widthCB->isEnabled()) {
549 widthED->setEnabled(false);
550 widthUnitsLC->setEnabled(false);
552 // halign is only allowed if a width is used
553 halignCO->setEnabled(widthCB->isChecked());
554 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
555 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
556 && halignCO->count() < 4)
557 halignCO->addItem(toqstr("Stretch"));
558 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
559 halignCO->removeItem(3);
560 // pagebreak is only allowed for Boxed without inner box
561 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
563 heightED->setEnabled(itype != "none" && heightCB->isChecked());
564 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
565 heightCB->setEnabled(ibox);
567 // enable line thickness for the rectangular frame types and drop shadow
568 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
569 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
570 // set default values if empty
571 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
572 thicknessED->setText("0.4");
573 thicknessUnitsLC->setCurrentItem(Length::PT);
575 // enable line separation for the allowed frame types
576 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
577 || outer == "Doublebox" || outer == "Shadowbox");
578 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
579 || outer == "Doublebox" || outer == "Shadowbox");
580 // set default values if empty
581 if (separationED->text().isEmpty() && separationED->isEnabled()) {
582 separationED->setText("3");
583 separationUnitsLC->setCurrentItem(Length::PT);
585 // enable shadow size for drop shadow
586 shadowsizeED->setEnabled(outer == "Shadowbox");
587 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
588 // set default values if empty
589 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
590 shadowsizeED->setText("4");
591 shadowsizeUnitsLC->setCurrentItem(Length::PT);
593 // \fboxcolor and \colorbox cannot be used for fancybox boxes
594 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
595 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
598 return InsetParamsWidget::checkWidgets();
602 void GuiBox::setSpecial(bool ibox)
604 QString const last_item =
605 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
607 // check if the widget contains the special units
608 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
609 // insert 'em if needed...
610 if (!ibox && !has_special) {
611 for (int i = 1; i < ids_spec_.size(); ++i)
612 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
613 // ... or remove 'em if needed
614 } else if (ibox && has_special) {
615 for (int i = 1; i < ids_spec_.size(); ++i) {
616 int n = widthUnitsLC->findData(ids_spec_[i]);
618 widthUnitsLC->removeItem(n);
621 // restore selected text, if possible
622 widthUnitsLC->setCurrentItem(last_item);
626 void GuiBox::setInnerType(bool frameless, QString const & type)
628 // with "frameless" boxes, inner box is mandatory
629 // (i.e. is the actual box)
630 // we have to remove "none" then and adjust the combo
633 innerBoxCO->addItem(qt_("None"), toqstr("none"));
635 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
636 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
637 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
638 int i = (innerBoxCO->findData(type) != -1)
639 ? innerBoxCO->findData(type) : 0;
640 innerBoxCO->setCurrentIndex(i);
643 } // namespace frontend
647 #include "moc_GuiBox.cpp"