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 "FuncRequest.h"
18 #include "LengthCombo.h"
20 #include "LyXRC.h" // to set the default length values
21 #include "qt_helpers.h"
22 #include "Validator.h"
24 #include "insets/InsetBox.h"
26 #include "support/gettext.h"
27 #include "support/foreach.h"
28 #include "support/lstrings.h"
30 #include <QPushButton>
39 static QStringList boxGuiIds()
42 << "Frameless" << "Boxed"
43 << "ovalbox" << "Ovalbox"
44 << "Shadowbox" << "Shaded"
49 static QStringList boxGuiNames()
52 << qt_("No frame") << qt_("Simple rectangular frame")
53 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
54 << qt_("Drop shadow") << qt_("Shaded background")
55 << qt_("Double rectangular frame");
59 static QStringList boxGuiSpecialLengthIds()
61 return QStringList() << "none" << "height" << "depth"
62 << "totalheight" << "width";
66 static QStringList boxGuiSpecialLengthNames()
68 return QStringList() << qt_("None") << qt_("Height") << qt_("Depth")
69 << qt_("Total Height") << qt_("Width");
73 GuiBox::GuiBox(GuiView & lv)
74 : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
78 // fill the box type choice
80 gui_names_ = boxGuiNames();
81 foreach (QString const & str, gui_names_)
84 // add the special units to the height choice
85 // width needs different handling
86 ids_spec_ = boxGuiSpecialLengthIds();
87 gui_names_spec_ = boxGuiSpecialLengthNames();
88 foreach (QString const & str, gui_names_spec_)
89 heightUnitsLC->addItem(str);
91 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
92 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
93 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
94 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
96 connect(widthED, SIGNAL(textChanged(QString)),
97 this, SLOT(change_adaptor()));
98 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
99 this, SLOT(change_adaptor()));
100 connect(valignCO, SIGNAL(highlighted(QString)),
101 this, SLOT(change_adaptor()));
102 connect(heightCB, SIGNAL(stateChanged(int)),
103 this, SLOT(change_adaptor()));
104 connect(heightED, SIGNAL(textChanged(QString)),
105 this, SLOT(change_adaptor()));
106 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
107 this, SLOT(change_adaptor()));
108 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
109 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
110 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
111 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
112 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
113 connect(innerBoxCO, SIGNAL(activated(QString)),
114 this, SLOT(innerBoxChanged(QString)));
115 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
116 connect(pagebreakCB, SIGNAL(stateChanged(int)),
117 this, SLOT(pagebreakClicked()));
119 heightED->setValidator(unsignedLengthValidator(heightED));
120 widthED->setValidator(unsignedLengthValidator(widthED));
122 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
124 bc().addReadOnly(typeCO);
125 bc().addReadOnly(innerBoxCO);
126 bc().addReadOnly(valignCO);
127 bc().addReadOnly(ialignCO);
128 bc().addReadOnly(halignCO);
129 bc().addReadOnly(widthED);
130 bc().addReadOnly(widthUnitsLC);
131 bc().addReadOnly(heightCB);
132 bc().addReadOnly(heightED);
133 bc().addReadOnly(heightUnitsLC);
134 bc().addReadOnly(pagebreakCB);
136 bc().setRestore(restorePB);
138 bc().setApply(applyPB);
139 bc().setCancel(closePB);
141 // initialize the length validator
142 bc().addCheckedLineEdit(widthED, widthLA);
143 bc().addCheckedLineEdit(heightED, heightCB);
147 void GuiBox::change_adaptor()
153 void GuiBox::innerBoxChanged(QString const & str)
155 bool const ibox = (str != qt_("None"));
156 valignCO->setEnabled(ibox);
157 ialignCO->setEnabled(ibox);
158 halignCO->setEnabled(!ibox);
159 heightCB->setEnabled(ibox);
160 pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
161 if (heightCB->checkState() == Qt::Checked && ibox) {
162 heightED->setEnabled(true);
163 heightUnitsLC->setEnabled(true);
169 void GuiBox::typeChanged(int index)
171 bool const frameless = (index == 0);
173 valignCO->setEnabled(true);
174 ialignCO->setEnabled(true);
175 halignCO->setEnabled(false);
176 heightCB->setEnabled(true);
177 heightED->setEnabled(true);
178 heightUnitsLC->setEnabled(true);
182 pagebreakCB->setChecked(false);
183 int itype = innerBoxCO->currentIndex();
184 pagebreakCB->setEnabled(index == 1 && itype == 0);
185 setInnerType(frameless, itype);
189 void GuiBox::restoreClicked()
191 setInnerType(true, 2);
192 widthED->setText("100");
193 widthUnitsLC->setCurrentItem(Length::PCW);
194 heightCB->setCheckState(Qt::Checked);
195 heightED->setText("1");
196 for (int i = 0; i != heightUnitsLC->count(); ++i) {
197 if (heightUnitsLC->itemText(i) == qt_("Total Height"))
198 heightUnitsLC->setCurrentItem(i);
203 void GuiBox::pagebreakClicked()
205 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
206 innerBoxCO->setEnabled(!pbreak);
208 valignCO->setEnabled(false);
209 ialignCO->setEnabled(false);
210 halignCO->setEnabled(false);
211 heightCB->setEnabled(false);
212 heightED->setEnabled(false);
213 heightUnitsLC->setEnabled(false);
216 typeChanged(typeCO->currentIndex());
222 void GuiBox::updateContents()
224 QString type = toqstr(params_.type);
225 if (type == "Framed") {
226 pagebreakCB->setChecked(true);
229 pagebreakCB->setChecked(false);
232 pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
234 for (int i = 0; i != gui_names_.size(); ++i) {
236 typeCO->setCurrentIndex(i);
241 if (!params_.inner_box)
244 if (params_.use_parbox)
247 bool frameless = (params_.type == "Frameless");
248 setInnerType(frameless, inner_type);
250 char c = params_.pos;
251 valignCO->setCurrentIndex(string("tcb").find(c, 0));
252 c = params_.inner_pos;
253 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
255 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
257 bool ibox = params_.inner_box;
258 valignCO->setEnabled(ibox);
259 ialignCO->setEnabled(ibox);
260 halignCO->setEnabled(!ibox);
263 Length::UNIT default_unit =
264 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
266 lengthToWidgets(widthED, widthUnitsLC,
267 (params_.width).asString(), default_unit);
269 QString const special = toqstr(params_.special);
270 if (!special.isEmpty() && special != "none") {
272 for (int i = 0; i != gui_names_spec_.size(); ++i) {
273 if (special == ids_spec_[i])
274 spc = gui_names_spec_[i];
276 for (int i = 0; i != widthUnitsLC->count(); ++i) {
277 if (widthUnitsLC->itemText(i) == spc)
278 widthUnitsLC->setCurrentIndex(i);
282 lengthToWidgets(heightED, heightUnitsLC,
283 (params_.height).asString(), default_unit);
285 QString const height_special = toqstr(params_.height_special);
286 if (!height_special.isEmpty() && height_special != "none") {
288 for (int i = 0; i != gui_names_spec_.size(); ++i) {
289 if (height_special == ids_spec_[i])
290 hspc = gui_names_spec_[i];
292 for (int i = 0; i != heightUnitsLC->count(); ++i) {
293 if (heightUnitsLC->itemText(i) == hspc)
294 heightUnitsLC->setCurrentIndex(i);
297 // set no optional height when the value is the default "1\height"
298 // (special units like \height are handled as "in",
299 if (height_special == "totalheight" && params_.height == Length("1in"))
300 heightCB->setCheckState(Qt::Unchecked);
302 heightCB->setCheckState(Qt::Checked);
304 heightCB->setEnabled(ibox);
308 void GuiBox::applyView()
310 bool pagebreak = pagebreakCB->isEnabled() && pagebreakCB->isChecked();
312 params_.type = "Framed";
314 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
316 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
317 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
319 params_.pos = "tcb"[valignCO->currentIndex()];
320 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
321 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
325 QString special = widthUnitsLC->currentText();
326 QString value = widthED->text();
327 if (special == qt_("Height")) {
330 } else if (special == qt_("Depth")) {
333 } else if (special == qt_("Total Height")) {
336 } else if (special == qt_("Width")) {
340 // the user might insert a non-special value in the line edit
341 if (isValidLength(fromqstr(value))) {
345 params_.special = fromqstr(ids_spec_[i]);
349 width = fromqstr(value);
350 // beware: bogosity! the unit is simply ignored in this case
353 width = widgetsToLength(widthED, widthUnitsLC);
356 params_.width = Length(width);
360 special = heightUnitsLC->currentText();
361 value = heightED->text();
362 if (special == qt_("Height")) {
365 } else if (special == qt_("Depth")) {
368 } else if (special == qt_("Total Height")) {
371 } else if (special == qt_("Width")) {
375 // the user might insert a non-special value in the line edit
376 if (isValidLength(fromqstr(value))) {
380 params_.height_special = fromqstr(ids_spec_[i]);
383 if (spec && !isValidLength(fromqstr(heightED->text()))) {
384 height = fromqstr(value);
385 // beware: bogosity! the unit is simply ignored in this case
388 height = widgetsToLength(heightED, heightUnitsLC);
390 // the height parameter is omitted in InsetBox.cpp when the value
391 // is "1in" and "Total Height" is used as unit.
392 // 1in + "Total Height" means "1\height" which is the LaTeX default when
393 // no height is given
394 if (heightCB->checkState() == Qt::Checked)
395 params_.height = Length(height);
397 params_.height = Length("1in");
398 params_.height_special = fromqstr(ids_spec_[3]);
403 void GuiBox::setSpecial(bool ibox)
405 // FIXME: Needed? Already done in the constructor
406 ids_spec_ = boxGuiSpecialLengthIds();
407 gui_names_spec_ = boxGuiSpecialLengthNames();
409 // check if the widget contains the special units
410 int count = widthUnitsLC->count();
411 bool has_special = false;
412 for (int i = 0; i != count; ++i)
413 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
415 // insert 'em if needed...
416 if (!ibox && !has_special) {
417 for (int i = 1; i < gui_names_spec_.size(); ++i)
418 widthUnitsLC->addItem(gui_names_spec_[i]);
419 // ... or remove 'em if needed
420 } else if (ibox && has_special) {
421 widthUnitsLC->clear();
422 for (int i = 0; i != num_units; ++i)
423 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
428 void GuiBox::setInnerType(bool frameless, int i)
430 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
431 // we have to remove "none" then and adjust the combo
434 innerBoxCO->addItem(qt_("Parbox"));
435 innerBoxCO->addItem(qt_("Minipage"));
437 innerBoxCO->setCurrentIndex(i - 1);
439 innerBoxCO->setCurrentIndex(i);
441 if (innerBoxCO->count() == 2)
444 innerBoxCO->addItem(qt_("None"));
445 innerBoxCO->addItem(qt_("Parbox"));
446 innerBoxCO->addItem(qt_("Minipage"));
447 innerBoxCO->setCurrentIndex(i);
451 bool GuiBox::initialiseParams(string const & data)
453 InsetBox::string2params(data, params_);
459 void GuiBox::clearParams()
461 params_ = InsetBoxParams("");
465 void GuiBox::dispatchParams()
467 dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
471 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
474 } // namespace frontend
478 #include "GuiBox_moc.cpp"