]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
* fix spelling in comments to please John.
[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 "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(GuiView & lv)
77         : GuiDialog(lv, "box", qt_("Box Settings")), params_("")
78 {
79         setupUi(this);
80
81         // fill the box type choice
82         ids_ = boxGuiIds();
83         gui_names_ = boxGuiNames();
84         foreach (QString const & str, gui_names_)
85                 typeCO->addItem(str);
86
87         // add the special units to the height choice
88         // width needs different handling
89         ids_spec_ = boxGuiSpecialLengthIds();
90         gui_names_spec_ = boxGuiSpecialLengthNames();
91         for (int i = 0; i != ids_spec_.size(); ++i)
92                 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
93
94         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
95         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
96         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
97         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
98
99         connect(widthED, SIGNAL(textChanged(QString)),
100                 this, SLOT(change_adaptor()));
101         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
102                 this, SLOT(change_adaptor()));
103         connect(valignCO, SIGNAL(highlighted(QString)),
104                 this, SLOT(change_adaptor()));
105         connect(heightCB, SIGNAL(stateChanged(int)),
106                 this, SLOT(change_adaptor()));
107         connect(heightED, SIGNAL(textChanged(QString)),
108                 this, SLOT(change_adaptor()));
109         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
110                 this, SLOT(change_adaptor()));
111         connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
112         connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
113         connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
114         connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
115         connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
116         connect(innerBoxCO, SIGNAL(activated(QString)),
117                 this, SLOT(innerBoxChanged(QString)));
118         connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
119         connect(pagebreakCB, SIGNAL(stateChanged(int)),
120                 this, SLOT(pagebreakClicked()));
121
122         heightED->setValidator(unsignedLengthValidator(heightED));
123         widthED->setValidator(unsignedLengthValidator(widthED));
124
125         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
126
127         bc().addReadOnly(typeCO);
128         bc().addReadOnly(innerBoxCO);
129         bc().addReadOnly(valignCO);
130         bc().addReadOnly(ialignCO);
131         bc().addReadOnly(halignCO);
132         bc().addReadOnly(widthED);
133         bc().addReadOnly(widthUnitsLC);
134         bc().addReadOnly(heightCB);
135         bc().addReadOnly(heightED);
136         bc().addReadOnly(heightUnitsLC);
137         bc().addReadOnly(pagebreakCB);
138
139         bc().setRestore(restorePB);
140         bc().setOK(okPB);
141         bc().setApply(applyPB);
142         bc().setCancel(closePB);
143
144         // initialize the length validator
145         bc().addCheckedLineEdit(widthED, widthLA);
146         bc().addCheckedLineEdit(heightED, heightCB);
147 }
148
149
150 void GuiBox::change_adaptor()
151 {
152         changed();
153 }
154
155
156 void GuiBox::innerBoxChanged(QString const & str)
157 {
158         bool const ibox = (str != qt_("None"));
159         valignCO->setEnabled(ibox);
160         ialignCO->setEnabled(ibox);
161         halignCO->setEnabled(!ibox);
162         heightCB->setEnabled(ibox);
163         pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
164         heightED->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
165         heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
166         setSpecial(ibox);
167 }
168
169
170 void GuiBox::typeChanged(int index)
171 {
172         bool const frameless = (index == 0);
173         if (frameless) {
174                 valignCO->setEnabled(true);
175                 ialignCO->setEnabled(true);
176                 halignCO->setEnabled(false);
177                 heightCB->setEnabled(true);
178                 heightED->setEnabled(heightCB->checkState() == Qt::Checked);
179                 heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked);
180                 setSpecial(true);
181         }
182         if (index != 1)
183                 pagebreakCB->setChecked(false);
184         int itype = innerBoxCO->currentIndex();
185         if (innerBoxCO->count() == 2)
186                 ++itype;
187         pagebreakCB->setEnabled(index == 1 && itype == 0);
188         widthED->setEnabled(index != 5);
189         widthUnitsLC->setEnabled(index != 5);
190         setInnerType(frameless, itype);
191 }
192
193
194 void GuiBox::restoreClicked()
195 {
196         setInnerType(true, 2);
197         widthED->setText("100");
198         widthUnitsLC->setCurrentItem(Length::PCW);
199         heightCB->setCheckState(Qt::Checked);
200         heightED->setText("1");
201         heightUnitsLC->setCurrentItem("totalheight");
202 }
203
204
205 void GuiBox::pagebreakClicked()
206 {
207         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
208         innerBoxCO->setEnabled(!pbreak);
209         widthED->setEnabled(!pbreak);
210         widthUnitsLC->setEnabled(!pbreak);
211         if (pbreak) {
212                 valignCO->setEnabled(false);
213                 ialignCO->setEnabled(false);
214                 halignCO->setEnabled(false);
215                 heightCB->setEnabled(false);
216                 heightED->setEnabled(false);
217                 heightUnitsLC->setEnabled(false);
218                 setSpecial(false);
219         } else {
220                 typeChanged(typeCO->currentIndex());
221         }
222         change_adaptor();
223 }
224
225
226 void GuiBox::updateContents()
227 {
228         QString type = toqstr(params_.type);
229         if (type == "Framed") {
230                 pagebreakCB->setChecked(true);
231                 type = "Boxed";
232         } else {
233                 pagebreakCB->setChecked(false);
234         }
235
236         pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
237
238         for (int i = 0; i != gui_names_.size(); ++i) {
239                 if (type == ids_[i])
240                         typeCO->setCurrentIndex(i);
241         }
242
243         // default: minipage
244         int inner_type = 2;
245         if (!params_.inner_box)
246                 // none
247                 inner_type = 0;
248         if (params_.use_parbox)
249                 // parbox
250                 inner_type = 1;
251         bool frameless = (params_.type == "Frameless");
252         setInnerType(frameless, inner_type);
253
254         char c = params_.pos;
255         valignCO->setCurrentIndex(string("tcb").find(c, 0));
256         c = params_.inner_pos;
257         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
258         c = params_.hor_pos;
259         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
260
261         bool ibox = params_.inner_box;
262         valignCO->setEnabled(ibox);
263         ialignCO->setEnabled(ibox);
264         halignCO->setEnabled(!ibox);
265         setSpecial(ibox);
266
267         Length::UNIT const default_unit = Length::defaultUnit();
268
269         lengthToWidgets(widthED, widthUnitsLC,
270                 (params_.width).asString(), default_unit);
271
272         QString const special = toqstr(params_.special);
273         if (!special.isEmpty() && special != "none")
274                 widthUnitsLC->setCurrentItem(special);
275
276         lengthToWidgets(heightED, heightUnitsLC,
277                 (params_.height).asString(), default_unit);
278         
279         QString const height_special = toqstr(params_.height_special);
280         if (!height_special.isEmpty() && height_special != "none")
281                 heightUnitsLC->setCurrentItem(height_special);
282         // set no optional height if the value is the default "1\height"
283         // (special units like \height are handled as "in",
284         if (height_special == "totalheight" && params_.height == Length("1in"))
285                 heightCB->setCheckState(Qt::Unchecked);
286         else
287                 heightCB->setCheckState(Qt::Checked);
288
289         heightCB->setEnabled(ibox);
290 }
291
292
293 void GuiBox::applyView()
294 {
295         bool pagebreak =
296                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
297         if (pagebreak)
298                 params_.type = "Framed";
299         else
300                 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
301
302         params_.inner_box =
303                 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
304         params_.use_parbox =
305                 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
306
307         params_.pos = "tcb"[valignCO->currentIndex()];
308         params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
309         params_.hor_pos = "lcrs"[halignCO->currentIndex()];
310
311         QString unit =
312                 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
313         QString value = widthED->text();
314         if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
315                 params_.special = fromqstr(unit);
316                 // Note: the unit is simply ignored in this case
317                 params_.width = Length(value.toDouble(), Length::IN);
318         } else {
319                 params_.special = "none";
320                 params_.width = Length(widgetsToLength(widthED, widthUnitsLC));
321         }
322
323         // the height parameter is omitted if the value
324         // is "1in" and "Total Height" is used as unit.
325         // 1in + "Total Height" means "1\height" which is the LaTeX default
326         // if no height is given
327         if (heightCB->checkState() == Qt::Unchecked) {
328                 params_.height = Length("1in");
329                 params_.height_special = "totalheight";
330         } else {
331                 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
332                 value = heightED->text();
333                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
334                         params_.height_special = fromqstr(unit);
335                         // Note: the unit is simply ignored in this case
336                         params_.height = Length(value.toDouble(), Length::IN);
337                 } else {
338                         params_.height_special = "none";
339                         params_.height =
340                                 Length(widgetsToLength(heightED, heightUnitsLC));
341                 }
342         }
343 }
344
345
346 void GuiBox::setSpecial(bool ibox)
347 {
348         QString const last_item =
349                 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
350
351         // check if the widget contains the special units
352         bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
353         // insert 'em if needed...
354         if (!ibox && !has_special) {
355                 for (int i = 1; i < ids_spec_.size(); ++i)
356                         widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
357         // ... or remove 'em if needed
358         } else if (ibox && has_special) {
359                 for (int i = 1; i < ids_spec_.size(); ++i) {
360                         int n = widthUnitsLC->findData(ids_spec_[i]);
361                         if (n != -1)
362                                 widthUnitsLC->removeItem(n);
363                 }
364         }
365         // restore selected text, if possible
366         widthUnitsLC->setCurrentItem(last_item);
367 }
368
369
370 void GuiBox::setInnerType(bool frameless, int i)
371 {
372         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
373         // we have to remove "none" then and adjust the combo
374         if (frameless) {
375                 innerBoxCO->clear();
376                 innerBoxCO->addItem(qt_("Parbox"));
377                 innerBoxCO->addItem(qt_("Minipage"));
378                 if (i != 0)
379                         innerBoxCO->setCurrentIndex(i - 1);
380                 else
381                         innerBoxCO->setCurrentIndex(i);
382         } else {
383                 innerBoxCO->clear();
384                 innerBoxCO->addItem(qt_("None"));
385                 innerBoxCO->addItem(qt_("Parbox"));
386                 innerBoxCO->addItem(qt_("Minipage"));
387                 innerBoxCO->setCurrentIndex(i);
388         }
389 }
390
391 bool GuiBox::initialiseParams(string const & data)
392 {
393         InsetBox::string2params(data, params_);
394         return true;
395 }
396
397
398 void GuiBox::clearParams()
399 {
400         params_ = InsetBoxParams("");
401 }
402
403
404 void GuiBox::dispatchParams()
405 {
406         dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
407 }
408
409
410 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
411
412
413 } // namespace frontend
414 } // namespace lyx
415
416
417 #include "moc_GuiBox.cpp"