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 widthCB->setChecked(itype != "none");
147 pagebreakCB->setChecked(false);
152 void GuiBox::initDialog()
154 setInnerType(true, toqstr("minipage"));
155 widthED->setText("100");
156 widthCB->setChecked(true);
157 widthCB->setEnabled(false);
158 widthUnitsLC->setCurrentItem(Length::PCW);
159 heightED->setText("1");
160 heightUnitsLC->setCurrentItem("totalheight");
164 void GuiBox::on_widthCB_stateChanged(int)
170 void GuiBox::on_heightCB_stateChanged(int /*state*/)
176 void GuiBox::on_pagebreakCB_stateChanged()
178 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
180 widthCB->setChecked(!pbreak);
182 on_typeCO_activated(typeCO->currentIndex());
190 void GuiBox::paramsToDialog(Inset const * inset)
192 InsetBox const * box = static_cast<InsetBox const *>(inset);
193 InsetBoxParams const & params = box->params();
194 QString type = toqstr(params.type);
195 if (type == "Framed") {
196 pagebreakCB->setChecked(true);
199 pagebreakCB->setChecked(false);
202 typeCO->setCurrentIndex(typeCO->findData(type));
205 QString inner_type = "minipage";
206 if (!params.inner_box)
208 if (params.use_parbox)
209 inner_type = "parbox";
210 if (params.use_makebox)
211 inner_type = "makebox";
212 bool const frameless = (params.type == "Frameless");
213 setInnerType(frameless, inner_type);
216 valignCO->setCurrentIndex(string("tcb").find(c, 0));
217 c = params.inner_pos;
218 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
220 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
222 bool ibox = (params.inner_box && !params.use_makebox);
223 valignCO->setEnabled(ibox);
224 ialignCO->setEnabled(ibox);
227 // halign is only allowed without inner box and if a width is used and if
228 // pagebreak is not used
229 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
230 && ((!ibox && type == "Boxed") || params.use_makebox));
231 // pagebreak is only allowed for Boxed without inner box
232 pagebreakCB->setEnabled(!ibox && type == "Boxed");
234 Length::UNIT const default_unit = Length::defaultUnit();
236 // the width can only be selected for makebox or framebox
237 widthCB->setEnabled(inner_type == "makebox"
239 && !ibox && !pagebreakCB->isChecked()));
240 if (params.width.empty()) {
241 widthCB->setChecked(false);
242 lengthToWidgets(widthED, widthUnitsLC,
243 params.width, default_unit);
245 widthCB->setChecked(true);
246 lengthToWidgets(widthED, widthUnitsLC,
247 params.width, default_unit);
248 QString const special = toqstr(params.special);
249 if (!special.isEmpty() && special != "none")
250 widthUnitsLC->setCurrentItem(special);
253 widthED->setEnabled(widthCB->isChecked());
254 widthUnitsLC->setEnabled(widthCB->isChecked());
256 lengthToWidgets(heightED, heightUnitsLC,
257 (params.height).asString(), default_unit);
259 QString const height_special = toqstr(params.height_special);
260 if (!height_special.isEmpty() && height_special != "none")
261 heightUnitsLC->setCurrentItem(height_special);
262 // set no optional height if the value is the default "1\height"
263 // (special units like \height are handled as "in",
264 // FIXME: this is a very bad UI, this check box should be disabled in
265 // this case, not forced to 'unchecked' state.
266 if (height_special == "totalheight" && params.height == Length("1in"))
267 heightCB->setCheckState(Qt::Unchecked);
269 heightCB->setCheckState(Qt::Checked);
271 heightCB->setEnabled(ibox);
275 docstring GuiBox::dialogToParams() const
277 bool const pagebreak =
278 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
283 box_type = fromqstr(typeCO->itemData(
284 typeCO->currentIndex()).toString());
286 InsetBoxParams params(box_type);
288 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
290 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
292 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
294 params.pos = "tcb"[valignCO->currentIndex()];
295 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
296 params.hor_pos = "lcrs"[halignCO->currentIndex()];
299 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
300 QString value = widthED->text();
302 if (widthED->isEnabled()) {
303 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
304 params.special = fromqstr(unit);
305 // Note: the unit is simply ignored in this case
306 params.width = Length(value.toDouble(), Length::IN);
308 params.special = "none";
309 // we must specify a valid length in this case
311 widthED->setText("0");
312 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
315 params.special = "none";
316 params.width = Length();
319 // the height parameter is omitted if the value
320 // is "1in" and "Total Height" is used as unit.
321 // 1in + "Total Height" means "1\height" which is the LaTeX default
322 // if no height is given
323 if (heightCB->checkState() == Qt::Unchecked) {
324 params.height = Length("1in");
325 params.height_special = "totalheight";
327 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
328 value = heightED->text();
329 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
330 params.height_special = fromqstr(unit);
331 // Note: the unit is simply ignored in this case
332 params.height = Length(value.toDouble(), Length::IN);
334 params.height_special = "none";
336 Length(widgetsToLength(heightED, heightUnitsLC));
339 return from_ascii(InsetBox::params2string(params));
343 bool GuiBox::checkWidgets(bool readonly) const
345 typeCO->setEnabled(!readonly);
348 pagebreakCB->setEnabled(false);
349 innerBoxCO->setEnabled(false);
350 valignCO->setEnabled(false);
351 ialignCO->setEnabled(false);
352 halignCO->setEnabled(false);
353 widthCB->setEnabled(false);
354 widthED->setEnabled(false);
355 widthUnitsLC->setEnabled(false);
356 heightED->setEnabled(false);
357 heightUnitsLC->setEnabled(false);
358 heightCB->setEnabled(false);
360 QString const outer =
361 typeCO->itemData(typeCO->currentIndex()).toString();
362 QString const itype =
363 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
364 bool const ibox = (itype != "none" && itype != "makebox");
365 // pagebreak is only allowed for Boxed without inner box
366 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
367 innerBoxCO->setEnabled(!pagebreakCB->isChecked());
368 valignCO->setEnabled(ibox);
369 ialignCO->setEnabled(ibox);
370 // halign is only allowed without inner box and if a width is used and if
371 // pagebreak is not used
372 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
373 && ((!ibox && outer == "Boxed") || itype == "makebox"));
374 // the width can only be selected for makebox or framebox
375 widthCB->setEnabled(itype == "makebox"
377 && !ibox && !pagebreakCB->isChecked()));
378 // except for frameless and boxed, the width cannot be specified if
379 // there is no inner box
380 bool const width_enabled =
381 ibox || outer == "Frameless" || outer == "Boxed";
382 // enable if width_enabled, except if checkbox is active but unset
383 widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
384 widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
385 if (!widthCB->isChecked() && widthCB->isEnabled()) {
386 widthED->setEnabled(false);
387 widthUnitsLC->setEnabled(false);
389 heightED->setEnabled(itype != "none" && heightCB->isChecked());
390 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
391 heightCB->setEnabled(ibox);
394 return InsetParamsWidget::checkWidgets();
398 void GuiBox::setSpecial(bool ibox)
400 QString const last_item =
401 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
403 // check if the widget contains the special units
404 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
405 // insert 'em if needed...
406 if (!ibox && !has_special) {
407 for (int i = 1; i < ids_spec_.size(); ++i)
408 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
409 // ... or remove 'em if needed
410 } else if (ibox && has_special) {
411 for (int i = 1; i < ids_spec_.size(); ++i) {
412 int n = widthUnitsLC->findData(ids_spec_[i]);
414 widthUnitsLC->removeItem(n);
417 // restore selected text, if possible
418 widthUnitsLC->setCurrentItem(last_item);
422 void GuiBox::setInnerType(bool frameless, QString const & type)
424 // with "frameless" boxes, inner box is mandatory
425 // (i.e. is the actual box)
426 // we have to remove "none" then and adjust the combo
429 innerBoxCO->addItem(qt_("None"), toqstr("none"));
431 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
432 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
433 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
434 int i = (innerBoxCO->findData(type) != -1)
435 ? innerBoxCO->findData(type) : 0;
436 innerBoxCO->setCurrentIndex(i);
439 } // namespace frontend
443 #include "moc_GuiBox.cpp"