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);
146 if (type != "Frameless")
147 widthCB->setChecked(itype != "none");
148 pagebreakCB->setChecked(false);
153 void GuiBox::initDialog()
155 setInnerType(true, toqstr("minipage"));
156 widthED->setText("100");
157 widthCB->setChecked(true);
158 widthCB->setEnabled(false);
159 widthUnitsLC->setCurrentItem(Length::PCW);
160 heightED->setText("1");
161 heightUnitsLC->setCurrentItem("totalheight");
165 void GuiBox::on_widthCB_stateChanged(int)
171 void GuiBox::on_heightCB_stateChanged(int /*state*/)
177 void GuiBox::on_pagebreakCB_stateChanged()
179 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
181 widthCB->setChecked(!pbreak);
183 on_typeCO_activated(typeCO->currentIndex());
191 void GuiBox::paramsToDialog(Inset const * inset)
193 InsetBox const * box = static_cast<InsetBox const *>(inset);
194 InsetBoxParams const & params = box->params();
195 QString type = toqstr(params.type);
196 if (type == "Framed") {
197 pagebreakCB->setChecked(true);
200 pagebreakCB->setChecked(false);
203 typeCO->setCurrentIndex(typeCO->findData(type));
206 QString inner_type = "minipage";
207 if (!params.inner_box)
209 if (params.use_parbox)
210 inner_type = "parbox";
211 if (params.use_makebox)
212 inner_type = "makebox";
213 bool const frameless = (params.type == "Frameless");
214 setInnerType(frameless, inner_type);
217 valignCO->setCurrentIndex(string("tcb").find(c, 0));
218 c = params.inner_pos;
219 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
221 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
223 bool ibox = (params.inner_box && !params.use_makebox);
224 valignCO->setEnabled(ibox);
225 ialignCO->setEnabled(ibox);
228 // halign is only allowed without inner box and if a width is used and if
229 // pagebreak is not used
230 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
231 && ((!ibox && type == "Boxed") || params.use_makebox));
232 // pagebreak is only allowed for Boxed without inner box
233 pagebreakCB->setEnabled(!ibox && type == "Boxed");
235 Length::UNIT const default_unit = Length::defaultUnit();
237 // the width can only be selected for makebox or framebox
238 widthCB->setEnabled(inner_type == "makebox"
240 && !ibox && !pagebreakCB->isChecked()));
241 if (params.width.empty()) {
242 widthCB->setChecked(false);
243 lengthToWidgets(widthED, widthUnitsLC,
244 params.width, default_unit);
246 widthCB->setChecked(true);
247 lengthToWidgets(widthED, widthUnitsLC,
248 params.width, default_unit);
249 QString const special = toqstr(params.special);
250 if (!special.isEmpty() && special != "none")
251 widthUnitsLC->setCurrentItem(special);
254 widthED->setEnabled(widthCB->isChecked());
255 widthUnitsLC->setEnabled(widthCB->isChecked());
257 lengthToWidgets(heightED, heightUnitsLC,
258 (params.height).asString(), default_unit);
260 QString const height_special = toqstr(params.height_special);
261 if (!height_special.isEmpty() && height_special != "none")
262 heightUnitsLC->setCurrentItem(height_special);
263 // set no optional height if the value is the default "1\height"
264 // (special units like \height are handled as "in",
265 // FIXME: this is a very bad UI, this check box should be disabled in
266 // this case, not forced to 'unchecked' state.
267 if (height_special == "totalheight" && params.height == Length("1in"))
268 heightCB->setCheckState(Qt::Unchecked);
270 heightCB->setCheckState(Qt::Checked);
272 heightCB->setEnabled(ibox);
276 docstring GuiBox::dialogToParams() const
278 bool const pagebreak =
279 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
284 box_type = fromqstr(typeCO->itemData(
285 typeCO->currentIndex()).toString());
287 InsetBoxParams params(box_type);
289 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
291 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
293 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
295 params.pos = "tcb"[valignCO->currentIndex()];
296 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
297 params.hor_pos = "lcrs"[halignCO->currentIndex()];
300 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
301 QString value = widthED->text();
303 if (widthED->isEnabled()) {
304 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
305 params.special = fromqstr(unit);
306 // Note: the unit is simply ignored in this case
307 params.width = Length(value.toDouble(), Length::IN);
309 params.special = "none";
310 // we must specify a valid length in this case
312 widthED->setText("0");
313 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
316 params.special = "none";
317 params.width = Length();
320 // the height parameter is omitted if the value
321 // is "1in" and "Total Height" is used as unit.
322 // 1in + "Total Height" means "1\height" which is the LaTeX default
323 // if no height is given
324 if (heightCB->checkState() == Qt::Unchecked) {
325 params.height = Length("1in");
326 params.height_special = "totalheight";
328 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
329 value = heightED->text();
330 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
331 params.height_special = fromqstr(unit);
332 // Note: the unit is simply ignored in this case
333 params.height = Length(value.toDouble(), Length::IN);
335 params.height_special = "none";
337 Length(widgetsToLength(heightED, heightUnitsLC));
340 return from_ascii(InsetBox::params2string(params));
344 bool GuiBox::checkWidgets(bool readonly) const
346 typeCO->setEnabled(!readonly);
349 pagebreakCB->setEnabled(false);
350 innerBoxCO->setEnabled(false);
351 valignCO->setEnabled(false);
352 ialignCO->setEnabled(false);
353 halignCO->setEnabled(false);
354 widthCB->setEnabled(false);
355 widthED->setEnabled(false);
356 widthUnitsLC->setEnabled(false);
357 heightED->setEnabled(false);
358 heightUnitsLC->setEnabled(false);
359 heightCB->setEnabled(false);
361 QString const outer =
362 typeCO->itemData(typeCO->currentIndex()).toString();
363 QString const itype =
364 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
365 bool const ibox = (itype != "none" && itype != "makebox");
366 // pagebreak is only allowed for Boxed without inner box
367 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
368 innerBoxCO->setEnabled(!pagebreakCB->isChecked());
369 valignCO->setEnabled(ibox);
370 ialignCO->setEnabled(ibox);
371 // halign is only allowed without inner box and if a width is used and if
372 // pagebreak is not used
373 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
374 && ((!ibox && outer == "Boxed") || itype == "makebox"));
375 // the width can only be selected for makebox or framebox
376 widthCB->setEnabled(itype == "makebox"
378 && !ibox && !pagebreakCB->isChecked()));
379 // except for frameless and boxed, the width cannot be specified if
380 // there is no inner box
381 bool const width_enabled =
382 ibox || outer == "Frameless" || outer == "Boxed";
383 // enable if width_enabled, except if checkbox is active but unset
384 widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
385 widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
386 if (!widthCB->isChecked() && widthCB->isEnabled()) {
387 widthED->setEnabled(false);
388 widthUnitsLC->setEnabled(false);
390 heightED->setEnabled(itype != "none" && heightCB->isChecked());
391 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
392 heightCB->setEnabled(ibox);
395 return InsetParamsWidget::checkWidgets();
399 void GuiBox::setSpecial(bool ibox)
401 QString const last_item =
402 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
404 // check if the widget contains the special units
405 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
406 // insert 'em if needed...
407 if (!ibox && !has_special) {
408 for (int i = 1; i < ids_spec_.size(); ++i)
409 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
410 // ... or remove 'em if needed
411 } else if (ibox && has_special) {
412 for (int i = 1; i < ids_spec_.size(); ++i) {
413 int n = widthUnitsLC->findData(ids_spec_[i]);
415 widthUnitsLC->removeItem(n);
418 // restore selected text, if possible
419 widthUnitsLC->setCurrentItem(last_item);
423 void GuiBox::setInnerType(bool frameless, QString const & type)
425 // with "frameless" boxes, inner box is mandatory
426 // (i.e. is the actual box)
427 // we have to remove "none" then and adjust the combo
430 innerBoxCO->addItem(qt_("None"), toqstr("none"));
432 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
433 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
434 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
435 int i = (innerBoxCO->findData(type) != -1)
436 ? innerBoxCO->findData(type) : 0;
437 innerBoxCO->setCurrentIndex(i);
440 } // namespace frontend
444 #include "moc_GuiBox.cpp"