]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
Fix a crash following the input of an invalid paragraph separation value in the docum...
[lyx.git] / src / frontends / qt4 / GuiBox.cpp
1 /**
2  * \file GuiBox.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna (Minipage stuff)
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiBox.h"
16
17 #include "FuncRequest.h"
18 #include "support/gettext.h"
19
20 #include "LengthCombo.h"
21 #include "Length.h"
22 #include "qt_helpers.h"
23 #include "LyXRC.h" // to set the default length values
24 #include "Validator.h"
25
26 #include "insets/InsetBox.h"
27
28 #include "support/lstrings.h"
29
30 #include <QPushButton>
31 #include <QLineEdit>
32 #include <QCloseEvent>
33
34 using namespace std;
35
36 namespace lyx {
37 namespace frontend {
38
39
40 void box_gui_tokens(vector<string> & ids, vector<docstring> & gui_names)
41 {
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);
47         gui_names.clear();
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"));
55 }
56
57
58 void box_gui_tokens_special_length(vector<string> & ids,
59         vector<docstring> & gui_names)
60 {
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);
66         gui_names.clear();
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"));
72 }
73
74
75 GuiBox::GuiBox(GuiView & lv)
76         : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
77 {
78         setupUi(this);
79
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]));
84
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]));
90
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()));
95
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()));
118
119         heightED->setValidator(unsignedLengthValidator(heightED));
120         widthED->setValidator(unsignedLengthValidator(widthED));
121
122         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
123
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);
135
136         bc().setRestore(restorePB);
137         bc().setOK(okPB);
138         bc().setApply(applyPB);
139         bc().setCancel(closePB);
140
141         // initialize the length validator
142         bc().addCheckedLineEdit(widthED, widthLA);
143         bc().addCheckedLineEdit(heightED, heightCB);
144 }
145
146
147 void GuiBox::closeEvent(QCloseEvent * e)
148 {
149         slotClose();
150         e->accept();
151 }
152
153
154 void GuiBox::change_adaptor()
155 {
156         changed();
157 }
158
159
160 void GuiBox::innerBoxChanged(const QString & str)
161 {
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);
170         }
171         setSpecial(ibox);
172 }
173
174
175 void GuiBox::typeChanged(int index)
176 {
177         bool const frameless = (index == 0);
178         if (frameless) {
179                 valignCO->setEnabled(true);
180                 ialignCO->setEnabled(true);
181                 halignCO->setEnabled(false);
182                 heightCB->setEnabled(true);
183                 heightED->setEnabled(true);
184                 heightUnitsLC->setEnabled(true);
185                 setSpecial(true);
186         }
187         if (index != 1)
188                 pagebreakCB->setChecked(false);
189         pagebreakCB->setEnabled(index == 1);
190         int itype = innerBoxCO->currentIndex();
191         setInnerType(frameless, itype);
192 }
193
194
195 void GuiBox::restoreClicked()
196 {
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);
205         }
206 }
207
208
209 void GuiBox::pagebreakClicked()
210 {
211         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
212         innerBoxCO->setEnabled(!pbreak);
213         if (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);
220                 setSpecial(false);
221         } else
222                 typeChanged(typeCO->currentIndex());
223         change_adaptor();
224 }
225
226 void GuiBox::updateContents()
227 {
228         string type = params_.type;
229         if (type == "Framed") {
230                 pagebreakCB->setChecked(true);
231                 type = "Boxed";
232         } else
233                 pagebreakCB->setChecked(false);
234
235         pagebreakCB->setEnabled(type == "Boxed");
236
237         for (unsigned int i = 0; i < gui_names_.size(); ++i) {
238                 if (type == ids_[i])
239                         typeCO->setCurrentIndex(i);
240         }
241
242         // default: minipage
243         unsigned int inner_type = 2;
244         if (!params_.inner_box)
245                 // none
246                 inner_type = 0;
247         if (params_.use_parbox)
248                 // parbox
249                 inner_type = 1;
250         bool frameless = (params_.type == "Frameless");
251         setInnerType(frameless, inner_type);
252
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));
257         c = params_.hor_pos;
258         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
259
260         bool ibox = params_.inner_box;
261         valignCO->setEnabled(ibox);
262         ialignCO->setEnabled(ibox);
263         halignCO->setEnabled(!ibox);
264         setSpecial(ibox);
265
266         Length::UNIT default_unit =
267                 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
268
269         lengthToWidgets(widthED, widthUnitsLC,
270                 (params_.width).asString(), default_unit);
271
272         string const special = params_.special;
273         if (!special.empty() && special != "none") {
274                 QString spc;
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());
278                 }
279                 for (int j = 0; j < widthUnitsLC->count(); j++) {
280                         if (widthUnitsLC->itemText(j) == spc)
281                                 widthUnitsLC->setCurrentIndex(j);
282                 }
283         }
284
285         lengthToWidgets(heightED, heightUnitsLC,
286                 (params_.height).asString(), default_unit);
287         
288         string const height_special = params_.height_special;
289         if (!height_special.empty() && height_special != "none") {
290                 QString hspc;
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());
294                 }
295                 for (int j = 0; j != heightUnitsLC->count(); j++) {
296                         if (heightUnitsLC->itemText(j) == hspc)
297                                 heightUnitsLC->setCurrentIndex(j);
298                 }
299         }
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);
305         else
306                 heightCB->setCheckState(Qt::Checked);
307
308         heightCB->setEnabled(ibox);
309 }
310
311
312 void GuiBox::applyView()
313 {
314         bool pagebreak = pagebreakCB->isChecked();
315         if (pagebreak)
316                 params_.type = "Framed";
317         else
318                 params_.type = ids_[typeCO->currentIndex()];
319
320         params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
321         params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
322
323         params_.pos = "tcb"[valignCO->currentIndex()];
324         params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
325         params_.hor_pos = "lcrs"[halignCO->currentIndex()];
326
327         int i = 0;
328         bool spec = false;
329         QString special = widthUnitsLC->currentText();
330         QString value = widthED->text();
331         if (special == qt_("Height")) {
332                 i = 1;
333                 spec = true;
334         } else if (special == qt_("Depth")) {
335                 i = 2;
336                 spec = true;
337         } else if (special == qt_("Total Height")) {
338                 i = 3;
339                 spec = true;
340         } else if (special == qt_("Width")) {
341                 i = 4;
342                 spec = true;
343         }
344         // the user might insert a non-special value in the line edit
345         if (isValidLength(fromqstr(value))) {
346                 i = 0;
347                 spec = false;
348         }
349         params_.special = ids_spec_[i];
350
351         string width;
352         if (spec) {
353                 width = fromqstr(value);
354                 // beware: bogosity! the unit is simply ignored in this case
355                 width += "in";
356         } else {
357                 width = widgetsToLength(widthED, widthUnitsLC);
358         }
359
360         params_.width = Length(width);
361
362         i = 0;
363         spec = false;
364         special = heightUnitsLC->currentText();
365         value = heightED->text();
366         if (special == qt_("Height")) {
367                 i = 1;
368                 spec = true;
369         } else if (special == qt_("Depth")) {
370                 i = 2;
371                 spec = true;
372         } else if (special == qt_("Total Height")) {
373                 i = 3;
374                 spec = true;
375         } else if (special == qt_("Width")) {
376                 i = 4;
377                 spec = true;
378         }
379         // the user might insert a non-special value in the line edit
380         if (isValidLength(fromqstr(value))) {
381                 i = 0;
382                 spec = false;
383         }
384         params_.height_special = ids_spec_[i];
385
386         string height;
387         if (spec  && !isValidLength(fromqstr(heightED->text()))) {
388                 height = fromqstr(value);
389                 // beware: bogosity! the unit is simply ignored in this case
390                 height += "in";
391         } else
392                 height = widgetsToLength(heightED, heightUnitsLC);
393
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);
400         else {
401                 params_.height = Length("1in");
402                 params_.height_special = ids_spec_[3];
403         }
404 }
405
406
407 void GuiBox::setSpecial(bool ibox)
408 {
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)
415                         has_special = true;
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]));
425         }
426 }
427
428
429 void GuiBox::setInnerType(bool frameless, int i)
430 {
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
433         if (frameless) {
434                 innerBoxCO->clear();
435                 innerBoxCO->addItem(qt_("Parbox"));
436                 innerBoxCO->addItem(qt_("Minipage"));
437                 if (i != 0)
438                         innerBoxCO->setCurrentIndex(i - 1);
439                 else
440                         innerBoxCO->setCurrentIndex(i);
441         } else {
442                 if (innerBoxCO->count() == 2)
443                         ++i;
444                 innerBoxCO->clear();
445                 innerBoxCO->addItem(qt_("None"));
446                 innerBoxCO->addItem(qt_("Parbox"));
447                 innerBoxCO->addItem(qt_("Minipage"));
448                 innerBoxCO->setCurrentIndex(i);
449         }
450 }
451
452 bool GuiBox::initialiseParams(string const & data)
453 {
454         InsetBoxMailer::string2params(data, params_);
455         return true;
456
457 }
458
459
460 void GuiBox::clearParams()
461 {
462         params_ = InsetBoxParams("");
463 }
464
465
466 void GuiBox::dispatchParams()
467 {
468         dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
469 }
470
471
472 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
473
474
475 } // namespace frontend
476 } // namespace lyx
477
478
479 #include "GuiBox_moc.cpp"