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
120 && itype != "makebox");
121 QString const outer =
122 typeCO->itemData(typeCO->currentIndex()).toString();
123 valignCO->setEnabled(ibox);
124 ialignCO->setEnabled(ibox);
125 if (heightCB->isChecked() && !ibox)
126 heightCB->setChecked(false);
127 heightCB->setEnabled(ibox);
128 // the width can only be selected for makebox or framebox
129 widthCB->setEnabled(itype == "makebox"
130 || (outer == "Boxed" && itype == "none"));
131 widthCB->setChecked(itype != "none" && !widthCB->isEnabled());
132 // except for frameless and boxed, the width cannot be specified if
133 // there is no inner box
134 bool const width_enabled =
135 ibox || outer == "Frameless" || outer == "Boxed";
136 // enable if width_enabled, except if checkbaox is active but unset
137 widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
138 widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
139 if (!widthCB->isChecked() && widthCB->isEnabled()) {
140 widthED->setEnabled(false);
141 widthUnitsLC->setEnabled(false);
143 // halign is only allowed without inner box and if a width is used and if
144 // pagebreak is not used
145 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
146 && ((!ibox && outer == "Boxed") || itype == "makebox"));
147 // pagebreak is only allowed for Boxed without inner box
148 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
154 void GuiBox::on_typeCO_activated(int index)
157 typeCO->itemData(index).toString();
158 bool const frameless = (type == "Frameless");
160 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
161 setInnerType(frameless, itype);
162 // refresh itype because it might have been changed in setInnerType
164 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
165 // handle parbox and minipage the same way
168 && itype != "makebox");
169 if (frameless && itype != "makebox") {
170 valignCO->setEnabled(ibox);
171 ialignCO->setEnabled(ibox);
172 if (heightCB->isChecked() && !ibox)
173 heightCB->setChecked(false);
174 heightCB->setEnabled(ibox);
177 // the width can only be selected for makebox or framebox
178 widthCB->setEnabled(itype == "makebox"
179 || (type == "Boxed" && itype == "none"));
180 widthCB->setChecked(itype != "none" && !widthCB->isEnabled());
181 // except for frameless and boxed, the width cannot be specified if
182 // there is no inner box
183 bool const width_enabled =
184 itype != "none" || frameless || type == "Boxed";
185 // enable if width_enabled, except if checkbaox is active but unset
186 widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
187 widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
188 if (!widthCB->isChecked() && widthCB->isEnabled()) {
189 widthED->setEnabled(false);
190 widthUnitsLC->setEnabled(false);
192 // halign is only allowed without inner box and if a width is used and if
193 // pagebreak is not used
194 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
195 && ((itype == "none" && type == "Boxed") || itype == "makebox"));
196 // pagebreak is only allowed for Boxed without inner box
197 pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
199 pagebreakCB->setChecked(false);
204 void GuiBox::initDialog()
206 setInnerType(true, toqstr("minipage"));
207 widthED->setText("100");
208 widthCB->setChecked(true);
209 widthCB->setEnabled(false);
210 widthUnitsLC->setCurrentItem(Length::PCW);
211 heightED->setText("1");
212 heightUnitsLC->setCurrentItem("totalheight");
216 void GuiBox::on_widthCB_stateChanged(int)
218 if (widthCB->isEnabled()) {
219 widthED->setEnabled(widthCB->isChecked());
220 widthUnitsLC->setEnabled(widthCB->isChecked());
221 halignCO->setEnabled(widthCB->isChecked());
227 void GuiBox::on_heightCB_stateChanged(int state)
229 bool const enable = (innerBoxCO->currentText() != qt_("None"))
230 && (state == Qt::Checked);
231 heightED->setEnabled(enable);
232 heightUnitsLC->setEnabled(enable);
237 void GuiBox::on_pagebreakCB_stateChanged()
239 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
240 innerBoxCO->setEnabled(!pbreak);
241 widthCB->setEnabled(!pbreak);
243 widthCB->setChecked(!pbreak);
244 widthED->setEnabled(!pbreak);
245 widthUnitsLC->setEnabled(!pbreak);
247 on_typeCO_activated(typeCO->currentIndex());
250 valignCO->setEnabled(false);
251 ialignCO->setEnabled(false);
252 halignCO->setEnabled(false);
253 heightCB->setEnabled(false);
254 heightED->setEnabled(false);
255 heightUnitsLC->setEnabled(false);
261 void GuiBox::paramsToDialog(Inset const * inset)
263 InsetBox const * box = static_cast<InsetBox const *>(inset);
264 InsetBoxParams const & params = box->params();
265 QString type = toqstr(params.type);
266 if (type == "Framed") {
267 pagebreakCB->setChecked(true);
270 pagebreakCB->setChecked(false);
273 typeCO->setCurrentIndex(typeCO->findData(type));
276 QString inner_type = "minipage";
277 if (!params.inner_box)
279 if (params.use_parbox)
280 inner_type = "parbox";
281 if (params.use_makebox)
282 inner_type = "makebox";
283 bool const frameless = (params.type == "Frameless");
284 setInnerType(frameless, inner_type);
287 valignCO->setCurrentIndex(string("tcb").find(c, 0));
288 c = params.inner_pos;
289 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
291 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
293 bool ibox = (params.inner_box && !params.use_makebox);
294 valignCO->setEnabled(ibox);
295 ialignCO->setEnabled(ibox);
298 // halign is only allowed without inner box and if a width is used and if
299 // pagebreak is not used
300 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
301 && ((!ibox && type == "Boxed") || params.use_makebox));
302 // pagebreak is only allowed for Boxed without inner box
303 pagebreakCB->setEnabled(!ibox && type == "Boxed");
305 Length::UNIT const default_unit = Length::defaultUnit();
307 // the width can only be selected for makebox or framebox
308 widthCB->setEnabled(inner_type == "makebox"
310 && !ibox && !pagebreakCB->isChecked()));
311 if (params.width.empty()) {
312 widthCB->setChecked(false);
313 lengthToWidgets(widthED, widthUnitsLC,
314 params.width, default_unit);
316 if (widthCB->isEnabled())
317 widthCB->setChecked(true);
318 lengthToWidgets(widthED, widthUnitsLC,
319 params.width, default_unit);
320 QString const special = toqstr(params.special);
321 if (!special.isEmpty() && special != "none")
322 widthUnitsLC->setCurrentItem(special);
325 widthED->setEnabled(widthCB->isChecked());
326 widthUnitsLC->setEnabled(widthCB->isChecked());
328 lengthToWidgets(heightED, heightUnitsLC,
329 (params.height).asString(), default_unit);
331 QString const height_special = toqstr(params.height_special);
332 if (!height_special.isEmpty() && height_special != "none")
333 heightUnitsLC->setCurrentItem(height_special);
334 // set no optional height if the value is the default "1\height"
335 // (special units like \height are handled as "in",
336 // FIXME: this is a very bad UI, this check box should be disabled in
337 // this case, not forced to 'unchecked' state.
338 if (height_special == "totalheight" && params.height == Length("1in"))
339 heightCB->setCheckState(Qt::Unchecked);
341 heightCB->setCheckState(Qt::Checked);
343 heightCB->setEnabled(ibox);
347 docstring GuiBox::dialogToParams() const
349 bool const pagebreak =
350 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
355 box_type = fromqstr(typeCO->itemData(
356 typeCO->currentIndex()).toString());
358 InsetBoxParams params(box_type);
360 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
362 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
364 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
366 params.pos = "tcb"[valignCO->currentIndex()];
367 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
368 params.hor_pos = "lcrs"[halignCO->currentIndex()];
371 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
372 QString value = widthED->text();
374 if (widthED->isEnabled()) {
375 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
376 params.special = fromqstr(unit);
377 // Note: the unit is simply ignored in this case
378 params.width = Length(value.toDouble(), Length::IN);
380 params.special = "none";
381 // we must specify a valid length in this case
383 widthED->setText("0");
384 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
387 params.special = "none";
388 params.width = Length();
391 // the height parameter is omitted if the value
392 // is "1in" and "Total Height" is used as unit.
393 // 1in + "Total Height" means "1\height" which is the LaTeX default
394 // if no height is given
395 if (heightCB->checkState() == Qt::Unchecked) {
396 params.height = Length("1in");
397 params.height_special = "totalheight";
399 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
400 value = heightED->text();
401 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
402 params.height_special = fromqstr(unit);
403 // Note: the unit is simply ignored in this case
404 params.height = Length(value.toDouble(), Length::IN);
406 params.height_special = "none";
408 Length(widgetsToLength(heightED, heightUnitsLC));
411 return from_ascii(InsetBox::params2string(params));
415 void GuiBox::setSpecial(bool ibox)
417 QString const last_item =
418 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
420 // check if the widget contains the special units
421 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
422 // insert 'em if needed...
423 if (!ibox && !has_special) {
424 for (int i = 1; i < ids_spec_.size(); ++i)
425 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
426 // ... or remove 'em if needed
427 } else if (ibox && has_special) {
428 for (int i = 1; i < ids_spec_.size(); ++i) {
429 int n = widthUnitsLC->findData(ids_spec_[i]);
431 widthUnitsLC->removeItem(n);
434 // restore selected text, if possible
435 widthUnitsLC->setCurrentItem(last_item);
439 void GuiBox::setInnerType(bool frameless, QString const & type)
441 // with "frameless" boxes, inner box is mandatory
442 // (i.e. is the actual box)
443 // we have to remove "none" then and adjust the combo
446 innerBoxCO->addItem(qt_("None"), toqstr("none"));
448 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
449 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
450 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
451 int i = (innerBoxCO->findData(type) != -1)
452 ? innerBoxCO->findData(type) : 0;
453 innerBoxCO->setCurrentIndex(i);
456 } // namespace frontend
460 #include "moc_GuiBox.cpp"