]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
GuiBox.cpp: fix button logic (fixes bug #9543)
[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 = (itype != "none" && itype != "makebox");
119         if (heightCB->isChecked() && !ibox)
120                 heightCB->setChecked(false);
121         widthCB->setChecked(!widthED->text().isEmpty());
122         setSpecial(ibox);
123         changed();
124 }
125
126
127 void GuiBox::on_typeCO_activated(int index)
128 {
129         QString const type =
130                 typeCO->itemData(index).toString();
131         bool const frameless = (type == "Frameless");
132         QString itype =
133                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
134         setInnerType(frameless, itype);
135         // refresh itype because it might have been changed in setInnerType
136         itype =
137                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
138         // handle parbox and minipage the same way
139         bool const ibox = (itype != "none" && itype != "makebox");
140         if (frameless && itype != "makebox") {
141                 if (heightCB->isChecked() && !ibox)
142                         heightCB->setChecked(false);
143                 setSpecial(ibox);
144         }
145         if (type != "Boxed")
146                 if (type != "Frameless")
147                         widthCB->setChecked(itype != "none");
148                 pagebreakCB->setChecked(false);
149         changed();
150 }
151
152
153 void GuiBox::initDialog()
154 {
155         setInnerType(true, toqstr("minipage"));
156         widthED->setText("100");
157         widthCB->setChecked(true);
158         widthCB->setEnabled(false);
159         widthUnitsLC->setCurrentItem(Length::PCW);
160         heightED->setText("1");
161         heightUnitsLC->setCurrentItem("totalheight");
162 }
163
164
165 void GuiBox::on_widthCB_stateChanged(int)
166 {
167         changed();
168 }
169
170
171 void GuiBox::on_heightCB_stateChanged(int /*state*/)
172 {
173         changed();
174 }
175
176
177 void GuiBox::on_pagebreakCB_stateChanged()
178 {
179         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
180         if (pbreak)
181                 widthCB->setChecked(!pbreak);
182         if (!pbreak) {
183                 on_typeCO_activated(typeCO->currentIndex());
184                 return;
185         }
186         setSpecial(false);
187         changed();
188 }
189
190
191 void GuiBox::paramsToDialog(Inset const * inset)
192 {
193         InsetBox const * box = static_cast<InsetBox const *>(inset);
194         InsetBoxParams const & params = box->params();
195         QString type = toqstr(params.type);
196         if (type == "Framed") {
197                 pagebreakCB->setChecked(true);
198                 type = "Boxed";
199         } else {
200                 pagebreakCB->setChecked(false);
201         }
202
203         typeCO->setCurrentIndex(typeCO->findData(type));
204
205         // default: minipage
206         QString inner_type = "minipage";
207         if (!params.inner_box)
208                 inner_type = "none";
209         if (params.use_parbox)
210                 inner_type = "parbox";
211         if (params.use_makebox)
212                 inner_type = "makebox";
213         bool const frameless = (params.type == "Frameless");
214         setInnerType(frameless, inner_type);
215
216         char c = params.pos;
217         valignCO->setCurrentIndex(string("tcb").find(c, 0));
218         c = params.inner_pos;
219         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
220         c = params.hor_pos;
221         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
222
223         bool ibox = (params.inner_box && !params.use_makebox);
224         valignCO->setEnabled(ibox);
225         ialignCO->setEnabled(ibox);
226         setSpecial(ibox);
227
228         // halign is only allowed without inner box and if a width is used and if
229         // pagebreak is not used
230         halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
231                              && ((!ibox && type == "Boxed") || params.use_makebox));
232         // pagebreak is only allowed for Boxed without inner box
233         pagebreakCB->setEnabled(!ibox && type == "Boxed");
234
235         Length::UNIT const default_unit = Length::defaultUnit();
236
237         // the width can only be selected for makebox or framebox
238         widthCB->setEnabled(inner_type == "makebox"
239                             || (type == "Boxed"
240                                 && !ibox && !pagebreakCB->isChecked()));
241         if (params.width.empty()) {
242                 widthCB->setChecked(false);
243                 lengthToWidgets(widthED, widthUnitsLC,
244                         params.width, default_unit);
245         } else {
246                 widthCB->setChecked(true);
247                 lengthToWidgets(widthED, widthUnitsLC,
248                         params.width, default_unit);
249                 QString const special = toqstr(params.special);
250                 if (!special.isEmpty() && special != "none")
251                         widthUnitsLC->setCurrentItem(special);
252         }
253
254         widthED->setEnabled(widthCB->isChecked());
255         widthUnitsLC->setEnabled(widthCB->isChecked());
256
257         lengthToWidgets(heightED, heightUnitsLC,
258                 (params.height).asString(), default_unit);
259
260         QString const height_special = toqstr(params.height_special);
261         if (!height_special.isEmpty() && height_special != "none")
262                 heightUnitsLC->setCurrentItem(height_special);
263         // set no optional height if the value is the default "1\height"
264         // (special units like \height are handled as "in",
265         // FIXME: this is a very bad UI, this check box should be disabled in
266         // this case, not forced to 'unchecked' state.
267         if (height_special == "totalheight" && params.height == Length("1in"))
268                 heightCB->setCheckState(Qt::Unchecked);
269         else
270                 heightCB->setCheckState(Qt::Checked);
271
272         heightCB->setEnabled(ibox);
273 }
274
275
276 docstring GuiBox::dialogToParams() const
277 {
278         bool const pagebreak =
279                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
280         string box_type;
281         if (pagebreak)
282                 box_type = "Framed";
283         else
284                 box_type = fromqstr(typeCO->itemData(
285                                 typeCO->currentIndex()).toString());
286
287         InsetBoxParams params(box_type);
288         params.inner_box =
289                 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
290         params.use_parbox =
291                 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
292         params.use_makebox =
293                 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
294
295         params.pos = "tcb"[valignCO->currentIndex()];
296         params.inner_pos = "tcbs"[ialignCO->currentIndex()];
297         params.hor_pos = "lcrs"[halignCO->currentIndex()];
298
299         QString unit =
300                 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
301         QString value = widthED->text();
302
303         if (widthED->isEnabled()) {
304                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
305                         params.special = fromqstr(unit);
306                         // Note: the unit is simply ignored in this case
307                         params.width = Length(value.toDouble(), Length::IN);
308                 } else {
309                         params.special = "none";
310                         // we must specify a valid length in this case
311                         if (value.isEmpty())
312                                 widthED->setText("0");
313                         params.width = Length(widgetsToLength(widthED, widthUnitsLC));
314                 }
315         } else {
316                 params.special = "none";
317                 params.width = Length();
318         }
319
320         // the height parameter is omitted if the value
321         // is "1in" and "Total Height" is used as unit.
322         // 1in + "Total Height" means "1\height" which is the LaTeX default
323         // if no height is given
324         if (heightCB->checkState() == Qt::Unchecked) {
325                 params.height = Length("1in");
326                 params.height_special = "totalheight";
327         } else {
328                 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
329                 value = heightED->text();
330                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
331                         params.height_special = fromqstr(unit);
332                         // Note: the unit is simply ignored in this case
333                         params.height = Length(value.toDouble(), Length::IN);
334                 } else {
335                         params.height_special = "none";
336                         params.height =
337                                 Length(widgetsToLength(heightED, heightUnitsLC));
338                 }
339         }
340         return from_ascii(InsetBox::params2string(params));
341 }
342
343
344 bool GuiBox::checkWidgets(bool readonly) const
345 {
346         typeCO->setEnabled(!readonly);
347
348         if (readonly) {
349                 pagebreakCB->setEnabled(false);
350                 innerBoxCO->setEnabled(false);
351                 valignCO->setEnabled(false);
352                 ialignCO->setEnabled(false);
353                 halignCO->setEnabled(false);
354                 widthCB->setEnabled(false);
355                 widthED->setEnabled(false);
356                 widthUnitsLC->setEnabled(false);
357                 heightED->setEnabled(false);
358                 heightUnitsLC->setEnabled(false);
359                 heightCB->setEnabled(false);
360         } else {
361                 QString const outer =
362                         typeCO->itemData(typeCO->currentIndex()).toString();
363                 QString const itype =
364                         innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
365                 bool const ibox = (itype != "none" && itype != "makebox");
366                 // pagebreak is only allowed for Boxed without inner box
367                 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
368                 innerBoxCO->setEnabled(!pagebreakCB->isChecked());
369                 valignCO->setEnabled(ibox);
370                 ialignCO->setEnabled(ibox);
371                 // halign is only allowed without inner box and if a width is used and if
372                 // pagebreak is not used
373                 halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
374                                      && ((!ibox && outer == "Boxed") || itype == "makebox"));
375                 // the width can only be selected for makebox or framebox
376                 widthCB->setEnabled(itype == "makebox"
377                                     || (outer == "Boxed"
378                                         && !ibox && !pagebreakCB->isChecked()));
379                 // except for frameless and boxed, the width cannot be specified if
380                 // there is no inner box
381                 bool const width_enabled =
382                         ibox || outer == "Frameless" || outer == "Boxed";
383                 // enable if width_enabled, except if checkbox is active but unset
384                 widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
385                 widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
386                 if (!widthCB->isChecked() && widthCB->isEnabled()) {
387                         widthED->setEnabled(false);
388                         widthUnitsLC->setEnabled(false);
389                 }
390                 heightED->setEnabled(itype != "none" && heightCB->isChecked());
391                 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
392                 heightCB->setEnabled(ibox);
393         }
394
395         return InsetParamsWidget::checkWidgets();
396 }
397
398
399 void GuiBox::setSpecial(bool ibox)
400 {
401         QString const last_item =
402                 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
403
404         // check if the widget contains the special units
405         bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
406         // insert 'em if needed...
407         if (!ibox && !has_special) {
408                 for (int i = 1; i < ids_spec_.size(); ++i)
409                         widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
410         // ... or remove 'em if needed
411         } else if (ibox && has_special) {
412                 for (int i = 1; i < ids_spec_.size(); ++i) {
413                         int n = widthUnitsLC->findData(ids_spec_[i]);
414                         if (n != -1)
415                                 widthUnitsLC->removeItem(n);
416                 }
417         }
418         // restore selected text, if possible
419         widthUnitsLC->setCurrentItem(last_item);
420 }
421
422
423 void GuiBox::setInnerType(bool frameless, QString const & type)
424 {
425         // with "frameless" boxes, inner box is mandatory
426         // (i.e. is the actual box)
427         // we have to remove "none" then and adjust the combo
428         innerBoxCO->clear();
429         if (!frameless)
430                 innerBoxCO->addItem(qt_("None"), toqstr("none"));
431         else
432                 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
433         innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
434         innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
435         int i = (innerBoxCO->findData(type) != -1)
436                 ? innerBoxCO->findData(type) : 0;
437         innerBoxCO->setCurrentIndex(i);
438 }
439
440 } // namespace frontend
441 } // namespace lyx
442
443
444 #include "moc_GuiBox.cpp"