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 pagebreakCB->setEnabled(index == 1 && itype == 0);
189 setInnerType(frameless, itype);
193 void GuiBox::restoreClicked()
195 setInnerType(true, 2);
196 widthED->setText("100");
197 widthUnitsLC->setCurrentItem(Length::PCW);
198 heightCB->setCheckState(Qt::Checked);
199 heightED->setText("1");
200 for (int i = 0; i != heightUnitsLC->count(); ++i) {
201 if (heightUnitsLC->itemText(i) == qt_("Total Height"))
202 heightUnitsLC->setCurrentItem(i);
207 void GuiBox::pagebreakClicked()
209 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
210 innerBoxCO->setEnabled(!pbreak);
212 valignCO->setEnabled(false);
213 ialignCO->setEnabled(false);
214 halignCO->setEnabled(false);
215 heightCB->setEnabled(false);
216 heightED->setEnabled(false);
217 heightUnitsLC->setEnabled(false);
220 typeChanged(typeCO->currentIndex());
226 void GuiBox::updateContents()
228 QString type = toqstr(params_.type);
229 if (type == "Framed") {
230 pagebreakCB->setChecked(true);
233 pagebreakCB->setChecked(false);
236 pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
238 for (int i = 0; i != gui_names_.size(); ++i) {
240 typeCO->setCurrentIndex(i);
245 if (!params_.inner_box)
248 if (params_.use_parbox)
251 bool frameless = (params_.type == "Frameless");
252 setInnerType(frameless, inner_type);
254 char c = params_.pos;
255 valignCO->setCurrentIndex(string("tcb").find(c, 0));
256 c = params_.inner_pos;
257 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
259 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
261 bool ibox = params_.inner_box;
262 valignCO->setEnabled(ibox);
263 ialignCO->setEnabled(ibox);
264 halignCO->setEnabled(!ibox);
267 Length::UNIT default_unit =
268 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
270 lengthToWidgets(widthED, widthUnitsLC,
271 (params_.width).asString(), default_unit);
273 QString const special = toqstr(params_.special);
274 if (!special.isEmpty() && special != "none") {
276 for (int i = 0; i != gui_names_spec_.size(); ++i) {
277 if (special == ids_spec_[i])
278 spc = gui_names_spec_[i];
280 for (int i = 0; i != widthUnitsLC->count(); ++i) {
281 if (widthUnitsLC->itemText(i) == spc)
282 widthUnitsLC->setCurrentIndex(i);
286 lengthToWidgets(heightED, heightUnitsLC,
287 (params_.height).asString(), default_unit);
289 QString const height_special = toqstr(params_.height_special);
290 if (!height_special.isEmpty() && height_special != "none") {
292 for (int i = 0; i != gui_names_spec_.size(); ++i) {
293 if (height_special == ids_spec_[i])
294 hspc = gui_names_spec_[i];
296 for (int i = 0; i != heightUnitsLC->count(); ++i) {
297 if (heightUnitsLC->itemText(i) == hspc)
298 heightUnitsLC->setCurrentIndex(i);
301 // set no optional height when the value is the default "1\height"
302 // (special units like \height are handled as "in",
303 if (height_special == "totalheight" && params_.height == Length("1in"))
304 heightCB->setCheckState(Qt::Unchecked);
306 heightCB->setCheckState(Qt::Checked);
308 heightCB->setEnabled(ibox);
312 void GuiBox::applyView()
314 bool pagebreak = pagebreakCB->isEnabled() && pagebreakCB->isChecked();
316 params_.type = "Framed";
318 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
320 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
321 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
323 params_.pos = "tcb"[valignCO->currentIndex()];
324 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
325 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
329 QString special = widthUnitsLC->currentText();
330 QString value = widthED->text();
331 if (special == qt_("Height")) {
334 } else if (special == qt_("Depth")) {
337 } else if (special == qt_("Total Height")) {
340 } else if (special == qt_("Width")) {
344 // the user might insert a non-special value in the line edit
345 if (isValidLength(fromqstr(value))) {
349 params_.special = fromqstr(ids_spec_[i]);
353 width = fromqstr(value);
354 // beware: bogosity! the unit is simply ignored in this case
357 width = widgetsToLength(widthED, widthUnitsLC);
360 params_.width = Length(width);
364 special = heightUnitsLC->currentText();
365 value = heightED->text();
366 if (special == qt_("Height")) {
369 } else if (special == qt_("Depth")) {
372 } else if (special == qt_("Total Height")) {
375 } else if (special == qt_("Width")) {
379 // the user might insert a non-special value in the line edit
380 if (isValidLength(fromqstr(value))) {
384 params_.height_special = fromqstr(ids_spec_[i]);
387 if (spec && !isValidLength(fromqstr(heightED->text()))) {
388 height = fromqstr(value);
389 // beware: bogosity! the unit is simply ignored in this case
392 height = widgetsToLength(heightED, heightUnitsLC);
394 // the height parameter is omitted in InsetBox.cpp when the value
395 // is "1in" and "Total Height" is used as unit.
396 // 1in + "Total Height" means "1\height" which is the LaTeX default when
397 // no height is given
398 if (heightCB->checkState() == Qt::Checked)
399 params_.height = Length(height);
401 params_.height = Length("1in");
402 params_.height_special = fromqstr(ids_spec_[3]);
407 void GuiBox::setSpecial(bool ibox)
409 // FIXME: Needed? Already done in the constructor
410 ids_spec_ = boxGuiSpecialLengthIds();
411 gui_names_spec_ = boxGuiSpecialLengthNames();
413 // check if the widget contains the special units
414 int count = widthUnitsLC->count();
415 bool has_special = false;
416 for (int i = 0; i != count; ++i)
417 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
419 // insert 'em if needed...
420 if (!ibox && !has_special) {
421 for (int i = 1; i < gui_names_spec_.size(); ++i)
422 widthUnitsLC->addItem(gui_names_spec_[i]);
423 // ... or remove 'em if needed
424 } else if (ibox && has_special) {
425 widthUnitsLC->clear();
426 for (int i = 0; i != num_units; ++i)
427 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
432 void GuiBox::setInnerType(bool frameless, int i)
434 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
435 // we have to remove "none" then and adjust the combo
438 innerBoxCO->addItem(qt_("Parbox"));
439 innerBoxCO->addItem(qt_("Minipage"));
441 innerBoxCO->setCurrentIndex(i - 1);
443 innerBoxCO->setCurrentIndex(i);
445 if (innerBoxCO->count() == 2)
448 innerBoxCO->addItem(qt_("None"));
449 innerBoxCO->addItem(qt_("Parbox"));
450 innerBoxCO->addItem(qt_("Minipage"));
451 innerBoxCO->setCurrentIndex(i);
455 bool GuiBox::initialiseParams(string const & data)
457 InsetBox::string2params(data, params_);
463 void GuiBox::clearParams()
465 params_ = InsetBoxParams("");
469 void GuiBox::dispatchParams()
471 dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
475 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
478 } // namespace frontend
482 #include "GuiBox_moc.cpp"