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