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>
43 static QStringList boxGuiIds()
46 << "Frameless" << "Boxed"
47 << "ovalbox" << "Ovalbox"
48 << "Shadowbox" << "Shaded"
53 static QStringList boxGuiNames()
56 << qt_("No frame") << qt_("Simple rectangular frame")
57 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
58 << qt_("Drop shadow") << qt_("Shaded background")
59 << qt_("Double rectangular frame");
63 static QStringList boxGuiSpecialLengthIds()
65 return QStringList() << "none" << "height" << "depth"
66 << "totalheight" << "width";
70 static QStringList boxGuiSpecialLengthNames()
72 return QStringList() << qt_("None") << qt_("Height") << qt_("Depth")
73 << qt_("Total Height") << qt_("Width");
77 GuiBox::GuiBox(GuiView & lv)
78 : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
82 // fill the box type choice
84 gui_names_ = boxGuiNames();
85 foreach (QString const & str, gui_names_)
88 // add the special units to the height choice
89 // width needs different handling
90 ids_spec_ = boxGuiSpecialLengthIds();
91 gui_names_spec_ = boxGuiSpecialLengthNames();
92 foreach (QString const & str, gui_names_spec_)
93 heightUnitsLC->addItem(str);
95 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
96 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
97 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
98 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
100 connect(widthED, SIGNAL(textChanged(QString)),
101 this, SLOT(change_adaptor()));
102 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
103 this, SLOT(change_adaptor()));
104 connect(valignCO, SIGNAL(highlighted(QString)),
105 this, SLOT(change_adaptor()));
106 connect(heightCB, SIGNAL(stateChanged(int)),
107 this, SLOT(change_adaptor()));
108 connect(heightED, SIGNAL(textChanged(QString)),
109 this, SLOT(change_adaptor()));
110 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
111 this, SLOT(change_adaptor()));
112 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
113 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
114 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
115 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
116 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
117 connect(innerBoxCO, SIGNAL(activated(QString)),
118 this, SLOT(innerBoxChanged(QString)));
119 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
120 connect(pagebreakCB, SIGNAL(stateChanged(int)),
121 this, SLOT(pagebreakClicked()));
123 heightED->setValidator(unsignedLengthValidator(heightED));
124 widthED->setValidator(unsignedLengthValidator(widthED));
126 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
128 bc().addReadOnly(typeCO);
129 bc().addReadOnly(innerBoxCO);
130 bc().addReadOnly(valignCO);
131 bc().addReadOnly(ialignCO);
132 bc().addReadOnly(halignCO);
133 bc().addReadOnly(widthED);
134 bc().addReadOnly(widthUnitsLC);
135 bc().addReadOnly(heightCB);
136 bc().addReadOnly(heightED);
137 bc().addReadOnly(heightUnitsLC);
138 bc().addReadOnly(pagebreakCB);
140 bc().setRestore(restorePB);
142 bc().setApply(applyPB);
143 bc().setCancel(closePB);
145 // initialize the length validator
146 bc().addCheckedLineEdit(widthED, widthLA);
147 bc().addCheckedLineEdit(heightED, heightCB);
151 void GuiBox::change_adaptor()
157 void GuiBox::innerBoxChanged(QString const & str)
159 bool const ibox = (str != qt_("None"));
160 valignCO->setEnabled(ibox);
161 ialignCO->setEnabled(ibox);
162 halignCO->setEnabled(!ibox);
163 heightCB->setEnabled(ibox);
164 pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
165 if (heightCB->checkState() == Qt::Checked && ibox) {
166 heightED->setEnabled(true);
167 heightUnitsLC->setEnabled(true);
173 void GuiBox::typeChanged(int index)
175 bool const frameless = (index == 0);
177 valignCO->setEnabled(true);
178 ialignCO->setEnabled(true);
179 halignCO->setEnabled(false);
180 heightCB->setEnabled(true);
181 heightED->setEnabled(true);
182 heightUnitsLC->setEnabled(true);
186 pagebreakCB->setChecked(false);
187 int itype = innerBoxCO->currentIndex();
188 if (innerBoxCO->count() == 2)
190 pagebreakCB->setEnabled(index == 1 && itype == 0);
191 widthED->setEnabled(index != 5);
192 widthUnitsLC->setEnabled(index != 5);
193 setInnerType(frameless, itype);
197 void GuiBox::restoreClicked()
199 setInnerType(true, 2);
200 widthED->setText("100");
201 widthUnitsLC->setCurrentItem(Length::PCW);
202 heightCB->setCheckState(Qt::Checked);
203 heightED->setText("1");
204 for (int i = 0; i != heightUnitsLC->count(); ++i) {
205 if (heightUnitsLC->itemText(i) == qt_("Total Height"))
206 heightUnitsLC->setCurrentItem(i);
211 void GuiBox::pagebreakClicked()
213 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
214 innerBoxCO->setEnabled(!pbreak);
215 widthED->setEnabled(!pbreak);
216 widthUnitsLC->setEnabled(!pbreak);
218 valignCO->setEnabled(false);
219 ialignCO->setEnabled(false);
220 halignCO->setEnabled(false);
221 heightCB->setEnabled(false);
222 heightED->setEnabled(false);
223 heightUnitsLC->setEnabled(false);
226 typeChanged(typeCO->currentIndex());
232 void GuiBox::updateContents()
234 QString type = toqstr(params_.type);
235 if (type == "Framed") {
236 pagebreakCB->setChecked(true);
239 pagebreakCB->setChecked(false);
242 pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
244 for (int i = 0; i != gui_names_.size(); ++i) {
246 typeCO->setCurrentIndex(i);
251 if (!params_.inner_box)
254 if (params_.use_parbox)
257 bool frameless = (params_.type == "Frameless");
258 setInnerType(frameless, inner_type);
260 char c = params_.pos;
261 valignCO->setCurrentIndex(string("tcb").find(c, 0));
262 c = params_.inner_pos;
263 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
265 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
267 bool ibox = params_.inner_box;
268 valignCO->setEnabled(ibox);
269 ialignCO->setEnabled(ibox);
270 halignCO->setEnabled(!ibox);
273 Length::UNIT default_unit =
274 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
276 lengthToWidgets(widthED, widthUnitsLC,
277 (params_.width).asString(), default_unit);
279 QString const special = toqstr(params_.special);
280 if (!special.isEmpty() && special != "none") {
282 for (int i = 0; i != gui_names_spec_.size(); ++i) {
283 if (special == ids_spec_[i])
284 spc = gui_names_spec_[i];
286 for (int i = 0; i != widthUnitsLC->count(); ++i) {
287 if (widthUnitsLC->itemText(i) == spc)
288 widthUnitsLC->setCurrentIndex(i);
292 lengthToWidgets(heightED, heightUnitsLC,
293 (params_.height).asString(), default_unit);
295 QString const height_special = toqstr(params_.height_special);
296 if (!height_special.isEmpty() && height_special != "none") {
298 for (int i = 0; i != gui_names_spec_.size(); ++i) {
299 if (height_special == ids_spec_[i])
300 hspc = gui_names_spec_[i];
302 for (int i = 0; i != heightUnitsLC->count(); ++i) {
303 if (heightUnitsLC->itemText(i) == hspc)
304 heightUnitsLC->setCurrentIndex(i);
307 // set no optional height when the value is the default "1\height"
308 // (special units like \height are handled as "in",
309 if (height_special == "totalheight" && params_.height == Length("1in"))
310 heightCB->setCheckState(Qt::Unchecked);
312 heightCB->setCheckState(Qt::Checked);
314 heightCB->setEnabled(ibox);
318 void GuiBox::applyView()
320 bool pagebreak = pagebreakCB->isEnabled() && pagebreakCB->isChecked();
322 params_.type = "Framed";
324 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
326 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
327 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
329 params_.pos = "tcb"[valignCO->currentIndex()];
330 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
331 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
335 QString special = widthUnitsLC->currentText();
336 QString value = widthED->text();
337 if (special == qt_("Height")) {
340 } else if (special == qt_("Depth")) {
343 } else if (special == qt_("Total Height")) {
346 } else if (special == qt_("Width")) {
350 // the user might insert a non-special value in the line edit
351 if (isValidLength(fromqstr(value))) {
355 params_.special = fromqstr(ids_spec_[i]);
359 width = fromqstr(value);
360 // beware: bogosity! the unit is simply ignored in this case
363 width = widgetsToLength(widthED, widthUnitsLC);
366 params_.width = Length(width);
370 special = heightUnitsLC->currentText();
371 value = heightED->text();
372 if (special == qt_("Height")) {
375 } else if (special == qt_("Depth")) {
378 } else if (special == qt_("Total Height")) {
381 } else if (special == qt_("Width")) {
385 // the user might insert a non-special value in the line edit
386 if (isValidLength(fromqstr(value))) {
390 params_.height_special = fromqstr(ids_spec_[i]);
393 if (spec && !isValidLength(fromqstr(heightED->text()))) {
394 height = fromqstr(value);
395 // beware: bogosity! the unit is simply ignored in this case
398 height = widgetsToLength(heightED, heightUnitsLC);
400 // the height parameter is omitted in InsetBox.cpp when the value
401 // is "1in" and "Total Height" is used as unit.
402 // 1in + "Total Height" means "1\height" which is the LaTeX default when
403 // no height is given
404 if (heightCB->checkState() == Qt::Checked)
405 params_.height = Length(height);
407 params_.height = Length("1in");
408 params_.height_special = fromqstr(ids_spec_[3]);
413 void GuiBox::setSpecial(bool ibox)
415 // FIXME: Needed? Already done in the constructor
416 ids_spec_ = boxGuiSpecialLengthIds();
417 gui_names_spec_ = boxGuiSpecialLengthNames();
419 QString const current_text = widthUnitsLC->currentText();
421 // check if the widget contains the special units
422 int const count = widthUnitsLC->count();
423 bool has_special = false;
424 for (int i = 0; i != count; ++i)
425 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
427 // insert 'em if needed...
428 if (!ibox && !has_special) {
429 for (int i = 1; i < gui_names_spec_.size(); ++i)
430 widthUnitsLC->addItem(gui_names_spec_[i]);
431 // ... or remove 'em if needed
432 } else if (ibox && has_special) {
433 widthUnitsLC->clear();
434 for (int i = 0; i != num_units; ++i)
435 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
437 // restore selected text, if possible
438 int const idx = widthUnitsLC->findText(current_text);
440 widthUnitsLC->setCurrentIndex(idx);
444 void GuiBox::setInnerType(bool frameless, int i)
446 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
447 // we have to remove "none" then and adjust the combo
450 innerBoxCO->addItem(qt_("Parbox"));
451 innerBoxCO->addItem(qt_("Minipage"));
453 innerBoxCO->setCurrentIndex(i - 1);
455 innerBoxCO->setCurrentIndex(i);
458 innerBoxCO->addItem(qt_("None"));
459 innerBoxCO->addItem(qt_("Parbox"));
460 innerBoxCO->addItem(qt_("Minipage"));
461 innerBoxCO->setCurrentIndex(i);
465 bool GuiBox::initialiseParams(string const & data)
467 InsetBox::string2params(data, params_);
473 void GuiBox::clearParams()
475 params_ = InsetBoxParams("");
479 void GuiBox::dispatchParams()
481 dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
485 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
488 } // namespace frontend
492 #include "GuiBox_moc.cpp"