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::change_adaptor()
153 void GuiBox::innerBoxChanged(const QString & str)
155 bool const ibox = (str != qt_("None"));
156 valignCO->setEnabled(ibox);
157 ialignCO->setEnabled(ibox);
158 halignCO->setEnabled(!ibox);
159 heightCB->setEnabled(ibox);
160 if (heightCB->checkState() == Qt::Checked && ibox) {
161 heightED->setEnabled(true);
162 heightUnitsLC->setEnabled(true);
168 void GuiBox::typeChanged(int index)
170 bool const frameless = (index == 0);
172 valignCO->setEnabled(true);
173 ialignCO->setEnabled(true);
174 halignCO->setEnabled(false);
175 heightCB->setEnabled(true);
176 heightED->setEnabled(true);
177 heightUnitsLC->setEnabled(true);
181 pagebreakCB->setChecked(false);
182 pagebreakCB->setEnabled(index == 1);
183 int itype = innerBoxCO->currentIndex();
184 setInnerType(frameless, itype);
188 void GuiBox::restoreClicked()
190 setInnerType(true, 2);
191 widthED->setText("100");
192 widthUnitsLC->setCurrentItem(Length::PCW);
193 heightCB->setCheckState(Qt::Checked);
194 heightED->setText("1");
195 for (int j = 0; j < heightUnitsLC->count(); j++) {
196 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
197 heightUnitsLC->setCurrentItem(j);
202 void GuiBox::pagebreakClicked()
204 bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
205 innerBoxCO->setEnabled(!pbreak);
207 valignCO->setEnabled(false);
208 ialignCO->setEnabled(false);
209 halignCO->setEnabled(false);
210 heightCB->setEnabled(false);
211 heightED->setEnabled(false);
212 heightUnitsLC->setEnabled(false);
215 typeChanged(typeCO->currentIndex());
219 void GuiBox::updateContents()
221 string type = params_.type;
222 if (type == "Framed") {
223 pagebreakCB->setChecked(true);
226 pagebreakCB->setChecked(false);
228 pagebreakCB->setEnabled(type == "Boxed");
230 for (unsigned int i = 0; i < gui_names_.size(); ++i) {
232 typeCO->setCurrentIndex(i);
236 unsigned int inner_type = 2;
237 if (!params_.inner_box)
240 if (params_.use_parbox)
243 bool frameless = (params_.type == "Frameless");
244 setInnerType(frameless, inner_type);
246 char c = params_.pos;
247 valignCO->setCurrentIndex(string("tcb").find(c, 0));
248 c = params_.inner_pos;
249 ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
251 halignCO->setCurrentIndex(string("lcrs").find(c, 0));
253 bool ibox = params_.inner_box;
254 valignCO->setEnabled(ibox);
255 ialignCO->setEnabled(ibox);
256 halignCO->setEnabled(!ibox);
259 Length::UNIT default_unit =
260 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
262 lengthToWidgets(widthED, widthUnitsLC,
263 (params_.width).asString(), default_unit);
265 string const special = params_.special;
266 if (!special.empty() && special != "none") {
268 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
269 if (special == ids_spec_[i])
270 spc = toqstr(gui_names_spec_[i].c_str());
272 for (int j = 0; j < widthUnitsLC->count(); j++) {
273 if (widthUnitsLC->itemText(j) == spc)
274 widthUnitsLC->setCurrentIndex(j);
278 lengthToWidgets(heightED, heightUnitsLC,
279 (params_.height).asString(), default_unit);
281 string const height_special = params_.height_special;
282 if (!height_special.empty() && height_special != "none") {
284 for (unsigned int i = 0; i != gui_names_spec_.size(); i++) {
285 if (height_special == ids_spec_[i])
286 hspc = toqstr(gui_names_spec_[i].c_str());
288 for (int j = 0; j != heightUnitsLC->count(); j++) {
289 if (heightUnitsLC->itemText(j) == hspc)
290 heightUnitsLC->setCurrentIndex(j);
293 // set no optional height when the value is the default "1\height"
294 // (special units like \height are handled as "in",
295 if (height_special == "totalheight" &&
296 params_.height == Length("1in"))
297 heightCB->setCheckState(Qt::Unchecked);
299 heightCB->setCheckState(Qt::Checked);
301 heightCB->setEnabled(ibox);
305 void GuiBox::applyView()
307 bool pagebreak = pagebreakCB->isChecked();
309 params_.type = "Framed";
311 params_.type = ids_[typeCO->currentIndex()];
313 params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
314 params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
316 params_.pos = "tcb"[valignCO->currentIndex()];
317 params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
318 params_.hor_pos = "lcrs"[halignCO->currentIndex()];
322 QString special = widthUnitsLC->currentText();
323 QString value = widthED->text();
324 if (special == qt_("Height")) {
327 } else if (special == qt_("Depth")) {
330 } else if (special == qt_("Total Height")) {
333 } else if (special == qt_("Width")) {
337 // the user might insert a non-special value in the line edit
338 if (isValidLength(fromqstr(value))) {
342 params_.special = ids_spec_[i];
346 width = fromqstr(value);
347 // beware: bogosity! the unit is simply ignored in this case
350 width = widgetsToLength(widthED, widthUnitsLC);
353 params_.width = Length(width);
357 special = heightUnitsLC->currentText();
358 value = heightED->text();
359 if (special == qt_("Height")) {
362 } else if (special == qt_("Depth")) {
365 } else if (special == qt_("Total Height")) {
368 } else if (special == qt_("Width")) {
372 // the user might insert a non-special value in the line edit
373 if (isValidLength(fromqstr(value))) {
377 params_.height_special = ids_spec_[i];
380 if (spec && !isValidLength(fromqstr(heightED->text()))) {
381 height = fromqstr(value);
382 // beware: bogosity! the unit is simply ignored in this case
385 height = widgetsToLength(heightED, heightUnitsLC);
387 // the height parameter is omitted in InsetBox.cpp when the value
388 // is "1in" and "Total Height" is used as unit.
389 // 1in + "Total Height" means "1\height" which is the LaTeX default when
390 // no height is given
391 if (heightCB->checkState() == Qt::Checked)
392 params_.height = Length(height);
394 params_.height = Length("1in");
395 params_.height_special = ids_spec_[3];
400 void GuiBox::setSpecial(bool ibox)
402 box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
403 // check if the widget contains the special units
404 int count = widthUnitsLC->count();
405 bool has_special = false;
406 for (int i = 0; i < count; i++)
407 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
409 // insert 'em if needed...
410 if (!ibox && !has_special) {
411 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
412 widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
413 // ... or remove 'em if needed
414 } else if (ibox && has_special) {
415 widthUnitsLC->clear();
416 for (int i = 0; i < num_units; i++)
417 widthUnitsLC->addItem(qt_(unit_name_gui[i]));
422 void GuiBox::setInnerType(bool frameless, int i)
424 // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
425 // we have to remove "none" then and adjust the combo
428 innerBoxCO->addItem(qt_("Parbox"));
429 innerBoxCO->addItem(qt_("Minipage"));
431 innerBoxCO->setCurrentIndex(i - 1);
433 innerBoxCO->setCurrentIndex(i);
435 if (innerBoxCO->count() == 2)
438 innerBoxCO->addItem(qt_("None"));
439 innerBoxCO->addItem(qt_("Parbox"));
440 innerBoxCO->addItem(qt_("Minipage"));
441 innerBoxCO->setCurrentIndex(i);
445 bool GuiBox::initialiseParams(string const & data)
447 InsetBoxMailer::string2params(data, params_);
453 void GuiBox::clearParams()
455 params_ = InsetBoxParams("");
459 void GuiBox::dispatchParams()
461 dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
465 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
468 } // namespace frontend
472 #include "GuiBox_moc.cpp"