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 widthED->setEnabled(index != 5);
190 widthUnitsLC->setEnabled(index != 5);
191 setInnerType(frameless, itype);
195 void GuiBox::restoreClicked()
197 setInnerType(true, 2);
198 widthED->setText("100");
199 widthUnitsLC->setCurrentItem(Length::PCW);
200 heightCB->setCheckState(Qt::Checked);
201 heightED->setText("1");
202 for (int i = 0; i != heightUnitsLC->count(); ++i) {
203 if (heightUnitsLC->itemText(i) == qt_("Total Height"))
204 heightUnitsLC->setCurrentItem(i);
209 void GuiBox::pagebreakClicked()
211 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
212 innerBoxCO->setEnabled(!pbreak);
213 widthED->setEnabled(!pbreak);
214 widthUnitsLC->setEnabled(!pbreak);
216 valignCO->setEnabled(false);
217 ialignCO->setEnabled(false);
218 halignCO->setEnabled(false);
219 heightCB->setEnabled(false);
220 heightED->setEnabled(false);
221 heightUnitsLC->setEnabled(false);
224 typeChanged(typeCO->currentIndex());
230 void GuiBox::updateContents()
232 QString type = toqstr(params_.type);
233 if (type == "Framed") {
234 pagebreakCB->setChecked(true);
237 pagebreakCB->setChecked(false);
240 pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
242 for (int i = 0; i != gui_names_.size(); ++i) {
244 typeCO->setCurrentIndex(i);
249 if (!params_.inner_box)
252 if (params_.use_parbox)
255 bool frameless = (params_.type == "Frameless");
256 setInnerType(frameless, inner_type);
258 char c = params_.pos;
259 valignCO->setCurrentIndex(string("tcb").find(c, 0));
260 c = params_.inner_pos;
261 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
263 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
265 bool ibox = params_.inner_box;
266 valignCO->setEnabled(ibox);
267 ialignCO->setEnabled(ibox);
268 halignCO->setEnabled(!ibox);
271 Length::UNIT default_unit =
272 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
274 lengthToWidgets(widthED, widthUnitsLC,
275 (params_.width).asString(), default_unit);
277 QString const special = toqstr(params_.special);
278 if (!special.isEmpty() && special != "none") {
280 for (int i = 0; i != gui_names_spec_.size(); ++i) {
281 if (special == ids_spec_[i])
282 spc = gui_names_spec_[i];
284 for (int i = 0; i != widthUnitsLC->count(); ++i) {
285 if (widthUnitsLC->itemText(i) == spc)
286 widthUnitsLC->setCurrentIndex(i);
290 lengthToWidgets(heightED, heightUnitsLC,
291 (params_.height).asString(), default_unit);
293 QString const height_special = toqstr(params_.height_special);
294 if (!height_special.isEmpty() && height_special != "none") {
296 for (int i = 0; i != gui_names_spec_.size(); ++i) {
297 if (height_special == ids_spec_[i])
298 hspc = gui_names_spec_[i];
300 for (int i = 0; i != heightUnitsLC->count(); ++i) {
301 if (heightUnitsLC->itemText(i) == hspc)
302 heightUnitsLC->setCurrentIndex(i);
305 // set no optional height when the value is the default "1\height"
306 // (special units like \height are handled as "in",
307 if (height_special == "totalheight" && params_.height == Length("1in"))
308 heightCB->setCheckState(Qt::Unchecked);
310 heightCB->setCheckState(Qt::Checked);
312 heightCB->setEnabled(ibox);
316 void GuiBox::applyView()
318 bool pagebreak = pagebreakCB->isEnabled() && pagebreakCB->isChecked();
320 params_.type = "Framed";
322 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
324 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
325 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
327 params_.pos = "tcb"[valignCO->currentIndex()];
328 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
329 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
333 QString special = widthUnitsLC->currentText();
334 QString value = widthED->text();
335 if (special == qt_("Height")) {
338 } else if (special == qt_("Depth")) {
341 } else if (special == qt_("Total Height")) {
344 } else if (special == qt_("Width")) {
348 // the user might insert a non-special value in the line edit
349 if (isValidLength(fromqstr(value))) {
353 params_.special = fromqstr(ids_spec_[i]);
357 width = fromqstr(value);
358 // beware: bogosity! the unit is simply ignored in this case
361 width = widgetsToLength(widthED, widthUnitsLC);
364 params_.width = Length(width);
368 special = heightUnitsLC->currentText();
369 value = heightED->text();
370 if (special == qt_("Height")) {
373 } else if (special == qt_("Depth")) {
376 } else if (special == qt_("Total Height")) {
379 } else if (special == qt_("Width")) {
383 // the user might insert a non-special value in the line edit
384 if (isValidLength(fromqstr(value))) {
388 params_.height_special = fromqstr(ids_spec_[i]);
391 if (spec && !isValidLength(fromqstr(heightED->text()))) {
392 height = fromqstr(value);
393 // beware: bogosity! the unit is simply ignored in this case
396 height = widgetsToLength(heightED, heightUnitsLC);
398 // the height parameter is omitted in InsetBox.cpp when the value
399 // is "1in" and "Total Height" is used as unit.
400 // 1in + "Total Height" means "1\height" which is the LaTeX default when
401 // no height is given
402 if (heightCB->checkState() == Qt::Checked)
403 params_.height = Length(height);
405 params_.height = Length("1in");
406 params_.height_special = fromqstr(ids_spec_[3]);
411 void GuiBox::setSpecial(bool ibox)
413 // FIXME: Needed? Already done in the constructor
414 ids_spec_ = boxGuiSpecialLengthIds();
415 gui_names_spec_ = boxGuiSpecialLengthNames();
417 // check if the widget contains the special units
418 int count = widthUnitsLC->count();
419 bool has_special = false;
420 for (int i = 0; i != count; ++i)
421 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
423 // insert 'em if needed...
424 if (!ibox && !has_special) {
425 for (int i = 1; i < gui_names_spec_.size(); ++i)
426 widthUnitsLC->addItem(gui_names_spec_[i]);
427 // ... or remove 'em if needed
428 } else if (ibox && has_special) {
429 widthUnitsLC->clear();
430 for (int i = 0; i != num_units; ++i)
431 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
436 void GuiBox::setInnerType(bool frameless, int i)
438 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
439 // we have to remove "none" then and adjust the combo
442 innerBoxCO->addItem(qt_("Parbox"));
443 innerBoxCO->addItem(qt_("Minipage"));
445 innerBoxCO->setCurrentIndex(i - 1);
447 innerBoxCO->setCurrentIndex(i);
449 if (innerBoxCO->count() == 2)
452 innerBoxCO->addItem(qt_("None"));
453 innerBoxCO->addItem(qt_("Parbox"));
454 innerBoxCO->addItem(qt_("Minipage"));
455 innerBoxCO->setCurrentIndex(i);
459 bool GuiBox::initialiseParams(string const & data)
461 InsetBox::string2params(data, params_);
467 void GuiBox::clearParams()
469 params_ = InsetBoxParams("");
473 void GuiBox::dispatchParams()
475 dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
479 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
482 } // namespace frontend
486 #include "GuiBox_moc.cpp"