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;
106 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
110 // fill the box type choice
112 gui_names_ = boxGuiNames();
113 for (int i = 0; i != ids_.size(); ++i)
114 typeCO->addItem(gui_names_[i], ids_[i]);
116 // add the special units to the height choice
117 // width needs different handling
118 ids_spec_ = boxGuiSpecialLengthIds();
119 gui_names_spec_ = boxGuiSpecialLengthNames();
120 for (int i = 0; i != ids_spec_.size(); ++i)
121 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
123 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
124 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
125 this, SIGNAL(changed()));
126 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
127 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
128 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
129 this, SIGNAL(changed()));
130 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
131 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
132 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
133 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
134 this, SIGNAL(changed()));
135 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
136 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
137 this, SIGNAL(changed()));
138 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
139 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
140 this, SIGNAL(changed()));
141 connect(backgroundColorCO, SIGNAL(currentIndexChanged(int)),
142 this, SIGNAL(changed()));
144 heightED->setValidator(unsignedLengthValidator(heightED));
145 widthED->setValidator(unsignedLengthValidator(widthED));
146 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
147 separationED->setValidator(unsignedLengthValidator(separationED));
148 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
150 // initialize the length validator
151 addCheckedWidget(widthED, widthCB);
152 addCheckedWidget(heightED, heightCB);
153 addCheckedWidget(thicknessED, thicknessLA);
154 addCheckedWidget(separationED, separationLA);
155 addCheckedWidget(shadowsizeED, shadowsizeLA);
157 // the background can be uncolored while the frame cannot
158 color_codes_ = colors();
159 qSort(color_codes_.begin(), color_codes_.end(), ColorSorter);
160 fillComboColor(backgroundColorCO, true);
161 fillComboColor(frameColorCO, false);
167 void GuiBox::fillComboColor(QComboBox * combo, bool const is_none)
170 QPixmap coloritem(32, 32);
172 // frameColorCO cannot be uncolored
174 combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
175 toqstr(lcolor.getLaTeXName(Color_none)));
176 QList<ColorCode>::const_iterator cit = color_codes_.begin();
177 for (; cit != color_codes_.end(); ++cit) {
178 QString const latexname = toqstr(lcolor.getLaTeXName(*cit));
179 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
180 color = QColor(guiApp->colorCache().get(*cit, false));
181 coloritem.fill(color);
182 combo->addItem(QIcon(coloritem), guiname, latexname);
187 void GuiBox::on_innerBoxCO_activated(int index)
189 QString itype = innerBoxCO->itemData(index).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 int const n = backgroundColorCO->findData("none");
237 if (index != frameColorCO->findData("black")) {
239 if (backgroundColorCO->currentIndex() == n)
240 backgroundColorCO->setCurrentIndex(
241 backgroundColorCO->findData("white"));
242 backgroundColorCO->removeItem(n);
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 without inner box and if a width is used and if
338 // pagebreak is not used
339 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
340 && ((!ibox && type == "Boxed") || inner_type == "makebox"));
341 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
342 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
343 && halignCO->count() < 4)
344 halignCO->addItem(toqstr("Stretch"));
345 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
346 halignCO->removeItem(3);
347 // pagebreak is only allowed for Boxed without inner box
348 pagebreakCB->setEnabled(!ibox && type == "Boxed");
350 Length::UNIT const default_unit = Length::defaultUnit();
352 // the width can only be selected for makebox or framebox
353 widthCB->setEnabled(inner_type == "makebox"
355 && !ibox && !pagebreakCB->isChecked()));
356 if (params.width.empty()) {
357 widthCB->setChecked(false);
358 lengthToWidgets(widthED, widthUnitsLC,
359 params.width, default_unit);
361 widthCB->setChecked(true);
362 lengthToWidgets(widthED, widthUnitsLC,
363 params.width, default_unit);
364 QString const special = toqstr(params.special);
365 if (!special.isEmpty() && special != "none")
366 widthUnitsLC->setCurrentItem(special);
369 widthED->setEnabled(widthCB->isChecked());
370 widthUnitsLC->setEnabled(widthCB->isChecked());
372 lengthToWidgets(heightED, heightUnitsLC,
373 (params.height).asString(), default_unit);
375 QString const height_special = toqstr(params.height_special);
376 if (!height_special.isEmpty() && height_special != "none")
377 heightUnitsLC->setCurrentItem(height_special);
378 // set no optional height if the value is the default "1\height"
379 // (special units like \height are handled as "in",
380 // FIXME: this is a very bad UI, this check box should be disabled in
381 // this case, not forced to 'unchecked' state.
382 if (height_special == "totalheight" && params.height == Length("1in"))
383 heightCB->setCheckState(Qt::Unchecked);
385 heightCB->setCheckState(Qt::Checked);
387 heightCB->setEnabled(ibox);
389 // enable line thickness only for the rectangular frame types and drop shadow
390 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
391 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
392 lengthToWidgets(thicknessED, thicknessUnitsLC,
393 (params.thickness).asString(), default_unit);
394 // enable line separation for the allowed frame types
395 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
396 || type == "Doublebox" || type == "Shadowbox");
397 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
398 || type == "Doublebox" || type == "Shadowbox");
399 lengthToWidgets(separationED, separationUnitsLC,
400 (params.separation).asString(), default_unit);
401 // enable shadow size for drop shadow
402 shadowsizeED->setEnabled(type == "Shadowbox");
403 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
404 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
405 (params.shadowsize).asString(), default_unit);
407 frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
408 backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
412 docstring GuiBox::dialogToParams() const
414 bool const pagebreak =
415 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
420 box_type = fromqstr(typeCO->itemData(
421 typeCO->currentIndex()).toString());
423 InsetBoxParams params(box_type);
425 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
427 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
429 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
431 params.pos = "tcb"[valignCO->currentIndex()];
432 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
433 params.hor_pos = "lcrs"[halignCO->currentIndex()];
436 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
437 QString value = widthED->text();
439 if (widthED->isEnabled()) {
440 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
441 params.special = fromqstr(unit);
442 // Note: the unit is simply ignored in this case
443 params.width = Length(value.toDouble(), Length::IN);
445 params.special = "none";
446 // we must specify a valid length in this case
448 widthED->setText("0");
449 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
452 params.special = "none";
453 params.width = Length();
456 // the height parameter is omitted if the value
457 // is "1in" and "Total Height" is used as unit.
458 // 1in + "Total Height" means "1\height" which is the LaTeX default
459 // if no height is given
460 if (heightCB->checkState() == Qt::Unchecked) {
461 params.height = Length("1in");
462 params.height_special = "totalheight";
464 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
465 value = heightED->text();
466 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
467 params.height_special = fromqstr(unit);
468 // Note: the unit is simply ignored in this case
469 params.height = Length(value.toDouble(), Length::IN);
471 params.height_special = "none";
473 Length(widgetsToLength(heightED, heightUnitsLC));
477 // handle the line thickness, line separation and shadow size
478 if (thicknessED->isEnabled())
479 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
481 params.thickness = Length("0.4pt");
482 if (separationED->isEnabled())
483 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
485 params.separation = Length("3pt");
486 if (separationED->isEnabled())
487 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
489 params.shadowsize = Length("4pt");
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 without inner box and if a width is used and if
553 // pagebreak is not used
554 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
555 && ((!ibox && outer == "Boxed") || itype == "makebox"));
556 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
557 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
558 && halignCO->count() < 4)
559 halignCO->addItem(toqstr("Stretch"));
560 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
561 halignCO->removeItem(3);
562 // pagebreak is only allowed for Boxed without inner box
563 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
565 heightED->setEnabled(itype != "none" && heightCB->isChecked());
566 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
567 heightCB->setEnabled(ibox);
569 // enable line thickness for the rectangular frame types and drop shadow
570 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
571 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
572 // set default values if empty
573 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
574 thicknessED->setText("0.4");
575 thicknessUnitsLC->setCurrentItem(Length::PT);
577 // enable line separation for the allowed frame types
578 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
579 || outer == "Doublebox" || outer == "Shadowbox");
580 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
581 || outer == "Doublebox" || outer == "Shadowbox");
582 // set default values if empty
583 if (separationED->text().isEmpty() && separationED->isEnabled()) {
584 separationED->setText("3");
585 separationUnitsLC->setCurrentItem(Length::PT);
587 // enable shadow size for drop shadow
588 shadowsizeED->setEnabled(outer == "Shadowbox");
589 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
590 // set default values if empty
591 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
592 shadowsizeED->setText("4");
593 shadowsizeUnitsLC->setCurrentItem(Length::PT);
595 // \fboxcolor and \colorbox cannot be used for fancybox boxes
596 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
597 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
600 return InsetParamsWidget::checkWidgets();
604 void GuiBox::setSpecial(bool ibox)
606 QString const last_item =
607 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
609 // check if the widget contains the special units
610 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
611 // insert 'em if needed...
612 if (!ibox && !has_special) {
613 for (int i = 1; i < ids_spec_.size(); ++i)
614 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
615 // ... or remove 'em if needed
616 } else if (ibox && has_special) {
617 for (int i = 1; i < ids_spec_.size(); ++i) {
618 int n = widthUnitsLC->findData(ids_spec_[i]);
620 widthUnitsLC->removeItem(n);
623 // restore selected text, if possible
624 widthUnitsLC->setCurrentItem(last_item);
628 void GuiBox::setInnerType(bool frameless, QString const & type)
630 // with "frameless" boxes, inner box is mandatory
631 // (i.e. is the actual box)
632 // we have to remove "none" then and adjust the combo
635 innerBoxCO->addItem(qt_("None"), toqstr("none"));
637 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
638 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
639 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
640 int i = (innerBoxCO->findData(type) != -1)
641 ? innerBoxCO->findData(type) : 0;
642 innerBoxCO->setCurrentIndex(i);
645 } // namespace frontend
649 #include "moc_GuiBox.cpp"