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