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/lstrings.h"
33 #include <QPushButton>
45 static QStringList boxGuiIds()
48 << "Frameless" << "Boxed"
49 << "ovalbox" << "Ovalbox"
50 << "Shadowbox" << "Shaded"
55 static QStringList boxGuiNames()
58 << qt_("No frame") << qt_("Simple rectangular frame")
59 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
60 << qt_("Drop shadow") << qt_("Shaded background")
61 << qt_("Double rectangular frame");
65 static QStringList boxGuiSpecialLengthIds()
67 return QStringList() << "height" << "depth"
68 << "totalheight" << "width";
72 static QStringList boxGuiSpecialLengthNames()
74 return QStringList() << qt_("Height") << qt_("Depth")
75 << qt_("Total Height") << qt_("Width");
79 static QList<ColorCode> colors()
81 QList<ColorCode> colors;
82 colors << Color_black;
83 colors << Color_white;
85 colors << Color_brown;
87 colors << Color_darkgray;
89 colors << Color_green;
90 colors << Color_lightgray;
92 colors << Color_magenta;
93 colors << Color_olive;
94 colors << Color_orange;
96 colors << Color_purple;
99 colors << Color_violet;
100 colors << Color_yellow;
105 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
109 // fill the box type choice
111 gui_names_ = boxGuiNames();
112 for (int i = 0; i != ids_.size(); ++i)
113 typeCO->addItem(gui_names_[i], ids_[i]);
115 // add the special units to the height choice
116 // width needs different handling
117 ids_spec_ = boxGuiSpecialLengthIds();
118 gui_names_spec_ = boxGuiSpecialLengthNames();
119 for (int i = 0; i != ids_spec_.size(); ++i)
120 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
122 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
123 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
124 this, SIGNAL(changed()));
125 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
126 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
127 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
128 this, SIGNAL(changed()));
129 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
130 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
131 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
132 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
133 this, SIGNAL(changed()));
134 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
135 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
136 this, SIGNAL(changed()));
137 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
138 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
139 this, SIGNAL(changed()));
140 connect(backgroundColorCO, SIGNAL(currentIndexChanged(int)),
141 this, SIGNAL(changed()));
143 heightED->setValidator(unsignedLengthValidator(heightED));
144 widthED->setValidator(unsignedLengthValidator(widthED));
145 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
146 separationED->setValidator(unsignedLengthValidator(separationED));
147 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
149 // initialize the length validator
150 addCheckedWidget(widthED, widthCB);
151 addCheckedWidget(heightED, heightCB);
152 addCheckedWidget(thicknessED, thicknessLA);
153 addCheckedWidget(separationED, separationLA);
154 addCheckedWidget(shadowsizeED, shadowsizeLA);
156 // the background can be uncolored while the frame cannot
157 color_codes_ = colors();
158 qSort(color_codes_.begin(), color_codes_.end(), ColorSorter);
159 fillComboColor(backgroundColorCO, true);
160 fillComboColor(frameColorCO, false);
166 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();
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)
188 QString itype = innerBoxCO->itemData(index).toString();
189 // handle parbox and minipage the same way
190 bool const ibox = (itype != "none" && itype != "makebox");
191 if (heightCB->isChecked() && !ibox)
192 heightCB->setChecked(false);
193 widthCB->setChecked(!widthED->text().isEmpty());
199 void GuiBox::on_typeCO_activated(int index)
202 typeCO->itemData(index).toString();
203 bool const frameless = (type == "Frameless");
205 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
206 setInnerType(frameless, itype);
207 // refresh itype because it might have been changed in setInnerType
209 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
210 // handle parbox and minipage the same way
211 bool const ibox = (itype != "none" && itype != "makebox");
212 if (frameless && itype != "makebox") {
213 if (heightCB->isChecked() && !ibox)
214 heightCB->setChecked(false);
217 if (type != "Boxed") {
218 if (type != "Frameless")
219 widthCB->setChecked(itype != "none");
220 pagebreakCB->setChecked(false);
222 // assure that the frame color is black for frameless boxes to
223 // provide the color "none"
224 int const b = frameColorCO->findData("black");
225 if (frameless && frameColorCO->currentIndex() != b)
226 frameColorCO->setCurrentIndex(b);
231 void GuiBox::on_frameColorCO_currentIndexChanged(int index)
233 // if there is a non-black frame color the background cannot be uncolored
234 // therefore remove the entry "none" in this case
235 int const n = backgroundColorCO->findData("none");
236 if (index != frameColorCO->findData("black")) {
238 if (backgroundColorCO->currentIndex() == n)
239 backgroundColorCO->setCurrentIndex(
240 backgroundColorCO->findData("white"));
241 backgroundColorCO->removeItem(n);
245 backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
246 toqstr(lcolor.getLaTeXName(Color_none)));
252 void GuiBox::initDialog()
254 setInnerType(true, toqstr("minipage"));
255 widthED->setText("100");
256 widthCB->setChecked(true);
257 widthCB->setEnabled(false);
258 widthUnitsLC->setCurrentItem(Length::PCW);
259 heightED->setText("1");
260 heightUnitsLC->setCurrentItem("totalheight");
261 // LaTeX's default for \fboxrule is 0.4 pt
262 thicknessED->setText("0.4");
263 thicknessUnitsLC->setCurrentItem(Length::PT);
264 // LaTeX's default for \fboxsep is 3 pt
265 separationED->setText("3");
266 separationUnitsLC->setCurrentItem(Length::PT);
267 // LaTeX's default for \shadowsize is 4 pt
268 shadowsizeED->setText("4");
269 shadowsizeUnitsLC->setCurrentItem(Length::PT);
273 void GuiBox::on_widthCB_stateChanged(int)
279 void GuiBox::on_heightCB_stateChanged(int /*state*/)
285 void GuiBox::on_pagebreakCB_stateChanged()
287 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
289 widthCB->setChecked(!pbreak);
291 on_typeCO_activated(typeCO->currentIndex());
299 void GuiBox::paramsToDialog(Inset const * inset)
301 InsetBox const * box = static_cast<InsetBox const *>(inset);
302 InsetBoxParams const & params = box->params();
303 QString type = toqstr(params.type);
304 if (type == "Framed") {
305 pagebreakCB->setChecked(true);
308 pagebreakCB->setChecked(false);
311 typeCO->setCurrentIndex(typeCO->findData(type));
314 QString inner_type = "minipage";
315 if (!params.inner_box)
317 if (params.use_parbox)
318 inner_type = "parbox";
319 if (params.use_makebox)
320 inner_type = "makebox";
321 bool const frameless = (params.type == "Frameless");
322 setInnerType(frameless, inner_type);
325 valignCO->setCurrentIndex(string("tcb").find(c, 0));
326 c = params.inner_pos;
327 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
329 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
331 bool ibox = (params.inner_box && !params.use_makebox);
332 valignCO->setEnabled(ibox);
333 ialignCO->setEnabled(ibox);
336 // halign is only allowed without inner box and if a width is used and if
337 // pagebreak is not used
338 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
339 && ((!ibox && type == "Boxed") || inner_type == "makebox"));
340 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
341 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
342 && halignCO->count() < 4)
343 halignCO->addItem(toqstr("Stretch"));
344 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
345 halignCO->removeItem(3);
346 // pagebreak is only allowed for Boxed without inner box
347 pagebreakCB->setEnabled(!ibox && type == "Boxed");
349 Length::UNIT const default_unit = Length::defaultUnit();
351 // the width can only be selected for makebox or framebox
352 widthCB->setEnabled(inner_type == "makebox"
354 && !ibox && !pagebreakCB->isChecked()));
355 if (params.width.empty()) {
356 widthCB->setChecked(false);
357 lengthToWidgets(widthED, widthUnitsLC,
358 params.width, default_unit);
360 widthCB->setChecked(true);
361 lengthToWidgets(widthED, widthUnitsLC,
362 params.width, default_unit);
363 QString const special = toqstr(params.special);
364 if (!special.isEmpty() && special != "none")
365 widthUnitsLC->setCurrentItem(special);
368 widthED->setEnabled(widthCB->isChecked());
369 widthUnitsLC->setEnabled(widthCB->isChecked());
371 lengthToWidgets(heightED, heightUnitsLC,
372 (params.height).asString(), default_unit);
374 QString const height_special = toqstr(params.height_special);
375 if (!height_special.isEmpty() && height_special != "none")
376 heightUnitsLC->setCurrentItem(height_special);
377 // set no optional height if the value is the default "1\height"
378 // (special units like \height are handled as "in",
379 // FIXME: this is a very bad UI, this check box should be disabled in
380 // this case, not forced to 'unchecked' state.
381 if (height_special == "totalheight" && params.height == Length("1in"))
382 heightCB->setCheckState(Qt::Unchecked);
384 heightCB->setCheckState(Qt::Checked);
386 heightCB->setEnabled(ibox);
388 // enable line thickness only for the rectangular frame types and drop shadow
389 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
390 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
391 lengthToWidgets(thicknessED, thicknessUnitsLC,
392 (params.thickness).asString(), default_unit);
393 // enable line separation for the allowed frame types
394 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
395 || type == "Doublebox" || type == "Shadowbox");
396 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
397 || type == "Doublebox" || type == "Shadowbox");
398 lengthToWidgets(separationED, separationUnitsLC,
399 (params.separation).asString(), default_unit);
400 // enable shadow size for drop shadow
401 shadowsizeED->setEnabled(type == "Shadowbox");
402 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
403 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
404 (params.shadowsize).asString(), default_unit);
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 if (frameColorCO->isEnabled())
491 fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
493 params.framecolor = "black";
494 if (backgroundColorCO->isEnabled())
495 params.backgroundcolor =
496 fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
498 params.backgroundcolor = "none";
500 return from_ascii(InsetBox::params2string(params));
504 bool GuiBox::checkWidgets(bool readonly) const
506 typeCO->setEnabled(!readonly);
509 pagebreakCB->setEnabled(false);
510 innerBoxCO->setEnabled(false);
511 valignCO->setEnabled(false);
512 ialignCO->setEnabled(false);
513 halignCO->setEnabled(false);
514 widthCB->setEnabled(false);
515 widthED->setEnabled(false);
516 widthUnitsLC->setEnabled(false);
517 heightED->setEnabled(false);
518 heightUnitsLC->setEnabled(false);
519 heightCB->setEnabled(false);
520 thicknessED->setEnabled(false);
521 thicknessUnitsLC->setEnabled(false);
522 separationED->setEnabled(false);
523 separationUnitsLC->setEnabled(false);
524 shadowsizeED->setEnabled(false);
525 shadowsizeUnitsLC->setEnabled(false);
527 QString const outer =
528 typeCO->itemData(typeCO->currentIndex()).toString();
529 QString const itype =
530 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
531 bool const ibox = (itype != "none" && itype != "makebox");
532 valignCO->setEnabled(ibox);
533 ialignCO->setEnabled(ibox);
534 if (heightCB->isChecked() && !ibox)
535 heightCB->setChecked(false);
536 heightCB->setEnabled(ibox);
537 // the width can only be selected for makebox or framebox
538 widthCB->setEnabled(itype == "makebox"
539 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
540 // except for Frameless and Boxed, the width cannot be specified if
541 // there is no inner box
542 bool const width_enabled =
543 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
544 // enable if width_enabled
545 widthED->setEnabled(width_enabled);
546 widthUnitsLC->setEnabled(width_enabled);
547 if (!widthCB->isChecked() && widthCB->isEnabled()) {
548 widthED->setEnabled(false);
549 widthUnitsLC->setEnabled(false);
551 // halign is only allowed without inner box and if a width is used and if
552 // pagebreak is not used
553 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
554 && ((!ibox && outer == "Boxed") || itype == "makebox"));
555 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
556 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
557 && halignCO->count() < 4)
558 halignCO->addItem(toqstr("Stretch"));
559 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
560 halignCO->removeItem(3);
561 // pagebreak is only allowed for Boxed without inner box
562 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
564 heightED->setEnabled(itype != "none" && heightCB->isChecked());
565 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
566 heightCB->setEnabled(ibox);
568 // enable line thickness for the rectangular frame types and drop shadow
569 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
570 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
571 // set default values if empty
572 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
573 thicknessED->setText("0.4");
574 thicknessUnitsLC->setCurrentItem(Length::PT);
576 // enable line separation for the allowed frame types
577 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
578 || outer == "Doublebox" || outer == "Shadowbox");
579 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
580 || outer == "Doublebox" || outer == "Shadowbox");
581 // set default values if empty
582 if (separationED->text().isEmpty() && separationED->isEnabled()) {
583 separationED->setText("3");
584 separationUnitsLC->setCurrentItem(Length::PT);
586 // enable shadow size for drop shadow
587 shadowsizeED->setEnabled(outer == "Shadowbox");
588 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
589 // set default values if empty
590 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
591 shadowsizeED->setText("4");
592 shadowsizeUnitsLC->setCurrentItem(Length::PT);
594 // \fboxcolor and \colorbox cannot be used for fancybox boxes
595 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
596 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
599 return InsetParamsWidget::checkWidgets();
603 void GuiBox::setSpecial(bool ibox)
605 QString const last_item =
606 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
608 // check if the widget contains the special units
609 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
610 // insert 'em if needed...
611 if (!ibox && !has_special) {
612 for (int i = 1; i < ids_spec_.size(); ++i)
613 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
614 // ... or remove 'em if needed
615 } else if (ibox && has_special) {
616 for (int i = 1; i < ids_spec_.size(); ++i) {
617 int n = widthUnitsLC->findData(ids_spec_[i]);
619 widthUnitsLC->removeItem(n);
622 // restore selected text, if possible
623 widthUnitsLC->setCurrentItem(last_item);
627 void GuiBox::setInnerType(bool frameless, QString const & type)
629 // with "frameless" boxes, inner box is mandatory
630 // (i.e. is the actual box)
631 // we have to remove "none" then and adjust the combo
634 innerBoxCO->addItem(qt_("None"), toqstr("none"));
636 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
637 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
638 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
639 int i = (innerBoxCO->findData(type) != -1)
640 ? innerBoxCO->findData(type) : 0;
641 innerBoxCO->setCurrentIndex(i);
644 } // namespace frontend
648 #include "moc_GuiBox.cpp"