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 : InsetDialog(lv, BOX_CODE, LFUN_BOX_INSERT, "box", "Box Settings")
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(widthED, SIGNAL(textChanged(QString)), this, SLOT(applyView()));
95 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
96 this, SLOT(applyView()));
97 connect(valignCO, SIGNAL(highlighted(QString)), this, SLOT(applyView()));
98 connect(heightED, SIGNAL(textChanged(QString)), this, SLOT(applyView()));
99 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
100 this, SLOT(applyView()));
101 connect(halignCO, SIGNAL(activated(int)), this, SLOT(applyView()));
102 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(applyView()));
104 heightED->setValidator(unsignedLengthValidator(heightED));
105 widthED->setValidator(unsignedLengthValidator(widthED));
107 // initialize the length validator
108 addCheckedWidget(widthED, widthLA);
109 addCheckedWidget(heightED, heightCB);
115 void GuiBox::enableView(bool enable)
117 typeCO->setEnabled(enable);
118 innerBoxCO->setEnabled(enable);
119 valignCO->setEnabled(enable);
120 ialignCO->setEnabled(enable);
121 halignCO->setEnabled(enable);
122 widthED->setEnabled(enable);
123 widthUnitsLC->setEnabled(enable);
124 heightCB->setEnabled(enable);
125 heightED->setEnabled(enable);
126 heightUnitsLC->setEnabled(enable);
127 pagebreakCB->setEnabled(enable);
131 void GuiBox::on_innerBoxCO_activated(QString const & str)
133 bool const ibox = (str != qt_("None"));
134 valignCO->setEnabled(ibox);
135 ialignCO->setEnabled(ibox);
136 halignCO->setEnabled(!ibox);
137 heightCB->setEnabled(ibox);
138 pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
144 void GuiBox::on_typeCO_activated(int index)
146 bool const frameless = (index == 0);
148 valignCO->setEnabled(true);
149 ialignCO->setEnabled(true);
150 halignCO->setEnabled(false);
151 heightCB->setEnabled(true);
155 pagebreakCB->setChecked(false);
156 int itype = innerBoxCO->currentIndex();
157 if (innerBoxCO->count() == 2)
159 pagebreakCB->setEnabled(index == 1 && itype == 0);
160 widthED->setEnabled(index != 5);
161 widthUnitsLC->setEnabled(index != 5);
162 setInnerType(frameless, itype);
167 void GuiBox::initDialog()
169 setInnerType(true, 2);
170 widthED->setText("100");
171 widthUnitsLC->setCurrentItem(Length::PCW);
172 heightCB->setCheckState(Qt::Checked);
173 heightED->setText("1");
174 heightUnitsLC->setCurrentItem("totalheight");
178 void GuiBox::on_heightCB_stateChanged(int state)
180 bool const enable = (innerBoxCO->currentText() != qt_("None"))
181 && (state == Qt::Checked);
182 heightED->setEnabled(enable);
183 heightUnitsLC->setEnabled(enable);
188 void GuiBox::on_pagebreakCB_stateChanged()
190 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
191 innerBoxCO->setEnabled(!pbreak);
192 widthED->setEnabled(!pbreak);
193 widthUnitsLC->setEnabled(!pbreak);
195 on_typeCO_activated(typeCO->currentIndex());
198 valignCO->setEnabled(false);
199 ialignCO->setEnabled(false);
200 halignCO->setEnabled(false);
201 heightCB->setEnabled(false);
202 heightED->setEnabled(false);
203 heightUnitsLC->setEnabled(false);
209 void GuiBox::paramsToDialog(Inset const * inset)
211 InsetBox const * box = static_cast<InsetBox const *>(inset);
212 InsetBoxParams const & params = box->params();
213 QString type = toqstr(params.type);
214 if (type == "Framed") {
215 pagebreakCB->setChecked(true);
218 pagebreakCB->setChecked(false);
221 pagebreakCB->setEnabled(type == "Boxed" && !params.inner_box);
223 for (int i = 0; i != gui_names_.size(); ++i) {
225 typeCO->setCurrentIndex(i);
230 if (!params.inner_box)
233 if (params.use_parbox)
236 bool frameless = (params.type == "Frameless");
237 setInnerType(frameless, inner_type);
240 valignCO->setCurrentIndex(string("tcb").find(c, 0));
241 c = params.inner_pos;
242 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
244 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
246 bool ibox = params.inner_box;
247 valignCO->setEnabled(ibox);
248 ialignCO->setEnabled(ibox);
249 halignCO->setEnabled(!ibox);
252 Length::UNIT const default_unit = Length::defaultUnit();
254 lengthToWidgets(widthED, widthUnitsLC,
255 (params.width).asString(), default_unit);
257 QString const special = toqstr(params.special);
258 if (!special.isEmpty() && special != "none")
259 widthUnitsLC->setCurrentItem(special);
261 lengthToWidgets(heightED, heightUnitsLC,
262 (params.height).asString(), default_unit);
264 QString const height_special = toqstr(params.height_special);
265 if (!height_special.isEmpty() && height_special != "none")
266 heightUnitsLC->setCurrentItem(height_special);
267 // set no optional height if the value is the default "1\height"
268 // (special units like \height are handled as "in",
269 // FIXME: this is a very bad UI, this check box should be disabled in
270 // this case, not forced to 'unchecked' state.
271 if (height_special == "totalheight" && params.height == Length("1in"))
272 heightCB->setCheckState(Qt::Unchecked);
274 heightCB->setCheckState(Qt::Checked);
276 heightCB->setEnabled(ibox);
280 docstring GuiBox::dialogToParams() const
282 bool const pagebreak =
283 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
288 box_type = fromqstr(ids_[typeCO->currentIndex()]);
290 InsetBoxParams params(box_type);
292 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
294 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
296 params.pos = "tcb"[valignCO->currentIndex()];
297 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
298 params.hor_pos = "lcrs"[halignCO->currentIndex()];
301 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
302 QString value = widthED->text();
303 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
304 params.special = fromqstr(unit);
305 // Note: the unit is simply ignored in this case
306 params.width = Length(value.toDouble(), Length::IN);
308 params.special = "none";
309 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
312 // the height parameter is omitted if the value
313 // is "1in" and "Total Height" is used as unit.
314 // 1in + "Total Height" means "1\height" which is the LaTeX default
315 // if no height is given
316 if (heightCB->checkState() == Qt::Unchecked) {
317 params.height = Length("1in");
318 params.height_special = "totalheight";
320 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
321 value = heightED->text();
322 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
323 params.height_special = fromqstr(unit);
324 // Note: the unit is simply ignored in this case
325 params.height = Length(value.toDouble(), Length::IN);
327 params.height_special = "none";
329 Length(widgetsToLength(heightED, heightUnitsLC));
332 return from_ascii(InsetBox::params2string(params));
336 void GuiBox::setSpecial(bool ibox)
338 QString const last_item =
339 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
341 // check if the widget contains the special units
342 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
343 // insert 'em if needed...
344 if (!ibox && !has_special) {
345 for (int i = 1; i < ids_spec_.size(); ++i)
346 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
347 // ... or remove 'em if needed
348 } else if (ibox && has_special) {
349 for (int i = 1; i < ids_spec_.size(); ++i) {
350 int n = widthUnitsLC->findData(ids_spec_[i]);
352 widthUnitsLC->removeItem(n);
355 // restore selected text, if possible
356 widthUnitsLC->setCurrentItem(last_item);
360 void GuiBox::setInnerType(bool frameless, int i)
362 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
363 // we have to remove "none" then and adjust the combo
366 innerBoxCO->addItem(qt_("Parbox"));
367 innerBoxCO->addItem(qt_("Minipage"));
369 innerBoxCO->setCurrentIndex(i - 1);
371 innerBoxCO->setCurrentIndex(i);
374 innerBoxCO->addItem(qt_("None"));
375 innerBoxCO->addItem(qt_("Parbox"));
376 innerBoxCO->addItem(qt_("Minipage"));
377 innerBoxCO->setCurrentIndex(i);
382 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
385 } // namespace frontend
389 #include "moc_GuiBox.cpp"