]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
Crush warning.
[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(itype != "none" && !widthCB->isEnabled());
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         widthUnitsLC->setCurrentItem(Length::PCW);
209         heightED->setText("1");
210         heightUnitsLC->setCurrentItem("totalheight");
211 }
212
213
214 void GuiBox::on_widthCB_stateChanged(int)
215 {
216         if (widthCB->isEnabled()) {
217                 widthED->setEnabled(widthCB->isChecked());
218                 widthUnitsLC->setEnabled(widthCB->isChecked());
219                 halignCO->setEnabled(widthCB->isChecked());
220         }
221         changed();
222 }
223
224
225 void GuiBox::on_heightCB_stateChanged(int state)
226 {
227         bool const enable = (innerBoxCO->currentText() != qt_("None"))
228                 && (state == Qt::Checked);
229         heightED->setEnabled(enable);
230         heightUnitsLC->setEnabled(enable);
231         changed();
232 }
233
234
235 void GuiBox::on_pagebreakCB_stateChanged()
236 {
237         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
238         innerBoxCO->setEnabled(!pbreak);
239         widthCB->setEnabled(!pbreak);
240         if (pbreak)
241                 widthCB->setChecked(!pbreak);
242         widthED->setEnabled(!pbreak);
243         widthUnitsLC->setEnabled(!pbreak);
244         if (!pbreak) {
245                 on_typeCO_activated(typeCO->currentIndex());
246                 return;
247         }
248         valignCO->setEnabled(false);
249         ialignCO->setEnabled(false);
250         halignCO->setEnabled(false);
251         heightCB->setEnabled(false);
252         heightED->setEnabled(false);
253         heightUnitsLC->setEnabled(false);
254         setSpecial(false);
255         changed();
256 }
257
258
259 void GuiBox::paramsToDialog(Inset const * inset)
260 {
261         InsetBox const * box = static_cast<InsetBox const *>(inset);
262         InsetBoxParams const & params = box->params();
263         QString type = toqstr(params.type);
264         if (type == "Framed") {
265                 pagebreakCB->setChecked(true);
266                 type = "Boxed";
267         } else {
268                 pagebreakCB->setChecked(false);
269         }
270
271         typeCO->setCurrentIndex(typeCO->findData(type));
272
273         // default: minipage
274         QString inner_type = "minipage";
275         if (!params.inner_box)
276                 inner_type = "none";
277         if (params.use_parbox)
278                 inner_type = "parbox";
279         if (params.use_makebox)
280                 inner_type = "makebox";
281         bool const frameless = (params.type == "Frameless");
282         setInnerType(frameless, inner_type);
283
284         char c = params.pos;
285         valignCO->setCurrentIndex(string("tcb").find(c, 0));
286         c = params.inner_pos;
287         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
288         c = params.hor_pos;
289         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
290
291         bool ibox = (params.inner_box && !params.use_makebox);
292         valignCO->setEnabled(ibox);
293         ialignCO->setEnabled(ibox);
294         setSpecial(ibox);
295
296         // halign is only allowed without inner box and if a width is used and if
297         // pagebreak is not used
298         halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
299                              && ((!ibox && type == "Boxed") || params.use_makebox));
300         // pagebreak is only allowed for Boxed without inner box
301         pagebreakCB->setEnabled(!ibox && type == "Boxed");
302
303         Length::UNIT const default_unit = Length::defaultUnit();
304
305         // the width can only be selected for makebox or framebox
306         widthCB->setEnabled(inner_type == "makebox" 
307                             || (type == "Boxed" && !ibox && !pagebreakCB->isChecked()));
308         // "-999col%" is the code for no width
309         if ((params.width).asString() == "-999col%")
310                 widthCB->setCheckState(Qt::Unchecked);
311         else {
312                 if (widthCB->isEnabled())
313                         widthCB->setChecked(true);
314                 lengthToWidgets(widthED, widthUnitsLC,
315                         (params.width).asString(), default_unit);
316                 QString const special = toqstr(params.special);
317                 if (!special.isEmpty() && special != "none")
318                         widthUnitsLC->setCurrentItem(special);
319         }
320
321         widthED->setEnabled(widthCB->isChecked());
322         widthUnitsLC->setEnabled(widthCB->isChecked());
323
324         lengthToWidgets(heightED, heightUnitsLC,
325                 (params.height).asString(), default_unit);
326         
327         QString const height_special = toqstr(params.height_special);
328         if (!height_special.isEmpty() && height_special != "none")
329                 heightUnitsLC->setCurrentItem(height_special);
330         // set no optional height if the value is the default "1\height"
331         // (special units like \height are handled as "in",
332         // FIXME: this is a very bad UI, this check box should be disabled in
333         // this case, not forced to 'unchecked' state.
334         if (height_special == "totalheight" && params.height == Length("1in"))
335                 heightCB->setCheckState(Qt::Unchecked);
336         else
337                 heightCB->setCheckState(Qt::Checked);
338
339         heightCB->setEnabled(ibox);
340 }
341
342
343 docstring GuiBox::dialogToParams() const
344 {
345         bool const pagebreak =
346                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
347         string box_type;
348         if (pagebreak)
349                 box_type = "Framed";
350         else
351                 box_type = fromqstr(typeCO->itemData(
352                                 typeCO->currentIndex()).toString());
353
354         InsetBoxParams params(box_type);
355         params.inner_box =
356                 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
357         params.use_parbox =
358                 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
359         params.use_makebox =
360                 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
361
362         params.pos = "tcb"[valignCO->currentIndex()];
363         params.inner_pos = "tcbs"[ialignCO->currentIndex()];
364         params.hor_pos = "lcrs"[halignCO->currentIndex()];
365
366         QString unit =
367                 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
368         QString value = widthED->text();
369
370         if (widthCB->isChecked()) {
371          if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
372                  params.special = fromqstr(unit);
373                  // Note: the unit is simply ignored in this case
374                  params.width = Length(value.toDouble(), Length::IN);
375          } else {
376                  params.special = "none";
377                  params.width = Length(widgetsToLength(widthED, widthUnitsLC));
378          }
379         } else {
380                 if (widthCB->isEnabled()) {
381                         // use the code "-999col%" for the case that no width was selected
382                         params.special = "none";
383                         params.width = Length("-999col%");
384                 }
385         }
386
387         // the height parameter is omitted if the value
388         // is "1in" and "Total Height" is used as unit.
389         // 1in + "Total Height" means "1\height" which is the LaTeX default
390         // if no height is given
391         if (heightCB->checkState() == Qt::Unchecked) {
392                 params.height = Length("1in");
393                 params.height_special = "totalheight";
394         } else {
395                 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
396                 value = heightED->text();
397                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
398                         params.height_special = fromqstr(unit);
399                         // Note: the unit is simply ignored in this case
400                         params.height = Length(value.toDouble(), Length::IN);
401                 } else {
402                         params.height_special = "none";
403                         params.height =
404                                 Length(widgetsToLength(heightED, heightUnitsLC));
405                 }
406         }
407         return from_ascii(InsetBox::params2string(params));
408 }
409
410
411 void GuiBox::setSpecial(bool ibox)
412 {
413         QString const last_item =
414                 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
415
416         // check if the widget contains the special units
417         bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
418         // insert 'em if needed...
419         if (!ibox && !has_special) {
420                 for (int i = 1; i < ids_spec_.size(); ++i)
421                         widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
422         // ... or remove 'em if needed
423         } else if (ibox && has_special) {
424                 for (int i = 1; i < ids_spec_.size(); ++i) {
425                         int n = widthUnitsLC->findData(ids_spec_[i]);
426                         if (n != -1)
427                                 widthUnitsLC->removeItem(n);
428                 }
429         }
430         // restore selected text, if possible
431         widthUnitsLC->setCurrentItem(last_item);
432 }
433
434
435 void GuiBox::setInnerType(bool frameless, QString const & type)
436 {
437         // with "frameless" boxes, inner box is mandatory
438         // (i.e. is the actual box)
439         // we have to remove "none" then and adjust the combo
440         innerBoxCO->clear();
441         if (!frameless)
442                 innerBoxCO->addItem(qt_("None"), toqstr("none"));
443         else
444                 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
445         innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
446         innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
447         int i = (innerBoxCO->findData(type) != -1)
448                 ? innerBoxCO->findData(type) : 0;
449         innerBoxCO->setCurrentIndex(i);
450 }
451
452 } // namespace frontend
453 } // namespace lyx
454
455
456 #include "moc_GuiBox.cpp"