]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
Fix the tab ordering of PanelStack and PrefsUi.
[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 "LengthCombo.h"
18 #include "Length.h"
19 #include "qt_helpers.h"
20 #include "Validator.h"
21
22 #include "insets/InsetBox.h"
23
24 #include "support/gettext.h"
25 #include "support/foreach.h"
26 #include "support/lstrings.h"
27
28 #include <QPushButton>
29 #include <QLineEdit>
30
31 #ifdef IN
32 #undef IN
33 #endif
34
35 using namespace std;
36
37
38 namespace lyx {
39 namespace frontend {
40
41 static QStringList boxGuiIds()
42 {
43         return QStringList()
44                 << "Frameless" << "Boxed"
45                 << "ovalbox" << "Ovalbox"
46                 << "Shadowbox" << "Shaded"
47                 << "Doublebox";
48 }
49
50
51 static QStringList boxGuiNames()
52 {
53         return QStringList()
54                 << qt_("No frame") << qt_("Simple rectangular frame")
55                 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
56                 << qt_("Drop shadow") << qt_("Shaded background")
57                 << qt_("Double rectangular frame");
58 }
59
60
61 static QStringList boxGuiSpecialLengthIds()
62 {
63         return QStringList() << "height" << "depth"
64                 << "totalheight" << "width";
65 }
66
67
68 static QStringList boxGuiSpecialLengthNames()
69 {
70         return QStringList() << qt_("Height") << qt_("Depth")
71                 << qt_("Total Height") << qt_("Width");
72 }
73
74
75 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
76 {
77         setupUi(this);
78
79         // fill the box type choice
80         ids_ = boxGuiIds();
81         gui_names_ = boxGuiNames();
82         for (int i = 0; i != ids_.size(); ++i)
83                 typeCO->addItem(gui_names_[i], ids_[i]);
84
85         // add the special units to the height choice
86         // width needs different handling
87         ids_spec_ = boxGuiSpecialLengthIds();
88         gui_names_spec_ = boxGuiSpecialLengthNames();
89         for (int i = 0; i != ids_spec_.size(); ++i)
90                 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
91
92         connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
93         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
94                 this, SIGNAL(changed()));
95         connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
96         connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
97         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
98                 this, SIGNAL(changed()));
99         connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
100         connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
101
102         heightED->setValidator(unsignedLengthValidator(heightED));
103         widthED->setValidator(unsignedLengthValidator(widthED));
104
105         // initialize the length validator
106         addCheckedWidget(widthED, widthLA);
107         addCheckedWidget(heightED, heightCB);
108
109         initDialog();
110 }
111
112
113 void GuiBox::on_innerBoxCO_activated(int /* index */)
114 {
115         QString itype =
116                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
117         // handle parbox and minipage the same way
118         bool const ibox =
119                 (itype != "none"
120                  && itype != "makebox");
121         QString const outer =
122                 typeCO->itemData(typeCO->currentIndex()).toString();
123         valignCO->setEnabled(ibox);
124         ialignCO->setEnabled(ibox);
125         if (heightCB->isChecked() && !ibox)
126                 heightCB->setChecked(false);
127         heightCB->setEnabled(ibox);
128         // except for frameless and boxed, the width cannot be specified if
129         // there is no inner box
130         bool const width_enabled =
131                 ibox || outer == "Frameless" || outer == "Boxed";
132         widthED->setEnabled(width_enabled);
133         widthUnitsLC->setEnabled(width_enabled);
134         // halign is only allowed for Boxed without inner box or for makebox
135         halignCO->setEnabled((!ibox && outer == "Boxed")
136                 || (itype == "makebox"));
137         // pagebreak is only allowed for Boxed without inner box
138         pagebreakCB->setEnabled(!ibox && outer == "Boxed");
139         setSpecial(ibox);
140         changed();
141 }
142
143
144 void GuiBox::on_typeCO_activated(int index)
145 {
146         QString const type =
147                 typeCO->itemData(index).toString();
148         bool const frameless = (type == "Frameless");
149         QString itype =
150                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
151         setInnerType(frameless, itype);
152         // refresh itype because it might have been changed in setInnerType
153         itype =
154                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
155         // handle parbox and minipage the same way
156         bool const ibox =
157                 (itype != "none"
158                  && itype != "makebox");
159         if (frameless && itype != "makebox") {
160                 valignCO->setEnabled(ibox);
161                 ialignCO->setEnabled(ibox);
162                 if (heightCB->isChecked() && !ibox)
163                         heightCB->setChecked(false);
164                 heightCB->setEnabled(ibox);
165                 setSpecial(ibox);
166         }
167         // except for frameless and boxed, the width cannot be specified if
168         // there is no inner box
169         bool const width_enabled = 
170                 itype != "none" || frameless || type == "Boxed";
171         widthED->setEnabled(width_enabled);
172         widthUnitsLC->setEnabled(width_enabled);
173         // halign is only allowed for Boxed without inner box or for makebox
174         halignCO->setEnabled((type == "Boxed" && itype == "none") || (itype == "makebox"));
175         // pagebreak is only allowed for Boxed without inner box
176         pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
177         changed();
178 }
179
180
181 void GuiBox::initDialog()
182 {
183         setInnerType(true, toqstr("minipage"));
184         widthED->setText("100");
185         widthUnitsLC->setCurrentItem(Length::PCW);
186         heightED->setText("1");
187         heightUnitsLC->setCurrentItem("totalheight");
188 }
189
190
191 void GuiBox::on_heightCB_stateChanged(int state)
192 {
193         bool const enable = (innerBoxCO->currentText() != qt_("None"))
194                 && (state == Qt::Checked);
195         heightED->setEnabled(enable);
196         heightUnitsLC->setEnabled(enable);
197         changed();
198 }
199
200
201 void GuiBox::on_pagebreakCB_stateChanged()
202 {
203         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
204         innerBoxCO->setEnabled(!pbreak);
205         widthED->setEnabled(!pbreak);
206         widthUnitsLC->setEnabled(!pbreak);
207         if (!pbreak) {
208                 on_typeCO_activated(typeCO->currentIndex());
209                 return;
210         }
211         valignCO->setEnabled(false);
212         ialignCO->setEnabled(false);
213         halignCO->setEnabled(false);
214         heightCB->setEnabled(false);
215         heightED->setEnabled(false);
216         heightUnitsLC->setEnabled(false);
217         setSpecial(false);
218         changed();
219 }
220
221
222 void GuiBox::paramsToDialog(Inset const * inset)
223 {
224         InsetBox const * box = static_cast<InsetBox const *>(inset);
225         InsetBoxParams const & params = box->params();
226         QString type = toqstr(params.type);
227         if (type == "Framed") {
228                 pagebreakCB->setChecked(true);
229                 type = "Boxed";
230         } else {
231                 pagebreakCB->setChecked(false);
232         }
233
234         typeCO->setCurrentIndex(typeCO->findData(type));
235
236         // default: minipage
237         QString inner_type = "minipage";
238         if (!params.inner_box)
239                 inner_type = "none";
240         if (params.use_parbox)
241                 inner_type = "parbox";
242         if (params.use_makebox)
243                 inner_type = "makebox";
244         bool const frameless = (params.type == "Frameless");
245         setInnerType(frameless, inner_type);
246
247         char c = params.pos;
248         valignCO->setCurrentIndex(string("tcb").find(c, 0));
249         c = params.inner_pos;
250         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
251         c = params.hor_pos;
252         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
253
254         bool ibox = (params.inner_box && !params.use_makebox);
255         valignCO->setEnabled(ibox);
256         ialignCO->setEnabled(ibox);
257         setSpecial(ibox);
258
259         // halign and pagebreak are only allowed for Boxed without inner box
260         halignCO->setEnabled((!ibox && type == "Boxed") || (params.use_makebox));
261         // pagebreak is only allowed for Boxed without inner box
262         pagebreakCB->setEnabled(!ibox && type == "Boxed");
263
264         // except for frameless and boxed, the width cannot be specified if
265         // there is no inner box
266         bool const width_enabled = (ibox || frameless || type == "Boxed");
267         widthED->setEnabled(width_enabled);
268         widthUnitsLC->setEnabled(width_enabled);
269
270         Length::UNIT const default_unit = Length::defaultUnit();
271
272         lengthToWidgets(widthED, widthUnitsLC,
273                 (params.width).asString(), default_unit);
274
275         QString const special = toqstr(params.special);
276         if (!special.isEmpty() && special != "none")
277                 widthUnitsLC->setCurrentItem(special);
278
279         lengthToWidgets(heightED, heightUnitsLC,
280                 (params.height).asString(), default_unit);
281         
282         QString const height_special = toqstr(params.height_special);
283         if (!height_special.isEmpty() && height_special != "none")
284                 heightUnitsLC->setCurrentItem(height_special);
285         // set no optional height if the value is the default "1\height"
286         // (special units like \height are handled as "in",
287         // FIXME: this is a very bad UI, this check box should be disabled in
288         // this case, not forced to 'unchecked' state.
289         if (height_special == "totalheight" && params.height == Length("1in"))
290                 heightCB->setCheckState(Qt::Unchecked);
291         else
292                 heightCB->setCheckState(Qt::Checked);
293
294         heightCB->setEnabled(ibox);
295 }
296
297
298 docstring GuiBox::dialogToParams() const
299 {
300         bool const pagebreak =
301                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
302         string box_type;
303         if (pagebreak)
304                 box_type = "Framed";
305         else
306                 box_type = fromqstr(typeCO->itemData(
307                                 typeCO->currentIndex()).toString());
308
309         InsetBoxParams params(box_type);
310         params.inner_box =
311                 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
312         params.use_parbox =
313                 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
314         params.use_makebox =
315                 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
316
317         params.pos = "tcb"[valignCO->currentIndex()];
318         params.inner_pos = "tcbs"[ialignCO->currentIndex()];
319         params.hor_pos = "lcrs"[halignCO->currentIndex()];
320
321         QString unit =
322                 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
323         QString value = widthED->text();
324         if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
325                 params.special = fromqstr(unit);
326                 // Note: the unit is simply ignored in this case
327                 params.width = Length(value.toDouble(), Length::IN);
328         } else {
329                 params.special = "none";
330                 params.width = Length(widgetsToLength(widthED, widthUnitsLC));
331         }
332
333         // the height parameter is omitted if the value
334         // is "1in" and "Total Height" is used as unit.
335         // 1in + "Total Height" means "1\height" which is the LaTeX default
336         // if no height is given
337         if (heightCB->checkState() == Qt::Unchecked) {
338                 params.height = Length("1in");
339                 params.height_special = "totalheight";
340         } else {
341                 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
342                 value = heightED->text();
343                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
344                         params.height_special = fromqstr(unit);
345                         // Note: the unit is simply ignored in this case
346                         params.height = Length(value.toDouble(), Length::IN);
347                 } else {
348                         params.height_special = "none";
349                         params.height =
350                                 Length(widgetsToLength(heightED, heightUnitsLC));
351                 }
352         }
353         return from_ascii(InsetBox::params2string(params));
354 }
355
356
357 void GuiBox::setSpecial(bool ibox)
358 {
359         QString const last_item =
360                 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
361
362         // check if the widget contains the special units
363         bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
364         // insert 'em if needed...
365         if (!ibox && !has_special) {
366                 for (int i = 1; i < ids_spec_.size(); ++i)
367                         widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
368         // ... or remove 'em if needed
369         } else if (ibox && has_special) {
370                 for (int i = 1; i < ids_spec_.size(); ++i) {
371                         int n = widthUnitsLC->findData(ids_spec_[i]);
372                         if (n != -1)
373                                 widthUnitsLC->removeItem(n);
374                 }
375         }
376         // restore selected text, if possible
377         widthUnitsLC->setCurrentItem(last_item);
378 }
379
380
381 void GuiBox::setInnerType(bool frameless, QString const & type)
382 {
383         // with "frameless" boxes, inner box is mandatory
384         // (i.e. is the actual box)
385         // we have to remove "none" then and adjust the combo
386         innerBoxCO->clear();
387         if (!frameless)
388                 innerBoxCO->addItem(qt_("None"), toqstr("none"));
389         else
390                 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
391         innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
392         innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
393         int i = (innerBoxCO->findData(type) != -1)
394                 ? innerBoxCO->findData(type) : 0;
395         innerBoxCO->setCurrentIndex(i);
396 }
397
398 } // namespace frontend
399 } // namespace lyx
400
401
402 #include "moc_GuiBox.cpp"