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 for (int i = 0; i != ids_.size(); ++i)
84 typeCO->addItem(gui_names_[i], ids_[i]);
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(int index)
117 (innerBoxCO->itemData(index).toString() != "none");
118 QString const outer =
119 typeCO->itemData(typeCO->currentIndex()).toString();
120 valignCO->setEnabled(ibox);
121 ialignCO->setEnabled(ibox);
122 halignCO->setEnabled(!ibox);
123 heightCB->setEnabled(ibox);
124 // except for frameless and boxed, the width cannot be specified if
125 // there is no inner box
126 bool const width_disabled = (!ibox && outer != "Frameless" &&
128 widthED->setEnabled(!width_disabled);
129 widthUnitsLC->setEnabled(!width_disabled);
130 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
136 void GuiBox::on_typeCO_activated(int index)
139 typeCO->itemData(index).toString();
140 bool const frameless = (type == "Frameless");
142 valignCO->setEnabled(true);
143 ialignCO->setEnabled(true);
144 halignCO->setEnabled(false);
145 heightCB->setEnabled(true);
149 pagebreakCB->setChecked(false);
151 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
152 pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
153 // except for frameless and boxed, the width cannot be specified if
154 // there is no inner box
155 bool const width_disabled = (itype == "none" && !frameless
157 widthED->setEnabled(!width_disabled);
158 widthUnitsLC->setEnabled(!width_disabled);
159 setInnerType(frameless, itype);
164 void GuiBox::initDialog()
166 setInnerType(true, toqstr("minipage"));
167 widthED->setText("100");
168 widthUnitsLC->setCurrentItem(Length::PCW);
169 heightCB->setCheckState(Qt::Checked);
170 heightED->setText("1");
171 heightUnitsLC->setCurrentItem("totalheight");
175 void GuiBox::on_heightCB_stateChanged(int state)
177 bool const enable = (innerBoxCO->currentText() != qt_("None"))
178 && (state == Qt::Checked);
179 heightED->setEnabled(enable);
180 heightUnitsLC->setEnabled(enable);
185 void GuiBox::on_pagebreakCB_stateChanged()
187 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
188 innerBoxCO->setEnabled(!pbreak);
189 widthED->setEnabled(!pbreak);
190 widthUnitsLC->setEnabled(!pbreak);
192 on_typeCO_activated(typeCO->currentIndex());
195 valignCO->setEnabled(false);
196 ialignCO->setEnabled(false);
197 halignCO->setEnabled(false);
198 heightCB->setEnabled(false);
199 heightED->setEnabled(false);
200 heightUnitsLC->setEnabled(false);
206 void GuiBox::paramsToDialog(Inset const * inset)
208 InsetBox const * box = static_cast<InsetBox const *>(inset);
209 InsetBoxParams const & params = box->params();
210 QString type = toqstr(params.type);
211 if (type == "Framed") {
212 pagebreakCB->setChecked(true);
215 pagebreakCB->setChecked(false);
218 pagebreakCB->setEnabled(type == "Boxed" && !params.inner_box);
220 typeCO->setCurrentIndex(typeCO->findData(type));
223 QString inner_type = "minipage";
224 if (!params.inner_box)
226 if (params.use_parbox)
227 inner_type = "parbox";
228 bool const frameless = (params.type == "Frameless");
229 setInnerType(frameless, inner_type);
232 valignCO->setCurrentIndex(string("tcb").find(c, 0));
233 c = params.inner_pos;
234 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
236 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
238 bool ibox = params.inner_box;
239 valignCO->setEnabled(ibox);
240 ialignCO->setEnabled(ibox);
241 halignCO->setEnabled(!ibox);
244 Length::UNIT const default_unit = Length::defaultUnit();
246 lengthToWidgets(widthED, widthUnitsLC,
247 (params.width).asString(), default_unit);
249 QString const special = toqstr(params.special);
250 if (!special.isEmpty() && special != "none")
251 widthUnitsLC->setCurrentItem(special);
253 lengthToWidgets(heightED, heightUnitsLC,
254 (params.height).asString(), default_unit);
256 QString const height_special = toqstr(params.height_special);
257 if (!height_special.isEmpty() && height_special != "none")
258 heightUnitsLC->setCurrentItem(height_special);
259 // set no optional height if the value is the default "1\height"
260 // (special units like \height are handled as "in",
261 // FIXME: this is a very bad UI, this check box should be disabled in
262 // this case, not forced to 'unchecked' state.
263 if (height_special == "totalheight" && params.height == Length("1in"))
264 heightCB->setCheckState(Qt::Unchecked);
266 heightCB->setCheckState(Qt::Checked);
268 heightCB->setEnabled(ibox);
272 docstring GuiBox::dialogToParams() const
274 bool const pagebreak =
275 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
280 box_type = fromqstr(typeCO->itemData(
281 typeCO->currentIndex()).toString());
283 InsetBoxParams params(box_type);
285 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
287 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
289 params.pos = "tcb"[valignCO->currentIndex()];
290 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
291 params.hor_pos = "lcrs"[halignCO->currentIndex()];
294 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
295 QString value = widthED->text();
296 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
297 params.special = fromqstr(unit);
298 // Note: the unit is simply ignored in this case
299 params.width = Length(value.toDouble(), Length::IN);
301 params.special = "none";
302 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
305 // the height parameter is omitted if the value
306 // is "1in" and "Total Height" is used as unit.
307 // 1in + "Total Height" means "1\height" which is the LaTeX default
308 // if no height is given
309 if (heightCB->checkState() == Qt::Unchecked) {
310 params.height = Length("1in");
311 params.height_special = "totalheight";
313 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
314 value = heightED->text();
315 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
316 params.height_special = fromqstr(unit);
317 // Note: the unit is simply ignored in this case
318 params.height = Length(value.toDouble(), Length::IN);
320 params.height_special = "none";
322 Length(widgetsToLength(heightED, heightUnitsLC));
325 return from_ascii(InsetBox::params2string(params));
329 void GuiBox::setSpecial(bool ibox)
331 QString const last_item =
332 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
334 // check if the widget contains the special units
335 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
336 // insert 'em if needed...
337 if (!ibox && !has_special) {
338 for (int i = 1; i < ids_spec_.size(); ++i)
339 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
340 // ... or remove 'em if needed
341 } else if (ibox && has_special) {
342 for (int i = 1; i < ids_spec_.size(); ++i) {
343 int n = widthUnitsLC->findData(ids_spec_[i]);
345 widthUnitsLC->removeItem(n);
348 // restore selected text, if possible
349 widthUnitsLC->setCurrentItem(last_item);
353 void GuiBox::setInnerType(bool frameless, QString const & type)
355 // with "frameless" boxes, inner box is mandatory
356 // (i.e. is the actual box)
357 // we have to remove "none" then and adjust the combo
360 innerBoxCO->addItem(qt_("None"), toqstr("none"));
361 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
362 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
363 int i = (innerBoxCO->findData(type) != -1)
364 ? innerBoxCO->findData(type) : 0;
365 innerBoxCO->setCurrentIndex(i);
368 } // namespace frontend
372 #include "moc_GuiBox.cpp"