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(QWidget * parent) : InsetParamsWidget(parent)
80 // fill the box type choice
82 gui_names_ = boxGuiNames();
83 foreach (QString const & str, gui_names_)
86 // add the special units to the height choice
87 // width needs different handling
88 ids_spec_ = boxGuiSpecialLengthIds();
89 gui_names_spec_ = boxGuiSpecialLengthNames();
90 for (int i = 0; i != ids_spec_.size(); ++i)
91 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
93 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
94 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
95 this, SIGNAL(changed()));
96 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
97 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
98 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
99 this, SIGNAL(changed()));
100 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
101 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
103 heightED->setValidator(unsignedLengthValidator(heightED));
104 widthED->setValidator(unsignedLengthValidator(widthED));
106 // initialize the length validator
107 addCheckedWidget(widthED, widthLA);
108 addCheckedWidget(heightED, heightCB);
114 void GuiBox::on_innerBoxCO_activated(QString const & str)
116 bool const ibox = (str != qt_("None"));
117 valignCO->setEnabled(ibox);
118 ialignCO->setEnabled(ibox);
119 halignCO->setEnabled(!ibox);
120 heightCB->setEnabled(ibox);
121 pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
127 void GuiBox::on_typeCO_activated(int index)
129 bool const frameless = (index == 0);
131 valignCO->setEnabled(true);
132 ialignCO->setEnabled(true);
133 halignCO->setEnabled(false);
134 heightCB->setEnabled(true);
138 pagebreakCB->setChecked(false);
139 int itype = innerBoxCO->currentIndex();
140 if (innerBoxCO->count() == 2)
142 pagebreakCB->setEnabled(index == 1 && itype == 0);
143 widthED->setEnabled(index != 5);
144 widthUnitsLC->setEnabled(index != 5);
145 setInnerType(frameless, itype);
150 void GuiBox::initDialog()
152 setInnerType(true, 2);
153 widthED->setText("100");
154 widthUnitsLC->setCurrentItem(Length::PCW);
155 heightCB->setCheckState(Qt::Checked);
156 heightED->setText("1");
157 heightUnitsLC->setCurrentItem("totalheight");
161 void GuiBox::on_heightCB_stateChanged(int state)
163 bool const enable = (innerBoxCO->currentText() != qt_("None"))
164 && (state == Qt::Checked);
165 heightED->setEnabled(enable);
166 heightUnitsLC->setEnabled(enable);
171 void GuiBox::on_pagebreakCB_stateChanged()
173 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
174 innerBoxCO->setEnabled(!pbreak);
175 widthED->setEnabled(!pbreak);
176 widthUnitsLC->setEnabled(!pbreak);
178 on_typeCO_activated(typeCO->currentIndex());
181 valignCO->setEnabled(false);
182 ialignCO->setEnabled(false);
183 halignCO->setEnabled(false);
184 heightCB->setEnabled(false);
185 heightED->setEnabled(false);
186 heightUnitsLC->setEnabled(false);
192 void GuiBox::paramsToDialog(Inset const * inset)
194 InsetBox const * box = static_cast<InsetBox const *>(inset);
195 InsetBoxParams const & params = box->params();
196 QString type = toqstr(params.type);
197 if (type == "Framed") {
198 pagebreakCB->setChecked(true);
201 pagebreakCB->setChecked(false);
204 pagebreakCB->setEnabled(type == "Boxed" && !params.inner_box);
206 for (int i = 0; i != gui_names_.size(); ++i) {
208 typeCO->setCurrentIndex(i);
213 if (!params.inner_box)
216 if (params.use_parbox)
219 bool frameless = (params.type == "Frameless");
220 setInnerType(frameless, inner_type);
223 valignCO->setCurrentIndex(string("tcb").find(c, 0));
224 c = params.inner_pos;
225 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
227 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
229 bool ibox = params.inner_box;
230 valignCO->setEnabled(ibox);
231 ialignCO->setEnabled(ibox);
232 halignCO->setEnabled(!ibox);
235 Length::UNIT const default_unit = Length::defaultUnit();
237 lengthToWidgets(widthED, widthUnitsLC,
238 (params.width).asString(), default_unit);
240 QString const special = toqstr(params.special);
241 if (!special.isEmpty() && special != "none")
242 widthUnitsLC->setCurrentItem(special);
244 lengthToWidgets(heightED, heightUnitsLC,
245 (params.height).asString(), default_unit);
247 QString const height_special = toqstr(params.height_special);
248 if (!height_special.isEmpty() && height_special != "none")
249 heightUnitsLC->setCurrentItem(height_special);
250 // set no optional height if the value is the default "1\height"
251 // (special units like \height are handled as "in",
252 // FIXME: this is a very bad UI, this check box should be disabled in
253 // this case, not forced to 'unchecked' state.
254 if (height_special == "totalheight" && params.height == Length("1in"))
255 heightCB->setCheckState(Qt::Unchecked);
257 heightCB->setCheckState(Qt::Checked);
259 heightCB->setEnabled(ibox);
263 docstring GuiBox::dialogToParams() const
265 bool const pagebreak =
266 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
271 box_type = fromqstr(ids_[typeCO->currentIndex()]);
273 InsetBoxParams params(box_type);
275 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
277 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
279 params.pos = "tcb"[valignCO->currentIndex()];
280 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
281 params.hor_pos = "lcrs"[halignCO->currentIndex()];
284 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
285 QString value = widthED->text();
286 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
287 params.special = fromqstr(unit);
288 // Note: the unit is simply ignored in this case
289 params.width = Length(value.toDouble(), Length::IN);
291 params.special = "none";
292 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
295 // the height parameter is omitted if the value
296 // is "1in" and "Total Height" is used as unit.
297 // 1in + "Total Height" means "1\height" which is the LaTeX default
298 // if no height is given
299 if (heightCB->checkState() == Qt::Unchecked) {
300 params.height = Length("1in");
301 params.height_special = "totalheight";
303 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
304 value = heightED->text();
305 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
306 params.height_special = fromqstr(unit);
307 // Note: the unit is simply ignored in this case
308 params.height = Length(value.toDouble(), Length::IN);
310 params.height_special = "none";
312 Length(widgetsToLength(heightED, heightUnitsLC));
315 return from_ascii(InsetBox::params2string(params));
319 void GuiBox::setSpecial(bool ibox)
321 QString const last_item =
322 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
324 // check if the widget contains the special units
325 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
326 // insert 'em if needed...
327 if (!ibox && !has_special) {
328 for (int i = 1; i < ids_spec_.size(); ++i)
329 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
330 // ... or remove 'em if needed
331 } else if (ibox && has_special) {
332 for (int i = 1; i < ids_spec_.size(); ++i) {
333 int n = widthUnitsLC->findData(ids_spec_[i]);
335 widthUnitsLC->removeItem(n);
338 // restore selected text, if possible
339 widthUnitsLC->setCurrentItem(last_item);
343 void GuiBox::setInnerType(bool frameless, int i)
345 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
346 // we have to remove "none" then and adjust the combo
349 innerBoxCO->addItem(qt_("Parbox"));
350 innerBoxCO->addItem(qt_("Minipage"));
352 innerBoxCO->setCurrentIndex(i - 1);
354 innerBoxCO->setCurrentIndex(i);
357 innerBoxCO->addItem(qt_("None"));
358 innerBoxCO->addItem(qt_("Parbox"));
359 innerBoxCO->addItem(qt_("Minipage"));
360 innerBoxCO->setCurrentIndex(i);
364 } // namespace frontend
368 #include "moc_GuiBox.cpp"