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"
22 #include "qt_helpers.h"
23 #include "Validator.h"
25 #include "insets/InsetBox.h"
27 #include "support/gettext.h"
28 #include "support/Length.h"
29 #include "support/lstrings.h"
33 #include <QPushButton>
41 static QStringList boxGuiIds()
44 << "Frameless" << "Boxed"
45 << "ovalbox" << "Ovalbox"
46 << "Shadowbox" << "Shaded"
51 static QStringList boxGuiNames()
54 << qt_("No frame") << qt_("Simple rectangular frame")
55 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
56 << qt_("Drop shadow") << qt_("Shaded background")
57 << qt_("Double rectangular frame");
61 static QStringList boxGuiSpecialLengthIds()
63 return QStringList() << "height" << "depth"
64 << "totalheight" << "width";
68 static QStringList boxGuiSpecialLengthNames()
70 return QStringList() << qt_("Height") << qt_("Depth")
71 << qt_("Total Height") << qt_("Width");
75 static QList<ColorCode> colors()
77 QList<ColorCode> colors;
78 colors << Color_black;
79 colors << Color_white;
81 colors << Color_brown;
83 colors << Color_darkgray;
85 colors << Color_green;
86 colors << Color_lightgray;
88 colors << Color_magenta;
89 colors << Color_olive;
90 colors << Color_orange;
92 colors << Color_purple;
95 colors << Color_violet;
96 colors << Color_yellow;
101 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
105 // fill the box type choice
107 gui_names_ = boxGuiNames();
108 for (int i = 0; i != ids_.size(); ++i)
109 typeCO->addItem(gui_names_[i], ids_[i]);
111 // add the special units to the height choice
112 // width needs different handling
113 ids_spec_ = boxGuiSpecialLengthIds();
114 gui_names_spec_ = boxGuiSpecialLengthNames();
115 for (int i = 0; i != ids_spec_.size(); ++i)
116 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
118 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
119 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
120 this, SIGNAL(changed()));
121 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
122 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
123 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
124 this, SIGNAL(changed()));
125 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
126 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
127 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
128 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
129 this, SIGNAL(changed()));
130 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
131 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
132 this, SIGNAL(changed()));
133 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
134 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
135 this, SIGNAL(changed()));
136 connect(backgroundColorCO, SIGNAL(currentIndexChanged(int)),
137 this, SIGNAL(changed()));
139 heightED->setValidator(unsignedLengthValidator(heightED));
140 widthED->setValidator(unsignedLengthValidator(widthED));
141 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
142 separationED->setValidator(unsignedLengthValidator(separationED));
143 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
145 // initialize the length validator
146 addCheckedWidget(widthED, widthCB);
147 addCheckedWidget(heightED, heightCB);
148 addCheckedWidget(thicknessED, thicknessLA);
149 addCheckedWidget(separationED, separationLA);
150 addCheckedWidget(shadowsizeED, shadowsizeLA);
152 // the background can be uncolored while the frame cannot
153 color_codes_ = colors();
154 sort(color_codes_.begin(), color_codes_.end(), ColorSorter);
155 fillComboColor(backgroundColorCO, true);
156 fillComboColor(frameColorCO, false);
162 void GuiBox::fillComboColor(QComboBox * combo, bool const is_none)
165 QPixmap coloritem(32, 32);
167 // frameColorCO cannot be uncolored
169 combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
170 toqstr(lcolor.getLaTeXName(Color_none)));
171 QList<ColorCode>::const_iterator cit = color_codes_.begin();
172 for (; cit != color_codes_.end(); ++cit) {
173 QString const latexname = toqstr(lcolor.getLaTeXName(*cit));
174 QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
175 color = guiApp->colorCache().get(*cit, false);
176 coloritem.fill(color);
177 combo->addItem(QIcon(coloritem), guiname, latexname);
182 void GuiBox::on_innerBoxCO_activated(int index)
184 QString itype = innerBoxCO->itemData(index).toString();
185 // handle parbox and minipage the same way
186 bool const ibox = (itype != "none" && itype != "makebox");
187 if (heightCB->isChecked() && !ibox)
188 heightCB->setChecked(false);
189 widthCB->setChecked(!widthED->text().isEmpty());
195 void GuiBox::on_typeCO_activated(int index)
198 typeCO->itemData(index).toString();
199 bool const frameless = (type == "Frameless");
201 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
202 setInnerType(frameless, itype);
203 // refresh itype because it might have been changed in setInnerType
205 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
206 // handle parbox and minipage the same way
207 bool const ibox = (itype != "none" && itype != "makebox");
208 if (frameless && itype != "makebox") {
209 if (heightCB->isChecked() && !ibox)
210 heightCB->setChecked(false);
213 if (type != "Boxed") {
214 if (type != "Frameless")
215 widthCB->setChecked(itype != "none");
216 pagebreakCB->setChecked(false);
218 // assure that the frame color is black for frameless boxes to
219 // provide the color "none"
220 int const b = frameColorCO->findData("black");
221 if (frameless && frameColorCO->currentIndex() != b)
222 frameColorCO->setCurrentIndex(b);
227 void GuiBox::on_frameColorCO_currentIndexChanged(int index)
229 // if there is a non-black frame color the background cannot be uncolored
230 // therefore remove the entry "none" in this case
231 int const n = backgroundColorCO->findData("none");
232 if (index != frameColorCO->findData("black")) {
234 if (backgroundColorCO->currentIndex() == n)
235 backgroundColorCO->setCurrentIndex(
236 backgroundColorCO->findData("white"));
237 backgroundColorCO->removeItem(n);
241 backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
242 toqstr(lcolor.getLaTeXName(Color_none)));
248 void GuiBox::initDialog()
250 setInnerType(true, toqstr("minipage"));
251 widthED->setText("100");
252 widthCB->setChecked(true);
253 widthCB->setEnabled(false);
254 widthUnitsLC->setCurrentItem(Length::PCW);
255 heightED->setText("1");
256 heightUnitsLC->setCurrentItem("totalheight");
257 // LaTeX's default for \fboxrule is 0.4 pt
258 thicknessED->setText("0.4");
259 thicknessUnitsLC->setCurrentItem(Length::PT);
260 // LaTeX's default for \fboxsep is 3 pt
261 separationED->setText("3");
262 separationUnitsLC->setCurrentItem(Length::PT);
263 // LaTeX's default for \shadowsize is 4 pt
264 shadowsizeED->setText("4");
265 shadowsizeUnitsLC->setCurrentItem(Length::PT);
269 void GuiBox::on_widthCB_stateChanged(int)
275 void GuiBox::on_heightCB_stateChanged(int /*state*/)
281 void GuiBox::on_pagebreakCB_stateChanged()
283 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
285 widthCB->setChecked(false);
287 on_typeCO_activated(typeCO->currentIndex());
295 void GuiBox::paramsToDialog(Inset const * inset)
297 InsetBox const * box = static_cast<InsetBox const *>(inset);
298 InsetBoxParams const & params = box->params();
299 QString type = toqstr(params.type);
300 if (type == "Framed") {
301 pagebreakCB->setChecked(true);
304 pagebreakCB->setChecked(false);
307 typeCO->setCurrentIndex(typeCO->findData(type));
310 QString inner_type = "minipage";
311 if (!params.inner_box)
313 if (params.use_parbox)
314 inner_type = "parbox";
315 if (params.use_makebox)
316 inner_type = "makebox";
317 bool const frameless = (params.type == "Frameless");
318 setInnerType(frameless, inner_type);
321 valignCO->setCurrentIndex(string("tcb").find(c, 0));
322 c = params.inner_pos;
323 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
325 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
327 bool ibox = (params.inner_box && !params.use_makebox);
328 valignCO->setEnabled(ibox);
329 ialignCO->setEnabled(ibox);
332 // halign is only allowed without inner box and if a width is used and if
333 // pagebreak is not used
334 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
335 && ((!ibox && type == "Boxed") || inner_type == "makebox"));
336 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
337 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
338 && halignCO->count() < 4)
339 halignCO->addItem(qt_("Stretch"));
340 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
341 halignCO->removeItem(3);
342 // pagebreak is only allowed for Boxed without inner box
343 pagebreakCB->setEnabled(!ibox && type == "Boxed");
345 Length::UNIT const default_unit = Length::defaultUnit();
347 // the width can only be selected for makebox or framebox
348 widthCB->setEnabled(inner_type == "makebox"
350 && !ibox && !pagebreakCB->isChecked()));
351 if (params.width.empty()) {
352 widthCB->setChecked(false);
353 lengthToWidgets(widthED, widthUnitsLC,
354 params.width, default_unit);
356 widthCB->setChecked(true);
357 lengthToWidgets(widthED, widthUnitsLC,
358 params.width, default_unit);
359 QString const special = toqstr(params.special);
360 if (!special.isEmpty() && special != "none")
361 widthUnitsLC->setCurrentItem(special);
364 widthED->setEnabled(widthCB->isChecked());
365 widthUnitsLC->setEnabled(widthCB->isChecked());
367 lengthToWidgets(heightED, heightUnitsLC,
368 (params.height).asString(), default_unit);
370 QString const height_special = toqstr(params.height_special);
371 if (!height_special.isEmpty() && height_special != "none")
372 heightUnitsLC->setCurrentItem(height_special);
373 // set no optional height if the value is the default "1\height"
374 // (special units like \height are handled as "in",
375 // FIXME: this is a very bad UI, this check box should be disabled in
376 // this case, not forced to 'unchecked' state.
377 if (height_special == "totalheight" && params.height == Length("1in"))
378 heightCB->setCheckState(Qt::Unchecked);
380 heightCB->setCheckState(Qt::Checked);
382 heightCB->setEnabled(ibox);
384 // enable line thickness only for the rectangular frame types and drop shadow
385 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
386 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
387 lengthToWidgets(thicknessED, thicknessUnitsLC,
388 (params.thickness).asString(), default_unit);
389 // enable line separation for the allowed frame types
390 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
391 || type == "Doublebox" || type == "Shadowbox");
392 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
393 || type == "Doublebox" || type == "Shadowbox");
394 lengthToWidgets(separationED, separationUnitsLC,
395 (params.separation).asString(), default_unit);
396 // enable shadow size for drop shadow
397 shadowsizeED->setEnabled(type == "Shadowbox");
398 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
399 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
400 (params.shadowsize).asString(), default_unit);
402 frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
403 backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
407 docstring GuiBox::dialogToParams() const
409 bool const pagebreak =
410 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
415 box_type = fromqstr(typeCO->itemData(
416 typeCO->currentIndex()).toString());
418 InsetBoxParams params(box_type);
420 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
422 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
424 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
426 params.pos = "tcb"[valignCO->currentIndex()];
427 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
428 params.hor_pos = "lcrs"[halignCO->currentIndex()];
431 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
432 QString value = widthED->text();
434 if (widthED->isEnabled()) {
435 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
436 params.special = fromqstr(unit);
437 // Note: the unit is simply ignored in this case
438 params.width = Length(widgetToDouble(widthED), Length::IN);
440 params.special = "none";
441 // we must specify a valid length in this case
443 widthED->setText("0");
444 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
447 params.special = "none";
448 params.width = Length();
451 // the height parameter is omitted if the value
452 // is "1in" and "Total Height" is used as unit.
453 // 1in + "Total Height" means "1\height" which is the LaTeX default
454 // if no height is given
455 if (heightCB->checkState() == Qt::Unchecked) {
456 params.height = Length("1in");
457 params.height_special = "totalheight";
459 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
460 value = heightED->text();
461 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
462 params.height_special = fromqstr(unit);
463 // Note: the unit is simply ignored in this case
464 params.height = Length(widgetToDouble(heightED), Length::IN);
466 params.height_special = "none";
468 Length(widgetsToLength(heightED, heightUnitsLC));
472 // handle the line thickness, line separation and shadow size
473 if (thicknessED->isEnabled())
474 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
476 params.thickness = Length("0.4pt");
477 if (separationED->isEnabled())
478 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
480 params.separation = Length("3pt");
481 if (separationED->isEnabled())
482 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
484 params.shadowsize = Length("4pt");
485 if (frameColorCO->isEnabled())
487 fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
489 params.framecolor = "black";
490 if (backgroundColorCO->isEnabled())
491 params.backgroundcolor =
492 fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
494 params.backgroundcolor = "none";
496 return from_ascii(InsetBox::params2string(params));
500 bool GuiBox::checkWidgets(bool readonly) const
502 typeCO->setEnabled(!readonly);
505 pagebreakCB->setEnabled(false);
506 innerBoxCO->setEnabled(false);
507 valignCO->setEnabled(false);
508 ialignCO->setEnabled(false);
509 halignCO->setEnabled(false);
510 widthCB->setEnabled(false);
511 widthED->setEnabled(false);
512 widthUnitsLC->setEnabled(false);
513 heightED->setEnabled(false);
514 heightUnitsLC->setEnabled(false);
515 heightCB->setEnabled(false);
516 thicknessED->setEnabled(false);
517 thicknessUnitsLC->setEnabled(false);
518 separationED->setEnabled(false);
519 separationUnitsLC->setEnabled(false);
520 shadowsizeED->setEnabled(false);
521 shadowsizeUnitsLC->setEnabled(false);
523 QString const outer =
524 typeCO->itemData(typeCO->currentIndex()).toString();
525 QString const itype =
526 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
527 bool const ibox = (itype != "none" && itype != "makebox");
528 valignCO->setEnabled(ibox);
529 ialignCO->setEnabled(ibox);
530 if (heightCB->isChecked() && !ibox)
531 heightCB->setChecked(false);
532 heightCB->setEnabled(ibox);
533 // the width can only be selected for makebox or framebox
534 widthCB->setEnabled(itype == "makebox"
535 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
536 // except for Frameless and Boxed, the width cannot be specified if
537 // there is no inner box
538 bool const width_enabled =
539 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
540 // enable if width_enabled
541 widthED->setEnabled(width_enabled);
542 widthUnitsLC->setEnabled(width_enabled);
543 if (!widthCB->isChecked() && widthCB->isEnabled()) {
544 widthED->setEnabled(false);
545 widthUnitsLC->setEnabled(false);
547 // halign is only allowed without inner box and if a width is used and if
548 // pagebreak is not used
549 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
550 && ((!ibox && outer == "Boxed") || itype == "makebox"));
551 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
552 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
553 && halignCO->count() < 4)
554 halignCO->addItem(qt_("Stretch"));
555 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
556 halignCO->removeItem(3);
557 // pagebreak is only allowed for Boxed without inner box
558 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
560 heightED->setEnabled(itype != "none" && heightCB->isChecked());
561 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
562 heightCB->setEnabled(ibox);
564 // enable line thickness for the rectangular frame types and drop shadow
565 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
566 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
567 // set default values if empty
568 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
569 thicknessED->setText("0.4");
570 thicknessUnitsLC->setCurrentItem(Length::PT);
572 // enable line separation for the allowed frame types
573 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
574 || outer == "Doublebox" || outer == "Shadowbox");
575 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
576 || outer == "Doublebox" || outer == "Shadowbox");
577 // set default values if empty
578 if (separationED->text().isEmpty() && separationED->isEnabled()) {
579 separationED->setText("3");
580 separationUnitsLC->setCurrentItem(Length::PT);
582 // enable shadow size for drop shadow
583 shadowsizeED->setEnabled(outer == "Shadowbox");
584 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
585 // set default values if empty
586 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
587 shadowsizeED->setText("4");
588 shadowsizeUnitsLC->setCurrentItem(Length::PT);
590 // \fboxcolor and \colorbox cannot be used for fancybox boxes
591 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
592 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
595 return InsetParamsWidget::checkWidgets();
599 void GuiBox::setSpecial(bool ibox)
601 QString const last_item =
602 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
604 // check if the widget contains the special units
605 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
606 // insert 'em if needed...
607 if (!ibox && !has_special) {
608 for (int i = 1; i < ids_spec_.size(); ++i)
609 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
610 // ... or remove 'em if needed
611 } else if (ibox && has_special) {
612 for (int i = 1; i < ids_spec_.size(); ++i) {
613 int n = widthUnitsLC->findData(ids_spec_[i]);
615 widthUnitsLC->removeItem(n);
618 // restore selected text, if possible
619 widthUnitsLC->setCurrentItem(last_item);
623 void GuiBox::setInnerType(bool frameless, QString const & type)
625 // with "frameless" boxes, inner box is mandatory
626 // (i.e. is the actual box)
627 // we have to remove "none" then and adjust the combo
630 innerBoxCO->addItem(qt_("None"), toqstr("none"));
632 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
633 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
634 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
635 int i = (innerBoxCO->findData(type) != -1)
636 ? innerBoxCO->findData(type) : 0;
637 innerBoxCO->setCurrentIndex(i);
640 } // namespace frontend
644 #include "moc_GuiBox.cpp"