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(activated(int)), 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(frameColorCO, SIGNAL(currentIndexChanged(int)),
137 this, SIGNAL(changed()));
138 connect(backgroundColorCO, SIGNAL(currentIndexChanged(int)),
139 this, SIGNAL(changed()));
141 heightED->setValidator(unsignedLengthValidator(heightED));
142 widthED->setValidator(unsignedLengthValidator(widthED));
143 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
144 separationED->setValidator(unsignedLengthValidator(separationED));
145 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
147 // initialize the length validator
148 addCheckedWidget(widthED, widthCB);
149 addCheckedWidget(heightED, heightCB);
150 addCheckedWidget(thicknessED, thicknessLA);
151 addCheckedWidget(separationED, separationLA);
152 addCheckedWidget(shadowsizeED, shadowsizeLA);
154 // the background can be uncolored while the frame cannot
155 color_codes_ = colors();
156 sort(color_codes_.begin(), color_codes_.end(), ColorSorter);
157 fillComboColor(backgroundColorCO, true);
158 fillComboColor(frameColorCO, false);
164 void GuiBox::fillComboColor(QComboBox * combo, bool const is_background)
167 QPixmap coloritem(32, 32);
169 // condition on the two possible types
171 combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
172 toqstr(lcolor.getLaTeXName(Color_none)));
174 combo->addItem(qt_("Default"), toqstr("default"));
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 = 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);
226 void GuiBox::initDialog()
228 setInnerType(true, toqstr("minipage"));
229 widthED->setText("100");
230 widthCB->setChecked(true);
231 widthCB->setEnabled(false);
232 widthUnitsLC->setCurrentItem(Length::PCW);
233 heightED->setText("1");
234 heightUnitsLC->setCurrentItem("totalheight");
235 // LaTeX's default for \fboxrule is 0.4 pt
236 thicknessED->setText("0.4");
237 thicknessUnitsLC->setCurrentItem(Length::PT);
238 // LaTeX's default for \fboxsep is 3 pt
239 separationED->setText("3");
240 separationUnitsLC->setCurrentItem(Length::PT);
241 // LaTeX's default for \shadowsize is 4 pt
242 shadowsizeED->setText("4");
243 shadowsizeUnitsLC->setCurrentItem(Length::PT);
247 void GuiBox::on_widthCB_stateChanged(int)
253 void GuiBox::on_heightCB_stateChanged(int /*state*/)
259 void GuiBox::on_pagebreakCB_stateChanged()
261 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
263 widthCB->setChecked(false);
265 on_typeCO_activated(typeCO->currentIndex());
273 void GuiBox::paramsToDialog(Inset const * inset)
275 InsetBox const * box = static_cast<InsetBox const *>(inset);
276 InsetBoxParams const & params = box->params();
277 QString type = toqstr(params.type);
278 if (type == "Framed") {
279 pagebreakCB->setChecked(true);
282 pagebreakCB->setChecked(false);
285 typeCO->setCurrentIndex(typeCO->findData(type));
288 QString inner_type = "minipage";
289 if (!params.inner_box)
291 if (params.use_parbox)
292 inner_type = "parbox";
293 if (params.use_makebox)
294 inner_type = "makebox";
295 bool const frameless = (params.type == "Frameless");
296 setInnerType(frameless, inner_type);
299 valignCO->setCurrentIndex(string("tcb").find(c, 0));
300 c = params.inner_pos;
301 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
303 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
305 bool ibox = (params.inner_box && !params.use_makebox);
306 valignCO->setEnabled(ibox);
307 ialignCO->setEnabled(ibox);
310 // halign is only allowed without inner box and if a width is used and if
311 // pagebreak is not used
312 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
313 && ((!ibox && type == "Boxed") || inner_type == "makebox"));
314 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
315 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
316 && halignCO->count() < 4)
317 halignCO->addItem(qt_("Stretch"));
318 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
319 halignCO->removeItem(3);
320 // pagebreak is only allowed for Boxed without inner box
321 pagebreakCB->setEnabled(!ibox && type == "Boxed");
323 Length::UNIT const default_unit = Length::defaultUnit();
325 // the width can only be selected for makebox or framebox
326 widthCB->setEnabled(inner_type == "makebox"
328 && !ibox && !pagebreakCB->isChecked()));
329 if (params.width.empty()) {
330 widthCB->setChecked(false);
331 lengthToWidgets(widthED, widthUnitsLC,
332 params.width, default_unit);
334 widthCB->setChecked(true);
335 lengthToWidgets(widthED, widthUnitsLC,
336 params.width, default_unit);
337 QString const special = toqstr(params.special);
338 if (!special.isEmpty() && special != "none")
339 widthUnitsLC->setCurrentItem(special);
342 widthED->setEnabled(widthCB->isChecked());
343 widthUnitsLC->setEnabled(widthCB->isChecked());
345 lengthToWidgets(heightED, heightUnitsLC,
346 (params.height).asString(), default_unit);
348 QString const height_special = toqstr(params.height_special);
349 if (!height_special.isEmpty() && height_special != "none")
350 heightUnitsLC->setCurrentItem(height_special);
351 // set no optional height if the value is the default "1\height"
352 // (special units like \height are handled as "in",
353 // FIXME: this is a very bad UI, this check box should be disabled in
354 // this case, not forced to 'unchecked' state.
355 if (height_special == "totalheight" && params.height == Length("1in"))
356 heightCB->setCheckState(Qt::Unchecked);
358 heightCB->setCheckState(Qt::Checked);
360 heightCB->setEnabled(ibox);
362 // enable line thickness only for the rectangular frame types and drop shadow
363 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
364 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
365 lengthToWidgets(thicknessED, thicknessUnitsLC,
366 (params.thickness).asString(), default_unit);
367 // enable line separation for the allowed frame types
368 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
369 || type == "Doublebox" || type == "Shadowbox");
370 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
371 || type == "Doublebox" || type == "Shadowbox");
372 lengthToWidgets(separationED, separationUnitsLC,
373 (params.separation).asString(), default_unit);
374 // enable shadow size for drop shadow
375 shadowsizeED->setEnabled(type == "Shadowbox");
376 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
377 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
378 (params.shadowsize).asString(), default_unit);
380 frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
381 backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
385 docstring GuiBox::dialogToParams() const
387 bool const pagebreak =
388 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
393 box_type = fromqstr(typeCO->itemData(
394 typeCO->currentIndex()).toString());
396 InsetBoxParams params(box_type);
398 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
400 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
402 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
404 params.pos = "tcb"[valignCO->currentIndex()];
405 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
406 params.hor_pos = "lcrs"[halignCO->currentIndex()];
409 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
410 QString value = widthED->text();
412 if (widthED->isEnabled()) {
413 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
414 params.special = fromqstr(unit);
415 // Note: the unit is simply ignored in this case
416 params.width = Length(widgetToDouble(widthED), Length::IN);
418 params.special = "none";
419 // we must specify a valid length in this case
421 widthED->setText("0");
422 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
425 params.special = "none";
426 params.width = Length();
429 // the height parameter is omitted if the value
430 // is "1in" and "Total Height" is used as unit.
431 // 1in + "Total Height" means "1\height" which is the LaTeX default
432 // if no height is given
433 if (heightCB->checkState() == Qt::Unchecked) {
434 params.height = Length("1in");
435 params.height_special = "totalheight";
437 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
438 value = heightED->text();
439 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
440 params.height_special = fromqstr(unit);
441 // Note: the unit is simply ignored in this case
442 params.height = Length(widgetToDouble(heightED), Length::IN);
444 params.height_special = "none";
446 Length(widgetsToLength(heightED, heightUnitsLC));
450 // handle the line thickness, line separation and shadow size
451 if (thicknessED->isEnabled())
452 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
454 params.thickness = Length("0.4pt");
455 if (separationED->isEnabled())
456 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
458 params.separation = Length("3pt");
459 if (separationED->isEnabled())
460 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
462 params.shadowsize = Length("4pt");
463 if (frameColorCO->isEnabled())
465 fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
467 params.framecolor = "foreground";
468 if (backgroundColorCO->isEnabled())
469 params.backgroundcolor =
470 fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
472 params.backgroundcolor = "none";
474 return from_ascii(InsetBox::params2string(params));
478 bool GuiBox::checkWidgets(bool readonly) const
480 typeCO->setEnabled(!readonly);
483 pagebreakCB->setEnabled(false);
484 innerBoxCO->setEnabled(false);
485 valignCO->setEnabled(false);
486 ialignCO->setEnabled(false);
487 halignCO->setEnabled(false);
488 widthCB->setEnabled(false);
489 widthED->setEnabled(false);
490 widthUnitsLC->setEnabled(false);
491 heightED->setEnabled(false);
492 heightUnitsLC->setEnabled(false);
493 heightCB->setEnabled(false);
494 thicknessED->setEnabled(false);
495 thicknessUnitsLC->setEnabled(false);
496 separationED->setEnabled(false);
497 separationUnitsLC->setEnabled(false);
498 shadowsizeED->setEnabled(false);
499 shadowsizeUnitsLC->setEnabled(false);
501 QString const outer =
502 typeCO->itemData(typeCO->currentIndex()).toString();
503 QString const itype =
504 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
505 bool const ibox = (itype != "none" && itype != "makebox");
506 valignCO->setEnabled(ibox);
507 ialignCO->setEnabled(ibox);
508 if (heightCB->isChecked() && !ibox)
509 heightCB->setChecked(false);
510 heightCB->setEnabled(ibox);
511 // the width can only be selected for makebox or framebox
512 widthCB->setEnabled(itype == "makebox"
513 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
514 // except for Frameless and Boxed, the width cannot be specified if
515 // there is no inner box
516 bool const width_enabled =
517 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
518 // enable if width_enabled
519 widthED->setEnabled(width_enabled);
520 widthUnitsLC->setEnabled(width_enabled);
521 if (!widthCB->isChecked() && widthCB->isEnabled()) {
522 widthED->setEnabled(false);
523 widthUnitsLC->setEnabled(false);
525 // halign is only allowed without inner box and if a width is used and if
526 // pagebreak is not used
527 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
528 && ((!ibox && outer == "Boxed") || itype == "makebox"));
529 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
530 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
531 && halignCO->count() < 4)
532 halignCO->addItem(qt_("Stretch"));
533 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
534 halignCO->removeItem(3);
535 // pagebreak is only allowed for Boxed without inner box
536 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
538 heightED->setEnabled(itype != "none" && heightCB->isChecked());
539 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
540 heightCB->setEnabled(ibox);
542 // enable line thickness for the rectangular frame types and drop shadow
543 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
544 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
545 // set default values if empty
546 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
547 thicknessED->setText("0.4");
548 thicknessUnitsLC->setCurrentItem(Length::PT);
550 // enable line separation for the allowed frame types
551 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
552 || outer == "Doublebox" || outer == "Shadowbox");
553 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
554 || outer == "Doublebox" || outer == "Shadowbox");
555 // set default values if empty
556 if (separationED->text().isEmpty() && separationED->isEnabled()) {
557 separationED->setText("3");
558 separationUnitsLC->setCurrentItem(Length::PT);
560 // enable shadow size for drop shadow
561 shadowsizeED->setEnabled(outer == "Shadowbox");
562 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
563 // set default values if empty
564 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
565 shadowsizeED->setText("4");
566 shadowsizeUnitsLC->setCurrentItem(Length::PT);
568 // \fboxcolor and \colorbox cannot be used for fancybox boxes
569 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
570 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
573 return InsetParamsWidget::checkWidgets();
577 void GuiBox::setSpecial(bool ibox)
579 QString const last_item =
580 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
582 // check if the widget contains the special units
583 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
584 // insert 'em if needed...
585 if (!ibox && !has_special) {
586 for (int i = 1; i < ids_spec_.size(); ++i)
587 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
588 // ... or remove 'em if needed
589 } else if (ibox && has_special) {
590 for (int i = 1; i < ids_spec_.size(); ++i) {
591 int n = widthUnitsLC->findData(ids_spec_[i]);
593 widthUnitsLC->removeItem(n);
596 // restore selected text, if possible
597 widthUnitsLC->setCurrentItem(last_item);
601 void GuiBox::setInnerType(bool frameless, QString const & type)
603 // with "frameless" boxes, inner box is mandatory
604 // (i.e. is the actual box)
605 // we have to remove "none" then and adjust the combo
608 innerBoxCO->addItem(qt_("None"), toqstr("none"));
610 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
611 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
612 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
613 int i = (innerBoxCO->findData(type) != -1)
614 ? innerBoxCO->findData(type) : 0;
615 innerBoxCO->setCurrentIndex(i);
618 } // namespace frontend
622 #include "moc_GuiBox.cpp"