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 "LengthCombo.h"
18 #include "qt_helpers.h"
19 #include "lengthcommon.h"
20 #include "LyXRC.h" // to set the default length values
21 #include "Validator.h"
23 #include "insets/InsetBox.h"
25 #include "support/lstrings.h"
27 #include <QPushButton>
29 #include <QCloseEvent>
37 GuiBoxDialog::GuiBoxDialog(LyXView & lv)
38 : GuiDialog(lv, "box")
41 setViewTitle(_("Box Settings"));
42 setController(new ControlBox(*this));
44 // fill the box type choice
45 box_gui_tokens(ids_, gui_names_);
46 for (unsigned int i = 0; i < gui_names_.size(); ++i)
47 typeCO->addItem(toqstr(gui_names_[i]));
49 // add the special units to the height choice
50 // width needs different handling
51 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
52 for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
53 heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
55 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
56 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
57 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
58 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
60 connect(widthED, SIGNAL(textChanged(const QString &)),
61 this, SLOT(change_adaptor()));
62 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
63 this, SLOT(change_adaptor()));
64 connect(valignCO, SIGNAL(highlighted(const QString &)),
65 this, SLOT(change_adaptor()));
66 connect(heightCB, SIGNAL(stateChanged(int)),
67 this, SLOT(change_adaptor()));
68 connect(heightED, SIGNAL(textChanged(const QString &)),
69 this, SLOT(change_adaptor()));
70 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
71 this, SLOT(change_adaptor()));
72 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
73 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
74 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
75 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
76 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
77 connect(innerBoxCO, SIGNAL(activated(const QString&)),
78 this, SLOT(innerBoxChanged(const QString &)));
79 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
81 heightED->setValidator(unsignedLengthValidator(heightED));
82 widthED->setValidator(unsignedLengthValidator(widthED));
84 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
86 bc().addReadOnly(typeCO);
87 bc().addReadOnly(innerBoxCO);
88 bc().addReadOnly(valignCO);
89 bc().addReadOnly(ialignCO);
90 bc().addReadOnly(halignCO);
91 bc().addReadOnly(widthED);
92 bc().addReadOnly(widthUnitsLC);
93 bc().addReadOnly(heightCB);
94 bc().addReadOnly(heightED);
95 bc().addReadOnly(heightUnitsLC);
97 bc().setRestore(restorePB);
99 bc().setApply(applyPB);
100 bc().setCancel(closePB);
102 // initialize the length validator
103 bc().addCheckedLineEdit(widthED, widthLA);
104 bc().addCheckedLineEdit(heightED, heightCB);
108 ControlBox & GuiBoxDialog::controller()
110 return static_cast<ControlBox &>(GuiDialog::controller());
114 void GuiBoxDialog::closeEvent(QCloseEvent * e)
121 void GuiBoxDialog::change_adaptor()
127 void GuiBoxDialog::innerBoxChanged(const QString & str)
129 bool const ibox = (str != qt_("None"));
130 valignCO->setEnabled(ibox);
131 ialignCO->setEnabled(ibox);
132 halignCO->setEnabled(!ibox);
133 heightCB->setEnabled(ibox);
134 if (heightCB->checkState() == Qt::Checked && ibox) {
135 heightED->setEnabled(true);
136 heightUnitsLC->setEnabled(true);
142 void GuiBoxDialog::typeChanged(int index)
144 bool const frameless = (index == 0);
146 valignCO->setEnabled(true);
147 ialignCO->setEnabled(true);
148 halignCO->setEnabled(false);
149 heightCB->setEnabled(true);
150 heightED->setEnabled(true);
151 heightUnitsLC->setEnabled(true);
154 int itype = innerBoxCO->currentIndex();
155 setInnerType(frameless, itype);
159 void GuiBoxDialog::restoreClicked()
161 setInnerType(true, 2);
162 widthED->setText("100");
163 widthUnitsLC->setCurrentItem(Length::PCW);
164 heightCB->setCheckState(Qt::Checked);
165 heightED->setText("1");
166 for (int j = 0; j < heightUnitsLC->count(); j++) {
167 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
168 heightUnitsLC->setCurrentItem(j);
173 void GuiBoxDialog::updateContents()
175 string type(controller().params().type);
176 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
178 typeCO->setCurrentIndex(i);
182 unsigned int inner_type = 2;
183 if (!controller().params().inner_box)
186 if (controller().params().use_parbox)
189 bool frameless = (controller().params().type == "Frameless");
190 setInnerType(frameless, inner_type);
192 char c = controller().params().pos;
193 valignCO->setCurrentIndex(string("tcb").find(c, 0));
194 c = controller().params().inner_pos;
195 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
196 c = controller().params().hor_pos;
197 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
199 bool ibox = controller().params().inner_box;
200 valignCO->setEnabled(ibox);
201 ialignCO->setEnabled(ibox);
202 halignCO->setEnabled(!ibox);
205 Length::UNIT default_unit =
206 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
208 lengthToWidgets(widthED, widthUnitsLC,
209 (controller().params().width).asString(), default_unit);
211 string const special = controller().params().special;
212 if (!special.empty() && special != "none") {
214 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
215 if (special == ids_spec_[i])
216 spc = toqstr(gui_names_spec_[i].c_str());
218 for (int j = 0; j < widthUnitsLC->count(); j++) {
219 if (widthUnitsLC->itemText(j) == spc)
220 widthUnitsLC->setCurrentIndex(j);
224 lengthToWidgets(heightED, heightUnitsLC,
225 (controller().params().height).asString(), default_unit);
227 string const height_special = controller().params().height_special;
228 if (!height_special.empty() && height_special != "none") {
230 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
231 if (height_special == ids_spec_[i]) {
232 hspc = toqstr(gui_names_spec_[i].c_str());
235 for (int j = 0; j < heightUnitsLC->count(); j++) {
236 if (heightUnitsLC->itemText(j) == hspc) {
237 heightUnitsLC->setCurrentIndex(j);
241 // set no optional height when the value is the default "1\height"
242 // (special units like \height are handled as "in",
243 if (height_special == "totalheight" &&
244 controller().params().height == Length("1in"))
245 heightCB->setCheckState(Qt::Unchecked);
247 heightCB->setCheckState(Qt::Checked);
249 heightCB->setEnabled(ibox);
253 void GuiBoxDialog::applyView()
255 controller().params().type =
256 ids_[typeCO->currentIndex()];
258 controller().params().inner_box =
259 innerBoxCO->currentText() != qt_("None");
260 controller().params().use_parbox =
261 innerBoxCO->currentText() == qt_("Parbox");
263 controller().params().pos =
264 "tcb"[valignCO->currentIndex()];
265 controller().params().inner_pos =
266 "tcbs"[ialignCO->currentIndex()];
267 controller().params().hor_pos =
268 "lcrs"[halignCO->currentIndex()];
272 QString special = widthUnitsLC->currentText();
273 QString value = widthED->text();
274 if (special == qt_("Height")) {
277 } else if (special == qt_("Depth")) {
280 } else if (special == qt_("Total Height")) {
283 } else if (special == qt_("Width")) {
287 // the user might insert a non-special value in the line edit
288 if (isValidLength(fromqstr(value))) {
292 controller().params().special = ids_spec_[i];
296 width = fromqstr(value);
297 // beware: bogosity! the unit is simply ignored in this case
300 width = widgetsToLength(widthED, widthUnitsLC);
302 controller().params().width = Length(width);
306 special = heightUnitsLC->currentText();
307 value = heightED->text();
308 if (special == qt_("Height")) {
311 } else if (special == qt_("Depth")) {
314 } else if (special == qt_("Total Height")) {
317 } else if (special == qt_("Width")) {
321 // the user might insert a non-special value in the line edit
322 if (isValidLength(fromqstr(value))) {
326 controller().params().height_special = ids_spec_[i];
329 if (spec && !isValidLength(fromqstr(heightED->text()))) {
330 height = fromqstr(value);
331 // beware: bogosity! the unit is simply ignored in this case
334 height = widgetsToLength(heightED, heightUnitsLC);
336 // the height parameter is omitted in InsetBox.cpp when the value
337 // is "1in" and "Total Height" is used as unit.
338 // 1in + "Total Height" means "1\height" which is the LaTeX default when
339 // no height is given
340 if (heightCB->checkState() == Qt::Checked)
341 controller().params().height = Length(height);
343 controller().params().height = Length("1in");
344 controller().params().height_special = ids_spec_[3];
349 void GuiBoxDialog::setSpecial(bool ibox)
351 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
352 // check if the widget contains the special units
353 int count = widthUnitsLC->count();
354 bool has_special = false;
355 for (int i = 0; i < count; i++)
356 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
358 // insert 'em if needed...
359 if (!ibox && !has_special) {
360 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
361 widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
362 // ... or remove 'em if needed
363 } else if (ibox && has_special) {
364 widthUnitsLC->clear();
365 for (int i = 0; i < num_units; i++)
366 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
371 void GuiBoxDialog::setInnerType(bool frameless, int i)
373 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
374 // we have to remove "none" then and adjust the combo
377 innerBoxCO->addItem(qt_("Parbox"));
378 innerBoxCO->addItem(qt_("Minipage"));
380 innerBoxCO->setCurrentIndex(i - 1);
382 innerBoxCO->setCurrentIndex(i);
384 if (innerBoxCO->count() == 2)
387 innerBoxCO->addItem(qt_("None"));
388 innerBoxCO->addItem(qt_("Parbox"));
389 innerBoxCO->addItem(qt_("Minipage"));
390 innerBoxCO->setCurrentIndex(i);
394 } // namespace frontend
398 #include "GuiBox_moc.cpp"