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 "CheckedLineEdit.h"
18 #include "LengthCombo.h"
19 #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;
46 //////////////////////////////////////////////////////////////////
50 //////////////////////////////////////////////////////////////////
52 GuiBoxDialog::GuiBoxDialog(GuiBox * form)
56 connect(restorePB, SIGNAL(clicked()), form, SLOT(slotRestore()));
57 connect(okPB, SIGNAL(clicked()), form, SLOT(slotOK()));
58 connect(applyPB, SIGNAL(clicked()), form, SLOT(slotApply()));
59 connect(closePB, SIGNAL(clicked()), form, SLOT(slotClose()));
61 connect(widthED, SIGNAL(textChanged(const QString &)),
62 this, SLOT(change_adaptor()));
63 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
64 this, SLOT(change_adaptor()));
65 connect(valignCO, SIGNAL(highlighted(const QString &)),
66 this, SLOT(change_adaptor()));
67 connect(heightED, SIGNAL(textChanged(const QString &)),
68 this, SLOT(change_adaptor()));
69 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
70 this, SLOT(change_adaptor()));
71 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
72 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
73 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
74 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
75 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
76 connect(innerBoxCO, SIGNAL(activated(const QString&)),
77 this, SLOT(innerBoxChanged(const QString &)));
78 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
80 heightED->setValidator(unsignedLengthValidator(heightED));
81 widthED->setValidator(unsignedLengthValidator(widthED));
85 void GuiBoxDialog::closeEvent(QCloseEvent * e)
92 void GuiBoxDialog::change_adaptor()
98 void GuiBoxDialog::innerBoxChanged(const QString & str)
100 bool const ibox = (str != qt_("None"));
101 valignCO->setEnabled(ibox);
102 ialignCO->setEnabled(ibox);
103 halignCO->setEnabled(!ibox);
104 heightED->setEnabled(ibox);
105 heightUnitsLC->setEnabled(ibox);
106 form_->setSpecial(ibox);
110 void GuiBoxDialog::typeChanged(int index)
112 bool const frameless = (index == 0);
114 valignCO->setEnabled(true);
115 ialignCO->setEnabled(true);
116 halignCO->setEnabled(false);
117 heightED->setEnabled(true);
118 heightUnitsLC->setEnabled(true);
119 form_->setSpecial(true);
121 int itype = innerBoxCO->currentIndex();
122 form_->setInnerType(frameless, itype);
126 void GuiBoxDialog::restoreClicked()
128 form_->setInnerType(true, 2);
129 widthED->setText("100");
130 widthUnitsLC->setCurrentItem(Length::PCW);
131 heightED->setText("1");
132 for (int j = 0; j < heightUnitsLC->count(); j++) {
133 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
134 heightUnitsLC->setCurrentItem(j);
139 //////////////////////////////////////////////////////////////////
143 //////////////////////////////////////////////////////////////////
146 GuiBox::GuiBox(Dialog & parent)
147 : GuiView<GuiBoxDialog>(parent, _("Box Settings"))
151 void GuiBox::build_dialog()
153 dialog_.reset(new GuiBoxDialog(this));
155 // fill the box type choice
156 box_gui_tokens(ids_, gui_names_);
157 for (unsigned int i = 0; i < gui_names_.size(); ++i)
158 dialog_->typeCO->addItem(toqstr(gui_names_[i]));
160 // add the special units to the height choice
161 // width needs different handling
162 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
163 for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
164 dialog_->heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
166 bcview().addReadOnly(dialog_->typeCO);
167 bcview().addReadOnly(dialog_->innerBoxCO);
168 bcview().addReadOnly(dialog_->valignCO);
169 bcview().addReadOnly(dialog_->ialignCO);
170 bcview().addReadOnly(dialog_->halignCO);
171 bcview().addReadOnly(dialog_->widthED);
172 bcview().addReadOnly(dialog_->heightED);
173 bcview().addReadOnly(dialog_->widthUnitsLC);
174 bcview().addReadOnly(dialog_->heightUnitsLC);
176 bcview().setRestore(dialog_->restorePB);
177 bcview().setOK(dialog_->okPB);
178 bcview().setApply(dialog_->applyPB);
179 bcview().setCancel(dialog_->closePB);
181 // initialize the length validator
182 addCheckedLineEdit(bcview(), dialog_->widthED, dialog_->widthLA);
183 addCheckedLineEdit(bcview(), dialog_->heightED, dialog_->heightLA);
187 void GuiBox::update_contents()
189 string type(controller().params().type);
190 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
192 dialog_->typeCO->setCurrentIndex(i);
196 unsigned int inner_type = 2;
197 if (!controller().params().inner_box)
200 if (controller().params().use_parbox)
203 bool frameless = (controller().params().type == "Frameless");
204 setInnerType(frameless, inner_type);
206 char c = controller().params().pos;
207 dialog_->valignCO->setCurrentIndex(string("tcb").find(c, 0));
208 c = controller().params().inner_pos;
209 dialog_->ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
210 c = controller().params().hor_pos;
211 dialog_->halignCO->setCurrentIndex(string("lcrs").find(c, 0));
213 bool ibox = controller().params().inner_box;
214 dialog_->valignCO->setEnabled(ibox);
215 dialog_->ialignCO->setEnabled(ibox);
216 dialog_->halignCO->setEnabled(!ibox);
219 Length::UNIT default_unit =
220 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
222 lengthToWidgets(dialog_->widthED, dialog_->widthUnitsLC,
223 (controller().params().width).asString(), default_unit);
225 string const special(controller().params().special);
226 if (!special.empty() && special != "none") {
228 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
229 if (special == ids_spec_[i])
230 spc = toqstr(gui_names_spec_[i].c_str());
232 for (int j = 0; j < dialog_->widthUnitsLC->count(); j++) {
233 if (dialog_->widthUnitsLC->itemText(j) == spc)
234 dialog_->widthUnitsLC->setCurrentIndex(j);
238 lengthToWidgets(dialog_->heightED, dialog_->heightUnitsLC,
239 (controller().params().height).asString(), default_unit);
241 string const height_special(controller().params().height_special);
242 if (!height_special.empty() && height_special != "none") {
244 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
245 if (height_special == ids_spec_[i]) {
246 hspc = toqstr(gui_names_spec_[i].c_str());
249 for (int j = 0; j < dialog_->heightUnitsLC->count(); j++) {
250 if (dialog_->heightUnitsLC->itemText(j) == hspc) {
251 dialog_->heightUnitsLC->setCurrentIndex(j);
256 dialog_->heightED->setEnabled(ibox);
257 dialog_->heightUnitsLC->setEnabled(ibox);
263 controller().params().type =
264 ids_[dialog_->typeCO->currentIndex()];
266 controller().params().inner_box =
267 dialog_->innerBoxCO->currentText() != qt_("None");
268 controller().params().use_parbox =
269 dialog_->innerBoxCO->currentText() == qt_("Parbox");
271 controller().params().pos =
272 "tcb"[dialog_->valignCO->currentIndex()];
273 controller().params().inner_pos =
274 "tcbs"[dialog_->ialignCO->currentIndex()];
275 controller().params().hor_pos =
276 "lcrs"[dialog_->halignCO->currentIndex()];
280 QString special = dialog_->widthUnitsLC->currentText();
281 QString value = dialog_->widthED->text();
282 if (special == qt_("Height")) {
285 } else if (special == qt_("Depth")) {
288 } else if (special == qt_("Total Height")) {
291 } else if (special == qt_("Width")) {
295 // the user might insert a non-special value in the line edit
296 if (isValidLength(fromqstr(value))) {
300 controller().params().special = ids_spec_[i];
304 width = fromqstr(value);
305 // beware: bogosity! the unit is simply ignored in this case
308 width = widgetsToLength(dialog_->widthED, dialog_->widthUnitsLC);
310 controller().params().width = Length(width);
314 special = dialog_->heightUnitsLC->currentText();
315 value = dialog_->heightED->text();
316 if (special == qt_("Height")) {
319 } else if (special == qt_("Depth")) {
322 } else if (special == qt_("Total Height")) {
325 } else if (special == qt_("Width")) {
329 // the user might insert a non-special value in the line edit
330 if (isValidLength(fromqstr(value))) {
334 controller().params().height_special = ids_spec_[i];
337 if (spec && !isValidLength(fromqstr(dialog_->heightED->text()))) {
338 height = fromqstr(value);
339 // beware: bogosity! the unit is simply ignored in this case
342 height = widgetsToLength(dialog_->heightED, dialog_->heightUnitsLC);
344 controller().params().height = Length(height);
348 void GuiBox::setSpecial(bool ibox)
350 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
351 // check if the widget contains the special units
352 int count = dialog_->widthUnitsLC->count();
353 bool has_special = false;
354 for (int i = 0; i < count; i++)
355 if (dialog_->widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
357 // insert 'em if needed...
358 if (!ibox && !has_special) {
359 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
360 dialog_->widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
361 // ... or remove 'em if needed
362 } else if (ibox && has_special) {
363 dialog_->widthUnitsLC->clear();
364 for (int i = 0; i < num_units; i++)
365 dialog_->widthUnitsLC->addItem(qt_(unit_name_gui[i]));
370 void GuiBox::setInnerType(bool frameless, int i)
372 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
373 // we have to remove "none" then and adjust the combo
375 dialog_->innerBoxCO->clear();
376 dialog_->innerBoxCO->addItem(qt_("Parbox"));
377 dialog_->innerBoxCO->addItem(qt_("Minipage"));
378 dialog_->innerBoxCO->setCurrentIndex(i - 1);
380 if (dialog_->innerBoxCO->count() == 2)
382 dialog_->innerBoxCO->clear();
383 dialog_->innerBoxCO->addItem(qt_("None"));
384 dialog_->innerBoxCO->addItem(qt_("Parbox"));
385 dialog_->innerBoxCO->addItem(qt_("Minipage"));
386 dialog_->innerBoxCO->setCurrentIndex(i);
390 } // namespace frontend
394 #include "GuiBox_moc.cpp"