]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
GuiBox.cpp: fix this issue: horizontal box alignment is only possible without inner...
[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(QWidget * parent) : InsetParamsWidget(parent)
77 {
78         setupUi(this);
79
80         // fill the box type choice
81         ids_ = boxGuiIds();
82         gui_names_ = boxGuiNames();
83         for (int i = 0; i != ids_.size(); ++i)
84                 typeCO->addItem(gui_names_[i], ids_[i]);
85
86         // add the special units to the height choice
87         // width needs different handling
88         ids_spec_ = boxGuiSpecialLengthIds();
89         gui_names_spec_ = boxGuiSpecialLengthNames();
90         for (int i = 0; i != ids_spec_.size(); ++i)
91                 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
92
93         connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
94         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
95                 this, SIGNAL(changed()));
96         connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
97         connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
98         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
99                 this, SIGNAL(changed()));
100         connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
101         connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
102
103         heightED->setValidator(unsignedLengthValidator(heightED));
104         widthED->setValidator(unsignedLengthValidator(widthED));
105
106         // initialize the length validator
107         addCheckedWidget(widthED, widthLA);
108         addCheckedWidget(heightED, heightCB);
109
110         initDialog();
111 }
112
113
114 void GuiBox::on_innerBoxCO_activated(int index)
115 {
116         bool const ibox =
117                 (innerBoxCO->itemData(index).toString() != "none");
118         QString const outer =
119                 typeCO->itemData(typeCO->currentIndex()).toString();
120         valignCO->setEnabled(ibox);
121         ialignCO->setEnabled(ibox);
122         heightCB->setEnabled(ibox);
123         // except for frameless and boxed, the width cannot be specified if
124         // there is no inner box
125         bool const width_disabled = (!ibox && outer != "Frameless" &&
126                 outer != "Boxed");
127         widthED->setEnabled(!width_disabled);
128         widthUnitsLC->setEnabled(!width_disabled);
129         // halign and pagebreak are only allowed for Boxed without inner box
130         halignCO->setEnabled(!ibox && outer == "Boxed");
131         pagebreakCB->setEnabled(!ibox && outer == "Boxed");
132         setSpecial(ibox);
133         changed();
134 }
135
136
137 void GuiBox::on_typeCO_activated(int index)
138 {
139         QString const type =
140                 typeCO->itemData(index).toString();
141         bool const frameless = (type == "Frameless");
142         if (frameless) {
143                 valignCO->setEnabled(true);
144                 ialignCO->setEnabled(true);
145                 heightCB->setEnabled(true);
146                 setSpecial(true);
147         }
148         if (type != "Boxed")
149                 pagebreakCB->setChecked(false);
150         QString itype =
151                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
152         // except for frameless and boxed, the width cannot be specified if
153         // there is no inner box
154         bool const width_disabled = (itype == "none" && !frameless
155                 && type != "Boxed");
156         widthED->setEnabled(!width_disabled);
157         widthUnitsLC->setEnabled(!width_disabled);
158         // halign and pagebreak are only allowed for Boxed without inner box
159         halignCO->setEnabled(type == "Boxed" && itype == "none");
160         pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
161         setInnerType(frameless, itype);
162         changed();
163 }
164
165
166 void GuiBox::initDialog()
167 {
168         setInnerType(true, toqstr("minipage"));
169         widthED->setText("100");
170         widthUnitsLC->setCurrentItem(Length::PCW);
171         heightED->setText("1");
172         heightUnitsLC->setCurrentItem("totalheight");
173 }
174
175
176 void GuiBox::on_heightCB_stateChanged(int state)
177 {
178         bool const enable = (innerBoxCO->currentText() != qt_("None"))
179                 && (state == Qt::Checked);
180         heightED->setEnabled(enable);
181         heightUnitsLC->setEnabled(enable);
182         changed();
183 }
184
185
186 void GuiBox::on_pagebreakCB_stateChanged()
187 {
188         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
189         innerBoxCO->setEnabled(!pbreak);
190         widthED->setEnabled(!pbreak);
191         widthUnitsLC->setEnabled(!pbreak);
192         if (!pbreak) {
193                 on_typeCO_activated(typeCO->currentIndex());
194                 return;
195         }
196         valignCO->setEnabled(false);
197         ialignCO->setEnabled(false);
198         halignCO->setEnabled(false);
199         heightCB->setEnabled(false);
200         heightED->setEnabled(false);
201         heightUnitsLC->setEnabled(false);
202         setSpecial(false);
203         changed();
204 }
205
206
207 void GuiBox::paramsToDialog(Inset const * inset)
208 {
209         InsetBox const * box = static_cast<InsetBox const *>(inset);
210         InsetBoxParams const & params = box->params();
211         QString type = toqstr(params.type);
212         if (type == "Framed") {
213                 pagebreakCB->setChecked(true);
214                 type = "Boxed";
215         } else {
216                 pagebreakCB->setChecked(false);
217         }
218
219         typeCO->setCurrentIndex(typeCO->findData(type));
220
221         // default: minipage
222         QString inner_type = "minipage";
223         if (!params.inner_box)
224                 inner_type = "none";
225         if (params.use_parbox)
226                 inner_type = "parbox";
227         bool const frameless = (params.type == "Frameless");
228         setInnerType(frameless, inner_type);
229
230         char c = params.pos;
231         valignCO->setCurrentIndex(string("tcb").find(c, 0));
232         c = params.inner_pos;
233         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
234         c = params.hor_pos;
235         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
236
237         bool ibox = params.inner_box;
238         valignCO->setEnabled(ibox);
239         ialignCO->setEnabled(ibox);
240         setSpecial(ibox);
241
242         // halign and pagebreak are only allowed for Boxed without inner box
243         halignCO->setEnabled(!ibox && type == "Boxed");
244         pagebreakCB->setEnabled(!ibox && type == "Boxed");
245
246         // except for frameless and boxed, the width cannot be specified if
247         // there is no inner box
248         bool const width_disabled = (!ibox && !frameless
249                 && type != "Boxed");
250         widthED->setEnabled(!width_disabled);
251         widthUnitsLC->setEnabled(!width_disabled);
252
253         Length::UNIT const default_unit = Length::defaultUnit();
254
255         lengthToWidgets(widthED, widthUnitsLC,
256                 (params.width).asString(), default_unit);
257
258         QString const special = toqstr(params.special);
259         if (!special.isEmpty() && special != "none")
260                 widthUnitsLC->setCurrentItem(special);
261
262         lengthToWidgets(heightED, heightUnitsLC,
263                 (params.height).asString(), default_unit);
264         
265         QString const height_special = toqstr(params.height_special);
266         if (!height_special.isEmpty() && height_special != "none")
267                 heightUnitsLC->setCurrentItem(height_special);
268         // set no optional height if the value is the default "1\height"
269         // (special units like \height are handled as "in",
270         // FIXME: this is a very bad UI, this check box should be disabled in
271         // this case, not forced to 'unchecked' state.
272         if (height_special == "totalheight" && params.height == Length("1in"))
273                 heightCB->setCheckState(Qt::Unchecked);
274         else
275                 heightCB->setCheckState(Qt::Checked);
276
277         heightCB->setEnabled(ibox);
278 }
279
280
281 docstring GuiBox::dialogToParams() const
282 {
283         bool const pagebreak =
284                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
285         string box_type;
286         if (pagebreak)
287                 box_type = "Framed";
288         else
289                 box_type = fromqstr(typeCO->itemData(
290                                 typeCO->currentIndex()).toString());
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, QString const & type)
363 {
364         // with "frameless" boxes, inner box is mandatory
365         // (i.e. is the actual box)
366         // we have to remove "none" then and adjust the combo
367         innerBoxCO->clear();
368         if (!frameless)
369                 innerBoxCO->addItem(qt_("None"), toqstr("none"));
370         innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
371         innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
372         int i = (innerBoxCO->findData(type) != -1)
373                 ? innerBoxCO->findData(type) : 0;
374         innerBoxCO->setCurrentIndex(i);
375 }
376
377 } // namespace frontend
378 } // namespace lyx
379
380
381 #include "moc_GuiBox.cpp"