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