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;
83 colors << Color_black;
84 colors << Color_white;
86 colors << Color_brown;
88 colors << Color_darkgray;
90 colors << Color_green;
91 colors << Color_lightgray;
93 colors << Color_magenta;
94 colors << Color_olive;
95 colors << Color_orange;
97 colors << Color_purple;
100 colors << Color_violet;
101 colors << Color_yellow;
108 bool ColorSorter(ColorCode lhs, ColorCode rhs)
110 return support::compare_no_case(lcolor.getGUIName(lhs), lcolor.getGUIName(rhs)) < 0;
116 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
120 // fill the box type choice
122 gui_names_ = boxGuiNames();
123 for (int i = 0; i != ids_.size(); ++i)
124 typeCO->addItem(gui_names_[i], ids_[i]);
126 // add the special units to the height choice
127 // width needs different handling
128 ids_spec_ = boxGuiSpecialLengthIds();
129 gui_names_spec_ = boxGuiSpecialLengthNames();
130 for (int i = 0; i != ids_spec_.size(); ++i)
131 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
133 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
134 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
135 this, SIGNAL(changed()));
136 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
137 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
138 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
139 this, SIGNAL(changed()));
140 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
141 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
142 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
143 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
144 this, SIGNAL(changed()));
145 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
146 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
147 this, SIGNAL(changed()));
148 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
149 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
150 this, SIGNAL(changed()));
151 connect(backgroundColorCO, SIGNAL(currentIndexChanged(int)),
152 this, SIGNAL(changed()));
154 heightED->setValidator(unsignedLengthValidator(heightED));
155 widthED->setValidator(unsignedLengthValidator(widthED));
156 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
157 separationED->setValidator(unsignedLengthValidator(separationED));
158 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
160 // initialize the length validator
161 addCheckedWidget(widthED, widthCB);
162 addCheckedWidget(heightED, heightCB);
163 addCheckedWidget(thicknessED, thicknessLA);
164 addCheckedWidget(separationED, separationLA);
165 addCheckedWidget(shadowsizeED, shadowsizeLA);
167 // the background can be uncolored while the frame cannot
168 color_codes_ = colors();
169 qSort(color_codes_.begin(), color_codes_.end(), ColorSorter);
170 fillComboColor(backgroundColorCO, true);
171 fillComboColor(frameColorCO, false);
177 void GuiBox::fillComboColor(QComboBox * combo, bool const is_none)
180 QPixmap coloritem(32, 32);
182 // frameColorCO cannot be uncolored
184 combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
185 toqstr(lcolor.getLaTeXName(Color_none)));
186 QList<ColorCode>::const_iterator cit = color_codes_.begin();
187 for (; cit != color_codes_.end(); ++cit) {
188 QString const latexname = toqstr(lcolor.getLaTeXName(*cit));
189 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
190 color = QColor(guiApp->colorCache().get(*cit, false));
191 coloritem.fill(color);
192 combo->addItem(QIcon(coloritem), guiname, latexname);
197 void GuiBox::on_innerBoxCO_activated(int index)
199 QString itype = innerBoxCO->itemData(index).toString();
200 // handle parbox and minipage the same way
201 bool const ibox = (itype != "none" && itype != "makebox");
202 if (heightCB->isChecked() && !ibox)
203 heightCB->setChecked(false);
204 widthCB->setChecked(!widthED->text().isEmpty());
210 void GuiBox::on_typeCO_activated(int index)
213 typeCO->itemData(index).toString();
214 bool const frameless = (type == "Frameless");
216 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
217 setInnerType(frameless, itype);
218 // refresh itype because it might have been changed in setInnerType
220 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
221 // handle parbox and minipage the same way
222 bool const ibox = (itype != "none" && itype != "makebox");
223 if (frameless && itype != "makebox") {
224 if (heightCB->isChecked() && !ibox)
225 heightCB->setChecked(false);
228 if (type != "Boxed") {
229 if (type != "Frameless")
230 widthCB->setChecked(itype != "none");
231 pagebreakCB->setChecked(false);
233 // assure that the frame color is black for frameless boxes to
234 // provide the color "none"
235 int const b = frameColorCO->findData("black");
236 if (frameless && frameColorCO->currentIndex() != b)
237 frameColorCO->setCurrentIndex(b);
242 void GuiBox::on_frameColorCO_currentIndexChanged(int index)
244 // if there is a non-black frame color the background cannot be uncolored
245 // therefore remove the entry "none" in this case
246 int const n = backgroundColorCO->findData("none");
247 if (index != frameColorCO->findData("black")) {
249 if (backgroundColorCO->currentIndex() == n)
250 backgroundColorCO->setCurrentIndex(
251 backgroundColorCO->findData("white"));
252 backgroundColorCO->removeItem(n);
256 backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
257 toqstr(lcolor.getLaTeXName(Color_none)));
263 void GuiBox::initDialog()
265 setInnerType(true, toqstr("minipage"));
266 widthED->setText("100");
267 widthCB->setChecked(true);
268 widthCB->setEnabled(false);
269 widthUnitsLC->setCurrentItem(Length::PCW);
270 heightED->setText("1");
271 heightUnitsLC->setCurrentItem("totalheight");
272 // LaTeX's default for \fboxrule is 0.4 pt
273 thicknessED->setText("0.4");
274 thicknessUnitsLC->setCurrentItem(Length::PT);
275 // LaTeX's default for \fboxsep is 3 pt
276 separationED->setText("3");
277 separationUnitsLC->setCurrentItem(Length::PT);
278 // LaTeX's default for \shadowsize is 4 pt
279 shadowsizeED->setText("4");
280 shadowsizeUnitsLC->setCurrentItem(Length::PT);
284 void GuiBox::on_widthCB_stateChanged(int)
290 void GuiBox::on_heightCB_stateChanged(int /*state*/)
296 void GuiBox::on_pagebreakCB_stateChanged()
298 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
300 widthCB->setChecked(!pbreak);
302 on_typeCO_activated(typeCO->currentIndex());
310 void GuiBox::paramsToDialog(Inset const * inset)
312 InsetBox const * box = static_cast<InsetBox const *>(inset);
313 InsetBoxParams const & params = box->params();
314 QString type = toqstr(params.type);
315 if (type == "Framed") {
316 pagebreakCB->setChecked(true);
319 pagebreakCB->setChecked(false);
322 typeCO->setCurrentIndex(typeCO->findData(type));
325 QString inner_type = "minipage";
326 if (!params.inner_box)
328 if (params.use_parbox)
329 inner_type = "parbox";
330 if (params.use_makebox)
331 inner_type = "makebox";
332 bool const frameless = (params.type == "Frameless");
333 setInnerType(frameless, inner_type);
336 valignCO->setCurrentIndex(string("tcb").find(c, 0));
337 c = params.inner_pos;
338 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
340 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
342 bool ibox = (params.inner_box && !params.use_makebox);
343 valignCO->setEnabled(ibox);
344 ialignCO->setEnabled(ibox);
347 // halign is only allowed if a width is used
348 halignCO->setEnabled(widthCB->isChecked());
349 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
350 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
351 && halignCO->count() < 4)
352 halignCO->addItem(toqstr("Stretch"));
353 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
354 halignCO->removeItem(3);
355 // pagebreak is only allowed for Boxed without inner box
356 pagebreakCB->setEnabled(!ibox && type == "Boxed");
358 Length::UNIT const default_unit = Length::defaultUnit();
360 // the width can only be selected for makebox or framebox
361 widthCB->setEnabled(inner_type == "makebox"
363 && !ibox && !pagebreakCB->isChecked()));
364 if (params.width.empty()) {
365 widthCB->setChecked(false);
366 lengthToWidgets(widthED, widthUnitsLC,
367 params.width, default_unit);
369 widthCB->setChecked(true);
370 lengthToWidgets(widthED, widthUnitsLC,
371 params.width, default_unit);
372 QString const special = toqstr(params.special);
373 if (!special.isEmpty() && special != "none")
374 widthUnitsLC->setCurrentItem(special);
377 widthED->setEnabled(widthCB->isChecked());
378 widthUnitsLC->setEnabled(widthCB->isChecked());
380 lengthToWidgets(heightED, heightUnitsLC,
381 (params.height).asString(), default_unit);
383 QString const height_special = toqstr(params.height_special);
384 if (!height_special.isEmpty() && height_special != "none")
385 heightUnitsLC->setCurrentItem(height_special);
386 // set no optional height if the value is the default "1\height"
387 // (special units like \height are handled as "in",
388 // FIXME: this is a very bad UI, this check box should be disabled in
389 // this case, not forced to 'unchecked' state.
390 if (height_special == "totalheight" && params.height == Length("1in"))
391 heightCB->setCheckState(Qt::Unchecked);
393 heightCB->setCheckState(Qt::Checked);
395 heightCB->setEnabled(ibox);
397 // enable line thickness only for the rectangular frame types and drop shadow
398 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
399 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
400 lengthToWidgets(thicknessED, thicknessUnitsLC,
401 (params.thickness).asString(), default_unit);
402 // enable line separation for the allowed frame types
403 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
404 || type == "Doublebox" || type == "Shadowbox");
405 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
406 || type == "Doublebox" || type == "Shadowbox");
407 lengthToWidgets(separationED, separationUnitsLC,
408 (params.separation).asString(), default_unit);
409 // enable shadow size for drop shadow
410 shadowsizeED->setEnabled(type == "Shadowbox");
411 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
412 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
413 (params.shadowsize).asString(), default_unit);
415 frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
416 backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
420 docstring GuiBox::dialogToParams() const
422 bool const pagebreak =
423 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
428 box_type = fromqstr(typeCO->itemData(
429 typeCO->currentIndex()).toString());
431 InsetBoxParams params(box_type);
433 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
435 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
437 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
439 params.pos = "tcb"[valignCO->currentIndex()];
440 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
441 params.hor_pos = "lcrs"[halignCO->currentIndex()];
444 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
445 QString value = widthED->text();
447 if (widthED->isEnabled()) {
448 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
449 params.special = fromqstr(unit);
450 // Note: the unit is simply ignored in this case
451 params.width = Length(value.toDouble(), Length::IN);
453 params.special = "none";
454 // we must specify a valid length in this case
456 widthED->setText("0");
457 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
460 params.special = "none";
461 params.width = Length();
464 // the height parameter is omitted if the value
465 // is "1in" and "Total Height" is used as unit.
466 // 1in + "Total Height" means "1\height" which is the LaTeX default
467 // if no height is given
468 if (heightCB->checkState() == Qt::Unchecked) {
469 params.height = Length("1in");
470 params.height_special = "totalheight";
472 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
473 value = heightED->text();
474 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
475 params.height_special = fromqstr(unit);
476 // Note: the unit is simply ignored in this case
477 params.height = Length(value.toDouble(), Length::IN);
479 params.height_special = "none";
481 Length(widgetsToLength(heightED, heightUnitsLC));
485 // handle the line thickness, line separation and shadow size
486 if (thicknessED->isEnabled())
487 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
489 params.thickness = Length("0.4pt");
490 if (separationED->isEnabled())
491 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
493 params.separation = Length("3pt");
494 if (separationED->isEnabled())
495 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
497 params.shadowsize = Length("4pt");
498 if (frameColorCO->isEnabled())
500 fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
502 params.framecolor = "black";
503 if (backgroundColorCO->isEnabled())
504 params.backgroundcolor =
505 fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
507 params.backgroundcolor = "none";
509 return from_ascii(InsetBox::params2string(params));
513 bool GuiBox::checkWidgets(bool readonly) const
515 typeCO->setEnabled(!readonly);
518 pagebreakCB->setEnabled(false);
519 innerBoxCO->setEnabled(false);
520 valignCO->setEnabled(false);
521 ialignCO->setEnabled(false);
522 halignCO->setEnabled(false);
523 widthCB->setEnabled(false);
524 widthED->setEnabled(false);
525 widthUnitsLC->setEnabled(false);
526 heightED->setEnabled(false);
527 heightUnitsLC->setEnabled(false);
528 heightCB->setEnabled(false);
529 thicknessED->setEnabled(false);
530 thicknessUnitsLC->setEnabled(false);
531 separationED->setEnabled(false);
532 separationUnitsLC->setEnabled(false);
533 shadowsizeED->setEnabled(false);
534 shadowsizeUnitsLC->setEnabled(false);
536 QString const outer =
537 typeCO->itemData(typeCO->currentIndex()).toString();
538 QString const itype =
539 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
540 bool const ibox = (itype != "none" && itype != "makebox");
541 valignCO->setEnabled(ibox);
542 ialignCO->setEnabled(ibox);
543 if (heightCB->isChecked() && !ibox)
544 heightCB->setChecked(false);
545 heightCB->setEnabled(ibox);
546 // the width can only be selected for makebox or framebox
547 widthCB->setEnabled(itype == "makebox"
548 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
549 // except for Frameless and Boxed, the width cannot be specified if
550 // there is no inner box
551 bool const width_enabled =
552 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
553 // enable if width_enabled
554 widthED->setEnabled(width_enabled);
555 widthUnitsLC->setEnabled(width_enabled);
556 if (!widthCB->isChecked() && widthCB->isEnabled()) {
557 widthED->setEnabled(false);
558 widthUnitsLC->setEnabled(false);
560 // halign is only allowed if a width is used
561 halignCO->setEnabled(widthCB->isChecked());
562 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
563 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
564 && halignCO->count() < 4)
565 halignCO->addItem(toqstr("Stretch"));
566 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
567 halignCO->removeItem(3);
568 // pagebreak is only allowed for Boxed without inner box
569 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
571 heightED->setEnabled(itype != "none" && heightCB->isChecked());
572 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
573 heightCB->setEnabled(ibox);
575 // enable line thickness for the rectangular frame types and drop shadow
576 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
577 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
578 // set default values if empty
579 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
580 thicknessED->setText("0.4");
581 thicknessUnitsLC->setCurrentItem(Length::PT);
583 // enable line separation for the allowed frame types
584 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
585 || outer == "Doublebox" || outer == "Shadowbox");
586 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
587 || outer == "Doublebox" || outer == "Shadowbox");
588 // set default values if empty
589 if (separationED->text().isEmpty() && separationED->isEnabled()) {
590 separationED->setText("3");
591 separationUnitsLC->setCurrentItem(Length::PT);
593 // enable shadow size for drop shadow
594 shadowsizeED->setEnabled(outer == "Shadowbox");
595 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
596 // set default values if empty
597 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
598 shadowsizeED->setText("4");
599 shadowsizeUnitsLC->setCurrentItem(Length::PT);
601 // \fboxcolor and \colorbox cannot be used for fancybox boxes
602 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
603 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
606 return InsetParamsWidget::checkWidgets();
610 void GuiBox::setSpecial(bool ibox)
612 QString const last_item =
613 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
615 // check if the widget contains the special units
616 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
617 // insert 'em if needed...
618 if (!ibox && !has_special) {
619 for (int i = 1; i < ids_spec_.size(); ++i)
620 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
621 // ... or remove 'em if needed
622 } else if (ibox && has_special) {
623 for (int i = 1; i < ids_spec_.size(); ++i) {
624 int n = widthUnitsLC->findData(ids_spec_[i]);
626 widthUnitsLC->removeItem(n);
629 // restore selected text, if possible
630 widthUnitsLC->setCurrentItem(last_item);
634 void GuiBox::setInnerType(bool frameless, QString const & type)
636 // with "frameless" boxes, inner box is mandatory
637 // (i.e. is the actual box)
638 // we have to remove "none" then and adjust the combo
641 innerBoxCO->addItem(qt_("None"), toqstr("none"));
643 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
644 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
645 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
646 int i = (innerBoxCO->findData(type) != -1)
647 ? innerBoxCO->findData(type) : 0;
648 innerBoxCO->setCurrentIndex(i);
651 } // namespace frontend
655 #include "moc_GuiBox.cpp"