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 "qt_helpers.h"
21 #include "Validator.h"
23 #include "insets/InsetBox.h"
25 #include "support/gettext.h"
26 #include "support/foreach.h"
27 #include "support/lstrings.h"
29 #include <QPushButton>
42 static QStringList boxGuiIds()
45 << "Frameless" << "Boxed"
46 << "ovalbox" << "Ovalbox"
47 << "Shadowbox" << "Shaded"
52 static QStringList boxGuiNames()
55 << qt_("No frame") << qt_("Simple rectangular frame")
56 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
57 << qt_("Drop shadow") << qt_("Shaded background")
58 << qt_("Double rectangular frame");
62 static QStringList boxGuiSpecialLengthIds()
64 return QStringList() << "height" << "depth"
65 << "totalheight" << "width";
69 static QStringList boxGuiSpecialLengthNames()
71 return QStringList() << qt_("Height") << qt_("Depth")
72 << qt_("Total Height") << qt_("Width");
76 GuiBox::GuiBox(GuiView & lv)
77 : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
81 // fill the box type choice
83 gui_names_ = boxGuiNames();
84 foreach (QString const & str, gui_names_)
87 // add the special units to the height choice
88 // width needs different handling
89 ids_spec_ = boxGuiSpecialLengthIds();
90 gui_names_spec_ = boxGuiSpecialLengthNames();
91 for (int i = 0; i != ids_spec_.size(); ++i)
92 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
94 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
95 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
96 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
97 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
99 connect(widthED, SIGNAL(textChanged(QString)),
100 this, SLOT(change_adaptor()));
101 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
102 this, SLOT(change_adaptor()));
103 connect(valignCO, SIGNAL(highlighted(QString)),
104 this, SLOT(change_adaptor()));
105 connect(heightCB, SIGNAL(stateChanged(int)),
106 this, SLOT(change_adaptor()));
107 connect(heightED, SIGNAL(textChanged(QString)),
108 this, SLOT(change_adaptor()));
109 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
110 this, SLOT(change_adaptor()));
111 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
112 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
113 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
114 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
115 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
116 connect(innerBoxCO, SIGNAL(activated(QString)),
117 this, SLOT(innerBoxChanged(QString)));
118 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
119 connect(pagebreakCB, SIGNAL(stateChanged(int)),
120 this, SLOT(pagebreakClicked()));
122 heightED->setValidator(unsignedLengthValidator(heightED));
123 widthED->setValidator(unsignedLengthValidator(widthED));
125 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
127 bc().addReadOnly(typeCO);
128 bc().addReadOnly(innerBoxCO);
129 bc().addReadOnly(valignCO);
130 bc().addReadOnly(ialignCO);
131 bc().addReadOnly(halignCO);
132 bc().addReadOnly(widthED);
133 bc().addReadOnly(widthUnitsLC);
134 bc().addReadOnly(heightCB);
135 bc().addReadOnly(heightED);
136 bc().addReadOnly(heightUnitsLC);
137 bc().addReadOnly(pagebreakCB);
139 bc().setRestore(restorePB);
141 bc().setApply(applyPB);
142 bc().setCancel(closePB);
144 // initialize the length validator
145 bc().addCheckedLineEdit(widthED, widthLA);
146 bc().addCheckedLineEdit(heightED, heightCB);
150 void GuiBox::change_adaptor()
156 void GuiBox::innerBoxChanged(QString const & str)
158 bool const ibox = (str != qt_("None"));
159 valignCO->setEnabled(ibox);
160 ialignCO->setEnabled(ibox);
161 halignCO->setEnabled(!ibox);
162 heightCB->setEnabled(ibox);
163 pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
164 heightED->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
165 heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
170 void GuiBox::typeChanged(int index)
172 bool const frameless = (index == 0);
174 valignCO->setEnabled(true);
175 ialignCO->setEnabled(true);
176 halignCO->setEnabled(false);
177 heightCB->setEnabled(true);
178 heightED->setEnabled(heightCB->checkState() == Qt::Checked);
179 heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked);
183 pagebreakCB->setChecked(false);
184 int itype = innerBoxCO->currentIndex();
185 if (innerBoxCO->count() == 2)
187 pagebreakCB->setEnabled(index == 1 && itype == 0);
188 widthED->setEnabled(index != 5);
189 widthUnitsLC->setEnabled(index != 5);
190 setInnerType(frameless, itype);
194 void GuiBox::restoreClicked()
196 setInnerType(true, 2);
197 widthED->setText("100");
198 widthUnitsLC->setCurrentItem(Length::PCW);
199 heightCB->setCheckState(Qt::Checked);
200 heightED->setText("1");
201 heightUnitsLC->setCurrentItem("totalheight");
205 void GuiBox::pagebreakClicked()
207 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
208 innerBoxCO->setEnabled(!pbreak);
209 widthED->setEnabled(!pbreak);
210 widthUnitsLC->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 const default_unit = Length::defaultUnit();
269 lengthToWidgets(widthED, widthUnitsLC,
270 (params_.width).asString(), default_unit);
272 QString const special = toqstr(params_.special);
273 if (!special.isEmpty() && special != "none")
274 widthUnitsLC->setCurrentItem(special);
276 lengthToWidgets(heightED, heightUnitsLC,
277 (params_.height).asString(), default_unit);
279 QString const height_special = toqstr(params_.height_special);
280 if (!height_special.isEmpty() && height_special != "none")
281 heightUnitsLC->setCurrentItem(height_special);
282 // set no optional height if the value is the default "1\height"
283 // (special units like \height are handled as "in",
284 if (height_special == "totalheight" && params_.height == Length("1in"))
285 heightCB->setCheckState(Qt::Unchecked);
287 heightCB->setCheckState(Qt::Checked);
289 heightCB->setEnabled(ibox);
293 void GuiBox::applyView()
296 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
298 params_.type = "Framed";
300 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
303 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
305 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
307 params_.pos = "tcb"[valignCO->currentIndex()];
308 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
309 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
312 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
313 QString value = widthED->text();
314 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
315 params_.special = fromqstr(unit);
316 // Note: the unit is simply ignored in this case
317 params_.width = Length(value.toDouble(), Length::IN);
319 params_.special = "none";
320 params_.width = Length(widgetsToLength(widthED, widthUnitsLC));
323 // the height parameter is omitted if the value
324 // is "1in" and "Total Height" is used as unit.
325 // 1in + "Total Height" means "1\height" which is the LaTeX default
326 // if no height is given
327 if (heightCB->checkState() == Qt::Unchecked) {
328 params_.height = Length("1in");
329 params_.height_special = "totalheight";
331 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
332 value = heightED->text();
333 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
334 params_.height_special = fromqstr(unit);
335 // Note: the unit is simply ignored in this case
336 params_.height = Length(value.toDouble(), Length::IN);
338 params_.height_special = "none";
340 Length(widgetsToLength(heightED, heightUnitsLC));
346 void GuiBox::setSpecial(bool ibox)
348 QString const last_item =
349 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
351 // check if the widget contains the special units
352 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
353 // insert 'em if needed...
354 if (!ibox && !has_special) {
355 for (int i = 1; i < ids_spec_.size(); ++i)
356 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
357 // ... or remove 'em if needed
358 } else if (ibox && has_special) {
359 for (int i = 1; i < ids_spec_.size(); ++i) {
360 int n = widthUnitsLC->findData(ids_spec_[i]);
362 widthUnitsLC->removeItem(n);
365 // restore selected text, if possible
366 widthUnitsLC->setCurrentItem(last_item);
370 void GuiBox::setInnerType(bool frameless, int i)
372 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
373 // we have to remove "none" then and adjust the combo
376 innerBoxCO->addItem(qt_("Parbox"));
377 innerBoxCO->addItem(qt_("Minipage"));
379 innerBoxCO->setCurrentIndex(i - 1);
381 innerBoxCO->setCurrentIndex(i);
384 innerBoxCO->addItem(qt_("None"));
385 innerBoxCO->addItem(qt_("Parbox"));
386 innerBoxCO->addItem(qt_("Minipage"));
387 innerBoxCO->setCurrentIndex(i);
391 bool GuiBox::initialiseParams(string const & data)
393 InsetBox::string2params(data, params_);
398 void GuiBox::clearParams()
400 params_ = InsetBoxParams("");
404 void GuiBox::dispatchParams()
406 dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
410 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
413 } // namespace frontend
417 #include "moc_GuiBox.cpp"