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