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_disabled = (!ibox && outer != "Frameless" &&
133 widthED->setEnabled(!width_disabled);
134 widthUnitsLC->setEnabled(!width_disabled);
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_disabled = (itype == "none" && !frameless
172 widthED->setEnabled(!width_disabled);
173 widthUnitsLC->setEnabled(!width_disabled);
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_disabled = (!ibox && !frameless
269 widthED->setEnabled(!width_disabled);
270 widthUnitsLC->setEnabled(!width_disabled);
272 Length::UNIT const default_unit = Length::defaultUnit();
274 lengthToWidgets(widthED, widthUnitsLC,
275 (params.width).asString(), default_unit);
277 QString const special = toqstr(params.special);
278 if (!special.isEmpty() && special != "none")
279 widthUnitsLC->setCurrentItem(special);
281 lengthToWidgets(heightED, heightUnitsLC,
282 (params.height).asString(), default_unit);
284 QString const height_special = toqstr(params.height_special);
285 if (!height_special.isEmpty() && height_special != "none")
286 heightUnitsLC->setCurrentItem(height_special);
287 // set no optional height if the value is the default "1\height"
288 // (special units like \height are handled as "in",
289 // FIXME: this is a very bad UI, this check box should be disabled in
290 // this case, not forced to 'unchecked' state.
291 if (height_special == "totalheight" && params.height == Length("1in"))
292 heightCB->setCheckState(Qt::Unchecked);
294 heightCB->setCheckState(Qt::Checked);
296 heightCB->setEnabled(ibox);
300 docstring GuiBox::dialogToParams() const
302 bool const pagebreak =
303 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
308 box_type = fromqstr(typeCO->itemData(
309 typeCO->currentIndex()).toString());
311 InsetBoxParams params(box_type);
313 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
315 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
317 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
319 params.pos = "tcb"[valignCO->currentIndex()];
320 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
321 params.hor_pos = "lcrs"[halignCO->currentIndex()];
324 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
325 QString value = widthED->text();
326 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
327 params.special = fromqstr(unit);
328 // Note: the unit is simply ignored in this case
329 params.width = Length(value.toDouble(), Length::IN);
331 params.special = "none";
332 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
335 // the height parameter is omitted if the value
336 // is "1in" and "Total Height" is used as unit.
337 // 1in + "Total Height" means "1\height" which is the LaTeX default
338 // if no height is given
339 if (heightCB->checkState() == Qt::Unchecked) {
340 params.height = Length("1in");
341 params.height_special = "totalheight";
343 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
344 value = heightED->text();
345 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
346 params.height_special = fromqstr(unit);
347 // Note: the unit is simply ignored in this case
348 params.height = Length(value.toDouble(), Length::IN);
350 params.height_special = "none";
352 Length(widgetsToLength(heightED, heightUnitsLC));
355 return from_ascii(InsetBox::params2string(params));
359 void GuiBox::setSpecial(bool ibox)
361 QString const last_item =
362 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
364 // check if the widget contains the special units
365 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
366 // insert 'em if needed...
367 if (!ibox && !has_special) {
368 for (int i = 1; i < ids_spec_.size(); ++i)
369 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
370 // ... or remove 'em if needed
371 } else if (ibox && has_special) {
372 for (int i = 1; i < ids_spec_.size(); ++i) {
373 int n = widthUnitsLC->findData(ids_spec_[i]);
375 widthUnitsLC->removeItem(n);
378 // restore selected text, if possible
379 widthUnitsLC->setCurrentItem(last_item);
383 void GuiBox::setInnerType(bool frameless, QString const & type)
385 // with "frameless" boxes, inner box is mandatory
386 // (i.e. is the actual box)
387 // we have to remove "none" then and adjust the combo
390 innerBoxCO->addItem(qt_("None"), toqstr("none"));
392 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
393 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
394 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
395 int i = (innerBoxCO->findData(type) != -1)
396 ? innerBoxCO->findData(type) : 0;
397 innerBoxCO->setCurrentIndex(i);
400 } // namespace frontend
404 #include "moc_GuiBox.cpp"