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 if (heightCB->checkState() == Qt::Checked && ibox) {
161 heightED->setEnabled(true);
162 heightUnitsLC->setEnabled(true);
168 void GuiBox::typeChanged(int index)
170 bool const frameless = (index == 0);
172 valignCO->setEnabled(true);
173 ialignCO->setEnabled(true);
174 halignCO->setEnabled(false);
175 heightCB->setEnabled(true);
176 heightED->setEnabled(true);
177 heightUnitsLC->setEnabled(true);
181 pagebreakCB->setChecked(false);
182 pagebreakCB->setEnabled(index == 1);
183 int itype = innerBoxCO->currentIndex();
184 setInnerType(frameless, itype);
188 void GuiBox::restoreClicked()
190 setInnerType(true, 2);
191 widthED->setText("100");
192 widthUnitsLC->setCurrentItem(Length::PCW);
193 heightCB->setCheckState(Qt::Checked);
194 heightED->setText("1");
195 for (int i = 0; i != heightUnitsLC->count(); ++i) {
196 if (heightUnitsLC->itemText(i) == qt_("Total Height"))
197 heightUnitsLC->setCurrentItem(i);
202 void GuiBox::pagebreakClicked()
204 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
205 innerBoxCO->setEnabled(!pbreak);
207 valignCO->setEnabled(false);
208 ialignCO->setEnabled(false);
209 halignCO->setEnabled(false);
210 heightCB->setEnabled(false);
211 heightED->setEnabled(false);
212 heightUnitsLC->setEnabled(false);
215 typeChanged(typeCO->currentIndex());
221 void GuiBox::updateContents()
223 QString type = toqstr(params_.type);
224 if (type == "Framed") {
225 pagebreakCB->setChecked(true);
228 pagebreakCB->setChecked(false);
231 pagebreakCB->setEnabled(type == "Boxed");
233 for (int i = 0; i != gui_names_.size(); ++i) {
235 typeCO->setCurrentIndex(i);
240 if (!params_.inner_box)
243 if (params_.use_parbox)
246 bool frameless = (params_.type == "Frameless");
247 setInnerType(frameless, inner_type);
249 char c = params_.pos;
250 valignCO->setCurrentIndex(string("tcb").find(c, 0));
251 c = params_.inner_pos;
252 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
254 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
256 bool ibox = params_.inner_box;
257 valignCO->setEnabled(ibox);
258 ialignCO->setEnabled(ibox);
259 halignCO->setEnabled(!ibox);
262 Length::UNIT default_unit =
263 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
265 lengthToWidgets(widthED, widthUnitsLC,
266 (params_.width).asString(), default_unit);
268 QString const special = toqstr(params_.special);
269 if (!special.isEmpty() && special != "none") {
271 for (int i = 0; i != gui_names_spec_.size(); ++i) {
272 if (special == ids_spec_[i])
273 spc = gui_names_spec_[i];
275 for (int i = 0; i != widthUnitsLC->count(); ++i) {
276 if (widthUnitsLC->itemText(i) == spc)
277 widthUnitsLC->setCurrentIndex(i);
281 lengthToWidgets(heightED, heightUnitsLC,
282 (params_.height).asString(), default_unit);
284 QString const height_special = toqstr(params_.height_special);
285 if (!height_special.isEmpty() && height_special != "none") {
287 for (int i = 0; i != gui_names_spec_.size(); ++i) {
288 if (height_special == ids_spec_[i])
289 hspc = gui_names_spec_[i];
291 for (int i = 0; i != heightUnitsLC->count(); ++i) {
292 if (heightUnitsLC->itemText(i) == hspc)
293 heightUnitsLC->setCurrentIndex(i);
296 // set no optional height when the value is the default "1\height"
297 // (special units like \height are handled as "in",
298 if (height_special == "totalheight" && params_.height == Length("1in"))
299 heightCB->setCheckState(Qt::Unchecked);
301 heightCB->setCheckState(Qt::Checked);
303 heightCB->setEnabled(ibox);
307 void GuiBox::applyView()
309 bool pagebreak = pagebreakCB->isChecked();
311 params_.type = "Framed";
313 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
315 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
316 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
318 params_.pos = "tcb"[valignCO->currentIndex()];
319 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
320 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
324 QString special = widthUnitsLC->currentText();
325 QString value = widthED->text();
326 if (special == qt_("Height")) {
329 } else if (special == qt_("Depth")) {
332 } else if (special == qt_("Total Height")) {
335 } else if (special == qt_("Width")) {
339 // the user might insert a non-special value in the line edit
340 if (isValidLength(fromqstr(value))) {
344 params_.special = fromqstr(ids_spec_[i]);
348 width = fromqstr(value);
349 // beware: bogosity! the unit is simply ignored in this case
352 width = widgetsToLength(widthED, widthUnitsLC);
355 params_.width = Length(width);
359 special = heightUnitsLC->currentText();
360 value = heightED->text();
361 if (special == qt_("Height")) {
364 } else if (special == qt_("Depth")) {
367 } else if (special == qt_("Total Height")) {
370 } else if (special == qt_("Width")) {
374 // the user might insert a non-special value in the line edit
375 if (isValidLength(fromqstr(value))) {
379 params_.height_special = fromqstr(ids_spec_[i]);
382 if (spec && !isValidLength(fromqstr(heightED->text()))) {
383 height = fromqstr(value);
384 // beware: bogosity! the unit is simply ignored in this case
387 height = widgetsToLength(heightED, heightUnitsLC);
389 // the height parameter is omitted in InsetBox.cpp when the value
390 // is "1in" and "Total Height" is used as unit.
391 // 1in + "Total Height" means "1\height" which is the LaTeX default when
392 // no height is given
393 if (heightCB->checkState() == Qt::Checked)
394 params_.height = Length(height);
396 params_.height = Length("1in");
397 params_.height_special = fromqstr(ids_spec_[3]);
402 void GuiBox::setSpecial(bool ibox)
404 // FIXME: Needed? Already done in the constructor
405 ids_spec_ = boxGuiSpecialLengthIds();
406 gui_names_spec_ = boxGuiSpecialLengthNames();
408 // check if the widget contains the special units
409 int count = widthUnitsLC->count();
410 bool has_special = false;
411 for (int i = 0; i != count; ++i)
412 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
414 // insert 'em if needed...
415 if (!ibox && !has_special) {
416 for (int i = 1; i < gui_names_spec_.size(); ++i)
417 widthUnitsLC->addItem(gui_names_spec_[i]);
418 // ... or remove 'em if needed
419 } else if (ibox && has_special) {
420 widthUnitsLC->clear();
421 for (int i = 0; i != num_units; ++i)
422 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
427 void GuiBox::setInnerType(bool frameless, int i)
429 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
430 // we have to remove "none" then and adjust the combo
433 innerBoxCO->addItem(qt_("Parbox"));
434 innerBoxCO->addItem(qt_("Minipage"));
436 innerBoxCO->setCurrentIndex(i - 1);
438 innerBoxCO->setCurrentIndex(i);
440 if (innerBoxCO->count() == 2)
443 innerBoxCO->addItem(qt_("None"));
444 innerBoxCO->addItem(qt_("Parbox"));
445 innerBoxCO->addItem(qt_("Minipage"));
446 innerBoxCO->setCurrentIndex(i);
450 bool GuiBox::initialiseParams(string const & data)
452 InsetBox::string2params(data, params_);
458 void GuiBox::clearParams()
460 params_ = InsetBoxParams("");
464 void GuiBox::dispatchParams()
466 dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
470 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
473 } // namespace frontend
477 #include "GuiBox_moc.cpp"