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