]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
remove unnneded #include
[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 "LengthCombo.h"
18 #include "qt_helpers.h"
19 #include "lengthcommon.h"
20 #include "LyXRC.h" // to set the default length values
21 #include "Validator.h"
22
23 #include "insets/InsetBox.h"
24
25 #include "support/lstrings.h"
26
27 #include <QPushButton>
28 #include <QLineEdit>
29 #include <QCloseEvent>
30
31
32 using std::string;
33
34 namespace lyx {
35 namespace frontend {
36
37 GuiBoxDialog::GuiBoxDialog(LyXView & lv)
38         : GuiDialog(lv, "box")
39 {
40         setupUi(this);
41         setViewTitle(_("Box Settings"));
42         setController(new ControlBox(*this));
43
44         // fill the box type choice
45         box_gui_tokens(ids_, gui_names_);
46         for (unsigned int i = 0; i < gui_names_.size(); ++i)
47                 typeCO->addItem(toqstr(gui_names_[i]));
48
49         // add the special units to the height choice
50         // width needs different handling
51         box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
52         for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
53                 heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
54
55         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
56         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
57         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
58         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
59
60         connect(widthED, SIGNAL(textChanged(const QString &)),
61                 this, SLOT(change_adaptor()));
62         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
63                 this, SLOT(change_adaptor()));
64         connect(valignCO, SIGNAL(highlighted(const QString &)),
65                 this, SLOT(change_adaptor()));
66         connect(heightED, SIGNAL(textChanged(const QString &)),
67                 this, SLOT(change_adaptor()));
68         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
69                 this, SLOT(change_adaptor()));
70         connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
71         connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
72         connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
73         connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
74         connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
75         connect(innerBoxCO, SIGNAL(activated(const QString&)),
76                 this, SLOT(innerBoxChanged(const QString &)));
77         connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
78
79         heightED->setValidator(unsignedLengthValidator(heightED));
80         widthED->setValidator(unsignedLengthValidator(widthED));
81
82         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
83
84         bc().addReadOnly(typeCO);
85         bc().addReadOnly(innerBoxCO);
86         bc().addReadOnly(valignCO);
87         bc().addReadOnly(ialignCO);
88         bc().addReadOnly(halignCO);
89         bc().addReadOnly(widthED);
90         bc().addReadOnly(heightED);
91         bc().addReadOnly(widthUnitsLC);
92         bc().addReadOnly(heightUnitsLC);
93
94         bc().setRestore(restorePB);
95         bc().setOK(okPB);
96         bc().setApply(applyPB);
97         bc().setCancel(closePB);
98
99         // initialize the length validator
100         bc().addCheckedLineEdit(widthED, widthLA);
101         bc().addCheckedLineEdit(heightED, heightLA);
102 }
103
104
105 ControlBox & GuiBoxDialog::controller()
106 {
107         return static_cast<ControlBox &>(GuiDialog::controller());
108 }
109
110
111 void GuiBoxDialog::closeEvent(QCloseEvent * e)
112 {
113         slotClose();
114         e->accept();
115 }
116
117
118 void GuiBoxDialog::change_adaptor()
119 {
120         changed();
121 }
122
123
124 void GuiBoxDialog::innerBoxChanged(const QString & str)
125 {
126         bool const ibox = (str != qt_("None"));
127         valignCO->setEnabled(ibox);
128         ialignCO->setEnabled(ibox);
129         halignCO->setEnabled(!ibox);
130         heightED->setEnabled(ibox);
131         heightUnitsLC->setEnabled(ibox);
132         setSpecial(ibox);
133 }
134
135
136 void GuiBoxDialog::typeChanged(int index)
137 {
138         bool const frameless = (index == 0);
139         if (frameless) {
140                 valignCO->setEnabled(true);
141                 ialignCO->setEnabled(true);
142                 halignCO->setEnabled(false);
143                 heightED->setEnabled(true);
144                 heightUnitsLC->setEnabled(true);
145                 setSpecial(true);
146         }
147         int itype = innerBoxCO->currentIndex();
148         setInnerType(frameless, itype);
149 }
150
151
152 void GuiBoxDialog::restoreClicked()
153 {
154         setInnerType(true, 2);
155         widthED->setText("100");
156         widthUnitsLC->setCurrentItem(Length::PCW);
157         heightED->setText("1");
158         for (int j = 0; j < heightUnitsLC->count(); j++) {
159                 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
160                         heightUnitsLC->setCurrentItem(j);
161         }
162 }
163
164
165 void GuiBoxDialog::updateContents()
166 {
167         string type(controller().params().type);
168         for (unsigned int i = 0; i < gui_names_.size(); ++i) {
169                 if (type == ids_[i])
170                         typeCO->setCurrentIndex(i);
171         }
172
173         // default: minipage
174         unsigned int inner_type = 2;
175         if (!controller().params().inner_box)
176                 // none
177                 inner_type = 0;
178         if (controller().params().use_parbox)
179                 // parbox
180                 inner_type = 1;
181         bool frameless = (controller().params().type == "Frameless");
182         setInnerType(frameless, inner_type);
183
184         char c = controller().params().pos;
185         valignCO->setCurrentIndex(string("tcb").find(c, 0));
186         c = controller().params().inner_pos;
187         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
188         c = controller().params().hor_pos;
189         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
190
191         bool ibox = controller().params().inner_box;
192         valignCO->setEnabled(ibox);
193         ialignCO->setEnabled(ibox);
194         halignCO->setEnabled(!ibox);
195         setSpecial(ibox);
196
197         Length::UNIT default_unit =
198                 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
199
200         lengthToWidgets(widthED, widthUnitsLC,
201                 (controller().params().width).asString(), default_unit);
202
203         string const special = controller().params().special;
204         if (!special.empty() && special != "none") {
205                 QString spc;
206                 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
207                         if (special == ids_spec_[i])
208                                 spc = toqstr(gui_names_spec_[i].c_str());
209                 }
210                 for (int j = 0; j < widthUnitsLC->count(); j++) {
211                         if (widthUnitsLC->itemText(j) == spc)
212                                 widthUnitsLC->setCurrentIndex(j);
213                 }
214         }
215
216         lengthToWidgets(heightED, heightUnitsLC,
217                 (controller().params().height).asString(), default_unit);
218
219         string const height_special = controller().params().height_special;
220         if (!height_special.empty() && height_special != "none") {
221                 QString hspc;
222                 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
223                         if (height_special == ids_spec_[i]) {
224                                 hspc = toqstr(gui_names_spec_[i].c_str());
225                         }
226                 }
227                 for (int j = 0; j < heightUnitsLC->count(); j++) {
228                         if (heightUnitsLC->itemText(j) == hspc) {
229                                 heightUnitsLC->setCurrentIndex(j);
230                         }
231                 }
232         }
233
234         heightED->setEnabled(ibox);
235         heightUnitsLC->setEnabled(ibox);
236 }
237
238
239 void GuiBoxDialog::applyView()
240 {
241         controller().params().type =
242                 ids_[typeCO->currentIndex()];
243
244         controller().params().inner_box =
245                 innerBoxCO->currentText() != qt_("None");
246         controller().params().use_parbox =
247                 innerBoxCO->currentText() ==  qt_("Parbox");
248
249         controller().params().pos =
250                 "tcb"[valignCO->currentIndex()];
251         controller().params().inner_pos =
252                 "tcbs"[ialignCO->currentIndex()];
253         controller().params().hor_pos =
254                 "lcrs"[halignCO->currentIndex()];
255
256         int i = 0;
257         bool spec = false;
258         QString special = widthUnitsLC->currentText();
259         QString value = widthED->text();
260         if (special == qt_("Height")) {
261                 i = 1;
262                 spec = true;
263         } else if (special == qt_("Depth")) {
264                 i = 2;
265                 spec = true;
266         } else if (special == qt_("Total Height")) {
267                 i = 3;
268                 spec = true;
269         } else if (special == qt_("Width")) {
270                 i = 4;
271                 spec = true;
272         }
273         // the user might insert a non-special value in the line edit
274         if (isValidLength(fromqstr(value))) {
275                 i = 0;
276                 spec = false;
277         }
278         controller().params().special = ids_spec_[i];
279
280         string width;
281         if (spec) {
282                 width = fromqstr(value);
283                 // beware: bogosity! the unit is simply ignored in this case
284                 width += "in";
285         } else
286                 width = widgetsToLength(widthED, widthUnitsLC);
287
288         controller().params().width = Length(width);
289
290         i = 0;
291         spec = false;
292         special = heightUnitsLC->currentText();
293         value = heightED->text();
294         if (special == qt_("Height")) {
295                 i = 1;
296                 spec = true;
297         } else if (special == qt_("Depth")) {
298                 i = 2;
299                 spec = true;
300         } else if (special == qt_("Total Height")) {
301                 i = 3;
302                 spec = true;
303         } else if (special == qt_("Width")) {
304                 i = 4;
305                 spec = true;
306         }
307         // the user might insert a non-special value in the line edit
308         if (isValidLength(fromqstr(value))) {
309                 i = 0;
310                 spec = false;
311         }
312         controller().params().height_special = ids_spec_[i];
313
314         string height;
315         if (spec  && !isValidLength(fromqstr(heightED->text()))) {
316                 height = fromqstr(value);
317                 // beware: bogosity! the unit is simply ignored in this case
318                 height += "in";
319         } else
320                 height = widgetsToLength(heightED, heightUnitsLC);
321
322         controller().params().height = Length(height);
323 }
324
325
326 void GuiBoxDialog::setSpecial(bool ibox)
327 {
328         box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
329         // check if the widget contains the special units
330         int count = widthUnitsLC->count();
331         bool has_special = false;
332         for (int i = 0; i < count; i++)
333                 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
334                         has_special = true;
335         // insert 'em if needed...
336         if (!ibox && !has_special) {
337                 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
338                         widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
339         // ... or remove 'em if needed
340         } else if (ibox && has_special) {
341                 widthUnitsLC->clear();
342                 for (int i = 0; i < num_units; i++)
343                         widthUnitsLC->addItem(qt_(unit_name_gui[i]));
344         }
345 }
346
347
348 void GuiBoxDialog::setInnerType(bool frameless, int i)
349 {
350         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
351         // we have to remove "none" then and adjust the combo
352         if (frameless) {
353                 innerBoxCO->clear();
354                 innerBoxCO->addItem(qt_("Parbox"));
355                 innerBoxCO->addItem(qt_("Minipage"));
356                 innerBoxCO->setCurrentIndex(i - 1);
357         } else {
358                 if (innerBoxCO->count() == 2)
359                         ++i;
360                 innerBoxCO->clear();
361                 innerBoxCO->addItem(qt_("None"));
362                 innerBoxCO->addItem(qt_("Parbox"));
363                 innerBoxCO->addItem(qt_("Minipage"));
364                 innerBoxCO->setCurrentIndex(i);
365         }
366 }
367
368 } // namespace frontend
369 } // namespace lyx
370
371
372 #include "GuiBox_moc.cpp"