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
10 * Full author contact details are available in file CREDITS.
17 #include "LengthCombo.h"
19 #include "qt_helpers.h"
20 #include "Validator.h"
22 #include "insets/InsetBox.h"
24 #include "support/gettext.h"
25 #include "support/foreach.h"
26 #include "support/lstrings.h"
28 #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 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
79 // fill the box type choice
81 gui_names_ = boxGuiNames();
82 for (int i = 0; i != ids_.size(); ++i)
83 typeCO->addItem(gui_names_[i], ids_[i]);
85 // add the special units to the height choice
86 // width needs different handling
87 ids_spec_ = boxGuiSpecialLengthIds();
88 gui_names_spec_ = boxGuiSpecialLengthNames();
89 for (int i = 0; i != ids_spec_.size(); ++i)
90 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
92 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
93 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
94 this, SIGNAL(changed()));
95 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
96 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
97 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
98 this, SIGNAL(changed()));
99 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
100 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
102 heightED->setValidator(unsignedLengthValidator(heightED));
103 widthED->setValidator(unsignedLengthValidator(widthED));
105 // initialize the length validator
106 addCheckedWidget(widthED, widthCB);
107 addCheckedWidget(heightED, heightCB);
113 void GuiBox::on_innerBoxCO_activated(int /* index */)
116 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
117 // handle parbox and minipage the same way
118 bool const ibox = (itype != "none" && itype != "makebox");
119 if (heightCB->isChecked() && !ibox)
120 heightCB->setChecked(false);
121 widthCB->setChecked(!widthED->text().isEmpty());
127 void GuiBox::on_typeCO_activated(int index)
130 typeCO->itemData(index).toString();
131 bool const frameless = (type == "Frameless");
133 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
134 setInnerType(frameless, itype);
135 // refresh itype because it might have been changed in setInnerType
137 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
138 // handle parbox and minipage the same way
139 bool const ibox = (itype != "none" && itype != "makebox");
140 if (frameless && itype != "makebox") {
141 if (heightCB->isChecked() && !ibox)
142 heightCB->setChecked(false);
145 if (type != "Boxed") {
146 if (type != "Frameless")
147 widthCB->setChecked(itype != "none");
148 pagebreakCB->setChecked(false);
154 void GuiBox::initDialog()
156 setInnerType(true, toqstr("minipage"));
157 widthED->setText("100");
158 widthCB->setChecked(true);
159 widthCB->setEnabled(false);
160 widthUnitsLC->setCurrentItem(Length::PCW);
161 heightED->setText("1");
162 heightUnitsLC->setCurrentItem("totalheight");
166 void GuiBox::on_widthCB_stateChanged(int)
172 void GuiBox::on_heightCB_stateChanged(int /*state*/)
178 void GuiBox::on_pagebreakCB_stateChanged()
180 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
182 widthCB->setChecked(!pbreak);
184 on_typeCO_activated(typeCO->currentIndex());
192 void GuiBox::paramsToDialog(Inset const * inset)
194 InsetBox const * box = static_cast<InsetBox const *>(inset);
195 InsetBoxParams const & params = box->params();
196 QString type = toqstr(params.type);
197 if (type == "Framed") {
198 pagebreakCB->setChecked(true);
201 pagebreakCB->setChecked(false);
204 typeCO->setCurrentIndex(typeCO->findData(type));
207 QString inner_type = "minipage";
208 if (!params.inner_box)
210 if (params.use_parbox)
211 inner_type = "parbox";
212 if (params.use_makebox)
213 inner_type = "makebox";
214 bool const frameless = (params.type == "Frameless");
215 setInnerType(frameless, inner_type);
218 valignCO->setCurrentIndex(string("tcb").find(c, 0));
219 c = params.inner_pos;
220 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
222 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
224 bool ibox = (params.inner_box && !params.use_makebox);
225 valignCO->setEnabled(ibox);
226 ialignCO->setEnabled(ibox);
229 // halign is only allowed without inner box and if a width is used and if
230 // pagebreak is not used
231 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
232 && ((!ibox && type == "Boxed") || params.use_makebox));
233 // pagebreak is only allowed for Boxed without inner box
234 pagebreakCB->setEnabled(!ibox && type == "Boxed");
236 Length::UNIT const default_unit = Length::defaultUnit();
238 // the width can only be selected for makebox or framebox
239 widthCB->setEnabled(inner_type == "makebox"
241 && !ibox && !pagebreakCB->isChecked()));
242 if (params.width.empty()) {
243 widthCB->setChecked(false);
244 lengthToWidgets(widthED, widthUnitsLC,
245 params.width, default_unit);
247 widthCB->setChecked(true);
248 lengthToWidgets(widthED, widthUnitsLC,
249 params.width, default_unit);
250 QString const special = toqstr(params.special);
251 if (!special.isEmpty() && special != "none")
252 widthUnitsLC->setCurrentItem(special);
255 widthED->setEnabled(widthCB->isChecked());
256 widthUnitsLC->setEnabled(widthCB->isChecked());
258 lengthToWidgets(heightED, heightUnitsLC,
259 (params.height).asString(), default_unit);
261 QString const height_special = toqstr(params.height_special);
262 if (!height_special.isEmpty() && height_special != "none")
263 heightUnitsLC->setCurrentItem(height_special);
264 // set no optional height if the value is the default "1\height"
265 // (special units like \height are handled as "in",
266 // FIXME: this is a very bad UI, this check box should be disabled in
267 // this case, not forced to 'unchecked' state.
268 if (height_special == "totalheight" && params.height == Length("1in"))
269 heightCB->setCheckState(Qt::Unchecked);
271 heightCB->setCheckState(Qt::Checked);
273 heightCB->setEnabled(ibox);
277 docstring GuiBox::dialogToParams() const
279 bool const pagebreak =
280 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
285 box_type = fromqstr(typeCO->itemData(
286 typeCO->currentIndex()).toString());
288 InsetBoxParams params(box_type);
290 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
292 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
294 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
296 params.pos = "tcb"[valignCO->currentIndex()];
297 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
298 params.hor_pos = "lcrs"[halignCO->currentIndex()];
301 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
302 QString value = widthED->text();
304 if (widthED->isEnabled()) {
305 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
306 params.special = fromqstr(unit);
307 // Note: the unit is simply ignored in this case
308 params.width = Length(value.toDouble(), Length::IN);
310 params.special = "none";
311 // we must specify a valid length in this case
313 widthED->setText("0");
314 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
317 params.special = "none";
318 params.width = Length();
321 // the height parameter is omitted if the value
322 // is "1in" and "Total Height" is used as unit.
323 // 1in + "Total Height" means "1\height" which is the LaTeX default
324 // if no height is given
325 if (heightCB->checkState() == Qt::Unchecked) {
326 params.height = Length("1in");
327 params.height_special = "totalheight";
329 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
330 value = heightED->text();
331 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
332 params.height_special = fromqstr(unit);
333 // Note: the unit is simply ignored in this case
334 params.height = Length(value.toDouble(), Length::IN);
336 params.height_special = "none";
338 Length(widgetsToLength(heightED, heightUnitsLC));
341 return from_ascii(InsetBox::params2string(params));
345 bool GuiBox::checkWidgets(bool readonly) const
347 typeCO->setEnabled(!readonly);
350 pagebreakCB->setEnabled(false);
351 innerBoxCO->setEnabled(false);
352 valignCO->setEnabled(false);
353 ialignCO->setEnabled(false);
354 halignCO->setEnabled(false);
355 widthCB->setEnabled(false);
356 widthED->setEnabled(false);
357 widthUnitsLC->setEnabled(false);
358 heightED->setEnabled(false);
359 heightUnitsLC->setEnabled(false);
360 heightCB->setEnabled(false);
362 QString const outer =
363 typeCO->itemData(typeCO->currentIndex()).toString();
364 QString const itype =
365 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
366 bool const ibox = (itype != "none" && itype != "makebox");
367 // pagebreak is only allowed for Boxed without inner box
368 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
369 innerBoxCO->setEnabled(!pagebreakCB->isChecked());
370 valignCO->setEnabled(ibox);
371 ialignCO->setEnabled(ibox);
372 // halign is only allowed without inner box and if a width is used and if
373 // pagebreak is not used
374 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
375 && ((!ibox && outer == "Boxed") || itype == "makebox"));
376 // the width can only be selected for makebox or framebox
377 widthCB->setEnabled(itype == "makebox"
379 && !ibox && !pagebreakCB->isChecked()));
380 // except for frameless and boxed, the width cannot be specified if
381 // there is no inner box
382 bool const width_enabled =
383 ibox || outer == "Frameless" || outer == "Boxed";
384 // enable if width_enabled, except if checkbox is active but unset
385 widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
386 widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
387 if (!widthCB->isChecked() && widthCB->isEnabled()) {
388 widthED->setEnabled(false);
389 widthUnitsLC->setEnabled(false);
391 heightED->setEnabled(itype != "none" && heightCB->isChecked());
392 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
393 heightCB->setEnabled(ibox);
396 return InsetParamsWidget::checkWidgets();
400 void GuiBox::setSpecial(bool ibox)
402 QString const last_item =
403 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
405 // check if the widget contains the special units
406 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
407 // insert 'em if needed...
408 if (!ibox && !has_special) {
409 for (int i = 1; i < ids_spec_.size(); ++i)
410 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
411 // ... or remove 'em if needed
412 } else if (ibox && has_special) {
413 for (int i = 1; i < ids_spec_.size(); ++i) {
414 int n = widthUnitsLC->findData(ids_spec_[i]);
416 widthUnitsLC->removeItem(n);
419 // restore selected text, if possible
420 widthUnitsLC->setCurrentItem(last_item);
424 void GuiBox::setInnerType(bool frameless, QString const & type)
426 // with "frameless" boxes, inner box is mandatory
427 // (i.e. is the actual box)
428 // we have to remove "none" then and adjust the combo
431 innerBoxCO->addItem(qt_("None"), toqstr("none"));
433 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
434 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
435 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
436 int i = (innerBoxCO->findData(type) != -1)
437 ? innerBoxCO->findData(type) : 0;
438 innerBoxCO->setCurrentIndex(i);
441 } // namespace frontend
445 #include "moc_GuiBox.cpp"