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