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(innerBoxCO->currentIndex()).toString();
118 // handle parbox and minipage the same way
121 && itype != "makebox");
122 QString const outer =
123 typeCO->itemData(typeCO->currentIndex()).toString();
124 valignCO->setEnabled(ibox);
125 ialignCO->setEnabled(ibox);
126 if (heightCB->isChecked() && !ibox)
127 heightCB->setChecked(false);
128 heightCB->setEnabled(ibox);
129 // except for frameless and boxed, the width cannot be specified if
130 // there is no inner box
131 bool const width_enabled =
132 ibox || outer == "Frameless" || outer == "Boxed";
133 widthED->setEnabled(width_enabled);
134 widthUnitsLC->setEnabled(width_enabled);
135 // halign is only allowed for Boxed without inner box or for makebox
136 halignCO->setEnabled((!ibox && outer == "Boxed")
137 || (itype == "makebox"));
138 // pagebreak is only allowed for Boxed without inner box
139 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
145 void GuiBox::on_typeCO_activated(int index)
148 typeCO->itemData(index).toString();
149 bool const frameless = (type == "Frameless");
151 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
152 setInnerType(frameless, itype);
153 // refresh itype because it might have been changed in setInnerType
155 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
156 // handle parbox and minipage the same way
159 && itype != "makebox");
160 if (frameless && itype != "makebox") {
161 valignCO->setEnabled(ibox);
162 ialignCO->setEnabled(ibox);
163 if (heightCB->isChecked() && !ibox)
164 heightCB->setChecked(false);
165 heightCB->setEnabled(ibox);
168 // except for frameless and boxed, the width cannot be specified if
169 // there is no inner box
170 bool const width_enabled =
171 itype != "none" || frameless || type == "Boxed";
172 widthED->setEnabled(width_enabled);
173 widthUnitsLC->setEnabled(width_enabled);
174 // halign is only allowed for Boxed without inner box or for makebox
175 halignCO->setEnabled((type == "Boxed" && itype == "none") || (itype == "makebox"));
176 // pagebreak is only allowed for Boxed without inner box
177 pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
182 void GuiBox::initDialog()
184 setInnerType(true, toqstr("minipage"));
185 widthED->setText("100");
186 widthUnitsLC->setCurrentItem(Length::PCW);
187 heightED->setText("1");
188 heightUnitsLC->setCurrentItem("totalheight");
192 void GuiBox::on_heightCB_stateChanged(int state)
194 bool const enable = (innerBoxCO->currentText() != qt_("None"))
195 && (state == Qt::Checked);
196 heightED->setEnabled(enable);
197 heightUnitsLC->setEnabled(enable);
202 void GuiBox::on_pagebreakCB_stateChanged()
204 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
205 innerBoxCO->setEnabled(!pbreak);
206 widthED->setEnabled(!pbreak);
207 widthUnitsLC->setEnabled(!pbreak);
209 on_typeCO_activated(typeCO->currentIndex());
212 valignCO->setEnabled(false);
213 ialignCO->setEnabled(false);
214 halignCO->setEnabled(false);
215 heightCB->setEnabled(false);
216 heightED->setEnabled(false);
217 heightUnitsLC->setEnabled(false);
223 void GuiBox::paramsToDialog(Inset const * inset)
225 InsetBox const * box = static_cast<InsetBox const *>(inset);
226 InsetBoxParams const & params = box->params();
227 QString type = toqstr(params.type);
228 if (type == "Framed") {
229 pagebreakCB->setChecked(true);
232 pagebreakCB->setChecked(false);
235 typeCO->setCurrentIndex(typeCO->findData(type));
238 QString inner_type = "minipage";
239 if (!params.inner_box)
241 if (params.use_parbox)
242 inner_type = "parbox";
243 if (params.use_makebox)
244 inner_type = "makebox";
245 bool const frameless = (params.type == "Frameless");
246 setInnerType(frameless, inner_type);
249 valignCO->setCurrentIndex(string("tcb").find(c, 0));
250 c = params.inner_pos;
251 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
253 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
255 bool ibox = (params.inner_box && !params.use_makebox);
256 valignCO->setEnabled(ibox);
257 ialignCO->setEnabled(ibox);
260 // halign and pagebreak are only allowed for Boxed without inner box
261 halignCO->setEnabled((!ibox && type == "Boxed") || (params.use_makebox));
262 // pagebreak is only allowed for Boxed without inner box
263 pagebreakCB->setEnabled(!ibox && type == "Boxed");
265 // except for frameless and boxed, the width cannot be specified if
266 // there is no inner box
267 bool const width_enabled = (ibox || frameless || type == "Boxed");
268 widthED->setEnabled(width_enabled);
269 widthUnitsLC->setEnabled(width_enabled);
271 Length::UNIT const default_unit = Length::defaultUnit();
273 lengthToWidgets(widthED, widthUnitsLC,
274 (params.width).asString(), default_unit);
276 QString const special = toqstr(params.special);
277 if (!special.isEmpty() && special != "none")
278 widthUnitsLC->setCurrentItem(special);
280 lengthToWidgets(heightED, heightUnitsLC,
281 (params.height).asString(), default_unit);
283 QString const height_special = toqstr(params.height_special);
284 if (!height_special.isEmpty() && height_special != "none")
285 heightUnitsLC->setCurrentItem(height_special);
286 // set no optional height if the value is the default "1\height"
287 // (special units like \height are handled as "in",
288 // FIXME: this is a very bad UI, this check box should be disabled in
289 // this case, not forced to 'unchecked' state.
290 if (height_special == "totalheight" && params.height == Length("1in"))
291 heightCB->setCheckState(Qt::Unchecked);
293 heightCB->setCheckState(Qt::Checked);
295 heightCB->setEnabled(ibox);
299 docstring GuiBox::dialogToParams() const
301 bool const pagebreak =
302 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
307 box_type = fromqstr(typeCO->itemData(
308 typeCO->currentIndex()).toString());
310 InsetBoxParams params(box_type);
312 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
314 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
316 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
318 params.pos = "tcb"[valignCO->currentIndex()];
319 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
320 params.hor_pos = "lcrs"[halignCO->currentIndex()];
323 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
324 QString value = widthED->text();
325 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
326 params.special = fromqstr(unit);
327 // Note: the unit is simply ignored in this case
328 params.width = Length(value.toDouble(), Length::IN);
330 params.special = "none";
331 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
334 // the height parameter is omitted if the value
335 // is "1in" and "Total Height" is used as unit.
336 // 1in + "Total Height" means "1\height" which is the LaTeX default
337 // if no height is given
338 if (heightCB->checkState() == Qt::Unchecked) {
339 params.height = Length("1in");
340 params.height_special = "totalheight";
342 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
343 value = heightED->text();
344 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
345 params.height_special = fromqstr(unit);
346 // Note: the unit is simply ignored in this case
347 params.height = Length(value.toDouble(), Length::IN);
349 params.height_special = "none";
351 Length(widgetsToLength(heightED, heightUnitsLC));
354 return from_ascii(InsetBox::params2string(params));
358 void GuiBox::setSpecial(bool ibox)
360 QString const last_item =
361 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
363 // check if the widget contains the special units
364 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
365 // insert 'em if needed...
366 if (!ibox && !has_special) {
367 for (int i = 1; i < ids_spec_.size(); ++i)
368 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
369 // ... or remove 'em if needed
370 } else if (ibox && has_special) {
371 for (int i = 1; i < ids_spec_.size(); ++i) {
372 int n = widthUnitsLC->findData(ids_spec_[i]);
374 widthUnitsLC->removeItem(n);
377 // restore selected text, if possible
378 widthUnitsLC->setCurrentItem(last_item);
382 void GuiBox::setInnerType(bool frameless, QString const & type)
384 // with "frameless" boxes, inner box is mandatory
385 // (i.e. is the actual box)
386 // we have to remove "none" then and adjust the combo
389 innerBoxCO->addItem(qt_("None"), toqstr("none"));
391 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
392 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
393 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
394 int i = (innerBoxCO->findData(type) != -1)
395 ? innerBoxCO->findData(type) : 0;
396 innerBoxCO->setCurrentIndex(i);
399 } // namespace frontend
403 #include "moc_GuiBox.cpp"