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"
20 #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/foreach.h"
29 #include "support/lstrings.h"
33 #include <QPushButton>
45 static QStringList boxGuiIds()
48 << "Frameless" << "Boxed"
49 << "ovalbox" << "Ovalbox"
50 << "Shadowbox" << "Shaded"
55 static QStringList boxGuiNames()
58 << qt_("No frame") << qt_("Simple rectangular frame")
59 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
60 << qt_("Drop shadow") << qt_("Shaded background")
61 << qt_("Double rectangular frame");
65 static QStringList boxGuiSpecialLengthIds()
67 return QStringList() << "height" << "depth"
68 << "totalheight" << "width";
72 static QStringList boxGuiSpecialLengthNames()
74 return QStringList() << qt_("Height") << qt_("Depth")
75 << qt_("Total Height") << qt_("Width");
79 static QList<ColorPair> colorData()
81 QList<ColorPair> colors;
82 colors << ColorPair(qt_("none"), Color_none);
83 colors << ColorPair(qt_("black"), Color_black);
84 colors << ColorPair(qt_("white"), Color_white);
85 colors << ColorPair(qt_("blue"), Color_blue);
86 colors << ColorPair(qt_("brown"), Color_brown);
87 colors << ColorPair(qt_("cyan"), Color_cyan);
88 colors << ColorPair(qt_("darkgray"), Color_darkgray);
89 colors << ColorPair(qt_("gray"), Color_gray);
90 colors << ColorPair(qt_("green"), Color_green);
91 colors << ColorPair(qt_("lightgray"), Color_lightgray);
92 colors << ColorPair(qt_("lime"), Color_lime);
93 colors << ColorPair(qt_("magenta"), Color_magenta);
94 colors << ColorPair(qt_("olive"), Color_olive);
95 colors << ColorPair(qt_("orange"), Color_orange);
96 colors << ColorPair(qt_("pink"), Color_pink);
97 colors << ColorPair(qt_("purple"), Color_purple);
98 colors << ColorPair(qt_("red"), Color_red);
99 colors << ColorPair(qt_("teal"), Color_teal);
100 colors << ColorPair(qt_("violet"), Color_violet);
101 colors << ColorPair(qt_("yellow"), Color_yellow);
107 void fillComboColor(QComboBox * combo, QList<T> const & list, bool const is_none)
109 QPixmap coloritem(32, 32);
111 // frameColorCO cannot be uncolored
113 combo->addItem(qt_("none"));
114 typename QList<T>::const_iterator cit = list.begin() + 1;
115 for (; cit != list.end(); ++cit) {
116 color = QColor(guiApp->colorCache().get(cit->second, false));
117 coloritem.fill(color);
118 combo->addItem(QIcon(coloritem), cit->first);
124 static int findPos2nd(QList<P> const & vec, QString val)
126 for (int i = 0; i != vec.size(); ++i)
127 if (vec[i].first == val)
133 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
137 // fill the box type choice
139 gui_names_ = boxGuiNames();
140 for (int i = 0; i != ids_.size(); ++i)
141 typeCO->addItem(gui_names_[i], ids_[i]);
143 // add the special units to the height choice
144 // width needs different handling
145 ids_spec_ = boxGuiSpecialLengthIds();
146 gui_names_spec_ = boxGuiSpecialLengthNames();
147 for (int i = 0; i != ids_spec_.size(); ++i)
148 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
150 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
151 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
152 this, SIGNAL(changed()));
153 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
154 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
155 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
156 this, SIGNAL(changed()));
157 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
158 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
159 connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
160 connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
161 this, SIGNAL(changed()));
162 connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
163 connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
164 this, SIGNAL(changed()));
165 connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
166 connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
167 this, SIGNAL(changed()));
168 connect(frameColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
169 connect(backgroundColorCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
171 heightED->setValidator(unsignedLengthValidator(heightED));
172 widthED->setValidator(unsignedLengthValidator(widthED));
173 thicknessED->setValidator(unsignedLengthValidator(thicknessED));
174 separationED->setValidator(unsignedLengthValidator(separationED));
175 shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
177 // initialize the length validator
178 addCheckedWidget(widthED, widthCB);
179 addCheckedWidget(heightED, heightCB);
180 addCheckedWidget(thicknessED, thicknessLA);
181 addCheckedWidget(separationED, separationLA);
182 addCheckedWidget(shadowsizeED, shadowsizeLA);
186 // the background can be uncolored while the frame cannot
187 fillComboColor(frameColorCO, color, false);
188 fillComboColor(backgroundColorCO, color, true);
194 void GuiBox::on_innerBoxCO_activated(int /* index */)
197 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
198 // handle parbox and minipage the same way
199 bool const ibox = (itype != "none" && itype != "makebox");
200 if (heightCB->isChecked() && !ibox)
201 heightCB->setChecked(false);
202 widthCB->setChecked(!widthED->text().isEmpty());
208 void GuiBox::on_typeCO_activated(int index)
211 typeCO->itemData(index).toString();
212 bool const frameless = (type == "Frameless");
214 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
215 setInnerType(frameless, itype);
216 // refresh itype because it might have been changed in setInnerType
218 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
219 // handle parbox and minipage the same way
220 bool const ibox = (itype != "none" && itype != "makebox");
221 if (frameless && itype != "makebox") {
222 if (heightCB->isChecked() && !ibox)
223 heightCB->setChecked(false);
226 if (type != "Boxed") {
227 if (type != "Frameless")
228 widthCB->setChecked(itype != "none");
229 pagebreakCB->setChecked(false);
231 // assure that the frame color is black for frameless boxes to
232 // provide the color "none"
233 if (frameless && frameColorCO->currentText() != qt_("black"))
234 frameColorCO->setCurrentIndex(0);
239 void GuiBox::on_frameColorCO_currentIndexChanged(int /* index */)
241 // if there is a special frme color the background canot be uncolored
242 if (frameColorCO->currentText() != qt_("black")) {
243 if (backgroundColorCO->currentText() == qt_("none"))
244 backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_("white")));
245 if (backgroundColorCO->itemText(0) == qt_("none"))
246 backgroundColorCO->removeItem(0);
248 if (backgroundColorCO->itemText(0) != qt_("none"))
249 backgroundColorCO->insertItem(0, qt_("none"));
255 void GuiBox::initDialog()
257 setInnerType(true, toqstr("minipage"));
258 widthED->setText("100");
259 widthCB->setChecked(true);
260 widthCB->setEnabled(false);
261 widthUnitsLC->setCurrentItem(Length::PCW);
262 heightED->setText("1");
263 heightUnitsLC->setCurrentItem("totalheight");
264 // LaTeX's default for \fboxrule is 0.4 pt
265 thicknessED->setText("0.4");
266 thicknessUnitsLC->setCurrentItem(Length::PT);
267 // LaTeX's default for \fboxsep is 3 pt
268 separationED->setText("3");
269 separationUnitsLC->setCurrentItem(Length::PT);
270 // LaTeX's default for \shadowsize is 4 pt
271 shadowsizeED->setText("4");
272 shadowsizeUnitsLC->setCurrentItem(Length::PT);
273 // the default color is black and none
274 frameColorCO->setCurrentIndex(findPos2nd(color, qt_("black")) - 1);
275 backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_("none")));
279 void GuiBox::on_widthCB_stateChanged(int)
285 void GuiBox::on_heightCB_stateChanged(int /*state*/)
291 void GuiBox::on_pagebreakCB_stateChanged()
293 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
295 widthCB->setChecked(!pbreak);
297 on_typeCO_activated(typeCO->currentIndex());
305 void GuiBox::paramsToDialog(Inset const * inset)
307 InsetBox const * box = static_cast<InsetBox const *>(inset);
308 InsetBoxParams const & params = box->params();
309 QString type = toqstr(params.type);
310 if (type == "Framed") {
311 pagebreakCB->setChecked(true);
314 pagebreakCB->setChecked(false);
317 typeCO->setCurrentIndex(typeCO->findData(type));
320 QString inner_type = "minipage";
321 if (!params.inner_box)
323 if (params.use_parbox)
324 inner_type = "parbox";
325 if (params.use_makebox)
326 inner_type = "makebox";
327 bool const frameless = (params.type == "Frameless");
328 setInnerType(frameless, inner_type);
331 valignCO->setCurrentIndex(string("tcb").find(c, 0));
332 c = params.inner_pos;
333 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
335 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
337 bool ibox = (params.inner_box && !params.use_makebox);
338 valignCO->setEnabled(ibox);
339 ialignCO->setEnabled(ibox);
342 // halign is only allowed if a width is used
343 halignCO->setEnabled(widthCB->isChecked());
344 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
345 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
346 && halignCO->count() < 4)
347 halignCO->addItem(toqstr("Stretch"));
348 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
349 halignCO->removeItem(3);
350 // pagebreak is only allowed for Boxed without inner box
351 pagebreakCB->setEnabled(!ibox && type == "Boxed");
353 Length::UNIT const default_unit = Length::defaultUnit();
355 // the width can only be selected for makebox or framebox
356 widthCB->setEnabled(inner_type == "makebox"
358 && !ibox && !pagebreakCB->isChecked()));
359 if (params.width.empty()) {
360 widthCB->setChecked(false);
361 lengthToWidgets(widthED, widthUnitsLC,
362 params.width, default_unit);
364 widthCB->setChecked(true);
365 lengthToWidgets(widthED, widthUnitsLC,
366 params.width, default_unit);
367 QString const special = toqstr(params.special);
368 if (!special.isEmpty() && special != "none")
369 widthUnitsLC->setCurrentItem(special);
372 widthED->setEnabled(widthCB->isChecked());
373 widthUnitsLC->setEnabled(widthCB->isChecked());
375 lengthToWidgets(heightED, heightUnitsLC,
376 (params.height).asString(), default_unit);
378 QString const height_special = toqstr(params.height_special);
379 if (!height_special.isEmpty() && height_special != "none")
380 heightUnitsLC->setCurrentItem(height_special);
381 // set no optional height if the value is the default "1\height"
382 // (special units like \height are handled as "in",
383 // FIXME: this is a very bad UI, this check box should be disabled in
384 // this case, not forced to 'unchecked' state.
385 if (height_special == "totalheight" && params.height == Length("1in"))
386 heightCB->setCheckState(Qt::Unchecked);
388 heightCB->setCheckState(Qt::Checked);
390 heightCB->setEnabled(ibox);
392 // enable line thickness only for the rectangular frame types and drop shadow
393 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
394 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
395 lengthToWidgets(thicknessED, thicknessUnitsLC,
396 (params.thickness).asString(), default_unit);
397 // enable line separation for the allowed frame types
398 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
399 || type == "Doublebox" || type == "Shadowbox");
400 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
401 || type == "Doublebox" || type == "Shadowbox");
402 lengthToWidgets(separationED, separationUnitsLC,
403 (params.separation).asString(), default_unit);
404 // enable shadow size for drop shadow
405 shadowsizeED->setEnabled(type == "Shadowbox");
406 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
407 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
408 (params.shadowsize).asString(), default_unit);
410 frameColorCO->setCurrentIndex(findPos2nd(color, qt_(params.framecolor)) - 1);
411 if (frameColorCO->currentText() != qt_("black"))
412 backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_(params.backgroundcolor)) - 1);
414 backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_(params.backgroundcolor)));
418 docstring GuiBox::dialogToParams() const
420 bool const pagebreak =
421 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
426 box_type = fromqstr(typeCO->itemData(
427 typeCO->currentIndex()).toString());
429 InsetBoxParams params(box_type);
431 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
433 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
435 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
437 params.pos = "tcb"[valignCO->currentIndex()];
438 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
439 params.hor_pos = "lcrs"[halignCO->currentIndex()];
442 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
443 QString value = widthED->text();
445 if (widthED->isEnabled()) {
446 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
447 params.special = fromqstr(unit);
448 // Note: the unit is simply ignored in this case
449 params.width = Length(value.toDouble(), Length::IN);
451 params.special = "none";
452 // we must specify a valid length in this case
454 widthED->setText("0");
455 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
458 params.special = "none";
459 params.width = Length();
462 // the height parameter is omitted if the value
463 // is "1in" and "Total Height" is used as unit.
464 // 1in + "Total Height" means "1\height" which is the LaTeX default
465 // if no height is given
466 if (heightCB->checkState() == Qt::Unchecked) {
467 params.height = Length("1in");
468 params.height_special = "totalheight";
470 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
471 value = heightED->text();
472 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
473 params.height_special = fromqstr(unit);
474 // Note: the unit is simply ignored in this case
475 params.height = Length(value.toDouble(), Length::IN);
477 params.height_special = "none";
479 Length(widgetsToLength(heightED, heightUnitsLC));
483 // handle the line thickness, line separation and shadow size
484 if (thicknessED->isEnabled())
485 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
487 params.thickness = Length("0.4pt");
488 if (separationED->isEnabled())
489 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
491 params.separation = Length("3pt");
492 if (separationED->isEnabled())
493 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
495 params.shadowsize = Length("4pt");
496 if (frameColorCO->isEnabled())
497 params.framecolor = fromqstr(color[frameColorCO->currentIndex() + 1].first);
499 params.framecolor = "black";
500 if (backgroundColorCO->isEnabled()) {
501 // only if the framecolor is black the backgroundcolor has the entry "none"
502 if (frameColorCO->currentText() != qt_("black"))
503 params.backgroundcolor = fromqstr(color[backgroundColorCO->currentIndex() + 1].first);
505 params.backgroundcolor = fromqstr(color[backgroundColorCO->currentIndex()].first);
507 params.backgroundcolor = "none";
509 return from_ascii(InsetBox::params2string(params));
513 bool GuiBox::checkWidgets(bool readonly) const
515 typeCO->setEnabled(!readonly);
518 pagebreakCB->setEnabled(false);
519 innerBoxCO->setEnabled(false);
520 valignCO->setEnabled(false);
521 ialignCO->setEnabled(false);
522 halignCO->setEnabled(false);
523 widthCB->setEnabled(false);
524 widthED->setEnabled(false);
525 widthUnitsLC->setEnabled(false);
526 heightED->setEnabled(false);
527 heightUnitsLC->setEnabled(false);
528 heightCB->setEnabled(false);
529 thicknessED->setEnabled(false);
530 thicknessUnitsLC->setEnabled(false);
531 separationED->setEnabled(false);
532 separationUnitsLC->setEnabled(false);
533 shadowsizeED->setEnabled(false);
534 shadowsizeUnitsLC->setEnabled(false);
536 QString const outer =
537 typeCO->itemData(typeCO->currentIndex()).toString();
538 QString const itype =
539 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
540 bool const ibox = (itype != "none" && itype != "makebox");
541 valignCO->setEnabled(ibox);
542 ialignCO->setEnabled(ibox);
543 if (heightCB->isChecked() && !ibox)
544 heightCB->setChecked(false);
545 heightCB->setEnabled(ibox);
546 // the width can only be selected for makebox or framebox
547 widthCB->setEnabled(itype == "makebox"
548 || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
549 // except for Frameless and Boxed, the width cannot be specified if
550 // there is no inner box
551 bool const width_enabled =
552 ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
553 // enable if width_enabled
554 widthED->setEnabled(width_enabled);
555 widthUnitsLC->setEnabled(width_enabled);
556 if (!widthCB->isChecked() && widthCB->isEnabled()) {
557 widthED->setEnabled(false);
558 widthUnitsLC->setEnabled(false);
560 // halign is only allowed if a width is used
561 halignCO->setEnabled(widthCB->isChecked());
562 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
563 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
564 && halignCO->count() < 4)
565 halignCO->addItem(toqstr("Stretch"));
566 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
567 halignCO->removeItem(3);
568 // pagebreak is only allowed for Boxed without inner box
569 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
571 heightED->setEnabled(itype != "none" && heightCB->isChecked());
572 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
573 heightCB->setEnabled(ibox);
575 // enable line thickness for the rectangular frame types and drop shadow
576 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
577 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
578 // set default values if empty
579 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
580 thicknessED->setText("0.4");
581 thicknessUnitsLC->setCurrentItem(Length::PT);
583 // enable line separation for the allowed frame types
584 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
585 || outer == "Doublebox" || outer == "Shadowbox");
586 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
587 || outer == "Doublebox" || outer == "Shadowbox");
588 // set default values if empty
589 if (separationED->text().isEmpty() && separationED->isEnabled()) {
590 separationED->setText("3");
591 separationUnitsLC->setCurrentItem(Length::PT);
593 // enable shadow size for drop shadow
594 shadowsizeED->setEnabled(outer == "Shadowbox");
595 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
596 // set default values if empty
597 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
598 shadowsizeED->setText("4");
599 shadowsizeUnitsLC->setCurrentItem(Length::PT);
601 // \fboxcolor and \colorbox cannot be used for fancybox boxes
602 frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
603 backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
606 return InsetParamsWidget::checkWidgets();
610 void GuiBox::setSpecial(bool ibox)
612 QString const last_item =
613 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
615 // check if the widget contains the special units
616 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
617 // insert 'em if needed...
618 if (!ibox && !has_special) {
619 for (int i = 1; i < ids_spec_.size(); ++i)
620 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
621 // ... or remove 'em if needed
622 } else if (ibox && has_special) {
623 for (int i = 1; i < ids_spec_.size(); ++i) {
624 int n = widthUnitsLC->findData(ids_spec_[i]);
626 widthUnitsLC->removeItem(n);
629 // restore selected text, if possible
630 widthUnitsLC->setCurrentItem(last_item);
634 void GuiBox::setInnerType(bool frameless, QString const & type)
636 // with "frameless" boxes, inner box is mandatory
637 // (i.e. is the actual box)
638 // we have to remove "none" then and adjust the combo
641 innerBoxCO->addItem(qt_("None"), toqstr("none"));
643 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
644 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
645 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
646 int i = (innerBoxCO->findData(type) != -1)
647 ? innerBoxCO->findData(type) : 0;
648 innerBoxCO->setCurrentIndex(i);
651 } // namespace frontend
655 #include "moc_GuiBox.cpp"