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;
84 colors << Color_black;
85 colors << Color_white;
87 colors << Color_brown;
89 colors << Color_darkgray;
91 colors << Color_green;
92 colors << Color_lightgray;
94 colors << Color_magenta;
95 colors << Color_olive;
96 colors << Color_orange;
98 colors << Color_purple;
100 colors << Color_teal;
101 colors << Color_violet;
102 colors << Color_yellow;
107 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
111 // fill the box type choice
113 gui_names_ = boxGuiNames();
114 for (int i = 0; i != ids_.size(); ++i)
115 typeCO->addItem(gui_names_[i], ids_[i]);
117 // add the special units to the height choice
118 // width needs different handling
119 ids_spec_ = boxGuiSpecialLengthIds();
120 gui_names_spec_ = boxGuiSpecialLengthNames();
121 for (int i = 0; i != ids_spec_.size(); ++i)
122 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
124 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
125 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
126 this, SIGNAL(changed()));
127 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
128 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
129 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
130 this, SIGNAL(changed()));
131 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
132 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
133 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
134 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
135 this, SIGNAL(changed()));
136 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
137 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
138 this, SIGNAL(changed()));
139 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
140 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
141 this, SIGNAL(changed()));
142 connect(frameColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
143 connect(backgroundColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
145 heightED->setValidator(unsignedLengthValidator(heightED));
146 widthED->setValidator(unsignedLengthValidator(widthED));
147 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
148 separationED->setValidator(unsignedLengthValidator(separationED));
149 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
151 // initialize the length validator
152 addCheckedWidget(widthED, widthCB);
153 addCheckedWidget(heightED, heightCB);
154 addCheckedWidget(thicknessED, thicknessLA);
155 addCheckedWidget(separationED, separationLA);
156 addCheckedWidget(shadowsizeED, shadowsizeLA);
158 // the background can be uncolored while the frame cannot
159 color_codes_ = colors();
160 fillComboColor(frameColorCO, false);
161 fillComboColor(backgroundColorCO, true);
167 void GuiBox::fillComboColor(QComboBox * combo, bool const is_none)
169 QPixmap coloritem(32, 32);
171 // frameColorCO cannot be uncolored
173 combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
174 toqstr(lcolor.getLaTeXName(Color_none)));
175 QList<ColorCode>::const_iterator cit = color_codes_.begin() + 1;
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 = QColor(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);
222 // assure that the frame color is black for frameless boxes to
223 // provide the color "none"
224 int const b = frameColorCO->findData("black");
225 if (frameless && frameColorCO->currentIndex() != b)
226 frameColorCO->setCurrentIndex(b);
231 void GuiBox::on_frameColorCO_currentIndexChanged(int index)
233 // if there is a non-black frame color the background cannot be uncolored
234 // therefore remove the entry "none" in this case
235 if (index != frameColorCO->findData("black")) {
236 int const n = backgroundColorCO->findData("none");
238 if (backgroundColorCO->currentIndex() == n)
239 backgroundColorCO->setCurrentIndex(
240 backgroundColorCO->findData("white"));
241 backgroundColorCO->removeItem(n);
244 if (backgroundColorCO->count() == color_codes_.count() - 1)
245 backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
246 toqstr(lcolor.getLaTeXName(Color_none)));
252 void GuiBox::initDialog()
254 setInnerType(true, toqstr("minipage"));
255 widthED->setText("100");
256 widthCB->setChecked(true);
257 widthCB->setEnabled(false);
258 widthUnitsLC->setCurrentItem(Length::PCW);
259 heightED->setText("1");
260 heightUnitsLC->setCurrentItem("totalheight");
261 // LaTeX's default for \fboxrule is 0.4 pt
262 thicknessED->setText("0.4");
263 thicknessUnitsLC->setCurrentItem(Length::PT);
264 // LaTeX's default for \fboxsep is 3 pt
265 separationED->setText("3");
266 separationUnitsLC->setCurrentItem(Length::PT);
267 // LaTeX's default for \shadowsize is 4 pt
268 shadowsizeED->setText("4");
269 shadowsizeUnitsLC->setCurrentItem(Length::PT);
273 void GuiBox::on_widthCB_stateChanged(int)
279 void GuiBox::on_heightCB_stateChanged(int /*state*/)
285 void GuiBox::on_pagebreakCB_stateChanged()
287 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
289 widthCB->setChecked(!pbreak);
291 on_typeCO_activated(typeCO->currentIndex());
299 void GuiBox::paramsToDialog(Inset const * inset)
301 InsetBox const * box = static_cast<InsetBox const *>(inset);
302 InsetBoxParams const & params = box->params();
303 QString type = toqstr(params.type);
304 if (type == "Framed") {
305 pagebreakCB->setChecked(true);
308 pagebreakCB->setChecked(false);
311 typeCO->setCurrentIndex(typeCO->findData(type));
314 QString inner_type = "minipage";
315 if (!params.inner_box)
317 if (params.use_parbox)
318 inner_type = "parbox";
319 if (params.use_makebox)
320 inner_type = "makebox";
321 bool const frameless = (params.type == "Frameless");
322 setInnerType(frameless, inner_type);
325 valignCO->setCurrentIndex(string("tcb").find(c, 0));
326 c = params.inner_pos;
327 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
329 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
331 bool ibox = (params.inner_box && !params.use_makebox);
332 valignCO->setEnabled(ibox);
333 ialignCO->setEnabled(ibox);
336 // halign is only allowed if a width is used
337 halignCO->setEnabled(widthCB->isChecked());
338 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
339 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
340 && halignCO->count() < 4)
341 halignCO->addItem(toqstr("Stretch"));
342 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
343 halignCO->removeItem(3);
344 // pagebreak is only allowed for Boxed without inner box
345 pagebreakCB->setEnabled(!ibox && type == "Boxed");
347 Length::UNIT const default_unit = Length::defaultUnit();
349 // the width can only be selected for makebox or framebox
350 widthCB->setEnabled(inner_type == "makebox"
352 && !ibox && !pagebreakCB->isChecked()));
353 if (params.width.empty()) {
354 widthCB->setChecked(false);
355 lengthToWidgets(widthED, widthUnitsLC,
356 params.width, default_unit);
358 widthCB->setChecked(true);
359 lengthToWidgets(widthED, widthUnitsLC,
360 params.width, default_unit);
361 QString const special = toqstr(params.special);
362 if (!special.isEmpty() && special != "none")
363 widthUnitsLC->setCurrentItem(special);
366 widthED->setEnabled(widthCB->isChecked());
367 widthUnitsLC->setEnabled(widthCB->isChecked());
369 lengthToWidgets(heightED, heightUnitsLC,
370 (params.height).asString(), default_unit);
372 QString const height_special = toqstr(params.height_special);
373 if (!height_special.isEmpty() && height_special != "none")
374 heightUnitsLC->setCurrentItem(height_special);
375 // set no optional height if the value is the default "1\height"
376 // (special units like \height are handled as "in",
377 // FIXME: this is a very bad UI, this check box should be disabled in
378 // this case, not forced to 'unchecked' state.
379 if (height_special == "totalheight" && params.height == Length("1in"))
380 heightCB->setCheckState(Qt::Unchecked);
382 heightCB->setCheckState(Qt::Checked);
384 heightCB->setEnabled(ibox);
386 // enable line thickness only for the rectangular frame types and drop shadow
387 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
388 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
389 lengthToWidgets(thicknessED, thicknessUnitsLC,
390 (params.thickness).asString(), default_unit);
391 // enable line separation for the allowed frame types
392 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
393 || type == "Doublebox" || type == "Shadowbox");
394 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
395 || type == "Doublebox" || type == "Shadowbox");
396 lengthToWidgets(separationED, separationUnitsLC,
397 (params.separation).asString(), default_unit);
398 // enable shadow size for drop shadow
399 shadowsizeED->setEnabled(type == "Shadowbox");
400 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
401 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
402 (params.shadowsize).asString(), default_unit);
404 frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
405 backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
409 docstring GuiBox::dialogToParams() const
411 bool const pagebreak =
412 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
417 box_type = fromqstr(typeCO->itemData(
418 typeCO->currentIndex()).toString());
420 InsetBoxParams params(box_type);
422 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
424 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
426 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
428 params.pos = "tcb"[valignCO->currentIndex()];
429 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
430 params.hor_pos = "lcrs"[halignCO->currentIndex()];
433 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
434 QString value = widthED->text();
436 if (widthED->isEnabled()) {
437 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
438 params.special = fromqstr(unit);
439 // Note: the unit is simply ignored in this case
440 params.width = Length(value.toDouble(), Length::IN);
442 params.special = "none";
443 // we must specify a valid length in this case
445 widthED->setText("0");
446 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
449 params.special = "none";
450 params.width = Length();
453 // the height parameter is omitted if the value
454 // is "1in" and "Total Height" is used as unit.
455 // 1in + "Total Height" means "1\height" which is the LaTeX default
456 // if no height is given
457 if (heightCB->checkState() == Qt::Unchecked) {
458 params.height = Length("1in");
459 params.height_special = "totalheight";
461 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
462 value = heightED->text();
463 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
464 params.height_special = fromqstr(unit);
465 // Note: the unit is simply ignored in this case
466 params.height = Length(value.toDouble(), Length::IN);
468 params.height_special = "none";
470 Length(widgetsToLength(heightED, heightUnitsLC));
474 // handle the line thickness, line separation and shadow size
475 if (thicknessED->isEnabled())
476 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
478 params.thickness = Length("0.4pt");
479 if (separationED->isEnabled())
480 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
482 params.separation = Length("3pt");
483 if (separationED->isEnabled())
484 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
486 params.shadowsize = Length("4pt");
487 if (frameColorCO->isEnabled())
489 fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
491 params.framecolor = "black";
492 if (backgroundColorCO->isEnabled())
493 params.backgroundcolor =
494 fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
496 params.backgroundcolor = "none";
498 return from_ascii(InsetBox::params2string(params));
502 bool GuiBox::checkWidgets(bool readonly) const
504 typeCO->setEnabled(!readonly);
507 pagebreakCB->setEnabled(false);
508 innerBoxCO->setEnabled(false);
509 valignCO->setEnabled(false);
510 ialignCO->setEnabled(false);
511 halignCO->setEnabled(false);
512 widthCB->setEnabled(false);
513 widthED->setEnabled(false);
514 widthUnitsLC->setEnabled(false);
515 heightED->setEnabled(false);
516 heightUnitsLC->setEnabled(false);
517 heightCB->setEnabled(false);
518 thicknessED->setEnabled(false);
519 thicknessUnitsLC->setEnabled(false);
520 separationED->setEnabled(false);
521 separationUnitsLC->setEnabled(false);
522 shadowsizeED->setEnabled(false);
523 shadowsizeUnitsLC->setEnabled(false);
525 QString const outer =
526 typeCO->itemData(typeCO->currentIndex()).toString();
527 QString const itype =
528 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
529 bool const ibox = (itype != "none" && itype != "makebox");
530 valignCO->setEnabled(ibox);
531 ialignCO->setEnabled(ibox);
532 if (heightCB->isChecked() && !ibox)
533 heightCB->setChecked(false);
534 heightCB->setEnabled(ibox);
535 // the width can only be selected for makebox or framebox
536 widthCB->setEnabled(itype == "makebox"
537 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
538 // except for Frameless and Boxed, the width cannot be specified if
539 // there is no inner box
540 bool const width_enabled =
541 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
542 // enable if width_enabled
543 widthED->setEnabled(width_enabled);
544 widthUnitsLC->setEnabled(width_enabled);
545 if (!widthCB->isChecked() && widthCB->isEnabled()) {
546 widthED->setEnabled(false);
547 widthUnitsLC->setEnabled(false);
549 // halign is only allowed if a width is used
550 halignCO->setEnabled(widthCB->isChecked());
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(toqstr("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"