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 "ControlBox.h"
19 #include "LengthCombo.h"
20 #include "qt_helpers.h"
21 #include "lengthcommon.h"
22 #include "LyXRC.h" // to set the default length values
23 #include "Validator.h"
25 #include "controllers/ControlBox.h"
26 #include "controllers/frontend_helpers.h"
28 #include "insets/InsetBox.h"
30 #include "support/lstrings.h"
32 #include <QPushButton>
34 #include <QCloseEvent>
37 using lyx::support::getStringFromVector;
38 using lyx::support::isStrDbl;
39 using lyx::support::subst;
47 GuiBoxDialog::GuiBoxDialog(LyXView & lv)
48 : GuiDialog(lv, "box")
51 setViewTitle(_("Box Settings"));
52 setController(new ControlBox(*this));
54 // fill the box type choice
55 box_gui_tokens(ids_, gui_names_);
56 for (unsigned int i = 0; i < gui_names_.size(); ++i)
57 typeCO->addItem(toqstr(gui_names_[i]));
59 // add the special units to the height choice
60 // width needs different handling
61 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
62 for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
63 heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
65 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
66 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
67 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
68 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
70 connect(widthED, SIGNAL(textChanged(const QString &)),
71 this, SLOT(change_adaptor()));
72 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
73 this, SLOT(change_adaptor()));
74 connect(valignCO, SIGNAL(highlighted(const QString &)),
75 this, SLOT(change_adaptor()));
76 connect(heightED, SIGNAL(textChanged(const QString &)),
77 this, SLOT(change_adaptor()));
78 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
79 this, SLOT(change_adaptor()));
80 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
81 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
82 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
83 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
84 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
85 connect(innerBoxCO, SIGNAL(activated(const QString&)),
86 this, SLOT(innerBoxChanged(const QString &)));
87 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
89 heightED->setValidator(unsignedLengthValidator(heightED));
90 widthED->setValidator(unsignedLengthValidator(widthED));
92 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
94 bc().addReadOnly(typeCO);
95 bc().addReadOnly(innerBoxCO);
96 bc().addReadOnly(valignCO);
97 bc().addReadOnly(ialignCO);
98 bc().addReadOnly(halignCO);
99 bc().addReadOnly(widthED);
100 bc().addReadOnly(heightED);
101 bc().addReadOnly(widthUnitsLC);
102 bc().addReadOnly(heightUnitsLC);
104 bc().setRestore(restorePB);
106 bc().setApply(applyPB);
107 bc().setCancel(closePB);
109 // initialize the length validator
110 bc().addCheckedLineEdit(widthED, widthLA);
111 bc().addCheckedLineEdit(heightED, heightLA);
115 ControlBox & GuiBoxDialog::controller() const
117 return static_cast<ControlBox &>(GuiDialog::controller());
121 void GuiBoxDialog::closeEvent(QCloseEvent * e)
128 void GuiBoxDialog::change_adaptor()
134 void GuiBoxDialog::innerBoxChanged(const QString & str)
136 bool const ibox = (str != qt_("None"));
137 valignCO->setEnabled(ibox);
138 ialignCO->setEnabled(ibox);
139 halignCO->setEnabled(!ibox);
140 heightED->setEnabled(ibox);
141 heightUnitsLC->setEnabled(ibox);
146 void GuiBoxDialog::typeChanged(int index)
148 bool const frameless = (index == 0);
150 valignCO->setEnabled(true);
151 ialignCO->setEnabled(true);
152 halignCO->setEnabled(false);
153 heightED->setEnabled(true);
154 heightUnitsLC->setEnabled(true);
157 int itype = innerBoxCO->currentIndex();
158 setInnerType(frameless, itype);
162 void GuiBoxDialog::restoreClicked()
164 setInnerType(true, 2);
165 widthED->setText("100");
166 widthUnitsLC->setCurrentItem(Length::PCW);
167 heightED->setText("1");
168 for (int j = 0; j < heightUnitsLC->count(); j++) {
169 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
170 heightUnitsLC->setCurrentItem(j);
175 void GuiBoxDialog::update_contents()
177 string type(controller().params().type);
178 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
180 typeCO->setCurrentIndex(i);
184 unsigned int inner_type = 2;
185 if (!controller().params().inner_box)
188 if (controller().params().use_parbox)
191 bool frameless = (controller().params().type == "Frameless");
192 setInnerType(frameless, inner_type);
194 char c = controller().params().pos;
195 valignCO->setCurrentIndex(string("tcb").find(c, 0));
196 c = controller().params().inner_pos;
197 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
198 c = controller().params().hor_pos;
199 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
201 bool ibox = controller().params().inner_box;
202 valignCO->setEnabled(ibox);
203 ialignCO->setEnabled(ibox);
204 halignCO->setEnabled(!ibox);
207 Length::UNIT default_unit =
208 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
210 lengthToWidgets(widthED, widthUnitsLC,
211 (controller().params().width).asString(), default_unit);
213 string const special(controller().params().special);
214 if (!special.empty() && special != "none") {
216 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
217 if (special == ids_spec_[i])
218 spc = toqstr(gui_names_spec_[i].c_str());
220 for (int j = 0; j < widthUnitsLC->count(); j++) {
221 if (widthUnitsLC->itemText(j) == spc)
222 widthUnitsLC->setCurrentIndex(j);
226 lengthToWidgets(heightED, heightUnitsLC,
227 (controller().params().height).asString(), default_unit);
229 string const height_special(controller().params().height_special);
230 if (!height_special.empty() && height_special != "none") {
232 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
233 if (height_special == ids_spec_[i]) {
234 hspc = toqstr(gui_names_spec_[i].c_str());
237 for (int j = 0; j < heightUnitsLC->count(); j++) {
238 if (heightUnitsLC->itemText(j) == hspc) {
239 heightUnitsLC->setCurrentIndex(j);
244 heightED->setEnabled(ibox);
245 heightUnitsLC->setEnabled(ibox);
249 void GuiBoxDialog::applyView()
251 controller().params().type =
252 ids_[typeCO->currentIndex()];
254 controller().params().inner_box =
255 innerBoxCO->currentText() != qt_("None");
256 controller().params().use_parbox =
257 innerBoxCO->currentText() == qt_("Parbox");
259 controller().params().pos =
260 "tcb"[valignCO->currentIndex()];
261 controller().params().inner_pos =
262 "tcbs"[ialignCO->currentIndex()];
263 controller().params().hor_pos =
264 "lcrs"[halignCO->currentIndex()];
268 QString special = widthUnitsLC->currentText();
269 QString value = widthED->text();
270 if (special == qt_("Height")) {
273 } else if (special == qt_("Depth")) {
276 } else if (special == qt_("Total Height")) {
279 } else if (special == qt_("Width")) {
283 // the user might insert a non-special value in the line edit
284 if (isValidLength(fromqstr(value))) {
288 controller().params().special = ids_spec_[i];
292 width = fromqstr(value);
293 // beware: bogosity! the unit is simply ignored in this case
296 width = widgetsToLength(widthED, widthUnitsLC);
298 controller().params().width = Length(width);
302 special = heightUnitsLC->currentText();
303 value = heightED->text();
304 if (special == qt_("Height")) {
307 } else if (special == qt_("Depth")) {
310 } else if (special == qt_("Total Height")) {
313 } else if (special == qt_("Width")) {
317 // the user might insert a non-special value in the line edit
318 if (isValidLength(fromqstr(value))) {
322 controller().params().height_special = ids_spec_[i];
325 if (spec && !isValidLength(fromqstr(heightED->text()))) {
326 height = fromqstr(value);
327 // beware: bogosity! the unit is simply ignored in this case
330 height = widgetsToLength(heightED, heightUnitsLC);
332 controller().params().height = Length(height);
336 void GuiBoxDialog::setSpecial(bool ibox)
338 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
339 // check if the widget contains the special units
340 int count = widthUnitsLC->count();
341 bool has_special = false;
342 for (int i = 0; i < count; i++)
343 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
345 // insert 'em if needed...
346 if (!ibox && !has_special) {
347 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
348 widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
349 // ... or remove 'em if needed
350 } else if (ibox && has_special) {
351 widthUnitsLC->clear();
352 for (int i = 0; i < num_units; i++)
353 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
358 void GuiBoxDialog::setInnerType(bool frameless, int i)
360 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
361 // we have to remove "none" then and adjust the combo
364 innerBoxCO->addItem(qt_("Parbox"));
365 innerBoxCO->addItem(qt_("Minipage"));
366 innerBoxCO->setCurrentIndex(i - 1);
368 if (innerBoxCO->count() == 2)
371 innerBoxCO->addItem(qt_("None"));
372 innerBoxCO->addItem(qt_("Parbox"));
373 innerBoxCO->addItem(qt_("Minipage"));
374 innerBoxCO->setCurrentIndex(i);
378 } // namespace frontend
382 #include "GuiBox_moc.cpp"