]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
Box dialog: migrate to the new InsetDialog architecture. This simplifies the code...
[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         : InsetDialog(lv, BOX_CODE, LFUN_BOX_INSERT, "box", "Box Settings")
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(widthED, SIGNAL(textChanged(QString)), this, SLOT(applyView()));
95         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
96                 this, SLOT(applyView()));
97         connect(valignCO, SIGNAL(highlighted(QString)), this, SLOT(applyView()));
98         connect(heightCB, SIGNAL(stateChanged(int)), this, SLOT(applyView()));
99         connect(heightED, SIGNAL(textChanged(QString)), this, SLOT(applyView()));
100         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
101                 this, SLOT(applyView()));
102         connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(applyView()));
103         connect(typeCO, SIGNAL(activated(int)), this, SLOT(applyView()));
104         connect(halignCO, SIGNAL(activated(int)), this, SLOT(applyView()));
105         connect(ialignCO, SIGNAL(activated(int)), this, SLOT(applyView()));
106
107         heightED->setValidator(unsignedLengthValidator(heightED));
108         widthED->setValidator(unsignedLengthValidator(widthED));
109
110         // initialize the length validator
111         addCheckedWidget(widthED, widthLA);
112         addCheckedWidget(heightED, heightCB);
113
114         initDialog();
115 }
116
117
118 void GuiBox::enableView(bool enable)
119 {
120         typeCO->setEnabled(enable);
121         innerBoxCO->setEnabled(enable);
122         valignCO->setEnabled(enable);
123         ialignCO->setEnabled(enable);
124         halignCO->setEnabled(enable);
125         widthED->setEnabled(enable);
126         widthUnitsLC->setEnabled(enable);
127         heightCB->setEnabled(enable);
128         heightED->setEnabled(enable);
129         heightUnitsLC->setEnabled(enable);
130         pagebreakCB->setEnabled(enable);
131 }
132
133
134 void GuiBox::on_innerBoxCO_activated(QString const & str)
135 {
136         bool const ibox = (str != qt_("None"));
137         valignCO->setEnabled(ibox);
138         ialignCO->setEnabled(ibox);
139         halignCO->setEnabled(!ibox);
140         heightCB->setEnabled(ibox);
141         pagebreakCB->setEnabled(!ibox && typeCO->currentIndex() == 1);
142         setSpecial(ibox);
143         applyView();
144 }
145
146
147 void GuiBox::on_typeCO_activated(int index)
148 {
149         bool const frameless = (index == 0);
150         if (frameless) {
151                 valignCO->setEnabled(true);
152                 ialignCO->setEnabled(true);
153                 halignCO->setEnabled(false);
154                 heightCB->setEnabled(true);
155                 setSpecial(true);
156         }
157         if (index != 1)
158                 pagebreakCB->setChecked(false);
159         int itype = innerBoxCO->currentIndex();
160         if (innerBoxCO->count() == 2)
161                 ++itype;
162         pagebreakCB->setEnabled(index == 1 && itype == 0);
163         widthED->setEnabled(index != 5);
164         widthUnitsLC->setEnabled(index != 5);
165         setInnerType(frameless, itype);
166         applyView();
167 }
168
169
170 void GuiBox::initDialog()
171 {
172         setInnerType(true, 2);
173         widthED->setText("100");
174         widthUnitsLC->setCurrentItem(Length::PCW);
175         heightCB->setCheckState(Qt::Checked);
176         heightED->setText("1");
177         heightUnitsLC->setCurrentItem("totalheight");
178 }
179
180
181 void GuiBox::on_heightCB_stateChanged(int state)
182 {
183         bool const enable = (innerBoxCO->currentText() != qt_("None"))
184                 && (state == Qt::Checked);
185         heightED->setEnabled(enable);
186         heightUnitsLC->setEnabled(enable);
187 }
188
189
190 void GuiBox::on_pagebreakCB_stateChanged()
191 {
192         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
193         innerBoxCO->setEnabled(!pbreak);
194         widthED->setEnabled(!pbreak);
195         widthUnitsLC->setEnabled(!pbreak);
196         if (pbreak) {
197                 valignCO->setEnabled(false);
198                 ialignCO->setEnabled(false);
199                 halignCO->setEnabled(false);
200                 heightCB->setEnabled(false);
201                 heightED->setEnabled(false);
202                 heightUnitsLC->setEnabled(false);
203                 setSpecial(false);
204         } else {
205                 on_typeCO_activated(typeCO->currentIndex());
206         }
207         applyView();
208 }
209
210
211 void GuiBox::paramsToDialog(Inset const * inset)
212 {
213         InsetBox const * box = static_cast<InsetBox const *>(inset);
214         InsetBoxParams const & params = box->params();
215         QString type = toqstr(params.type);
216         if (type == "Framed") {
217                 pagebreakCB->setChecked(true);
218                 type = "Boxed";
219         } else {
220                 pagebreakCB->setChecked(false);
221         }
222
223         pagebreakCB->setEnabled(type == "Boxed" && !params.inner_box);
224
225         for (int i = 0; i != gui_names_.size(); ++i) {
226                 if (type == ids_[i])
227                         typeCO->setCurrentIndex(i);
228         }
229
230         // default: minipage
231         int inner_type = 2;
232         if (!params.inner_box)
233                 // none
234                 inner_type = 0;
235         if (params.use_parbox)
236                 // parbox
237                 inner_type = 1;
238         bool frameless = (params.type == "Frameless");
239         setInnerType(frameless, inner_type);
240
241         char c = params.pos;
242         valignCO->setCurrentIndex(string("tcb").find(c, 0));
243         c = params.inner_pos;
244         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
245         c = params.hor_pos;
246         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
247
248         bool ibox = params.inner_box;
249         valignCO->setEnabled(ibox);
250         ialignCO->setEnabled(ibox);
251         halignCO->setEnabled(!ibox);
252         setSpecial(ibox);
253
254         Length::UNIT const default_unit = Length::defaultUnit();
255
256         lengthToWidgets(widthED, widthUnitsLC,
257                 (params.width).asString(), default_unit);
258
259         QString const special = toqstr(params.special);
260         if (!special.isEmpty() && special != "none")
261                 widthUnitsLC->setCurrentItem(special);
262
263         lengthToWidgets(heightED, heightUnitsLC,
264                 (params.height).asString(), default_unit);
265         
266         QString const height_special = toqstr(params.height_special);
267         if (!height_special.isEmpty() && height_special != "none")
268                 heightUnitsLC->setCurrentItem(height_special);
269         // set no optional height if the value is the default "1\height"
270         // (special units like \height are handled as "in",
271         // FIXME: this is a very bad UI, this check box should be disabled in
272         // this case, not forced to 'unchecked' state.
273         if (height_special == "totalheight" && params.height == Length("1in"))
274                 heightCB->setCheckState(Qt::Unchecked);
275         else
276                 heightCB->setCheckState(Qt::Checked);
277
278         heightCB->setEnabled(ibox);
279 }
280
281
282 docstring GuiBox::dialogToParams() const
283 {
284         bool const pagebreak =
285                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
286         string box_type;
287         if (pagebreak)
288                 box_type = "Framed";
289         else
290                 box_type = fromqstr(ids_[typeCO->currentIndex()]);
291
292         InsetBoxParams params(box_type);
293         params.inner_box =
294                 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
295         params.use_parbox =
296                 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
297
298         params.pos = "tcb"[valignCO->currentIndex()];
299         params.inner_pos = "tcbs"[ialignCO->currentIndex()];
300         params.hor_pos = "lcrs"[halignCO->currentIndex()];
301
302         QString unit =
303                 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
304         QString value = widthED->text();
305         if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
306                 params.special = fromqstr(unit);
307                 // Note: the unit is simply ignored in this case
308                 params.width = Length(value.toDouble(), Length::IN);
309         } else {
310                 params.special = "none";
311                 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
312         }
313
314         // the height parameter is omitted if the value
315         // is "1in" and "Total Height" is used as unit.
316         // 1in + "Total Height" means "1\height" which is the LaTeX default
317         // if no height is given
318         if (heightCB->checkState() == Qt::Unchecked) {
319                 params.height = Length("1in");
320                 params.height_special = "totalheight";
321         } else {
322                 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
323                 value = heightED->text();
324                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
325                         params.height_special = fromqstr(unit);
326                         // Note: the unit is simply ignored in this case
327                         params.height = Length(value.toDouble(), Length::IN);
328                 } else {
329                         params.height_special = "none";
330                         params.height =
331                                 Length(widgetsToLength(heightED, heightUnitsLC));
332                 }
333         }
334         return from_ascii(InsetBox::params2string(params));
335 }
336
337
338 void GuiBox::setSpecial(bool ibox)
339 {
340         QString const last_item =
341                 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
342
343         // check if the widget contains the special units
344         bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
345         // insert 'em if needed...
346         if (!ibox && !has_special) {
347                 for (int i = 1; i < ids_spec_.size(); ++i)
348                         widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
349         // ... or remove 'em if needed
350         } else if (ibox && has_special) {
351                 for (int i = 1; i < ids_spec_.size(); ++i) {
352                         int n = widthUnitsLC->findData(ids_spec_[i]);
353                         if (n != -1)
354                                 widthUnitsLC->removeItem(n);
355                 }
356         }
357         // restore selected text, if possible
358         widthUnitsLC->setCurrentItem(last_item);
359 }
360
361
362 void GuiBox::setInnerType(bool frameless, int i)
363 {
364         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
365         // we have to remove "none" then and adjust the combo
366         if (frameless) {
367                 innerBoxCO->clear();
368                 innerBoxCO->addItem(qt_("Parbox"));
369                 innerBoxCO->addItem(qt_("Minipage"));
370                 if (i != 0)
371                         innerBoxCO->setCurrentIndex(i - 1);
372                 else
373                         innerBoxCO->setCurrentIndex(i);
374         } else {
375                 innerBoxCO->clear();
376                 innerBoxCO->addItem(qt_("None"));
377                 innerBoxCO->addItem(qt_("Parbox"));
378                 innerBoxCO->addItem(qt_("Minipage"));
379                 innerBoxCO->setCurrentIndex(i);
380         }
381 }
382
383
384 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
385
386
387 } // namespace frontend
388 } // namespace lyx
389
390
391 #include "moc_GuiBox.cpp"