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