]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
Box fixes from Vincent and me.
[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 "FuncRequest.h"
18 #include "LengthCombo.h"
19 #include "Length.h"
20 #include "LyXRC.h" // to set the default length values
21 #include "qt_helpers.h"
22 #include "Validator.h"
23
24 #include "insets/InsetBox.h"
25
26 #include "support/gettext.h"
27 #include "support/foreach.h"
28 #include "support/lstrings.h"
29
30 #include <QPushButton>
31 #include <QLineEdit>
32
33 #ifdef IN
34 #undef IN
35 #endif
36
37 using namespace std;
38
39
40 namespace lyx {
41 namespace frontend {
42
43 static QStringList boxGuiIds()
44 {
45         return QStringList()
46                 << "Frameless" << "Boxed"
47                 << "ovalbox" << "Ovalbox"
48                 << "Shadowbox" << "Shaded"
49                 << "Doublebox";
50 }
51
52
53 static QStringList boxGuiNames()
54 {
55         return QStringList()
56                 << qt_("No frame") << qt_("Simple rectangular frame")
57                 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
58                 << qt_("Drop shadow") << qt_("Shaded background")
59                 << qt_("Double rectangular frame");
60 }
61
62
63 static QStringList boxGuiSpecialLengthIds()
64 {
65         return QStringList() << "none" << "height" << "depth"
66                 << "totalheight" << "width";
67 }
68
69
70 static QStringList boxGuiSpecialLengthNames()
71 {
72         return QStringList() << qt_("None") << qt_("Height") << qt_("Depth")
73                 << qt_("Total Height") << qt_("Width");
74 }
75
76
77 GuiBox::GuiBox(GuiView & lv)
78         : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
79 {
80         setupUi(this);
81
82         // fill the box type choice
83         ids_ = boxGuiIds();
84         gui_names_ = boxGuiNames();
85         foreach (QString const & str, gui_names_)
86                 typeCO->addItem(str);
87
88         // add the special units to the height choice
89         // width needs different handling
90         ids_spec_ = boxGuiSpecialLengthIds();
91         gui_names_spec_ = boxGuiSpecialLengthNames();
92         foreach (QString const & str, gui_names_spec_)
93                 heightUnitsLC->addItem(str);
94
95         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
96         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
97         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
98         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
99
100         connect(widthED, SIGNAL(textChanged(QString)),
101                 this, SLOT(change_adaptor()));
102         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
103                 this, SLOT(change_adaptor()));
104         connect(valignCO, SIGNAL(highlighted(QString)),
105                 this, SLOT(change_adaptor()));
106         connect(heightCB, SIGNAL(stateChanged(int)),
107                 this, SLOT(change_adaptor()));
108         connect(heightED, SIGNAL(textChanged(QString)),
109                 this, SLOT(change_adaptor()));
110         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
111                 this, SLOT(change_adaptor()));
112         connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
113         connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
114         connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
115         connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
116         connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
117         connect(innerBoxCO, SIGNAL(activated(QString)),
118                 this, SLOT(innerBoxChanged(QString)));
119         connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
120         connect(pagebreakCB, SIGNAL(stateChanged(int)),
121                 this, SLOT(pagebreakClicked()));
122
123         heightED->setValidator(unsignedLengthValidator(heightED));
124         widthED->setValidator(unsignedLengthValidator(widthED));
125
126         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
127
128         bc().addReadOnly(typeCO);
129         bc().addReadOnly(innerBoxCO);
130         bc().addReadOnly(valignCO);
131         bc().addReadOnly(ialignCO);
132         bc().addReadOnly(halignCO);
133         bc().addReadOnly(widthED);
134         bc().addReadOnly(widthUnitsLC);
135         bc().addReadOnly(heightCB);
136         bc().addReadOnly(heightED);
137         bc().addReadOnly(heightUnitsLC);
138         bc().addReadOnly(pagebreakCB);
139
140         bc().setRestore(restorePB);
141         bc().setOK(okPB);
142         bc().setApply(applyPB);
143         bc().setCancel(closePB);
144
145         // initialize the length validator
146         bc().addCheckedLineEdit(widthED, widthLA);
147         bc().addCheckedLineEdit(heightED, heightCB);
148 }
149
150
151 void GuiBox::change_adaptor()
152 {
153         changed();
154 }
155
156
157 void GuiBox::innerBoxChanged(QString const & str)
158 {
159         bool const ibox = (str != qt_("None"));
160         valignCO->setEnabled(ibox);
161         ialignCO->setEnabled(ibox);
162         halignCO->setEnabled(!ibox);
163         heightCB->setEnabled(ibox);
164         pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
165         if (heightCB->checkState() == Qt::Checked && ibox) {
166                 heightED->setEnabled(true);
167                 heightUnitsLC->setEnabled(true);
168         }
169         setSpecial(ibox);
170 }
171
172
173 void GuiBox::typeChanged(int index)
174 {
175         bool const frameless = (index == 0);
176         if (frameless) {
177                 valignCO->setEnabled(true);
178                 ialignCO->setEnabled(true);
179                 halignCO->setEnabled(false);
180                 heightCB->setEnabled(true);
181                 heightED->setEnabled(true);
182                 heightUnitsLC->setEnabled(true);
183                 setSpecial(true);
184         }
185         if (index != 1)
186                 pagebreakCB->setChecked(false);
187         int itype = innerBoxCO->currentIndex();
188         if (innerBoxCO->count() == 2)
189                 ++itype;
190         pagebreakCB->setEnabled(index == 1 && itype == 0);
191         widthED->setEnabled(index != 5);
192         widthUnitsLC->setEnabled(index != 5);
193         setInnerType(frameless, itype);
194 }
195
196
197 void GuiBox::restoreClicked()
198 {
199         setInnerType(true, 2);
200         widthED->setText("100");
201         widthUnitsLC->setCurrentItem(Length::PCW);
202         heightCB->setCheckState(Qt::Checked);
203         heightED->setText("1");
204         for (int i = 0; i != heightUnitsLC->count(); ++i) {
205                 if (heightUnitsLC->itemText(i) == qt_("Total Height"))
206                         heightUnitsLC->setCurrentItem(i);
207         }
208 }
209
210
211 void GuiBox::pagebreakClicked()
212 {
213         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
214         innerBoxCO->setEnabled(!pbreak);
215         widthED->setEnabled(!pbreak);
216         widthUnitsLC->setEnabled(!pbreak);
217         if (pbreak) {
218                 valignCO->setEnabled(false);
219                 ialignCO->setEnabled(false);
220                 halignCO->setEnabled(false);
221                 heightCB->setEnabled(false);
222                 heightED->setEnabled(false);
223                 heightUnitsLC->setEnabled(false);
224                 setSpecial(false);
225         } else {
226                 typeChanged(typeCO->currentIndex());
227         }
228         change_adaptor();
229 }
230
231
232 void GuiBox::updateContents()
233 {
234         QString type = toqstr(params_.type);
235         if (type == "Framed") {
236                 pagebreakCB->setChecked(true);
237                 type = "Boxed";
238         } else {
239                 pagebreakCB->setChecked(false);
240         }
241
242         pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
243
244         for (int i = 0; i != gui_names_.size(); ++i) {
245                 if (type == ids_[i])
246                         typeCO->setCurrentIndex(i);
247         }
248
249         // default: minipage
250         int inner_type = 2;
251         if (!params_.inner_box)
252                 // none
253                 inner_type = 0;
254         if (params_.use_parbox)
255                 // parbox
256                 inner_type = 1;
257         bool frameless = (params_.type == "Frameless");
258         setInnerType(frameless, inner_type);
259
260         char c = params_.pos;
261         valignCO->setCurrentIndex(string("tcb").find(c, 0));
262         c = params_.inner_pos;
263         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
264         c = params_.hor_pos;
265         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
266
267         bool ibox = params_.inner_box;
268         valignCO->setEnabled(ibox);
269         ialignCO->setEnabled(ibox);
270         halignCO->setEnabled(!ibox);
271         setSpecial(ibox);
272
273         Length::UNIT default_unit =
274                 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
275
276         lengthToWidgets(widthED, widthUnitsLC,
277                 (params_.width).asString(), default_unit);
278
279         QString const special = toqstr(params_.special);
280         if (!special.isEmpty() && special != "none") {
281                 QString spc;
282                 for (int i = 0; i != gui_names_spec_.size(); ++i) {
283                         if (special == ids_spec_[i])
284                                 spc = gui_names_spec_[i];
285                 }
286                 for (int i = 0; i != widthUnitsLC->count(); ++i) {
287                         if (widthUnitsLC->itemText(i) == spc)
288                                 widthUnitsLC->setCurrentIndex(i);
289                 }
290         }
291
292         lengthToWidgets(heightED, heightUnitsLC,
293                 (params_.height).asString(), default_unit);
294         
295         QString const height_special = toqstr(params_.height_special);
296         if (!height_special.isEmpty() && height_special != "none") {
297                 QString hspc;
298                 for (int i = 0; i != gui_names_spec_.size(); ++i) {
299                         if (height_special == ids_spec_[i])
300                                 hspc = gui_names_spec_[i];
301                 }
302                 for (int i = 0; i != heightUnitsLC->count(); ++i) {
303                         if (heightUnitsLC->itemText(i) == hspc)
304                                 heightUnitsLC->setCurrentIndex(i);
305                 }
306         }
307         // set no optional height when the value is the default "1\height"
308         // (special units like \height are handled as "in",
309         if (height_special == "totalheight" &&  params_.height == Length("1in"))
310                 heightCB->setCheckState(Qt::Unchecked);
311         else
312                 heightCB->setCheckState(Qt::Checked);
313
314         heightCB->setEnabled(ibox);
315 }
316
317
318 void GuiBox::applyView()
319 {
320         bool pagebreak = pagebreakCB->isEnabled() && pagebreakCB->isChecked();
321         if (pagebreak)
322                 params_.type = "Framed";
323         else
324                 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
325
326         params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
327         params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
328
329         params_.pos = "tcb"[valignCO->currentIndex()];
330         params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
331         params_.hor_pos = "lcrs"[halignCO->currentIndex()];
332
333         int i = 0;
334         bool spec = false;
335         QString special = widthUnitsLC->currentText();
336         QString value = widthED->text();
337         if (special == qt_("Height")) {
338                 i = 1;
339                 spec = true;
340         } else if (special == qt_("Depth")) {
341                 i = 2;
342                 spec = true;
343         } else if (special == qt_("Total Height")) {
344                 i = 3;
345                 spec = true;
346         } else if (special == qt_("Width")) {
347                 i = 4;
348                 spec = true;
349         }
350         // the user might insert a non-special value in the line edit
351         if (isValidLength(fromqstr(value))) {
352                 i = 0;
353                 spec = false;
354         }
355         params_.special = fromqstr(ids_spec_[i]);
356
357         string width;
358         if (spec) {
359                 width = fromqstr(value);
360                 // beware: bogosity! the unit is simply ignored in this case
361                 width += "in";
362         } else {
363                 width = widgetsToLength(widthED, widthUnitsLC);
364         }
365
366         params_.width = Length(width);
367
368         i = 0;
369         spec = false;
370         special = heightUnitsLC->currentText();
371         value = heightED->text();
372         if (special == qt_("Height")) {
373                 i = 1;
374                 spec = true;
375         } else if (special == qt_("Depth")) {
376                 i = 2;
377                 spec = true;
378         } else if (special == qt_("Total Height")) {
379                 i = 3;
380                 spec = true;
381         } else if (special == qt_("Width")) {
382                 i = 4;
383                 spec = true;
384         }
385         // the user might insert a non-special value in the line edit
386         if (isValidLength(fromqstr(value))) {
387                 i = 0;
388                 spec = false;
389         }
390         params_.height_special = fromqstr(ids_spec_[i]);
391
392         string height;
393         if (spec  && !isValidLength(fromqstr(heightED->text()))) {
394                 height = fromqstr(value);
395                 // beware: bogosity! the unit is simply ignored in this case
396                 height += "in";
397         } else
398                 height = widgetsToLength(heightED, heightUnitsLC);
399
400         // the height parameter is omitted in InsetBox.cpp when the value
401         // is "1in" and "Total Height" is used as unit.
402         // 1in + "Total Height" means "1\height" which is the LaTeX default when
403         // no height is given
404         if (heightCB->checkState() == Qt::Checked)
405                 params_.height = Length(height);
406         else {
407                 params_.height = Length("1in");
408                 params_.height_special = fromqstr(ids_spec_[3]);
409         }
410 }
411
412
413 void GuiBox::setSpecial(bool ibox)
414 {
415         // FIXME: Needed? Already done in the constructor
416         ids_spec_ = boxGuiSpecialLengthIds();
417         gui_names_spec_ = boxGuiSpecialLengthNames();
418
419         QString const current_text = widthUnitsLC->currentText();
420
421         // check if the widget contains the special units
422         int const count = widthUnitsLC->count();
423         bool has_special = false;
424         for (int i = 0; i != count; ++i)
425                 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
426                         has_special = true;
427         // insert 'em if needed...
428         if (!ibox && !has_special) {
429                 for (int i = 1; i < gui_names_spec_.size(); ++i)
430                         widthUnitsLC->addItem(gui_names_spec_[i]);
431         // ... or remove 'em if needed
432         } else if (ibox && has_special) {
433                 widthUnitsLC->clear();
434                 for (int i = 0; i != num_units; ++i)
435                         widthUnitsLC->addItem(qt_(unit_name_gui[i]));
436         }
437         // restore selected text, if possible
438         int const idx = widthUnitsLC->findText(current_text);
439         if (idx != -1)
440                 widthUnitsLC->setCurrentIndex(idx);
441 }
442
443
444 void GuiBox::setInnerType(bool frameless, int i)
445 {
446         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
447         // we have to remove "none" then and adjust the combo
448         if (frameless) {
449                 innerBoxCO->clear();
450                 innerBoxCO->addItem(qt_("Parbox"));
451                 innerBoxCO->addItem(qt_("Minipage"));
452                 if (i != 0)
453                         innerBoxCO->setCurrentIndex(i - 1);
454                 else
455                         innerBoxCO->setCurrentIndex(i);
456         } else {
457                 innerBoxCO->clear();
458                 innerBoxCO->addItem(qt_("None"));
459                 innerBoxCO->addItem(qt_("Parbox"));
460                 innerBoxCO->addItem(qt_("Minipage"));
461                 innerBoxCO->setCurrentIndex(i);
462         }
463 }
464
465 bool GuiBox::initialiseParams(string const & data)
466 {
467         InsetBox::string2params(data, params_);
468         return true;
469
470 }
471
472
473 void GuiBox::clearParams()
474 {
475         params_ = InsetBoxParams("");
476 }
477
478
479 void GuiBox::dispatchParams()
480 {
481         dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
482 }
483
484
485 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
486
487
488 } // namespace frontend
489 } // namespace lyx
490
491
492 #include "GuiBox_moc.cpp"