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 "controllers/ControlBox.h"
24 #include "controllers/frontend_helpers.h"
26 #include "insets/InsetBox.h"
28 #include "support/lstrings.h"
30 #include <QPushButton>
32 #include <QCloseEvent>
35 using lyx::support::getStringFromVector;
36 using lyx::support::isStrDbl;
37 using lyx::support::subst;
44 //////////////////////////////////////////////////////////////////
48 //////////////////////////////////////////////////////////////////
50 GuiBoxDialog::GuiBoxDialog(GuiBox * form)
54 connect(restorePB, SIGNAL(clicked()), form, SLOT(slotRestore()));
55 connect(okPB, SIGNAL(clicked()), form, SLOT(slotOK()));
56 connect(applyPB, SIGNAL(clicked()), form, SLOT(slotApply()));
57 connect(closePB, SIGNAL(clicked()), form, SLOT(slotClose()));
59 connect(widthED, SIGNAL(textChanged(const QString &)),
60 this, SLOT(change_adaptor()));
61 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
62 this, SLOT(change_adaptor()));
63 connect(valignCO, SIGNAL(highlighted(const QString &)),
64 this, SLOT(change_adaptor()));
65 connect(heightED, SIGNAL(textChanged(const QString &)),
66 this, SLOT(change_adaptor()));
67 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
68 this, SLOT(change_adaptor()));
69 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
70 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
71 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
72 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
73 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
74 connect(innerBoxCO, SIGNAL(activated(const QString&)),
75 this, SLOT(innerBoxChanged(const QString &)));
76 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
78 heightED->setValidator(unsignedLengthValidator(heightED));
79 widthED->setValidator(unsignedLengthValidator(widthED));
83 void GuiBoxDialog::closeEvent(QCloseEvent * e)
90 void GuiBoxDialog::change_adaptor()
96 void GuiBoxDialog::innerBoxChanged(const QString & str)
98 bool const ibox = (str != qt_("None"));
99 valignCO->setEnabled(ibox);
100 ialignCO->setEnabled(ibox);
101 halignCO->setEnabled(!ibox);
102 heightED->setEnabled(ibox);
103 heightUnitsLC->setEnabled(ibox);
104 form_->setSpecial(ibox);
108 void GuiBoxDialog::typeChanged(int index)
110 bool const frameless = (index == 0);
112 valignCO->setEnabled(true);
113 ialignCO->setEnabled(true);
114 halignCO->setEnabled(false);
115 heightED->setEnabled(true);
116 heightUnitsLC->setEnabled(true);
117 form_->setSpecial(true);
119 int itype = innerBoxCO->currentIndex();
120 form_->setInnerType(frameless, itype);
124 void GuiBoxDialog::restoreClicked()
126 form_->setInnerType(true, 2);
127 widthED->setText("100");
128 widthUnitsLC->setCurrentItem(Length::PCW);
129 heightED->setText("1");
130 for (int j = 0; j < heightUnitsLC->count(); j++) {
131 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
132 heightUnitsLC->setCurrentItem(j);
137 //////////////////////////////////////////////////////////////////
141 //////////////////////////////////////////////////////////////////
144 GuiBox::GuiBox(GuiDialog & parent)
145 : GuiView<GuiBoxDialog>(parent, _("Box Settings"))
149 void GuiBox::build_dialog()
151 dialog_.reset(new GuiBoxDialog(this));
153 // fill the box type choice
154 box_gui_tokens(ids_, gui_names_);
155 for (unsigned int i = 0; i < gui_names_.size(); ++i)
156 dialog_->typeCO->addItem(toqstr(gui_names_[i]));
158 // add the special units to the height choice
159 // width needs different handling
160 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
161 for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
162 dialog_->heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
164 bc().addReadOnly(dialog_->typeCO);
165 bc().addReadOnly(dialog_->innerBoxCO);
166 bc().addReadOnly(dialog_->valignCO);
167 bc().addReadOnly(dialog_->ialignCO);
168 bc().addReadOnly(dialog_->halignCO);
169 bc().addReadOnly(dialog_->widthED);
170 bc().addReadOnly(dialog_->heightED);
171 bc().addReadOnly(dialog_->widthUnitsLC);
172 bc().addReadOnly(dialog_->heightUnitsLC);
174 bc().setRestore(dialog_->restorePB);
175 bc().setOK(dialog_->okPB);
176 bc().setApply(dialog_->applyPB);
177 bc().setCancel(dialog_->closePB);
179 // initialize the length validator
180 bc().addCheckedLineEdit(dialog_->widthED, dialog_->widthLA);
181 bc().addCheckedLineEdit(dialog_->heightED, dialog_->heightLA);
185 void GuiBox::update_contents()
187 string type(controller().params().type);
188 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
190 dialog_->typeCO->setCurrentIndex(i);
194 unsigned int inner_type = 2;
195 if (!controller().params().inner_box)
198 if (controller().params().use_parbox)
201 bool frameless = (controller().params().type == "Frameless");
202 setInnerType(frameless, inner_type);
204 char c = controller().params().pos;
205 dialog_->valignCO->setCurrentIndex(string("tcb").find(c, 0));
206 c = controller().params().inner_pos;
207 dialog_->ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
208 c = controller().params().hor_pos;
209 dialog_->halignCO->setCurrentIndex(string("lcrs").find(c, 0));
211 bool ibox = controller().params().inner_box;
212 dialog_->valignCO->setEnabled(ibox);
213 dialog_->ialignCO->setEnabled(ibox);
214 dialog_->halignCO->setEnabled(!ibox);
217 Length::UNIT default_unit =
218 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
220 lengthToWidgets(dialog_->widthED, dialog_->widthUnitsLC,
221 (controller().params().width).asString(), default_unit);
223 string const special(controller().params().special);
224 if (!special.empty() && special != "none") {
226 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
227 if (special == ids_spec_[i])
228 spc = toqstr(gui_names_spec_[i].c_str());
230 for (int j = 0; j < dialog_->widthUnitsLC->count(); j++) {
231 if (dialog_->widthUnitsLC->itemText(j) == spc)
232 dialog_->widthUnitsLC->setCurrentIndex(j);
236 lengthToWidgets(dialog_->heightED, dialog_->heightUnitsLC,
237 (controller().params().height).asString(), default_unit);
239 string const height_special(controller().params().height_special);
240 if (!height_special.empty() && height_special != "none") {
242 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
243 if (height_special == ids_spec_[i]) {
244 hspc = toqstr(gui_names_spec_[i].c_str());
247 for (int j = 0; j < dialog_->heightUnitsLC->count(); j++) {
248 if (dialog_->heightUnitsLC->itemText(j) == hspc) {
249 dialog_->heightUnitsLC->setCurrentIndex(j);
254 dialog_->heightED->setEnabled(ibox);
255 dialog_->heightUnitsLC->setEnabled(ibox);
259 void GuiBox::applyView()
261 controller().params().type =
262 ids_[dialog_->typeCO->currentIndex()];
264 controller().params().inner_box =
265 dialog_->innerBoxCO->currentText() != qt_("None");
266 controller().params().use_parbox =
267 dialog_->innerBoxCO->currentText() == qt_("Parbox");
269 controller().params().pos =
270 "tcb"[dialog_->valignCO->currentIndex()];
271 controller().params().inner_pos =
272 "tcbs"[dialog_->ialignCO->currentIndex()];
273 controller().params().hor_pos =
274 "lcrs"[dialog_->halignCO->currentIndex()];
278 QString special = dialog_->widthUnitsLC->currentText();
279 QString value = dialog_->widthED->text();
280 if (special == qt_("Height")) {
283 } else if (special == qt_("Depth")) {
286 } else if (special == qt_("Total Height")) {
289 } else if (special == qt_("Width")) {
293 // the user might insert a non-special value in the line edit
294 if (isValidLength(fromqstr(value))) {
298 controller().params().special = ids_spec_[i];
302 width = fromqstr(value);
303 // beware: bogosity! the unit is simply ignored in this case
306 width = widgetsToLength(dialog_->widthED, dialog_->widthUnitsLC);
308 controller().params().width = Length(width);
312 special = dialog_->heightUnitsLC->currentText();
313 value = dialog_->heightED->text();
314 if (special == qt_("Height")) {
317 } else if (special == qt_("Depth")) {
320 } else if (special == qt_("Total Height")) {
323 } else if (special == qt_("Width")) {
327 // the user might insert a non-special value in the line edit
328 if (isValidLength(fromqstr(value))) {
332 controller().params().height_special = ids_spec_[i];
335 if (spec && !isValidLength(fromqstr(dialog_->heightED->text()))) {
336 height = fromqstr(value);
337 // beware: bogosity! the unit is simply ignored in this case
340 height = widgetsToLength(dialog_->heightED, dialog_->heightUnitsLC);
342 controller().params().height = Length(height);
346 void GuiBox::setSpecial(bool ibox)
348 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
349 // check if the widget contains the special units
350 int count = dialog_->widthUnitsLC->count();
351 bool has_special = false;
352 for (int i = 0; i < count; i++)
353 if (dialog_->widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
355 // insert 'em if needed...
356 if (!ibox && !has_special) {
357 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
358 dialog_->widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
359 // ... or remove 'em if needed
360 } else if (ibox && has_special) {
361 dialog_->widthUnitsLC->clear();
362 for (int i = 0; i < num_units; i++)
363 dialog_->widthUnitsLC->addItem(qt_(unit_name_gui[i]));
368 void GuiBox::setInnerType(bool frameless, int i)
370 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
371 // we have to remove "none" then and adjust the combo
373 dialog_->innerBoxCO->clear();
374 dialog_->innerBoxCO->addItem(qt_("Parbox"));
375 dialog_->innerBoxCO->addItem(qt_("Minipage"));
376 dialog_->innerBoxCO->setCurrentIndex(i - 1);
378 if (dialog_->innerBoxCO->count() == 2)
380 dialog_->innerBoxCO->clear();
381 dialog_->innerBoxCO->addItem(qt_("None"));
382 dialog_->innerBoxCO->addItem(qt_("Parbox"));
383 dialog_->innerBoxCO->addItem(qt_("Minipage"));
384 dialog_->innerBoxCO->setCurrentIndex(i);
388 } // namespace frontend
392 #include "GuiBox_moc.cpp"