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