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 "FuncRequest.h"
18 #include "support/gettext.h"
20 #include "LengthCombo.h"
22 #include "qt_helpers.h"
23 #include "LyXRC.h" // to set the default length values
24 #include "Validator.h"
26 #include "insets/InsetBox.h"
28 #include "support/lstrings.h"
30 #include <QPushButton>
32 #include <QCloseEvent>
40 void box_gui_tokens(vector<string> & ids, vector<docstring> & gui_names)
42 char const * const ids_[] = {
43 "Frameless", "Boxed", "ovalbox",
44 "Ovalbox", "Shadowbox", "Shaded", "Doublebox"};
45 size_t const ids_size = sizeof(ids_) / sizeof(char *);
46 ids = vector<string>(ids_, ids_ + ids_size);
48 gui_names.push_back(_("No frame"));
49 gui_names.push_back(_("Simple rectangular frame"));
50 gui_names.push_back(_("Oval frame, thin"));
51 gui_names.push_back(_("Oval frame, thick"));
52 gui_names.push_back(_("Drop shadow"));
53 gui_names.push_back(_("Shaded background"));
54 gui_names.push_back(_("Double rectangular frame"));
58 void box_gui_tokens_special_length(vector<string> & ids,
59 vector<docstring> & gui_names)
61 char const * const ids_[] = {
62 "none", "height", "depth",
63 "totalheight", "width"};
64 size_t const ids_size = sizeof(ids_) / sizeof(char *);
65 ids = vector<string>(ids_, ids_ + ids_size);
67 gui_names.push_back(_("None"));
68 gui_names.push_back(_("Height"));
69 gui_names.push_back(_("Depth"));
70 gui_names.push_back(_("Total Height"));
71 gui_names.push_back(_("Width"));
75 GuiBox::GuiBox(GuiView & lv)
76 : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
80 // fill the box type choice
81 box_gui_tokens(ids_, gui_names_);
82 for (unsigned int i = 0; i < gui_names_.size(); ++i)
83 typeCO->addItem(toqstr(gui_names_[i]));
85 // add the special units to the height choice
86 // width needs different handling
87 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
88 for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
89 heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
91 connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
92 connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
93 connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
94 connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
96 connect(widthED, SIGNAL(textChanged(QString)),
97 this, SLOT(change_adaptor()));
98 connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
99 this, SLOT(change_adaptor()));
100 connect(valignCO, SIGNAL(highlighted(QString)),
101 this, SLOT(change_adaptor()));
102 connect(heightCB, SIGNAL(stateChanged(int)),
103 this, SLOT(change_adaptor()));
104 connect(heightED, SIGNAL(textChanged(const QString &)),
105 this, SLOT(change_adaptor()));
106 connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
107 this, SLOT(change_adaptor()));
108 connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
109 connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
110 connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
111 connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
112 connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
113 connect(innerBoxCO, SIGNAL(activated(const QString&)),
114 this, SLOT(innerBoxChanged(const QString &)));
115 connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
116 connect(pagebreakCB, SIGNAL(stateChanged(int)),
117 this, SLOT(pagebreakClicked()));
119 heightED->setValidator(unsignedLengthValidator(heightED));
120 widthED->setValidator(unsignedLengthValidator(widthED));
122 bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
124 bc().addReadOnly(typeCO);
125 bc().addReadOnly(innerBoxCO);
126 bc().addReadOnly(valignCO);
127 bc().addReadOnly(ialignCO);
128 bc().addReadOnly(halignCO);
129 bc().addReadOnly(widthED);
130 bc().addReadOnly(widthUnitsLC);
131 bc().addReadOnly(heightCB);
132 bc().addReadOnly(heightED);
133 bc().addReadOnly(heightUnitsLC);
134 bc().addReadOnly(pagebreakCB);
136 bc().setRestore(restorePB);
138 bc().setApply(applyPB);
139 bc().setCancel(closePB);
141 // initialize the length validator
142 bc().addCheckedLineEdit(widthED, widthLA);
143 bc().addCheckedLineEdit(heightED, heightCB);
147 void GuiBox::closeEvent(QCloseEvent * e)
154 void GuiBox::change_adaptor()
160 void GuiBox::innerBoxChanged(const QString & str)
162 bool const ibox = (str != qt_("None"));
163 valignCO->setEnabled(ibox);
164 ialignCO->setEnabled(ibox);
165 halignCO->setEnabled(!ibox);
166 heightCB->setEnabled(ibox);
167 if (heightCB->checkState() == Qt::Checked && ibox) {
168 heightED->setEnabled(true);
169 heightUnitsLC->setEnabled(true);
175 void GuiBox::typeChanged(int index)
177 bool const frameless = (index == 0);
179 valignCO->setEnabled(true);
180 ialignCO->setEnabled(true);
181 halignCO->setEnabled(false);
182 heightCB->setEnabled(true);
183 heightED->setEnabled(true);
184 heightUnitsLC->setEnabled(true);
188 pagebreakCB->setChecked(false);
189 pagebreakCB->setEnabled(index == 1);
190 int itype = innerBoxCO->currentIndex();
191 setInnerType(frameless, itype);
195 void GuiBox::restoreClicked()
197 setInnerType(true, 2);
198 widthED->setText("100");
199 widthUnitsLC->setCurrentItem(Length::PCW);
200 heightCB->setCheckState(Qt::Checked);
201 heightED->setText("1");
202 for (int j = 0; j < heightUnitsLC->count(); j++) {
203 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
204 heightUnitsLC->setCurrentItem(j);
209 void GuiBox::pagebreakClicked()
211 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
212 innerBoxCO->setEnabled(!pbreak);
214 valignCO->setEnabled(false);
215 ialignCO->setEnabled(false);
216 halignCO->setEnabled(false);
217 heightCB->setEnabled(false);
218 heightED->setEnabled(false);
219 heightUnitsLC->setEnabled(false);
222 typeChanged(typeCO->currentIndex());
226 void GuiBox::updateContents()
228 string type = params_.type;
229 if (type == "Framed") {
230 pagebreakCB->setChecked(true);
233 pagebreakCB->setChecked(false);
235 pagebreakCB->setEnabled(type == "Boxed");
237 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
239 typeCO->setCurrentIndex(i);
243 unsigned int inner_type = 2;
244 if (!params_.inner_box)
247 if (params_.use_parbox)
250 bool frameless = (params_.type == "Frameless");
251 setInnerType(frameless, inner_type);
253 char c = params_.pos;
254 valignCO->setCurrentIndex(string("tcb").find(c, 0));
255 c = params_.inner_pos;
256 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
258 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
260 bool ibox = params_.inner_box;
261 valignCO->setEnabled(ibox);
262 ialignCO->setEnabled(ibox);
263 halignCO->setEnabled(!ibox);
266 Length::UNIT default_unit =
267 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
269 lengthToWidgets(widthED, widthUnitsLC,
270 (params_.width).asString(), default_unit);
272 string const special = params_.special;
273 if (!special.empty() && special != "none") {
275 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
276 if (special == ids_spec_[i])
277 spc = toqstr(gui_names_spec_[i].c_str());
279 for (int j = 0; j < widthUnitsLC->count(); j++) {
280 if (widthUnitsLC->itemText(j) == spc)
281 widthUnitsLC->setCurrentIndex(j);
285 lengthToWidgets(heightED, heightUnitsLC,
286 (params_.height).asString(), default_unit);
288 string const height_special = params_.height_special;
289 if (!height_special.empty() && height_special != "none") {
291 for (unsigned int i = 0; i != gui_names_spec_.size(); i++) {
292 if (height_special == ids_spec_[i])
293 hspc = toqstr(gui_names_spec_[i].c_str());
295 for (int j = 0; j != heightUnitsLC->count(); j++) {
296 if (heightUnitsLC->itemText(j) == hspc)
297 heightUnitsLC->setCurrentIndex(j);
300 // set no optional height when the value is the default "1\height"
301 // (special units like \height are handled as "in",
302 if (height_special == "totalheight" &&
303 params_.height == Length("1in"))
304 heightCB->setCheckState(Qt::Unchecked);
306 heightCB->setCheckState(Qt::Checked);
308 heightCB->setEnabled(ibox);
312 void GuiBox::applyView()
314 bool pagebreak = pagebreakCB->isChecked();
316 params_.type = "Framed";
318 params_.type = ids_[typeCO->currentIndex()];
320 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
321 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
323 params_.pos = "tcb"[valignCO->currentIndex()];
324 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
325 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
329 QString special = widthUnitsLC->currentText();
330 QString value = widthED->text();
331 if (special == qt_("Height")) {
334 } else if (special == qt_("Depth")) {
337 } else if (special == qt_("Total Height")) {
340 } else if (special == qt_("Width")) {
344 // the user might insert a non-special value in the line edit
345 if (isValidLength(fromqstr(value))) {
349 params_.special = ids_spec_[i];
353 width = fromqstr(value);
354 // beware: bogosity! the unit is simply ignored in this case
357 width = widgetsToLength(widthED, widthUnitsLC);
360 params_.width = Length(width);
364 special = heightUnitsLC->currentText();
365 value = heightED->text();
366 if (special == qt_("Height")) {
369 } else if (special == qt_("Depth")) {
372 } else if (special == qt_("Total Height")) {
375 } else if (special == qt_("Width")) {
379 // the user might insert a non-special value in the line edit
380 if (isValidLength(fromqstr(value))) {
384 params_.height_special = ids_spec_[i];
387 if (spec && !isValidLength(fromqstr(heightED->text()))) {
388 height = fromqstr(value);
389 // beware: bogosity! the unit is simply ignored in this case
392 height = widgetsToLength(heightED, heightUnitsLC);
394 // the height parameter is omitted in InsetBox.cpp when the value
395 // is "1in" and "Total Height" is used as unit.
396 // 1in + "Total Height" means "1\height" which is the LaTeX default when
397 // no height is given
398 if (heightCB->checkState() == Qt::Checked)
399 params_.height = Length(height);
401 params_.height = Length("1in");
402 params_.height_special = ids_spec_[3];
407 void GuiBox::setSpecial(bool ibox)
409 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
410 // check if the widget contains the special units
411 int count = widthUnitsLC->count();
412 bool has_special = false;
413 for (int i = 0; i < count; i++)
414 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
416 // insert 'em if needed...
417 if (!ibox && !has_special) {
418 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
419 widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
420 // ... or remove 'em if needed
421 } else if (ibox && has_special) {
422 widthUnitsLC->clear();
423 for (int i = 0; i < num_units; i++)
424 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
429 void GuiBox::setInnerType(bool frameless, int i)
431 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
432 // we have to remove "none" then and adjust the combo
435 innerBoxCO->addItem(qt_("Parbox"));
436 innerBoxCO->addItem(qt_("Minipage"));
438 innerBoxCO->setCurrentIndex(i - 1);
440 innerBoxCO->setCurrentIndex(i);
442 if (innerBoxCO->count() == 2)
445 innerBoxCO->addItem(qt_("None"));
446 innerBoxCO->addItem(qt_("Parbox"));
447 innerBoxCO->addItem(qt_("Minipage"));
448 innerBoxCO->setCurrentIndex(i);
452 bool GuiBox::initialiseParams(string const & data)
454 InsetBoxMailer::string2params(data, params_);
460 void GuiBox::clearParams()
462 params_ = InsetBoxParams("");
466 void GuiBox::dispatchParams()
468 dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
472 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
475 } // namespace frontend
479 #include "GuiBox_moc.cpp"