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