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 "LengthCombo.h"
19 #include "qt_helpers.h"
20 #include "Validator.h"
22 #include "insets/InsetBox.h"
24 #include "support/gettext.h"
25 #include "support/foreach.h"
26 #include "support/lstrings.h"
28 #include <QPushButton>
41 static QStringList boxGuiIds()
44 << "Frameless" << "Boxed"
45 << "ovalbox" << "Ovalbox"
46 << "Shadowbox" << "Shaded"
51 static QStringList boxGuiNames()
54 << qt_("No frame") << qt_("Simple rectangular frame")
55 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
56 << qt_("Drop shadow") << qt_("Shaded background")
57 << qt_("Double rectangular frame");
61 static QStringList boxGuiSpecialLengthIds()
63 return QStringList() << "height" << "depth"
64 << "totalheight" << "width";
68 static QStringList boxGuiSpecialLengthNames()
70 return QStringList() << qt_("Height") << qt_("Depth")
71 << qt_("Total Height") << qt_("Width");
75 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
79 // fill the box type choice
81 gui_names_ = boxGuiNames();
82 for (int i = 0; i != ids_.size(); ++i)
83 typeCO->addItem(gui_names_[i], ids_[i]);
85 // add the special units to the height choice
86 // width needs different handling
87 ids_spec_ = boxGuiSpecialLengthIds();
88 gui_names_spec_ = boxGuiSpecialLengthNames();
89 for (int i = 0; i != ids_spec_.size(); ++i)
90 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
92 connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
93 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
94 this, SIGNAL(changed()));
95 connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
96 connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
97 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
98 this, SIGNAL(changed()));
99 connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
100 connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
102 heightED->setValidator(unsignedLengthValidator(heightED));
103 widthED->setValidator(unsignedLengthValidator(widthED));
105 // initialize the length validator
106 addCheckedWidget(widthED, widthLA);
107 addCheckedWidget(heightED, heightCB);
113 void GuiBox::on_innerBoxCO_activated(int /* index */)
116 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
117 // handle parbox and minipage the same way
120 && itype != "makebox");
121 QString const outer =
122 typeCO->itemData(typeCO->currentIndex()).toString();
123 valignCO->setEnabled(ibox);
124 ialignCO->setEnabled(ibox);
125 if (heightCB->isChecked() && !ibox)
126 heightCB->setChecked(false);
127 heightCB->setEnabled(ibox);
128 // except for frameless and boxed, the width cannot be specified if
129 // there is no inner box
130 bool const width_enabled =
131 ibox || outer == "Frameless" || outer == "Boxed";
132 widthED->setEnabled(width_enabled);
133 widthUnitsLC->setEnabled(width_enabled);
134 // halign is only allowed for Boxed without inner box or for makebox
135 halignCO->setEnabled((!ibox && outer == "Boxed")
136 || (itype == "makebox"));
137 // pagebreak is only allowed for Boxed without inner box
138 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
144 void GuiBox::on_typeCO_activated(int index)
147 typeCO->itemData(index).toString();
148 bool const frameless = (type == "Frameless");
150 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
151 setInnerType(frameless, itype);
152 // refresh itype because it might have been changed in setInnerType
154 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
155 // handle parbox and minipage the same way
158 && itype != "makebox");
159 if (frameless && itype != "makebox") {
160 valignCO->setEnabled(ibox);
161 ialignCO->setEnabled(ibox);
162 if (heightCB->isChecked() && !ibox)
163 heightCB->setChecked(false);
164 heightCB->setEnabled(ibox);
167 // except for frameless and boxed, the width cannot be specified if
168 // there is no inner box
169 bool const width_enabled =
170 itype != "none" || frameless || type == "Boxed";
171 widthED->setEnabled(width_enabled);
172 widthUnitsLC->setEnabled(width_enabled);
173 // halign is only allowed for Boxed without inner box or for makebox
174 halignCO->setEnabled((type == "Boxed" && itype == "none") || (itype == "makebox"));
175 // pagebreak is only allowed for Boxed without inner box
176 pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
181 void GuiBox::initDialog()
183 setInnerType(true, toqstr("minipage"));
184 widthED->setText("100");
185 widthUnitsLC->setCurrentItem(Length::PCW);
186 heightED->setText("1");
187 heightUnitsLC->setCurrentItem("totalheight");
191 void GuiBox::on_heightCB_stateChanged(int state)
193 bool const enable = (innerBoxCO->currentText() != qt_("None"))
194 && (state == Qt::Checked);
195 heightED->setEnabled(enable);
196 heightUnitsLC->setEnabled(enable);
201 void GuiBox::on_pagebreakCB_stateChanged()
203 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
204 innerBoxCO->setEnabled(!pbreak);
205 widthED->setEnabled(!pbreak);
206 widthUnitsLC->setEnabled(!pbreak);
208 on_typeCO_activated(typeCO->currentIndex());
211 valignCO->setEnabled(false);
212 ialignCO->setEnabled(false);
213 halignCO->setEnabled(false);
214 heightCB->setEnabled(false);
215 heightED->setEnabled(false);
216 heightUnitsLC->setEnabled(false);
222 void GuiBox::paramsToDialog(Inset const * inset)
224 InsetBox const * box = static_cast<InsetBox const *>(inset);
225 InsetBoxParams const & params = box->params();
226 QString type = toqstr(params.type);
227 if (type == "Framed") {
228 pagebreakCB->setChecked(true);
231 pagebreakCB->setChecked(false);
234 typeCO->setCurrentIndex(typeCO->findData(type));
237 QString inner_type = "minipage";
238 if (!params.inner_box)
240 if (params.use_parbox)
241 inner_type = "parbox";
242 if (params.use_makebox)
243 inner_type = "makebox";
244 bool const frameless = (params.type == "Frameless");
245 setInnerType(frameless, inner_type);
248 valignCO->setCurrentIndex(string("tcb").find(c, 0));
249 c = params.inner_pos;
250 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
252 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
254 bool ibox = (params.inner_box && !params.use_makebox);
255 valignCO->setEnabled(ibox);
256 ialignCO->setEnabled(ibox);
259 // halign and pagebreak are only allowed for Boxed without inner box
260 halignCO->setEnabled((!ibox && type == "Boxed") || (params.use_makebox));
261 // pagebreak is only allowed for Boxed without inner box
262 pagebreakCB->setEnabled(!ibox && type == "Boxed");
264 // except for frameless and boxed, the width cannot be specified if
265 // there is no inner box
266 bool const width_enabled = (ibox || frameless || type == "Boxed");
267 widthED->setEnabled(width_enabled);
268 widthUnitsLC->setEnabled(width_enabled);
270 Length::UNIT const default_unit = Length::defaultUnit();
272 lengthToWidgets(widthED, widthUnitsLC,
273 (params.width).asString(), default_unit);
275 QString const special = toqstr(params.special);
276 if (!special.isEmpty() && special != "none")
277 widthUnitsLC->setCurrentItem(special);
279 lengthToWidgets(heightED, heightUnitsLC,
280 (params.height).asString(), default_unit);
282 QString const height_special = toqstr(params.height_special);
283 if (!height_special.isEmpty() && height_special != "none")
284 heightUnitsLC->setCurrentItem(height_special);
285 // set no optional height if the value is the default "1\height"
286 // (special units like \height are handled as "in",
287 // FIXME: this is a very bad UI, this check box should be disabled in
288 // this case, not forced to 'unchecked' state.
289 if (height_special == "totalheight" && params.height == Length("1in"))
290 heightCB->setCheckState(Qt::Unchecked);
292 heightCB->setCheckState(Qt::Checked);
294 heightCB->setEnabled(ibox);
298 docstring GuiBox::dialogToParams() const
300 bool const pagebreak =
301 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
306 box_type = fromqstr(typeCO->itemData(
307 typeCO->currentIndex()).toString());
309 InsetBoxParams params(box_type);
311 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
313 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
315 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
317 params.pos = "tcb"[valignCO->currentIndex()];
318 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
319 params.hor_pos = "lcrs"[halignCO->currentIndex()];
322 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
323 QString value = widthED->text();
324 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
325 params.special = fromqstr(unit);
326 // Note: the unit is simply ignored in this case
327 params.width = Length(value.toDouble(), Length::IN);
329 params.special = "none";
330 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
333 // the height parameter is omitted if the value
334 // is "1in" and "Total Height" is used as unit.
335 // 1in + "Total Height" means "1\height" which is the LaTeX default
336 // if no height is given
337 if (heightCB->checkState() == Qt::Unchecked) {
338 params.height = Length("1in");
339 params.height_special = "totalheight";
341 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
342 value = heightED->text();
343 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
344 params.height_special = fromqstr(unit);
345 // Note: the unit is simply ignored in this case
346 params.height = Length(value.toDouble(), Length::IN);
348 params.height_special = "none";
350 Length(widgetsToLength(heightED, heightUnitsLC));
353 return from_ascii(InsetBox::params2string(params));
357 void GuiBox::setSpecial(bool ibox)
359 QString const last_item =
360 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
362 // check if the widget contains the special units
363 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
364 // insert 'em if needed...
365 if (!ibox && !has_special) {
366 for (int i = 1; i < ids_spec_.size(); ++i)
367 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
368 // ... or remove 'em if needed
369 } else if (ibox && has_special) {
370 for (int i = 1; i < ids_spec_.size(); ++i) {
371 int n = widthUnitsLC->findData(ids_spec_[i]);
373 widthUnitsLC->removeItem(n);
376 // restore selected text, if possible
377 widthUnitsLC->setCurrentItem(last_item);
381 void GuiBox::setInnerType(bool frameless, QString const & type)
383 // with "frameless" boxes, inner box is mandatory
384 // (i.e. is the actual box)
385 // we have to remove "none" then and adjust the combo
388 innerBoxCO->addItem(qt_("None"), toqstr("none"));
390 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
391 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
392 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
393 int i = (innerBoxCO->findData(type) != -1)
394 ? innerBoxCO->findData(type) : 0;
395 innerBoxCO->setCurrentIndex(i);
398 } // namespace frontend
402 #include "moc_GuiBox.cpp"