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 foreach (QString const & str, gui_names_)
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 int outer = typeCO->currentIndex();
118 valignCO->setEnabled(ibox);
119 ialignCO->setEnabled(ibox);
120 halignCO->setEnabled(!ibox);
121 heightCB->setEnabled(ibox);
122 // except for fremeless and boxed, the width cannot be specified if
123 // there is no inner box
124 bool const width_disabled = (!ibox && ids_[outer] != "Frameless" &&
125 ids_[outer] != "Boxed");
126 widthED->setEnabled(!width_disabled);
127 widthUnitsLC->setEnabled(!width_disabled);
128 pagebreakCB->setEnabled(!ibox && ids_[outer] == "Boxed");
134 void GuiBox::on_typeCO_activated(int index)
136 bool const frameless = (index == 0);
138 valignCO->setEnabled(true);
139 ialignCO->setEnabled(true);
140 halignCO->setEnabled(false);
141 heightCB->setEnabled(true);
145 pagebreakCB->setChecked(false);
146 int itype = innerBoxCO->currentIndex();
147 if (innerBoxCO->count() == 2)
149 pagebreakCB->setEnabled(ids_[index] == "Boxed" && itype == 0);
150 // except for fremeless and boxed, the width cannot be specified if
151 // there is no inner box
152 bool const width_disabled = (itype == 0 && ids_[index] != "Frameless"
153 && ids_[index] != "Boxed");
154 widthED->setEnabled(!width_disabled);
155 widthUnitsLC->setEnabled(!width_disabled);
156 setInnerType(frameless, itype);
161 void GuiBox::initDialog()
163 setInnerType(true, 2);
164 widthED->setText("100");
165 widthUnitsLC->setCurrentItem(Length::PCW);
166 heightCB->setCheckState(Qt::Checked);
167 heightED->setText("1");
168 heightUnitsLC->setCurrentItem("totalheight");
172 void GuiBox::on_heightCB_stateChanged(int state)
174 bool const enable = (innerBoxCO->currentText() != qt_("None"))
175 && (state == Qt::Checked);
176 heightED->setEnabled(enable);
177 heightUnitsLC->setEnabled(enable);
182 void GuiBox::on_pagebreakCB_stateChanged()
184 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
185 innerBoxCO->setEnabled(!pbreak);
186 widthED->setEnabled(!pbreak);
187 widthUnitsLC->setEnabled(!pbreak);
189 on_typeCO_activated(typeCO->currentIndex());
192 valignCO->setEnabled(false);
193 ialignCO->setEnabled(false);
194 halignCO->setEnabled(false);
195 heightCB->setEnabled(false);
196 heightED->setEnabled(false);
197 heightUnitsLC->setEnabled(false);
203 void GuiBox::paramsToDialog(Inset const * inset)
205 InsetBox const * box = static_cast<InsetBox const *>(inset);
206 InsetBoxParams const & params = box->params();
207 QString type = toqstr(params.type);
208 if (type == "Framed") {
209 pagebreakCB->setChecked(true);
212 pagebreakCB->setChecked(false);
215 pagebreakCB->setEnabled(type == "Boxed" && !params.inner_box);
217 for (int i = 0; i != gui_names_.size(); ++i) {
219 typeCO->setCurrentIndex(i);
224 if (!params.inner_box)
227 if (params.use_parbox)
230 bool frameless = (params.type == "Frameless");
231 setInnerType(frameless, inner_type);
234 valignCO->setCurrentIndex(string("tcb").find(c, 0));
235 c = params.inner_pos;
236 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
238 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
240 bool ibox = params.inner_box;
241 valignCO->setEnabled(ibox);
242 ialignCO->setEnabled(ibox);
243 halignCO->setEnabled(!ibox);
246 Length::UNIT const default_unit = Length::defaultUnit();
248 lengthToWidgets(widthED, widthUnitsLC,
249 (params.width).asString(), default_unit);
251 QString const special = toqstr(params.special);
252 if (!special.isEmpty() && special != "none")
253 widthUnitsLC->setCurrentItem(special);
255 lengthToWidgets(heightED, heightUnitsLC,
256 (params.height).asString(), default_unit);
258 QString const height_special = toqstr(params.height_special);
259 if (!height_special.isEmpty() && height_special != "none")
260 heightUnitsLC->setCurrentItem(height_special);
261 // set no optional height if the value is the default "1\height"
262 // (special units like \height are handled as "in",
263 // FIXME: this is a very bad UI, this check box should be disabled in
264 // this case, not forced to 'unchecked' state.
265 if (height_special == "totalheight" && params.height == Length("1in"))
266 heightCB->setCheckState(Qt::Unchecked);
268 heightCB->setCheckState(Qt::Checked);
270 heightCB->setEnabled(ibox);
274 docstring GuiBox::dialogToParams() const
276 bool const pagebreak =
277 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
282 box_type = fromqstr(ids_[typeCO->currentIndex()]);
284 InsetBoxParams params(box_type);
286 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
288 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
290 params.pos = "tcb"[valignCO->currentIndex()];
291 params.inner_pos = "tcbs"[ialignCO->currentIndex()];
292 params.hor_pos = "lcrs"[halignCO->currentIndex()];
295 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
296 QString value = widthED->text();
297 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
298 params.special = fromqstr(unit);
299 // Note: the unit is simply ignored in this case
300 params.width = Length(value.toDouble(), Length::IN);
302 params.special = "none";
303 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
306 // the height parameter is omitted if the value
307 // is "1in" and "Total Height" is used as unit.
308 // 1in + "Total Height" means "1\height" which is the LaTeX default
309 // if no height is given
310 if (heightCB->checkState() == Qt::Unchecked) {
311 params.height = Length("1in");
312 params.height_special = "totalheight";
314 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
315 value = heightED->text();
316 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
317 params.height_special = fromqstr(unit);
318 // Note: the unit is simply ignored in this case
319 params.height = Length(value.toDouble(), Length::IN);
321 params.height_special = "none";
323 Length(widgetsToLength(heightED, heightUnitsLC));
326 return from_ascii(InsetBox::params2string(params));
330 void GuiBox::setSpecial(bool ibox)
332 QString const last_item =
333 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
335 // check if the widget contains the special units
336 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
337 // insert 'em if needed...
338 if (!ibox && !has_special) {
339 for (int i = 1; i < ids_spec_.size(); ++i)
340 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
341 // ... or remove 'em if needed
342 } else if (ibox && has_special) {
343 for (int i = 1; i < ids_spec_.size(); ++i) {
344 int n = widthUnitsLC->findData(ids_spec_[i]);
346 widthUnitsLC->removeItem(n);
349 // restore selected text, if possible
350 widthUnitsLC->setCurrentItem(last_item);
354 void GuiBox::setInnerType(bool frameless, int i)
356 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
357 // we have to remove "none" then and adjust the combo
360 innerBoxCO->addItem(qt_("Parbox"));
361 innerBoxCO->addItem(qt_("Minipage"));
363 innerBoxCO->setCurrentIndex(i - 1);
365 innerBoxCO->setCurrentIndex(i);
368 innerBoxCO->addItem(qt_("None"));
369 innerBoxCO->addItem(qt_("Parbox"));
370 innerBoxCO->addItem(qt_("Minipage"));
371 innerBoxCO->setCurrentIndex(i);
375 } // namespace frontend
379 #include "moc_GuiBox.cpp"