]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
* LengthCombo.{cpp,h}:
[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         for (int i = 0; i != ids_spec_.size(); ++i)
93                 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
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         heightED->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
166         heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked && ibox);
167         setSpecial(ibox);
168 }
169
170
171 void GuiBox::typeChanged(int index)
172 {
173         bool const frameless = (index == 0);
174         if (frameless) {
175                 valignCO->setEnabled(true);
176                 ialignCO->setEnabled(true);
177                 halignCO->setEnabled(false);
178                 heightCB->setEnabled(true);
179                 heightED->setEnabled(heightCB->checkState() == Qt::Checked);
180                 heightUnitsLC->setEnabled(heightCB->checkState() == Qt::Checked);
181                 setSpecial(true);
182         }
183         if (index != 1)
184                 pagebreakCB->setChecked(false);
185         int itype = innerBoxCO->currentIndex();
186         if (innerBoxCO->count() == 2)
187                 ++itype;
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         heightUnitsLC->setCurrentItem("totalheight");
203 }
204
205
206 void GuiBox::pagebreakClicked()
207 {
208         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
209         innerBoxCO->setEnabled(!pbreak);
210         widthED->setEnabled(!pbreak);
211         widthUnitsLC->setEnabled(!pbreak);
212         if (pbreak) {
213                 valignCO->setEnabled(false);
214                 ialignCO->setEnabled(false);
215                 halignCO->setEnabled(false);
216                 heightCB->setEnabled(false);
217                 heightED->setEnabled(false);
218                 heightUnitsLC->setEnabled(false);
219                 setSpecial(false);
220         } else {
221                 typeChanged(typeCO->currentIndex());
222         }
223         change_adaptor();
224 }
225
226
227 void GuiBox::updateContents()
228 {
229         QString type = toqstr(params_.type);
230         if (type == "Framed") {
231                 pagebreakCB->setChecked(true);
232                 type = "Boxed";
233         } else {
234                 pagebreakCB->setChecked(false);
235         }
236
237         pagebreakCB->setEnabled(type == "Boxed" && !params_.inner_box);
238
239         for (int i = 0; i != gui_names_.size(); ++i) {
240                 if (type == ids_[i])
241                         typeCO->setCurrentIndex(i);
242         }
243
244         // default: minipage
245         int inner_type = 2;
246         if (!params_.inner_box)
247                 // none
248                 inner_type = 0;
249         if (params_.use_parbox)
250                 // parbox
251                 inner_type = 1;
252         bool frameless = (params_.type == "Frameless");
253         setInnerType(frameless, inner_type);
254
255         char c = params_.pos;
256         valignCO->setCurrentIndex(string("tcb").find(c, 0));
257         c = params_.inner_pos;
258         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
259         c = params_.hor_pos;
260         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
261
262         bool ibox = params_.inner_box;
263         valignCO->setEnabled(ibox);
264         ialignCO->setEnabled(ibox);
265         halignCO->setEnabled(!ibox);
266         setSpecial(ibox);
267
268         Length::UNIT default_unit =
269                 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
270
271         lengthToWidgets(widthED, widthUnitsLC,
272                 (params_.width).asString(), default_unit);
273
274         QString const special = toqstr(params_.special);
275         if (!special.isEmpty() && special != "none")
276                 widthUnitsLC->setCurrentItem(special);
277
278         lengthToWidgets(heightED, heightUnitsLC,
279                 (params_.height).asString(), default_unit);
280         
281         QString const height_special = toqstr(params_.height_special);
282         if (!height_special.isEmpty() && height_special != "none")
283                 heightUnitsLC->setCurrentItem(height_special);
284         // set no optional height if the value is the default "1\height"
285         // (special units like \height are handled as "in",
286         if (height_special == "totalheight" && params_.height == Length("1in"))
287                 heightCB->setCheckState(Qt::Unchecked);
288         else
289                 heightCB->setCheckState(Qt::Checked);
290
291         heightCB->setEnabled(ibox);
292 }
293
294
295 void GuiBox::applyView()
296 {
297         bool pagebreak =
298                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
299         if (pagebreak)
300                 params_.type = "Framed";
301         else
302                 params_.type = fromqstr(ids_[typeCO->currentIndex()]);
303
304         params_.inner_box =
305                 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
306         params_.use_parbox =
307                 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
308
309         params_.pos = "tcb"[valignCO->currentIndex()];
310         params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
311         params_.hor_pos = "lcrs"[halignCO->currentIndex()];
312
313         QString unit =
314                 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
315         QString value = widthED->text();
316         if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
317                 params_.special = fromqstr(unit);
318                 // Note: the unit is simply ignored in this case
319                 params_.width = Length(value.toDouble(), Length::IN);
320         } else {
321                 params_.special = "none";
322                 params_.width = Length(widgetsToLength(widthED, widthUnitsLC));
323         }
324
325         // the height parameter is omitted in if the value
326         // is "1in" and "Total Height" is used as unit.
327         // 1in + "Total Height" means "1\height" which is the LaTeX default
328         // if no height is given
329         if (heightCB->checkState() == Qt::Unchecked) {
330                 params_.height = Length("1in");
331                 params_.height_special = "totalheight";
332         } else {
333                 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
334                 value = heightED->text();
335                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
336                         params_.height_special = fromqstr(unit);
337                         // Note: the unit is simply ignored in this case
338                         params_.height = Length(value.toDouble(), Length::IN);
339                 } else {
340                         params_.height_special = "none";
341                         params_.height =
342                                 Length(widgetsToLength(heightED, heightUnitsLC));
343                 }
344         }
345 }
346
347
348 void GuiBox::setSpecial(bool ibox)
349 {
350         QString const last_item =
351                 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
352
353         // check if the widget contains the special units
354         bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
355         // insert 'em if needed...
356         if (!ibox && !has_special) {
357                 for (int i = 1; i < ids_spec_.size(); ++i)
358                         widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
359         // ... or remove 'em if needed
360         } else if (ibox && has_special) {
361                 for (int i = 1; i < ids_spec_.size(); ++i) {
362                         int n = widthUnitsLC->findData(ids_spec_[i]);
363                         if (n != -1)
364                                 widthUnitsLC->removeItem(n);
365                 }
366         }
367         // restore selected text, if possible
368         widthUnitsLC->setCurrentItem(last_item);
369 }
370
371
372 void GuiBox::setInnerType(bool frameless, int i)
373 {
374         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
375         // we have to remove "none" then and adjust the combo
376         if (frameless) {
377                 innerBoxCO->clear();
378                 innerBoxCO->addItem(qt_("Parbox"));
379                 innerBoxCO->addItem(qt_("Minipage"));
380                 if (i != 0)
381                         innerBoxCO->setCurrentIndex(i - 1);
382                 else
383                         innerBoxCO->setCurrentIndex(i);
384         } else {
385                 innerBoxCO->clear();
386                 innerBoxCO->addItem(qt_("None"));
387                 innerBoxCO->addItem(qt_("Parbox"));
388                 innerBoxCO->addItem(qt_("Minipage"));
389                 innerBoxCO->setCurrentIndex(i);
390         }
391 }
392
393 bool GuiBox::initialiseParams(string const & data)
394 {
395         InsetBox::string2params(data, params_);
396         return true;
397 }
398
399
400 void GuiBox::clearParams()
401 {
402         params_ = InsetBoxParams("");
403 }
404
405
406 void GuiBox::dispatchParams()
407 {
408         dispatch(FuncRequest(getLfun(), InsetBox::params2string(params_)));
409 }
410
411
412 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
413
414
415 } // namespace frontend
416 } // namespace lyx
417
418
419 #include "moc_GuiBox.cpp"