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(heightED, SIGNAL(textChanged(const QString &)),
67 this, SLOT(change_adaptor()));
68 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
69 this, SLOT(change_adaptor()));
70 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
71 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
72 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
73 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
74 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
75 connect(innerBoxCO, SIGNAL(activated(const QString&)),
76 this, SLOT(innerBoxChanged(const QString &)));
77 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
79 heightED->setValidator(unsignedLengthValidator(heightED));
80 widthED->setValidator(unsignedLengthValidator(widthED));
82 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
84 bc().addReadOnly(typeCO);
85 bc().addReadOnly(innerBoxCO);
86 bc().addReadOnly(valignCO);
87 bc().addReadOnly(ialignCO);
88 bc().addReadOnly(halignCO);
89 bc().addReadOnly(widthED);
90 bc().addReadOnly(heightED);
91 bc().addReadOnly(widthUnitsLC);
92 bc().addReadOnly(heightUnitsLC);
94 bc().setRestore(restorePB);
96 bc().setApply(applyPB);
97 bc().setCancel(closePB);
99 // initialize the length validator
100 bc().addCheckedLineEdit(widthED, widthLA);
101 bc().addCheckedLineEdit(heightED, heightLA);
105 ControlBox & GuiBoxDialog::controller()
107 return static_cast<ControlBox &>(GuiDialog::controller());
111 void GuiBoxDialog::closeEvent(QCloseEvent * e)
118 void GuiBoxDialog::change_adaptor()
124 void GuiBoxDialog::innerBoxChanged(const QString & str)
126 bool const ibox = (str != qt_("None"));
127 valignCO->setEnabled(ibox);
128 ialignCO->setEnabled(ibox);
129 halignCO->setEnabled(!ibox);
130 heightED->setEnabled(ibox);
131 heightUnitsLC->setEnabled(ibox);
136 void GuiBoxDialog::typeChanged(int index)
138 bool const frameless = (index == 0);
140 valignCO->setEnabled(true);
141 ialignCO->setEnabled(true);
142 halignCO->setEnabled(false);
143 heightED->setEnabled(true);
144 heightUnitsLC->setEnabled(true);
147 int itype = innerBoxCO->currentIndex();
148 setInnerType(frameless, itype);
152 void GuiBoxDialog::restoreClicked()
154 setInnerType(true, 2);
155 widthED->setText("100");
156 widthUnitsLC->setCurrentItem(Length::PCW);
157 heightED->setText("1");
158 for (int j = 0; j < heightUnitsLC->count(); j++) {
159 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
160 heightUnitsLC->setCurrentItem(j);
165 void GuiBoxDialog::updateContents()
167 string type(controller().params().type);
168 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
170 typeCO->setCurrentIndex(i);
174 unsigned int inner_type = 2;
175 if (!controller().params().inner_box)
178 if (controller().params().use_parbox)
181 bool frameless = (controller().params().type == "Frameless");
182 setInnerType(frameless, inner_type);
184 char c = controller().params().pos;
185 valignCO->setCurrentIndex(string("tcb").find(c, 0));
186 c = controller().params().inner_pos;
187 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
188 c = controller().params().hor_pos;
189 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
191 bool ibox = controller().params().inner_box;
192 valignCO->setEnabled(ibox);
193 ialignCO->setEnabled(ibox);
194 halignCO->setEnabled(!ibox);
197 Length::UNIT default_unit =
198 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
200 lengthToWidgets(widthED, widthUnitsLC,
201 (controller().params().width).asString(), default_unit);
203 string const special = controller().params().special;
204 if (!special.empty() && special != "none") {
206 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
207 if (special == ids_spec_[i])
208 spc = toqstr(gui_names_spec_[i].c_str());
210 for (int j = 0; j < widthUnitsLC->count(); j++) {
211 if (widthUnitsLC->itemText(j) == spc)
212 widthUnitsLC->setCurrentIndex(j);
216 lengthToWidgets(heightED, heightUnitsLC,
217 (controller().params().height).asString(), default_unit);
219 string const height_special = controller().params().height_special;
220 if (!height_special.empty() && height_special != "none") {
222 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
223 if (height_special == ids_spec_[i]) {
224 hspc = toqstr(gui_names_spec_[i].c_str());
227 for (int j = 0; j < heightUnitsLC->count(); j++) {
228 if (heightUnitsLC->itemText(j) == hspc) {
229 heightUnitsLC->setCurrentIndex(j);
234 heightED->setEnabled(ibox);
235 heightUnitsLC->setEnabled(ibox);
239 void GuiBoxDialog::applyView()
241 controller().params().type =
242 ids_[typeCO->currentIndex()];
244 controller().params().inner_box =
245 innerBoxCO->currentText() != qt_("None");
246 controller().params().use_parbox =
247 innerBoxCO->currentText() == qt_("Parbox");
249 controller().params().pos =
250 "tcb"[valignCO->currentIndex()];
251 controller().params().inner_pos =
252 "tcbs"[ialignCO->currentIndex()];
253 controller().params().hor_pos =
254 "lcrs"[halignCO->currentIndex()];
258 QString special = widthUnitsLC->currentText();
259 QString value = widthED->text();
260 if (special == qt_("Height")) {
263 } else if (special == qt_("Depth")) {
266 } else if (special == qt_("Total Height")) {
269 } else if (special == qt_("Width")) {
273 // the user might insert a non-special value in the line edit
274 if (isValidLength(fromqstr(value))) {
278 controller().params().special = ids_spec_[i];
282 width = fromqstr(value);
283 // beware: bogosity! the unit is simply ignored in this case
286 width = widgetsToLength(widthED, widthUnitsLC);
288 controller().params().width = Length(width);
292 special = heightUnitsLC->currentText();
293 value = heightED->text();
294 if (special == qt_("Height")) {
297 } else if (special == qt_("Depth")) {
300 } else if (special == qt_("Total Height")) {
303 } else if (special == qt_("Width")) {
307 // the user might insert a non-special value in the line edit
308 if (isValidLength(fromqstr(value))) {
312 controller().params().height_special = ids_spec_[i];
315 if (spec && !isValidLength(fromqstr(heightED->text()))) {
316 height = fromqstr(value);
317 // beware: bogosity! the unit is simply ignored in this case
320 height = widgetsToLength(heightED, heightUnitsLC);
322 controller().params().height = Length(height);
326 void GuiBoxDialog::setSpecial(bool ibox)
328 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
329 // check if the widget contains the special units
330 int count = widthUnitsLC->count();
331 bool has_special = false;
332 for (int i = 0; i < count; i++)
333 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
335 // insert 'em if needed...
336 if (!ibox && !has_special) {
337 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
338 widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
339 // ... or remove 'em if needed
340 } else if (ibox && has_special) {
341 widthUnitsLC->clear();
342 for (int i = 0; i < num_units; i++)
343 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
348 void GuiBoxDialog::setInnerType(bool frameless, int i)
350 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
351 // we have to remove "none" then and adjust the combo
354 innerBoxCO->addItem(qt_("Parbox"));
355 innerBoxCO->addItem(qt_("Minipage"));
357 innerBoxCO->setCurrentIndex(i - 1);
359 innerBoxCO->setCurrentIndex(i);
361 if (innerBoxCO->count() == 2)
364 innerBoxCO->addItem(qt_("None"));
365 innerBoxCO->addItem(qt_("Parbox"));
366 innerBoxCO->addItem(qt_("Minipage"));
367 innerBoxCO->setCurrentIndex(i);
371 } // namespace frontend
375 #include "GuiBox_moc.cpp"