]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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         pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
161         if (heightCB->checkState() == Qt::Checked && ibox) {
162                 heightED->setEnabled(true);
163                 heightUnitsLC->setEnabled(true);
164         }
165         setSpecial(ibox);
166 }
167
168
169 void GuiBox::typeChanged(int index)
170 {
171         bool const frameless = (index == 0);
172         if (frameless) {
173                 valignCO->setEnabled(true);
174                 ialignCO->setEnabled(true);
175                 halignCO->setEnabled(false);
176                 heightCB->setEnabled(true);
177                 heightED->setEnabled(true);
178                 heightUnitsLC->setEnabled(true);
179                 setSpecial(true);
180         }
181         if (index != 1)
182                 pagebreakCB->setChecked(false);
183         int itype = innerBoxCO->currentIndex();
184         pagebreakCB->setEnabled(index == 1 && itype == 0);
185         setInnerType(frameless, itype);
186 }
187
188
189 void GuiBox::restoreClicked()
190 {
191         setInnerType(true, 2);
192         widthED->setText("100");
193         widthUnitsLC->setCurrentItem(Length::PCW);
194         heightCB->setCheckState(Qt::Checked);
195         heightED->setText("1");
196         for (int i = 0; i != heightUnitsLC->count(); ++i) {
197                 if (heightUnitsLC->itemText(i) == qt_("Total Height"))
198                         heightUnitsLC->setCurrentItem(i);
199         }
200 }
201
202
203 void GuiBox::pagebreakClicked()
204 {
205         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
206         innerBoxCO->setEnabled(!pbreak);
207         if (pbreak) {
208                 valignCO->setEnabled(false);
209                 ialignCO->setEnabled(false);
210                 halignCO->setEnabled(false);
211                 heightCB->setEnabled(false);
212                 heightED->setEnabled(false);
213                 heightUnitsLC->setEnabled(false);
214                 setSpecial(false);
215         } else {
216                 typeChanged(typeCO->currentIndex());
217         }
218         change_adaptor();
219 }
220
221
222 void GuiBox::updateContents()
223 {
224         QString type = toqstr(params_.type);
225         if (type == "Framed") {
226                 pagebreakCB->setChecked(true);
227                 type = "Boxed";
228         } else {
229                 pagebreakCB->setChecked(false);
230         }
231
232         pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
233
234         for (int i = 0; i != gui_names_.size(); ++i) {
235                 if (type == ids_[i])
236                         typeCO->setCurrentIndex(i);
237         }
238
239         // default: minipage
240         int inner_type = 2;
241         if (!params_.inner_box)
242                 // none
243                 inner_type = 0;
244         if (params_.use_parbox)
245                 // parbox
246                 inner_type = 1;
247         bool frameless = (params_.type == "Frameless");
248         setInnerType(frameless, inner_type);
249
250         char c = params_.pos;
251         valignCO->setCurrentIndex(string("tcb").find(c, 0));
252         c = params_.inner_pos;
253         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
254         c = params_.hor_pos;
255         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
256
257         bool ibox = params_.inner_box;
258         valignCO->setEnabled(ibox);
259         ialignCO->setEnabled(ibox);
260         halignCO->setEnabled(!ibox);
261         setSpecial(ibox);
262
263         Length::UNIT default_unit =
264                 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
265
266         lengthToWidgets(widthED, widthUnitsLC,
267                 (params_.width).asString(), default_unit);
268
269         QString const special = toqstr(params_.special);
270         if (!special.isEmpty() && special != "none") {
271                 QString spc;
272                 for (int i = 0; i != gui_names_spec_.size(); ++i) {
273                         if (special == ids_spec_[i])
274                                 spc = gui_names_spec_[i];
275                 }
276                 for (int i = 0; i != widthUnitsLC->count(); ++i) {
277                         if (widthUnitsLC->itemText(i) == spc)
278                                 widthUnitsLC->setCurrentIndex(i);
279                 }
280         }
281
282         lengthToWidgets(heightED, heightUnitsLC,
283                 (params_.height).asString(), default_unit);
284         
285         QString const height_special = toqstr(params_.height_special);
286         if (!height_special.isEmpty() && height_special != "none") {
287                 QString hspc;
288                 for (int i = 0; i != gui_names_spec_.size(); ++i) {
289                         if (height_special == ids_spec_[i])
290                                 hspc = gui_names_spec_[i];
291                 }
292                 for (int i = 0; i != heightUnitsLC->count(); ++i) {
293                         if (heightUnitsLC->itemText(i) == hspc)
294                                 heightUnitsLC->setCurrentIndex(i);
295                 }
296         }
297         // set no optional height when the value is the default "1\height"
298         // (special units like \height are handled as "in",
299         if (height_special == "totalheight" &&  params_.height == Length("1in"))
300                 heightCB->setCheckState(Qt::Unchecked);
301         else
302                 heightCB->setCheckState(Qt::Checked);
303
304         heightCB->setEnabled(ibox);
305 }
306
307
308 void GuiBox::applyView()
309 {
310         bool pagebreak = pagebreakCB->isEnabled() && pagebreakCB->isChecked();
311         if (pagebreak)
312                 params_.type = "Framed";
313         else
314                 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
315
316         params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
317         params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
318
319         params_.pos = "tcb"[valignCO->currentIndex()];
320         params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
321         params_.hor_pos = "lcrs"[halignCO->currentIndex()];
322
323         int i = 0;
324         bool spec = false;
325         QString special = widthUnitsLC->currentText();
326         QString value = widthED->text();
327         if (special == qt_("Height")) {
328                 i = 1;
329                 spec = true;
330         } else if (special == qt_("Depth")) {
331                 i = 2;
332                 spec = true;
333         } else if (special == qt_("Total Height")) {
334                 i = 3;
335                 spec = true;
336         } else if (special == qt_("Width")) {
337                 i = 4;
338                 spec = true;
339         }
340         // the user might insert a non-special value in the line edit
341         if (isValidLength(fromqstr(value))) {
342                 i = 0;
343                 spec = false;
344         }
345         params_.special = fromqstr(ids_spec_[i]);
346
347         string width;
348         if (spec) {
349                 width = fromqstr(value);
350                 // beware: bogosity! the unit is simply ignored in this case
351                 width += "in";
352         } else {
353                 width = widgetsToLength(widthED, widthUnitsLC);
354         }
355
356         params_.width = Length(width);
357
358         i = 0;
359         spec = false;
360         special = heightUnitsLC->currentText();
361         value = heightED->text();
362         if (special == qt_("Height")) {
363                 i = 1;
364                 spec = true;
365         } else if (special == qt_("Depth")) {
366                 i = 2;
367                 spec = true;
368         } else if (special == qt_("Total Height")) {
369                 i = 3;
370                 spec = true;
371         } else if (special == qt_("Width")) {
372                 i = 4;
373                 spec = true;
374         }
375         // the user might insert a non-special value in the line edit
376         if (isValidLength(fromqstr(value))) {
377                 i = 0;
378                 spec = false;
379         }
380         params_.height_special = fromqstr(ids_spec_[i]);
381
382         string height;
383         if (spec  && !isValidLength(fromqstr(heightED->text()))) {
384                 height = fromqstr(value);
385                 // beware: bogosity! the unit is simply ignored in this case
386                 height += "in";
387         } else
388                 height = widgetsToLength(heightED, heightUnitsLC);
389
390         // the height parameter is omitted in InsetBox.cpp when the value
391         // is "1in" and "Total Height" is used as unit.
392         // 1in + "Total Height" means "1\height" which is the LaTeX default when
393         // no height is given
394         if (heightCB->checkState() == Qt::Checked)
395                 params_.height = Length(height);
396         else {
397                 params_.height = Length("1in");
398                 params_.height_special = fromqstr(ids_spec_[3]);
399         }
400 }
401
402
403 void GuiBox::setSpecial(bool ibox)
404 {
405         // FIXME: Needed? Already done in the constructor
406         ids_spec_ = boxGuiSpecialLengthIds();
407         gui_names_spec_ = boxGuiSpecialLengthNames();
408
409         // check if the widget contains the special units
410         int count = widthUnitsLC->count();
411         bool has_special = false;
412         for (int i = 0; i != count; ++i)
413                 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
414                         has_special = true;
415         // insert 'em if needed...
416         if (!ibox && !has_special) {
417                 for (int i = 1; i < gui_names_spec_.size(); ++i)
418                         widthUnitsLC->addItem(gui_names_spec_[i]);
419         // ... or remove 'em if needed
420         } else if (ibox && has_special) {
421                 widthUnitsLC->clear();
422                 for (int i = 0; i != num_units; ++i)
423                         widthUnitsLC->addItem(qt_(unit_name_gui[i]));
424         }
425 }
426
427
428 void GuiBox::setInnerType(bool frameless, int i)
429 {
430         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
431         // we have to remove "none" then and adjust the combo
432         if (frameless) {
433                 innerBoxCO->clear();
434                 innerBoxCO->addItem(qt_("Parbox"));
435                 innerBoxCO->addItem(qt_("Minipage"));
436                 if (i != 0)
437                         innerBoxCO->setCurrentIndex(i - 1);
438                 else
439                         innerBoxCO->setCurrentIndex(i);
440         } else {
441                 if (innerBoxCO->count() == 2)
442                         ++i;
443                 innerBoxCO->clear();
444                 innerBoxCO->addItem(qt_("None"));
445                 innerBoxCO->addItem(qt_("Parbox"));
446                 innerBoxCO->addItem(qt_("Minipage"));
447                 innerBoxCO->setCurrentIndex(i);
448         }
449 }
450
451 bool GuiBox::initialiseParams(string const & data)
452 {
453         InsetBox::string2params(data, params_);
454         return true;
455
456 }
457
458
459 void GuiBox::clearParams()
460 {
461         params_ = InsetBoxParams("");
462 }
463
464
465 void GuiBox::dispatchParams()
466 {
467         dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
468 }
469
470
471 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
472
473
474 } // namespace frontend
475 } // namespace lyx
476
477
478 #include "GuiBox_moc.cpp"