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