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(QString const & str)
116 bool const ibox = (str != qt_("None"));
117 QString const outer =
118 typeCO->itemData(typeCO->currentIndex()).toString();
119 valignCO->setEnabled(ibox);
120 ialignCO->setEnabled(ibox);
121 halignCO->setEnabled(!ibox);
122 heightCB->setEnabled(ibox);
123 // except for frameless and boxed, the width cannot be specified if
124 // there is no inner box
125 bool const width_disabled = (!ibox && outer != "Frameless" &&
127 widthED->setEnabled(!width_disabled);
128 widthUnitsLC->setEnabled(!width_disabled);
129 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
135 void GuiBox::on_typeCO_activated(int index)
138 typeCO->itemData(index).toString();
139 bool const frameless = (type == "Frameless");
141 valignCO->setEnabled(true);
142 ialignCO->setEnabled(true);
143 halignCO->setEnabled(false);
144 heightCB->setEnabled(true);
148 pagebreakCB->setChecked(false);
150 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
151 pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
152 // except for frameless and boxed, the width cannot be specified if
153 // there is no inner box
154 bool const width_disabled = (itype == "none" && !frameless
156 widthED->setEnabled(!width_disabled);
157 widthUnitsLC->setEnabled(!width_disabled);
158 setInnerType(frameless, itype);
163 void GuiBox::initDialog()
165 setInnerType(true, toqstr("minipage"));
166 widthED->setText("100");
167 widthUnitsLC->setCurrentItem(Length::PCW);
168 heightCB->setCheckState(Qt::Checked);
169 heightED->setText("1");
170 heightUnitsLC->setCurrentItem("totalheight");
174 void GuiBox::on_heightCB_stateChanged(int state)
176 bool const enable = (innerBoxCO->currentText() != qt_("None"))
177 && (state == Qt::Checked);
178 heightED->setEnabled(enable);
179 heightUnitsLC->setEnabled(enable);
184 void GuiBox::on_pagebreakCB_stateChanged()
186 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
187 innerBoxCO->setEnabled(!pbreak);
188 widthED->setEnabled(!pbreak);
189 widthUnitsLC->setEnabled(!pbreak);
191 on_typeCO_activated(typeCO->currentIndex());
194 valignCO->setEnabled(false);
195 ialignCO->setEnabled(false);
196 halignCO->setEnabled(false);
197 heightCB->setEnabled(false);
198 heightED->setEnabled(false);
199 heightUnitsLC->setEnabled(false);
205 void GuiBox::paramsToDialog(Inset const * inset)
207 InsetBox const * box = static_cast<InsetBox const *>(inset);
208 InsetBoxParams const & params = box->params();
209 QString type = toqstr(params.type);
210 if (type == "Framed") {
211 pagebreakCB->setChecked(true);
214 pagebreakCB->setChecked(false);
217 pagebreakCB->setEnabled(type == "Boxed" && !params.inner_box);
219 typeCO->setCurrentIndex(typeCO->findData(type));
222 QString inner_type = "minipage";
223 if (!params.inner_box)
225 if (params.use_parbox)
226 inner_type = "parbox";
227 bool const frameless = (params.type == "Frameless");
228 setInnerType(frameless, inner_type);
231 valignCO->setCurrentIndex(string("tcb").find(c, 0));
232 c = params.inner_pos;
233 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
235 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
237 bool ibox = params.inner_box;
238 valignCO->setEnabled(ibox);
239 ialignCO->setEnabled(ibox);
240 halignCO->setEnabled(!ibox);
243 Length::UNIT const default_unit = Length::defaultUnit();
245 lengthToWidgets(widthED, widthUnitsLC,
246 (params.width).asString(), default_unit);
248 QString const special = toqstr(params.special);
249 if (!special.isEmpty() && special != "none")
250 widthUnitsLC->setCurrentItem(special);
252 lengthToWidgets(heightED, heightUnitsLC,
253 (params.height).asString(), default_unit);
255 QString const height_special = toqstr(params.height_special);
256 if (!height_special.isEmpty() && height_special != "none")
257 heightUnitsLC->setCurrentItem(height_special);
258 // set no optional height if the value is the default "1\height"
259 // (special units like \height are handled as "in",
260 // FIXME: this is a very bad UI, this check box should be disabled in
261 // this case, not forced to 'unchecked' state.
262 if (height_special == "totalheight" && params.height == Length("1in"))
263 heightCB->setCheckState(Qt::Unchecked);
265 heightCB->setCheckState(Qt::Checked);
267 heightCB->setEnabled(ibox);
271 docstring GuiBox::dialogToParams() const
273 bool const pagebreak =
274 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
279 box_type = fromqstr(typeCO->itemData(
280 typeCO->currentIndex()).toString());
282 InsetBoxParams params(box_type);
284 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
286 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
288 params.pos = "tcb"[valignCO->currentIndex()];
289 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
290 params.hor_pos = "lcrs"[halignCO->currentIndex()];
293 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
294 QString value = widthED->text();
295 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
296 params.special = fromqstr(unit);
297 // Note: the unit is simply ignored in this case
298 params.width = Length(value.toDouble(), Length::IN);
300 params.special = "none";
301 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
304 // the height parameter is omitted if the value
305 // is "1in" and "Total Height" is used as unit.
306 // 1in + "Total Height" means "1\height" which is the LaTeX default
307 // if no height is given
308 if (heightCB->checkState() == Qt::Unchecked) {
309 params.height = Length("1in");
310 params.height_special = "totalheight";
312 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
313 value = heightED->text();
314 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
315 params.height_special = fromqstr(unit);
316 // Note: the unit is simply ignored in this case
317 params.height = Length(value.toDouble(), Length::IN);
319 params.height_special = "none";
321 Length(widgetsToLength(heightED, heightUnitsLC));
324 return from_ascii(InsetBox::params2string(params));
328 void GuiBox::setSpecial(bool ibox)
330 QString const last_item =
331 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
333 // check if the widget contains the special units
334 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
335 // insert 'em if needed...
336 if (!ibox && !has_special) {
337 for (int i = 1; i < ids_spec_.size(); ++i)
338 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
339 // ... or remove 'em if needed
340 } else if (ibox && has_special) {
341 for (int i = 1; i < ids_spec_.size(); ++i) {
342 int n = widthUnitsLC->findData(ids_spec_[i]);
344 widthUnitsLC->removeItem(n);
347 // restore selected text, if possible
348 widthUnitsLC->setCurrentItem(last_item);
352 void GuiBox::setInnerType(bool frameless, QString const & type)
354 // with "frameless" boxes, inner box is mandatory
355 // (i.e. is the actual box)
356 // we have to remove "none" then and adjust the combo
359 innerBoxCO->addItem(qt_("None"), toqstr("none"));
360 innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
361 innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
362 int i = (innerBoxCO->findData(type) != -1)
363 ? innerBoxCO->findData(type) : 0;
364 innerBoxCO->setCurrentIndex(i);
367 } // namespace frontend
371 #include "moc_GuiBox.cpp"