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