]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
add ability to change box line thickness and separation
[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         changed();
166 }
167
168
169 void GuiBox::initDialog()
170 {
171         setInnerType(true, toqstr("minipage"));
172         widthED->setText("100");
173         widthCB->setChecked(true);
174         widthCB->setEnabled(false);
175         widthUnitsLC->setCurrentItem(Length::PCW);
176         heightED->setText("1");
177         heightUnitsLC->setCurrentItem("totalheight");
178         // LaTeX's default for \fboxrule is 0.4 pt
179         thicknessED->setText("0.4");
180         thicknessUnitsLC->setCurrentItem(Length::PT);
181         // LaTeX's default for \fboxsep is 3 pt
182         separationED->setText("3");
183         separationUnitsLC->setCurrentItem(Length::PT);
184         // LaTeX's default for \shadowsize is 4 pt
185         shadowsizeED->setText("4");
186         shadowsizeUnitsLC->setCurrentItem(Length::PT);
187 }
188
189
190 void GuiBox::on_widthCB_stateChanged(int)
191 {
192         changed();
193 }
194
195
196 void GuiBox::on_heightCB_stateChanged(int /*state*/)
197 {
198         changed();
199 }
200
201
202 void GuiBox::on_pagebreakCB_stateChanged()
203 {
204         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
205         if (pbreak)
206                 widthCB->setChecked(!pbreak);
207         if (!pbreak) {
208                 on_typeCO_activated(typeCO->currentIndex());
209                 return;
210         }
211         setSpecial(false);
212         changed();
213 }
214
215
216 void GuiBox::paramsToDialog(Inset const * inset)
217 {
218         InsetBox const * box = static_cast<InsetBox const *>(inset);
219         InsetBoxParams const & params = box->params();
220         QString type = toqstr(params.type);
221         if (type == "Framed") {
222                 pagebreakCB->setChecked(true);
223                 type = "Boxed";
224         } else {
225                 pagebreakCB->setChecked(false);
226         }
227
228         typeCO->setCurrentIndex(typeCO->findData(type));
229
230         // default: minipage
231         QString inner_type = "minipage";
232         if (!params.inner_box)
233                 inner_type = "none";
234         if (params.use_parbox)
235                 inner_type = "parbox";
236         if (params.use_makebox)
237                 inner_type = "makebox";
238         bool const frameless = (params.type == "Frameless");
239         setInnerType(frameless, inner_type);
240
241         char c = params.pos;
242         valignCO->setCurrentIndex(string("tcb").find(c, 0));
243         c = params.inner_pos;
244         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
245         c = params.hor_pos;
246         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
247
248         bool ibox = (params.inner_box && !params.use_makebox);
249         valignCO->setEnabled(ibox);
250         ialignCO->setEnabled(ibox);
251         setSpecial(ibox);
252
253         // halign is only allowed if a width is used
254         halignCO->setEnabled(widthCB->isChecked());
255         // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
256         if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
257                 && halignCO->count() < 4)
258                 halignCO->addItem(toqstr("Stretch"));
259         else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
260                 halignCO->removeItem(3); 
261         // pagebreak is only allowed for Boxed without inner box
262         pagebreakCB->setEnabled(!ibox && type == "Boxed");
263
264         Length::UNIT const default_unit = Length::defaultUnit();
265
266         // the width can only be selected for makebox or framebox
267         widthCB->setEnabled(inner_type == "makebox"
268                             || (type == "Boxed"
269                                 && !ibox && !pagebreakCB->isChecked()));
270         if (params.width.empty()) {
271                 widthCB->setChecked(false);
272                 lengthToWidgets(widthED, widthUnitsLC,
273                         params.width, default_unit);
274         } else {
275                 widthCB->setChecked(true);
276                 lengthToWidgets(widthED, widthUnitsLC,
277                         params.width, default_unit);
278                 QString const special = toqstr(params.special);
279                 if (!special.isEmpty() && special != "none")
280                         widthUnitsLC->setCurrentItem(special);
281         }
282
283         widthED->setEnabled(widthCB->isChecked());
284         widthUnitsLC->setEnabled(widthCB->isChecked());
285
286         lengthToWidgets(heightED, heightUnitsLC,
287                 (params.height).asString(), default_unit);
288
289         QString const height_special = toqstr(params.height_special);
290         if (!height_special.isEmpty() && height_special != "none")
291                 heightUnitsLC->setCurrentItem(height_special);
292         // set no optional height if the value is the default "1\height"
293         // (special units like \height are handled as "in",
294         // FIXME: this is a very bad UI, this check box should be disabled in
295         // this case, not forced to 'unchecked' state.
296         if (height_special == "totalheight" && params.height == Length("1in"))
297                 heightCB->setCheckState(Qt::Unchecked);
298         else
299                 heightCB->setCheckState(Qt::Checked);
300
301         heightCB->setEnabled(ibox);
302
303         // enable line thickness only for the rectangular frame types and drop shadow
304         thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
305         thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
306         lengthToWidgets(thicknessED, thicknessUnitsLC,
307                 (params.thickness).asString(), default_unit);
308         // enable line separation for the allowed frame types
309         separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
310                 || type == "Doublebox" || type == "Shadowbox");
311         separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
312                 || type == "Doublebox" || type == "Shadowbox");
313         lengthToWidgets(separationED, separationUnitsLC,
314                 (params.separation).asString(), default_unit);
315         // enable shadow size for drop shadow
316         shadowsizeED->setEnabled(type == "Shadowbox");
317         shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
318         lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
319                 (params.shadowsize).asString(), default_unit);
320 }
321
322
323 docstring GuiBox::dialogToParams() const
324 {
325         bool const pagebreak =
326                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
327         string box_type;
328         if (pagebreak)
329                 box_type = "Framed";
330         else
331                 box_type = fromqstr(typeCO->itemData(
332                                 typeCO->currentIndex()).toString());
333
334         InsetBoxParams params(box_type);
335         params.inner_box =
336                 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
337         params.use_parbox =
338                 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
339         params.use_makebox =
340                 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
341
342         params.pos = "tcb"[valignCO->currentIndex()];
343         params.inner_pos = "tcbs"[ialignCO->currentIndex()];
344         params.hor_pos = "lcrs"[halignCO->currentIndex()];
345
346         QString unit =
347                 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
348         QString value = widthED->text();
349
350         if (widthED->isEnabled()) {
351                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
352                         params.special = fromqstr(unit);
353                         // Note: the unit is simply ignored in this case
354                         params.width = Length(value.toDouble(), Length::IN);
355                 } else {
356                         params.special = "none";
357                         // we must specify a valid length in this case
358                         if (value.isEmpty())
359                                 widthED->setText("0");
360                         params.width = Length(widgetsToLength(widthED, widthUnitsLC));
361                 }
362         } else {
363                 params.special = "none";
364                 params.width = Length();
365         }
366
367         // the height parameter is omitted if the value
368         // is "1in" and "Total Height" is used as unit.
369         // 1in + "Total Height" means "1\height" which is the LaTeX default
370         // if no height is given
371         if (heightCB->checkState() == Qt::Unchecked) {
372                 params.height = Length("1in");
373                 params.height_special = "totalheight";
374         } else {
375                 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
376                 value = heightED->text();
377                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
378                         params.height_special = fromqstr(unit);
379                         // Note: the unit is simply ignored in this case
380                         params.height = Length(value.toDouble(), Length::IN);
381                 } else {
382                         params.height_special = "none";
383                         params.height =
384                                 Length(widgetsToLength(heightED, heightUnitsLC));
385                 }
386         }
387
388         // handle the line thickness, line separation and shadow size
389         if (thicknessED->isEnabled())
390                 params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
391         else
392                 params.thickness = Length();
393         if (separationED->isEnabled())
394                 params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
395         else
396                 params.separation = Length();
397         if (separationED->isEnabled())
398                 params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
399         else
400                 params.shadowsize = Length();
401
402         return from_ascii(InsetBox::params2string(params));
403 }
404
405
406 bool GuiBox::checkWidgets(bool readonly) const
407 {
408         typeCO->setEnabled(!readonly);
409
410         if (readonly) {
411                 pagebreakCB->setEnabled(false);
412                 innerBoxCO->setEnabled(false);
413                 valignCO->setEnabled(false);
414                 ialignCO->setEnabled(false);
415                 halignCO->setEnabled(false);
416                 widthCB->setEnabled(false);
417                 widthED->setEnabled(false);
418                 widthUnitsLC->setEnabled(false);
419                 heightED->setEnabled(false);
420                 heightUnitsLC->setEnabled(false);
421                 heightCB->setEnabled(false);
422                 thicknessED->setEnabled(false);
423                 thicknessUnitsLC->setEnabled(false);
424                 separationED->setEnabled(false);
425                 separationUnitsLC->setEnabled(false);
426                 shadowsizeED->setEnabled(false);
427                 shadowsizeUnitsLC->setEnabled(false);
428         } else {
429                 QString const outer =
430                         typeCO->itemData(typeCO->currentIndex()).toString();
431                 QString const itype =
432                         innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
433                 bool const ibox = (itype != "none" && itype != "makebox");
434                 valignCO->setEnabled(ibox);
435                 ialignCO->setEnabled(ibox);
436                 if (heightCB->isChecked() && !ibox)
437                         heightCB->setChecked(false);
438                 heightCB->setEnabled(ibox);
439                 // the width can only be selected for makebox or framebox
440                 widthCB->setEnabled(itype == "makebox"
441                         || (outer == "Boxed" && itype == "none")
442                         && !pagebreakCB->isChecked());
443                 // except for Frameless and Boxed, the width cannot be specified if
444                 // there is no inner box
445                 bool const width_enabled =
446                         ibox || outer == "Frameless" || outer == "Boxed";
447                 // enable if width_enabled
448                 widthED->setEnabled(width_enabled);
449                 widthUnitsLC->setEnabled(width_enabled);
450                 if (!widthCB->isChecked() && widthCB->isEnabled()) {
451                         widthED->setEnabled(false);
452                         widthUnitsLC->setEnabled(false);
453                 }
454                 // halign is only allowed if a width is used
455                 halignCO->setEnabled(widthCB->isChecked());
456                 // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
457                 if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
458                         && halignCO->count() < 4)
459                         halignCO->addItem(toqstr("Stretch"));
460                 else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
461                         halignCO->removeItem(3);
462                 // pagebreak is only allowed for Boxed without inner box
463                 pagebreakCB->setEnabled(!ibox && outer == "Boxed");
464
465                 heightED->setEnabled(itype != "none" && heightCB->isChecked());
466                 heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
467                 heightCB->setEnabled(ibox);
468
469                 // enable line thickness for the rectangular frame types and drop shadow
470                 thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
471                 thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
472                 // set default values if empty
473                 if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
474                         thicknessED->setText("0.4");
475                         thicknessUnitsLC->setCurrentItem(Length::PT);
476                 }
477                 // enable line separation for the allowed frame types
478                 separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
479                         || outer == "Doublebox" || outer == "Shadowbox");
480                 separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
481                         || outer == "Doublebox" || outer == "Shadowbox");
482                 // set default values if empty
483                 if (separationED->text().isEmpty() && separationED->isEnabled()) {
484                         separationED->setText("3");
485                         separationUnitsLC->setCurrentItem(Length::PT);
486                 }
487                 // enable shadow size for drop shadow
488                 shadowsizeED->setEnabled(outer == "Shadowbox");
489                 shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
490                 // set default values if empty
491                 if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
492                         shadowsizeED->setText("4");
493                         shadowsizeUnitsLC->setCurrentItem(Length::PT);
494                 }
495         }
496
497         return InsetParamsWidget::checkWidgets();
498 }
499
500
501 void GuiBox::setSpecial(bool ibox)
502 {
503         QString const last_item =
504                 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
505
506         // check if the widget contains the special units
507         bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
508         // insert 'em if needed...
509         if (!ibox && !has_special) {
510                 for (int i = 1; i < ids_spec_.size(); ++i)
511                         widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
512         // ... or remove 'em if needed
513         } else if (ibox && has_special) {
514                 for (int i = 1; i < ids_spec_.size(); ++i) {
515                         int n = widthUnitsLC->findData(ids_spec_[i]);
516                         if (n != -1)
517                                 widthUnitsLC->removeItem(n);
518                 }
519         }
520         // restore selected text, if possible
521         widthUnitsLC->setCurrentItem(last_item);
522 }
523
524
525 void GuiBox::setInnerType(bool frameless, QString const & type)
526 {
527         // with "frameless" boxes, inner box is mandatory
528         // (i.e. is the actual box)
529         // we have to remove "none" then and adjust the combo
530         innerBoxCO->clear();
531         if (!frameless)
532                 innerBoxCO->addItem(qt_("None"), toqstr("none"));
533         else
534                 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
535         innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
536         innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
537         int i = (innerBoxCO->findData(type) != -1)
538                 ? innerBoxCO->findData(type) : 0;
539         innerBoxCO->setCurrentIndex(i);
540 }
541
542 } // namespace frontend
543 } // namespace lyx
544
545
546 #include "moc_GuiBox.cpp"