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