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