]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
On Linux show in crash message box the backtrace
[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 "LengthCombo.h"
18 #include "Length.h"
19 #include "qt_helpers.h"
20 #include "Validator.h"
21
22 #include "insets/InsetBox.h"
23
24 #include "support/gettext.h"
25 #include "support/foreach.h"
26 #include "support/lstrings.h"
27
28 #include <QPushButton>
29 #include <QLineEdit>
30
31 #ifdef IN
32 #undef IN
33 #endif
34
35 using namespace std;
36
37
38 namespace lyx {
39 namespace frontend {
40
41 static QStringList boxGuiIds()
42 {
43         return QStringList()
44                 << "Frameless" << "Boxed"
45                 << "ovalbox" << "Ovalbox"
46                 << "Shadowbox" << "Shaded"
47                 << "Doublebox";
48 }
49
50
51 static QStringList boxGuiNames()
52 {
53         return QStringList()
54                 << qt_("No frame") << qt_("Simple rectangular frame")
55                 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
56                 << qt_("Drop shadow") << qt_("Shaded background")
57                 << qt_("Double rectangular frame");
58 }
59
60
61 static QStringList boxGuiSpecialLengthIds()
62 {
63         return QStringList() << "height" << "depth"
64                 << "totalheight" << "width";
65 }
66
67
68 static QStringList boxGuiSpecialLengthNames()
69 {
70         return QStringList() << qt_("Height") << qt_("Depth")
71                 << qt_("Total Height") << qt_("Width");
72 }
73
74
75 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
76 {
77         setupUi(this);
78
79         // fill the box type choice
80         ids_ = boxGuiIds();
81         gui_names_ = boxGuiNames();
82         for (int i = 0; i != ids_.size(); ++i)
83                 typeCO->addItem(gui_names_[i], ids_[i]);
84
85         // add the special units to the height choice
86         // width needs different handling
87         ids_spec_ = boxGuiSpecialLengthIds();
88         gui_names_spec_ = boxGuiSpecialLengthNames();
89         for (int i = 0; i != ids_spec_.size(); ++i)
90                 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
91
92         connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
93         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
94                 this, SIGNAL(changed()));
95         connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
96         connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
97         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
98                 this, SIGNAL(changed()));
99         connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
100         connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
101
102         heightED->setValidator(unsignedLengthValidator(heightED));
103         widthED->setValidator(unsignedLengthValidator(widthED));
104
105         // initialize the length validator
106         addCheckedWidget(widthED, widthCB);
107         addCheckedWidget(heightED, heightCB);
108
109         initDialog();
110 }
111
112
113 void GuiBox::on_innerBoxCO_activated(int /* index */)
114 {
115         QString itype =
116                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
117         // handle parbox and minipage the same way
118         bool const ibox =
119                 (itype != "none"
120                  && itype != "makebox");
121         QString const outer =
122                 typeCO->itemData(typeCO->currentIndex()).toString();
123         valignCO->setEnabled(ibox);
124         ialignCO->setEnabled(ibox);
125         if (heightCB->isChecked() && !ibox)
126                 heightCB->setChecked(false);
127         heightCB->setEnabled(ibox);
128         // the width can only be selected for makebox or framebox
129         widthCB->setEnabled(itype == "makebox"
130                             || (outer == "Boxed" && itype == "none"));
131         widthCB->setChecked(!widthED->text().isEmpty());
132         // except for frameless and boxed, the width cannot be specified if
133         // there is no inner box
134         bool const width_enabled =
135                 ibox || outer == "Frameless" || outer == "Boxed";
136         // enable if width_enabled, except if checkbaox is active but unset
137         widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
138         widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
139         if (!widthCB->isChecked() && widthCB->isEnabled()) {
140                 widthED->setEnabled(false);
141                 widthUnitsLC->setEnabled(false);
142         }
143         // halign is only allowed without inner box and if a width is used and if
144         // pagebreak is not used
145         halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
146                              && ((!ibox && outer == "Boxed") || itype == "makebox"));
147         // pagebreak is only allowed for Boxed without inner box
148         pagebreakCB->setEnabled(!ibox && outer == "Boxed");
149         setSpecial(ibox);
150         changed();
151 }
152
153
154 void GuiBox::on_typeCO_activated(int index)
155 {
156         QString const type =
157                 typeCO->itemData(index).toString();
158         bool const frameless = (type == "Frameless");
159         QString itype =
160                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
161         setInnerType(frameless, itype);
162         // refresh itype because it might have been changed in setInnerType
163         itype =
164                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
165         // handle parbox and minipage the same way
166         bool const ibox =
167                 (itype != "none"
168                  && itype != "makebox");
169         if (frameless && itype != "makebox") {
170                 valignCO->setEnabled(ibox);
171                 ialignCO->setEnabled(ibox);
172                 if (heightCB->isChecked() && !ibox)
173                         heightCB->setChecked(false);
174                 heightCB->setEnabled(ibox);
175                 setSpecial(ibox);
176         }
177         // the width can only be selected for makebox or framebox
178         widthCB->setEnabled(itype == "makebox"
179                             || (type == "Boxed" && itype == "none"));
180         widthCB->setChecked(itype != "none" && !widthCB->isEnabled());
181         // except for frameless and boxed, the width cannot be specified if
182         // there is no inner box
183         bool const width_enabled =
184                 itype != "none" || frameless || type == "Boxed";
185         // enable if width_enabled, except if checkbaox is active but unset
186         widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
187         widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
188         if (!widthCB->isChecked() && widthCB->isEnabled()) {
189                 widthED->setEnabled(false);
190                 widthUnitsLC->setEnabled(false);
191         }
192         // halign is only allowed without inner box and if a width is used and if
193         // pagebreak is not used
194         halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
195                              && ((itype == "none" && type == "Boxed") || itype == "makebox"));
196         // pagebreak is only allowed for Boxed without inner box
197         pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
198         if (type != "Boxed")
199                 pagebreakCB->setChecked(false);
200         changed();
201 }
202
203
204 void GuiBox::initDialog()
205 {
206         setInnerType(true, toqstr("minipage"));
207         widthED->setText("100");
208         widthCB->setChecked(true);
209         widthCB->setEnabled(false);
210         widthUnitsLC->setCurrentItem(Length::PCW);
211         heightED->setText("1");
212         heightUnitsLC->setCurrentItem("totalheight");
213 }
214
215
216 void GuiBox::on_widthCB_stateChanged(int)
217 {
218         if (widthCB->isEnabled()) {
219                 widthED->setEnabled(widthCB->isChecked());
220                 widthUnitsLC->setEnabled(widthCB->isChecked());
221                 halignCO->setEnabled(widthCB->isChecked());
222         }
223         changed();
224 }
225
226
227 void GuiBox::on_heightCB_stateChanged(int state)
228 {
229         bool const enable = (innerBoxCO->currentText() != qt_("None"))
230                 && (state == Qt::Checked);
231         heightED->setEnabled(enable);
232         heightUnitsLC->setEnabled(enable);
233         changed();
234 }
235
236
237 void GuiBox::on_pagebreakCB_stateChanged()
238 {
239         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
240         innerBoxCO->setEnabled(!pbreak);
241         widthCB->setEnabled(!pbreak);
242         if (pbreak)
243                 widthCB->setChecked(!pbreak);
244         widthED->setEnabled(!pbreak);
245         widthUnitsLC->setEnabled(!pbreak);
246         if (!pbreak) {
247                 on_typeCO_activated(typeCO->currentIndex());
248                 return;
249         }
250         valignCO->setEnabled(false);
251         ialignCO->setEnabled(false);
252         halignCO->setEnabled(false);
253         heightCB->setEnabled(false);
254         heightED->setEnabled(false);
255         heightUnitsLC->setEnabled(false);
256         setSpecial(false);
257         changed();
258 }
259
260
261 void GuiBox::paramsToDialog(Inset const * inset)
262 {
263         InsetBox const * box = static_cast<InsetBox const *>(inset);
264         InsetBoxParams const & params = box->params();
265         QString type = toqstr(params.type);
266         if (type == "Framed") {
267                 pagebreakCB->setChecked(true);
268                 type = "Boxed";
269         } else {
270                 pagebreakCB->setChecked(false);
271         }
272
273         typeCO->setCurrentIndex(typeCO->findData(type));
274
275         // default: minipage
276         QString inner_type = "minipage";
277         if (!params.inner_box)
278                 inner_type = "none";
279         if (params.use_parbox)
280                 inner_type = "parbox";
281         if (params.use_makebox)
282                 inner_type = "makebox";
283         bool const frameless = (params.type == "Frameless");
284         setInnerType(frameless, inner_type);
285
286         char c = params.pos;
287         valignCO->setCurrentIndex(string("tcb").find(c, 0));
288         c = params.inner_pos;
289         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
290         c = params.hor_pos;
291         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
292
293         bool ibox = (params.inner_box && !params.use_makebox);
294         valignCO->setEnabled(ibox);
295         ialignCO->setEnabled(ibox);
296         setSpecial(ibox);
297
298         // halign is only allowed without inner box and if a width is used and if
299         // pagebreak is not used
300         halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
301                              && ((!ibox && type == "Boxed") || params.use_makebox));
302         // pagebreak is only allowed for Boxed without inner box
303         pagebreakCB->setEnabled(!ibox && type == "Boxed");
304
305         Length::UNIT const default_unit = Length::defaultUnit();
306
307         // the width can only be selected for makebox or framebox
308         widthCB->setEnabled(inner_type == "makebox"
309                             || (type == "Boxed"
310                                 && !ibox && !pagebreakCB->isChecked()));
311         if (params.width.empty()) {
312                 widthCB->setChecked(false);
313                 lengthToWidgets(widthED, widthUnitsLC,
314                         params.width, default_unit);
315         } else {
316                 widthCB->setChecked(true);
317                 lengthToWidgets(widthED, widthUnitsLC,
318                         params.width, default_unit);
319                 QString const special = toqstr(params.special);
320                 if (!special.isEmpty() && special != "none")
321                         widthUnitsLC->setCurrentItem(special);
322         }
323
324         widthED->setEnabled(widthCB->isChecked());
325         widthUnitsLC->setEnabled(widthCB->isChecked());
326
327         lengthToWidgets(heightED, heightUnitsLC,
328                 (params.height).asString(), default_unit);
329
330         QString const height_special = toqstr(params.height_special);
331         if (!height_special.isEmpty() && height_special != "none")
332                 heightUnitsLC->setCurrentItem(height_special);
333         // set no optional height if the value is the default "1\height"
334         // (special units like \height are handled as "in",
335         // FIXME: this is a very bad UI, this check box should be disabled in
336         // this case, not forced to 'unchecked' state.
337         if (height_special == "totalheight" && params.height == Length("1in"))
338                 heightCB->setCheckState(Qt::Unchecked);
339         else
340                 heightCB->setCheckState(Qt::Checked);
341
342         heightCB->setEnabled(ibox);
343 }
344
345
346 docstring GuiBox::dialogToParams() const
347 {
348         bool const pagebreak =
349                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
350         string box_type;
351         if (pagebreak)
352                 box_type = "Framed";
353         else
354                 box_type = fromqstr(typeCO->itemData(
355                                 typeCO->currentIndex()).toString());
356
357         InsetBoxParams params(box_type);
358         params.inner_box =
359                 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
360         params.use_parbox =
361                 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
362         params.use_makebox =
363                 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
364
365         params.pos = "tcb"[valignCO->currentIndex()];
366         params.inner_pos = "tcbs"[ialignCO->currentIndex()];
367         params.hor_pos = "lcrs"[halignCO->currentIndex()];
368
369         QString unit =
370                 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
371         QString value = widthED->text();
372
373         if (widthED->isEnabled()) {
374                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
375                         params.special = fromqstr(unit);
376                         // Note: the unit is simply ignored in this case
377                         params.width = Length(value.toDouble(), Length::IN);
378                 } else {
379                         params.special = "none";
380                         // we must specify a valid length in this case
381                         if (value.isEmpty())
382                                 widthED->setText("0");
383                         params.width = Length(widgetsToLength(widthED, widthUnitsLC));
384                 }
385         } else {
386                 params.special = "none";
387                 params.width = Length();
388         }
389
390         // the height parameter is omitted if the value
391         // is "1in" and "Total Height" is used as unit.
392         // 1in + "Total Height" means "1\height" which is the LaTeX default
393         // if no height is given
394         if (heightCB->checkState() == Qt::Unchecked) {
395                 params.height = Length("1in");
396                 params.height_special = "totalheight";
397         } else {
398                 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
399                 value = heightED->text();
400                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
401                         params.height_special = fromqstr(unit);
402                         // Note: the unit is simply ignored in this case
403                         params.height = Length(value.toDouble(), Length::IN);
404                 } else {
405                         params.height_special = "none";
406                         params.height =
407                                 Length(widgetsToLength(heightED, heightUnitsLC));
408                 }
409         }
410         return from_ascii(InsetBox::params2string(params));
411 }
412
413
414 void GuiBox::setSpecial(bool ibox)
415 {
416         QString const last_item =
417                 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
418
419         // check if the widget contains the special units
420         bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
421         // insert 'em if needed...
422         if (!ibox && !has_special) {
423                 for (int i = 1; i < ids_spec_.size(); ++i)
424                         widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
425         // ... or remove 'em if needed
426         } else if (ibox && has_special) {
427                 for (int i = 1; i < ids_spec_.size(); ++i) {
428                         int n = widthUnitsLC->findData(ids_spec_[i]);
429                         if (n != -1)
430                                 widthUnitsLC->removeItem(n);
431                 }
432         }
433         // restore selected text, if possible
434         widthUnitsLC->setCurrentItem(last_item);
435 }
436
437
438 void GuiBox::setInnerType(bool frameless, QString const & type)
439 {
440         // with "frameless" boxes, inner box is mandatory
441         // (i.e. is the actual box)
442         // we have to remove "none" then and adjust the combo
443         innerBoxCO->clear();
444         if (!frameless)
445                 innerBoxCO->addItem(qt_("None"), toqstr("none"));
446         else
447                 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
448         innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
449         innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
450         int i = (innerBoxCO->findData(type) != -1)
451                 ? innerBoxCO->findData(type) : 0;
452         innerBoxCO->setCurrentIndex(i);
453 }
454
455 } // namespace frontend
456 } // namespace lyx
457
458
459 #include "moc_GuiBox.cpp"