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 "LyXRC.h" // to set the default length values
21 #include "qt_helpers.h"
22 #include "Validator.h"
24 #include "insets/InsetBox.h"
26 #include "support/gettext.h"
27 #include "support/foreach.h"
28 #include "support/lstrings.h"
30 #include <QPushButton>
43 static QStringList boxGuiIds()
46 << "Frameless" << "Boxed"
47 << "ovalbox" << "Ovalbox"
48 << "Shadowbox" << "Shaded"
53 static QStringList boxGuiNames()
56 << qt_("No frame") << qt_("Simple rectangular frame")
57 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
58 << qt_("Drop shadow") << qt_("Shaded background")
59 << qt_("Double rectangular frame");
63 static QStringList boxGuiSpecialLengthIds()
65 return QStringList() << "none" << "height" << "depth"
66 << "totalheight" << "width";
70 static QStringList boxGuiSpecialLengthNames()
72 return QStringList() << qt_("None") << qt_("Height") << qt_("Depth")
73 << qt_("Total Height") << qt_("Width");
77 GuiBox::GuiBox(GuiView & lv)
78 : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
82 // fill the box type choice
84 gui_names_ = boxGuiNames();
85 foreach (QString const & str, gui_names_)
88 // add the special units to the height choice
89 // width needs different handling
90 ids_spec_ = boxGuiSpecialLengthIds();
91 gui_names_spec_ = boxGuiSpecialLengthNames();
92 for (int i = 0; i != ids_spec_.size(); ++i)
93 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
95 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
96 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
97 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
98 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
100 connect(widthED, SIGNAL(textChanged(QString)),
101 this, SLOT(change_adaptor()));
102 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
103 this, SLOT(change_adaptor()));
104 connect(valignCO, SIGNAL(highlighted(QString)),
105 this, SLOT(change_adaptor()));
106 connect(heightCB, SIGNAL(stateChanged(int)),
107 this, SLOT(change_adaptor()));
108 connect(heightED, SIGNAL(textChanged(QString)),
109 this, SLOT(change_adaptor()));
110 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
111 this, SLOT(change_adaptor()));
112 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
113 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
114 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
115 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
116 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
117 connect(innerBoxCO, SIGNAL(activated(QString)),
118 this, SLOT(innerBoxChanged(QString)));
119 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
120 connect(pagebreakCB, SIGNAL(stateChanged(int)),
121 this, SLOT(pagebreakClicked()));
123 heightED->setValidator(unsignedLengthValidator(heightED));
124 widthED->setValidator(unsignedLengthValidator(widthED));
126 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
128 bc().addReadOnly(typeCO);
129 bc().addReadOnly(innerBoxCO);
130 bc().addReadOnly(valignCO);
131 bc().addReadOnly(ialignCO);
132 bc().addReadOnly(halignCO);
133 bc().addReadOnly(widthED);
134 bc().addReadOnly(widthUnitsLC);
135 bc().addReadOnly(heightCB);
136 bc().addReadOnly(heightED);
137 bc().addReadOnly(heightUnitsLC);
138 bc().addReadOnly(pagebreakCB);
140 bc().setRestore(restorePB);
142 bc().setApply(applyPB);
143 bc().setCancel(closePB);
145 // initialize the length validator
146 bc().addCheckedLineEdit(widthED, widthLA);
147 bc().addCheckedLineEdit(heightED, heightCB);
151 void GuiBox::change_adaptor()
157 void GuiBox::innerBoxChanged(QString const & str)
159 bool const ibox = (str != qt_("None"));
160 valignCO->setEnabled(ibox);
161 ialignCO->setEnabled(ibox);
162 halignCO->setEnabled(!ibox);
163 heightCB->setEnabled(ibox);
164 pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
165 heightED->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
166 heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
171 void GuiBox::typeChanged(int index)
173 bool const frameless = (index == 0);
175 valignCO->setEnabled(true);
176 ialignCO->setEnabled(true);
177 halignCO->setEnabled(false);
178 heightCB->setEnabled(true);
179 heightED->setEnabled(heightCB->checkState() == Qt::Checked);
180 heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked);
184 pagebreakCB->setChecked(false);
185 int itype = innerBoxCO->currentIndex();
186 if (innerBoxCO->count() == 2)
188 pagebreakCB->setEnabled(index == 1 && itype == 0);
189 widthED->setEnabled(index != 5);
190 widthUnitsLC->setEnabled(index != 5);
191 setInnerType(frameless, itype);
195 void GuiBox::restoreClicked()
197 setInnerType(true, 2);
198 widthED->setText("100");
199 widthUnitsLC->setCurrentItem(Length::PCW);
200 heightCB->setCheckState(Qt::Checked);
201 heightED->setText("1");
202 heightUnitsLC->setCurrentItem("totalheight");
206 void GuiBox::pagebreakClicked()
208 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
209 innerBoxCO->setEnabled(!pbreak);
210 widthED->setEnabled(!pbreak);
211 widthUnitsLC->setEnabled(!pbreak);
213 valignCO->setEnabled(false);
214 ialignCO->setEnabled(false);
215 halignCO->setEnabled(false);
216 heightCB->setEnabled(false);
217 heightED->setEnabled(false);
218 heightUnitsLC->setEnabled(false);
221 typeChanged(typeCO->currentIndex());
227 void GuiBox::updateContents()
229 QString type = toqstr(params_.type);
230 if (type == "Framed") {
231 pagebreakCB->setChecked(true);
234 pagebreakCB->setChecked(false);
237 pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
239 for (int i = 0; i != gui_names_.size(); ++i) {
241 typeCO->setCurrentIndex(i);
246 if (!params_.inner_box)
249 if (params_.use_parbox)
252 bool frameless = (params_.type == "Frameless");
253 setInnerType(frameless, inner_type);
255 char c = params_.pos;
256 valignCO->setCurrentIndex(string("tcb").find(c, 0));
257 c = params_.inner_pos;
258 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
260 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
262 bool ibox = params_.inner_box;
263 valignCO->setEnabled(ibox);
264 ialignCO->setEnabled(ibox);
265 halignCO->setEnabled(!ibox);
268 Length::UNIT default_unit =
269 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
271 lengthToWidgets(widthED, widthUnitsLC,
272 (params_.width).asString(), default_unit);
274 QString const special = toqstr(params_.special);
275 if (!special.isEmpty() && special != "none")
276 widthUnitsLC->setCurrentItem(special);
278 lengthToWidgets(heightED, heightUnitsLC,
279 (params_.height).asString(), default_unit);
281 QString const height_special = toqstr(params_.height_special);
282 if (!height_special.isEmpty() && height_special != "none")
283 heightUnitsLC->setCurrentItem(height_special);
284 // set no optional height if the value is the default "1\height"
285 // (special units like \height are handled as "in",
286 if (height_special == "totalheight" && params_.height == Length("1in"))
287 heightCB->setCheckState(Qt::Unchecked);
289 heightCB->setCheckState(Qt::Checked);
291 heightCB->setEnabled(ibox);
295 void GuiBox::applyView()
298 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
300 params_.type = "Framed";
302 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
305 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
307 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
309 params_.pos = "tcb"[valignCO->currentIndex()];
310 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
311 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
314 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
315 QString value = widthED->text();
316 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
317 params_.special = fromqstr(unit);
318 // Note: the unit is simply ignored in this case
319 params_.width = Length(value.toDouble(), Length::IN);
321 params_.special = "none";
322 params_.width = Length(widgetsToLength(widthED, widthUnitsLC));
325 // the height parameter is omitted in if the value
326 // is "1in" and "Total Height" is used as unit.
327 // 1in + "Total Height" means "1\height" which is the LaTeX default
328 // if no height is given
329 if (heightCB->checkState() == Qt::Unchecked) {
330 params_.height = Length("1in");
331 params_.height_special = "totalheight";
333 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
334 value = heightED->text();
335 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
336 params_.height_special = fromqstr(unit);
337 // Note: the unit is simply ignored in this case
338 params_.height = Length(value.toDouble(), Length::IN);
340 params_.height_special = "none";
342 Length(widgetsToLength(heightED, heightUnitsLC));
348 void GuiBox::setSpecial(bool ibox)
350 QString const last_item =
351 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
353 // check if the widget contains the special units
354 bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
355 // insert 'em if needed...
356 if (!ibox && !has_special) {
357 for (int i = 1; i < ids_spec_.size(); ++i)
358 widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
359 // ... or remove 'em if needed
360 } else if (ibox && has_special) {
361 for (int i = 1; i < ids_spec_.size(); ++i) {
362 int n = widthUnitsLC->findData(ids_spec_[i]);
364 widthUnitsLC->removeItem(n);
367 // restore selected text, if possible
368 widthUnitsLC->setCurrentItem(last_item);
372 void GuiBox::setInnerType(bool frameless, int i)
374 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
375 // we have to remove "none" then and adjust the combo
378 innerBoxCO->addItem(qt_("Parbox"));
379 innerBoxCO->addItem(qt_("Minipage"));
381 innerBoxCO->setCurrentIndex(i - 1);
383 innerBoxCO->setCurrentIndex(i);
386 innerBoxCO->addItem(qt_("None"));
387 innerBoxCO->addItem(qt_("Parbox"));
388 innerBoxCO->addItem(qt_("Minipage"));
389 innerBoxCO->setCurrentIndex(i);
393 bool GuiBox::initialiseParams(string const & data)
395 InsetBox::string2params(data, params_);
400 void GuiBox::clearParams()
402 params_ = InsetBoxParams("");
406 void GuiBox::dispatchParams()
408 dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
412 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
415 } // namespace frontend
419 #include "moc_GuiBox.cpp"