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("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);
235 void GuiBox::initDialog()
237 setInnerType(true, toqstr("minipage"));
238 widthED->setText("100");
239 widthCB->setChecked(true);
240 widthCB->setEnabled(false);
241 widthUnitsLC->setCurrentItem(Length::PCW);
242 heightED->setText("1");
243 heightUnitsLC->setCurrentItem("totalheight");
244 // LaTeX's default for \fboxrule is 0.4 pt
245 thicknessED->setText("0.4");
246 thicknessUnitsLC->setCurrentItem(Length::PT);
247 // LaTeX's default for \fboxsep is 3 pt
248 separationED->setText("3");
249 separationUnitsLC->setCurrentItem(Length::PT);
250 // LaTeX's default for \shadowsize is 4 pt
251 shadowsizeED->setText("4");
252 shadowsizeUnitsLC->setCurrentItem(Length::PT);
253 // the default color is black and none
254 frameColorCO->setCurrentIndex(findPos2nd(color, qt_("black")) - 1);
255 backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_("none")));
259 void GuiBox::on_widthCB_stateChanged(int)
265 void GuiBox::on_heightCB_stateChanged(int /*state*/)
271 void GuiBox::on_pagebreakCB_stateChanged()
273 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
275 widthCB->setChecked(!pbreak);
277 on_typeCO_activated(typeCO->currentIndex());
285 void GuiBox::paramsToDialog(Inset const * inset)
287 InsetBox const * box = static_cast<InsetBox const *>(inset);
288 InsetBoxParams const & params = box->params();
289 QString type = toqstr(params.type);
290 if (type == "Framed") {
291 pagebreakCB->setChecked(true);
294 pagebreakCB->setChecked(false);
297 typeCO->setCurrentIndex(typeCO->findData(type));
300 QString inner_type = "minipage";
301 if (!params.inner_box)
303 if (params.use_parbox)
304 inner_type = "parbox";
305 if (params.use_makebox)
306 inner_type = "makebox";
307 bool const frameless = (params.type == "Frameless");
308 setInnerType(frameless, inner_type);
311 valignCO->setCurrentIndex(string("tcb").find(c, 0));
312 c = params.inner_pos;
313 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
315 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
317 bool ibox = (params.inner_box && !params.use_makebox);
318 valignCO->setEnabled(ibox);
319 ialignCO->setEnabled(ibox);
322 // halign is only allowed if a width is used
323 halignCO->setEnabled(widthCB->isChecked());
324 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
325 if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
326 && halignCO->count() < 4)
327 halignCO->addItem(toqstr("Stretch"));
328 else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
329 halignCO->removeItem(3);
330 // pagebreak is only allowed for Boxed without inner box
331 pagebreakCB->setEnabled(!ibox && type == "Boxed");
333 Length::UNIT const default_unit = Length::defaultUnit();
335 // the width can only be selected for makebox or framebox
336 widthCB->setEnabled(inner_type == "makebox"
338 && !ibox && !pagebreakCB->isChecked()));
339 if (params.width.empty()) {
340 widthCB->setChecked(false);
341 lengthToWidgets(widthED, widthUnitsLC,
342 params.width, default_unit);
344 widthCB->setChecked(true);
345 lengthToWidgets(widthED, widthUnitsLC,
346 params.width, default_unit);
347 QString const special = toqstr(params.special);
348 if (!special.isEmpty() && special != "none")
349 widthUnitsLC->setCurrentItem(special);
352 widthED->setEnabled(widthCB->isChecked());
353 widthUnitsLC->setEnabled(widthCB->isChecked());
355 lengthToWidgets(heightED, heightUnitsLC,
356 (params.height).asString(), default_unit);
358 QString const height_special = toqstr(params.height_special);
359 if (!height_special.isEmpty() && height_special != "none")
360 heightUnitsLC->setCurrentItem(height_special);
361 // set no optional height if the value is the default "1\height"
362 // (special units like \height are handled as "in",
363 // FIXME: this is a very bad UI, this check box should be disabled in
364 // this case, not forced to 'unchecked' state.
365 if (height_special == "totalheight" && params.height == Length("1in"))
366 heightCB->setCheckState(Qt::Unchecked);
368 heightCB->setCheckState(Qt::Checked);
370 heightCB->setEnabled(ibox);
372 // enable line thickness only for the rectangular frame types and drop shadow
373 thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
374 thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
375 lengthToWidgets(thicknessED, thicknessUnitsLC,
376 (params.thickness).asString(), default_unit);
377 // enable line separation for the allowed frame types
378 separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
379 || type == "Doublebox" || type == "Shadowbox");
380 separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
381 || type == "Doublebox" || type == "Shadowbox");
382 lengthToWidgets(separationED, separationUnitsLC,
383 (params.separation).asString(), default_unit);
384 // enable shadow size for drop shadow
385 shadowsizeED->setEnabled(type == "Shadowbox");
386 shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
387 lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
388 (params.shadowsize).asString(), default_unit);
390 frameColorCO->setCurrentIndex(findPos2nd(color, qt_(params.framecolor)) - 1);
391 backgroundColorCO->setCurrentIndex(findPos2nd(color, qt_(params.backgroundcolor)));
395 docstring GuiBox::dialogToParams() const
397 bool const pagebreak =
398 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
403 box_type = fromqstr(typeCO->itemData(
404 typeCO->currentIndex()).toString());
406 InsetBoxParams params(box_type);
408 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
410 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
412 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
414 params.pos = "tcb"[valignCO->currentIndex()];
415 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
416 params.hor_pos = "lcrs"[halignCO->currentIndex()];
419 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
420 QString value = widthED->text();
422 if (widthED->isEnabled()) {
423 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
424 params.special = fromqstr(unit);
425 // Note: the unit is simply ignored in this case
426 params.width = Length(value.toDouble(), Length::IN);
428 params.special = "none";
429 // we must specify a valid length in this case
431 widthED->setText("0");
432 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
435 params.special = "none";
436 params.width = Length();
439 // the height parameter is omitted if the value
440 // is "1in" and "Total Height" is used as unit.
441 // 1in + "Total Height" means "1\height" which is the LaTeX default
442 // if no height is given
443 if (heightCB->checkState() == Qt::Unchecked) {
444 params.height = Length("1in");
445 params.height_special = "totalheight";
447 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
448 value = heightED->text();
449 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
450 params.height_special = fromqstr(unit);
451 // Note: the unit is simply ignored in this case
452 params.height = Length(value.toDouble(), Length::IN);
454 params.height_special = "none";
456 Length(widgetsToLength(heightED, heightUnitsLC));
460 // handle the line thickness, line separation and shadow size
461 if (thicknessED->isEnabled())
462 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
464 params.thickness = Length("0.4pt");
465 if (separationED->isEnabled())
466 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
468 params.separation = Length("3pt");
469 if (separationED->isEnabled())
470 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
472 params.shadowsize = Length("4pt");
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 if a width is used
526 halignCO->setEnabled(widthCB->isChecked());
527 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
528 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
529 && halignCO->count() < 4)
530 halignCO->addItem(toqstr("Stretch"));
531 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
532 halignCO->removeItem(3);
533 // pagebreak is only allowed for Boxed without inner box
534 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
536 heightED->setEnabled(itype != "none" && heightCB->isChecked());
537 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
538 heightCB->setEnabled(ibox);
540 // enable line thickness for the rectangular frame types and drop shadow
541 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
542 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
543 // set default values if empty
544 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
545 thicknessED->setText("0.4");
546 thicknessUnitsLC->setCurrentItem(Length::PT);
548 // enable line separation for the allowed frame types
549 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
550 || outer == "Doublebox" || outer == "Shadowbox");
551 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
552 || outer == "Doublebox" || outer == "Shadowbox");
553 // set default values if empty
554 if (separationED->text().isEmpty() && separationED->isEnabled()) {
555 separationED->setText("3");
556 separationUnitsLC->setCurrentItem(Length::PT);
558 // enable shadow size for drop shadow
559 shadowsizeED->setEnabled(outer == "Shadowbox");
560 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
561 // set default values if empty
562 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
563 shadowsizeED->setText("4");
564 shadowsizeUnitsLC->setCurrentItem(Length::PT);
568 return InsetParamsWidget::checkWidgets();
572 void GuiBox::setSpecial(bool ibox)
574 QString const last_item =
575 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
577 // check if the widget contains the special units
578 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
579 // insert 'em if needed...
580 if (!ibox && !has_special) {
581 for (int i = 1; i < ids_spec_.size(); ++i)
582 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
583 // ... or remove 'em if needed
584 } else if (ibox && has_special) {
585 for (int i = 1; i < ids_spec_.size(); ++i) {
586 int n = widthUnitsLC->findData(ids_spec_[i]);
588 widthUnitsLC->removeItem(n);
591 // restore selected text, if possible
592 widthUnitsLC->setCurrentItem(last_item);
596 void GuiBox::setInnerType(bool frameless, QString const & type)
598 // with "frameless" boxes, inner box is mandatory
599 // (i.e. is the actual box)
600 // we have to remove "none" then and adjust the combo
603 innerBoxCO->addItem(qt_("None"), toqstr("none"));
605 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
606 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
607 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
608 int i = (innerBoxCO->findData(type) != -1)
609 ? innerBoxCO->findData(type) : 0;
610 innerBoxCO->setCurrentIndex(i);
613 } // namespace frontend
617 #include "moc_GuiBox.cpp"