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