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;
110 bool operator()(ColorCode lhs, ColorCode rhs) const {
112 support::compare_no_case(lcolor.getGUIName(lhs), lcolor.getGUIName(rhs)) < 0;
119 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
123 // fill the box type choice
125 gui_names_ = boxGuiNames();
126 for (int i = 0; i != ids_.size(); ++i)
127 typeCO->addItem(gui_names_[i], ids_[i]);
129 // add the special units to the height choice
130 // width needs different handling
131 ids_spec_ = boxGuiSpecialLengthIds();
132 gui_names_spec_ = boxGuiSpecialLengthNames();
133 for (int i = 0; i != ids_spec_.size(); ++i)
134 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
136 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
137 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
138 this, SIGNAL(changed()));
139 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
140 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
141 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
142 this, SIGNAL(changed()));
143 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
144 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
145 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
146 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
147 this, SIGNAL(changed()));
148 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
149 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
150 this, SIGNAL(changed()));
151 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
152 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
153 this, SIGNAL(changed()));
154 connect(frameColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
155 connect(backgroundColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
157 heightED->setValidator(unsignedLengthValidator(heightED));
158 widthED->setValidator(unsignedLengthValidator(widthED));
159 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
160 separationED->setValidator(unsignedLengthValidator(separationED));
161 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
163 // initialize the length validator
164 addCheckedWidget(widthED, widthCB);
165 addCheckedWidget(heightED, heightCB);
166 addCheckedWidget(thicknessED, thicknessLA);
167 addCheckedWidget(separationED, separationLA);
168 addCheckedWidget(shadowsizeED, shadowsizeLA);
170 // the background can be uncolored while the frame cannot
171 color_codes_ = colors();
172 sort(color_codes_.begin(), color_codes_.end(), ColorSorter());
173 fillComboColor(backgroundColorCO, true);
174 fillComboColor(frameColorCO, false);
180 void GuiBox::fillComboColor(QComboBox * combo, bool const is_none)
183 QPixmap coloritem(32, 32);
185 // frameColorCO cannot be uncolored
187 combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
188 toqstr(lcolor.getLaTeXName(Color_none)));
189 QList<ColorCode>::const_iterator cit = color_codes_.begin();
190 for (; cit != color_codes_.end(); ++cit) {
191 QString const latexname = toqstr(lcolor.getLaTeXName(*cit));
192 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
193 color = QColor(guiApp->colorCache().get(*cit, false));
194 coloritem.fill(color);
195 combo->addItem(QIcon(coloritem), guiname, latexname);
200 void GuiBox::on_innerBoxCO_activated(int index)
202 QString itype = innerBoxCO->itemData(index).toString();
203 // handle parbox and minipage the same way
204 bool const ibox = (itype != "none" && itype != "makebox");
205 if (heightCB->isChecked() && !ibox)
206 heightCB->setChecked(false);
207 widthCB->setChecked(!widthED->text().isEmpty());
213 void GuiBox::on_typeCO_activated(int index)
216 typeCO->itemData(index).toString();
217 bool const frameless = (type == "Frameless");
219 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
220 setInnerType(frameless, itype);
221 // refresh itype because it might have been changed in setInnerType
223 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
224 // handle parbox and minipage the same way
225 bool const ibox = (itype != "none" && itype != "makebox");
226 if (frameless && itype != "makebox") {
227 if (heightCB->isChecked() && !ibox)
228 heightCB->setChecked(false);
231 if (type != "Boxed") {
232 if (type != "Frameless")
233 widthCB->setChecked(itype != "none");
234 pagebreakCB->setChecked(false);
236 // assure that the frame color is black for frameless boxes to
237 // provide the color "none"
238 int const b = frameColorCO->findData("black");
239 if (frameless && frameColorCO->currentIndex() != b)
240 frameColorCO->setCurrentIndex(b);
245 void GuiBox::on_frameColorCO_currentIndexChanged(int index)
247 // if there is a non-black frame color the background cannot be uncolored
248 // therefore remove the entry "none" in this case
249 int const n = backgroundColorCO->findData("none");
250 if (index != frameColorCO->findData("black")) {
252 if (backgroundColorCO->currentIndex() == n)
253 backgroundColorCO->setCurrentIndex(
254 backgroundColorCO->findData("white"));
255 backgroundColorCO->removeItem(n);
259 backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
260 toqstr(lcolor.getLaTeXName(Color_none)));
266 void GuiBox::initDialog()
268 setInnerType(true, toqstr("minipage"));
269 widthED->setText("100");
270 widthCB->setChecked(true);
271 widthCB->setEnabled(false);
272 widthUnitsLC->setCurrentItem(Length::PCW);
273 heightED->setText("1");
274 heightUnitsLC->setCurrentItem("totalheight");
275 // LaTeX's default for \fboxrule is 0.4 pt
276 thicknessED->setText("0.4");
277 thicknessUnitsLC->setCurrentItem(Length::PT);
278 // LaTeX's default for \fboxsep is 3 pt
279 separationED->setText("3");
280 separationUnitsLC->setCurrentItem(Length::PT);
281 // LaTeX's default for \shadowsize is 4 pt
282 shadowsizeED->setText("4");
283 shadowsizeUnitsLC->setCurrentItem(Length::PT);
287 void GuiBox::on_widthCB_stateChanged(int)
293 void GuiBox::on_heightCB_stateChanged(int /*state*/)
299 void GuiBox::on_pagebreakCB_stateChanged()
301 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
303 widthCB->setChecked(!pbreak);
305 on_typeCO_activated(typeCO->currentIndex());
313 void GuiBox::paramsToDialog(Inset const * inset)
315 InsetBox const * box = static_cast<InsetBox const *>(inset);
316 InsetBoxParams const & params = box->params();
317 QString type = toqstr(params.type);
318 if (type == "Framed") {
319 pagebreakCB->setChecked(true);
322 pagebreakCB->setChecked(false);
325 typeCO->setCurrentIndex(typeCO->findData(type));
328 QString inner_type = "minipage";
329 if (!params.inner_box)
331 if (params.use_parbox)
332 inner_type = "parbox";
333 if (params.use_makebox)
334 inner_type = "makebox";
335 bool const frameless = (params.type == "Frameless");
336 setInnerType(frameless, inner_type);
339 valignCO->setCurrentIndex(string("tcb").find(c, 0));
340 c = params.inner_pos;
341 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
343 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
345 bool ibox = (params.inner_box && !params.use_makebox);
346 valignCO->setEnabled(ibox);
347 ialignCO->setEnabled(ibox);
350 // halign is only allowed if a width is used
351 halignCO->setEnabled(widthCB->isChecked());
352 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
353 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
354 && halignCO->count() < 4)
355 halignCO->addItem(toqstr("Stretch"));
356 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
357 halignCO->removeItem(3);
358 // pagebreak is only allowed for Boxed without inner box
359 pagebreakCB->setEnabled(!ibox && type == "Boxed");
361 Length::UNIT const default_unit = Length::defaultUnit();
363 // the width can only be selected for makebox or framebox
364 widthCB->setEnabled(inner_type == "makebox"
366 && !ibox && !pagebreakCB->isChecked()));
367 if (params.width.empty()) {
368 widthCB->setChecked(false);
369 lengthToWidgets(widthED, widthUnitsLC,
370 params.width, default_unit);
372 widthCB->setChecked(true);
373 lengthToWidgets(widthED, widthUnitsLC,
374 params.width, default_unit);
375 QString const special = toqstr(params.special);
376 if (!special.isEmpty() && special != "none")
377 widthUnitsLC->setCurrentItem(special);
380 widthED->setEnabled(widthCB->isChecked());
381 widthUnitsLC->setEnabled(widthCB->isChecked());
383 lengthToWidgets(heightED, heightUnitsLC,
384 (params.height).asString(), default_unit);
386 QString const height_special = toqstr(params.height_special);
387 if (!height_special.isEmpty() && height_special != "none")
388 heightUnitsLC->setCurrentItem(height_special);
389 // set no optional height if the value is the default "1\height"
390 // (special units like \height are handled as "in",
391 // FIXME: this is a very bad UI, this check box should be disabled in
392 // this case, not forced to 'unchecked' state.
393 if (height_special == "totalheight" && params.height == Length("1in"))
394 heightCB->setCheckState(Qt::Unchecked);
396 heightCB->setCheckState(Qt::Checked);
398 heightCB->setEnabled(ibox);
400 // enable line thickness only for the rectangular frame types and drop shadow
401 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
402 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
403 lengthToWidgets(thicknessED, thicknessUnitsLC,
404 (params.thickness).asString(), default_unit);
405 // enable line separation for the allowed frame types
406 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
407 || type == "Doublebox" || type == "Shadowbox");
408 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
409 || type == "Doublebox" || type == "Shadowbox");
410 lengthToWidgets(separationED, separationUnitsLC,
411 (params.separation).asString(), default_unit);
412 // enable shadow size for drop shadow
413 shadowsizeED->setEnabled(type == "Shadowbox");
414 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
415 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
416 (params.shadowsize).asString(), default_unit);
418 frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
419 backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
423 docstring GuiBox::dialogToParams() const
425 bool const pagebreak =
426 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
431 box_type = fromqstr(typeCO->itemData(
432 typeCO->currentIndex()).toString());
434 InsetBoxParams params(box_type);
436 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
438 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
440 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
442 params.pos = "tcb"[valignCO->currentIndex()];
443 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
444 params.hor_pos = "lcrs"[halignCO->currentIndex()];
447 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
448 QString value = widthED->text();
450 if (widthED->isEnabled()) {
451 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
452 params.special = fromqstr(unit);
453 // Note: the unit is simply ignored in this case
454 params.width = Length(value.toDouble(), Length::IN);
456 params.special = "none";
457 // we must specify a valid length in this case
459 widthED->setText("0");
460 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
463 params.special = "none";
464 params.width = Length();
467 // the height parameter is omitted if the value
468 // is "1in" and "Total Height" is used as unit.
469 // 1in + "Total Height" means "1\height" which is the LaTeX default
470 // if no height is given
471 if (heightCB->checkState() == Qt::Unchecked) {
472 params.height = Length("1in");
473 params.height_special = "totalheight";
475 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
476 value = heightED->text();
477 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
478 params.height_special = fromqstr(unit);
479 // Note: the unit is simply ignored in this case
480 params.height = Length(value.toDouble(), Length::IN);
482 params.height_special = "none";
484 Length(widgetsToLength(heightED, heightUnitsLC));
488 // handle the line thickness, line separation and shadow size
489 if (thicknessED->isEnabled())
490 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
492 params.thickness = Length("0.4pt");
493 if (separationED->isEnabled())
494 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
496 params.separation = Length("3pt");
497 if (separationED->isEnabled())
498 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
500 params.shadowsize = Length("4pt");
501 if (frameColorCO->isEnabled())
503 fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
505 params.framecolor = "black";
506 if (backgroundColorCO->isEnabled())
507 params.backgroundcolor =
508 fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
510 params.backgroundcolor = "none";
512 return from_ascii(InsetBox::params2string(params));
516 bool GuiBox::checkWidgets(bool readonly) const
518 typeCO->setEnabled(!readonly);
521 pagebreakCB->setEnabled(false);
522 innerBoxCO->setEnabled(false);
523 valignCO->setEnabled(false);
524 ialignCO->setEnabled(false);
525 halignCO->setEnabled(false);
526 widthCB->setEnabled(false);
527 widthED->setEnabled(false);
528 widthUnitsLC->setEnabled(false);
529 heightED->setEnabled(false);
530 heightUnitsLC->setEnabled(false);
531 heightCB->setEnabled(false);
532 thicknessED->setEnabled(false);
533 thicknessUnitsLC->setEnabled(false);
534 separationED->setEnabled(false);
535 separationUnitsLC->setEnabled(false);
536 shadowsizeED->setEnabled(false);
537 shadowsizeUnitsLC->setEnabled(false);
539 QString const outer =
540 typeCO->itemData(typeCO->currentIndex()).toString();
541 QString const itype =
542 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
543 bool const ibox = (itype != "none" && itype != "makebox");
544 valignCO->setEnabled(ibox);
545 ialignCO->setEnabled(ibox);
546 if (heightCB->isChecked() && !ibox)
547 heightCB->setChecked(false);
548 heightCB->setEnabled(ibox);
549 // the width can only be selected for makebox or framebox
550 widthCB->setEnabled(itype == "makebox"
551 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
552 // except for Frameless and Boxed, the width cannot be specified if
553 // there is no inner box
554 bool const width_enabled =
555 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
556 // enable if width_enabled
557 widthED->setEnabled(width_enabled);
558 widthUnitsLC->setEnabled(width_enabled);
559 if (!widthCB->isChecked() && widthCB->isEnabled()) {
560 widthED->setEnabled(false);
561 widthUnitsLC->setEnabled(false);
563 // halign is only allowed if a width is used
564 halignCO->setEnabled(widthCB->isChecked());
565 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
566 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
567 && halignCO->count() < 4)
568 halignCO->addItem(toqstr("Stretch"));
569 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
570 halignCO->removeItem(3);
571 // pagebreak is only allowed for Boxed without inner box
572 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
574 heightED->setEnabled(itype != "none" && heightCB->isChecked());
575 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
576 heightCB->setEnabled(ibox);
578 // enable line thickness for the rectangular frame types and drop shadow
579 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
580 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
581 // set default values if empty
582 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
583 thicknessED->setText("0.4");
584 thicknessUnitsLC->setCurrentItem(Length::PT);
586 // enable line separation for the allowed frame types
587 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
588 || outer == "Doublebox" || outer == "Shadowbox");
589 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
590 || outer == "Doublebox" || outer == "Shadowbox");
591 // set default values if empty
592 if (separationED->text().isEmpty() && separationED->isEnabled()) {
593 separationED->setText("3");
594 separationUnitsLC->setCurrentItem(Length::PT);
596 // enable shadow size for drop shadow
597 shadowsizeED->setEnabled(outer == "Shadowbox");
598 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
599 // set default values if empty
600 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
601 shadowsizeED->setText("4");
602 shadowsizeUnitsLC->setCurrentItem(Length::PT);
604 // \fboxcolor and \colorbox cannot be used for fancybox boxes
605 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
606 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
609 return InsetParamsWidget::checkWidgets();
613 void GuiBox::setSpecial(bool ibox)
615 QString const last_item =
616 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
618 // check if the widget contains the special units
619 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
620 // insert 'em if needed...
621 if (!ibox && !has_special) {
622 for (int i = 1; i < ids_spec_.size(); ++i)
623 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
624 // ... or remove 'em if needed
625 } else if (ibox && has_special) {
626 for (int i = 1; i < ids_spec_.size(); ++i) {
627 int n = widthUnitsLC->findData(ids_spec_[i]);
629 widthUnitsLC->removeItem(n);
632 // restore selected text, if possible
633 widthUnitsLC->setCurrentItem(last_item);
637 void GuiBox::setInnerType(bool frameless, QString const & type)
639 // with "frameless" boxes, inner box is mandatory
640 // (i.e. is the actual box)
641 // we have to remove "none" then and adjust the combo
644 innerBoxCO->addItem(qt_("None"), toqstr("none"));
646 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
647 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
648 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
649 int i = (innerBoxCO->findData(type) != -1)
650 ? innerBoxCO->findData(type) : 0;
651 innerBoxCO->setCurrentIndex(i);
654 } // namespace frontend
658 #include "moc_GuiBox.cpp"