]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QBox.C
Extracted from r14281
[lyx.git] / src / frontends / qt3 / QBox.C
1 /**
2  * \file QBox.C
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 "QBox.h"
16
17 #include "checkedwidgets.h"
18 #include "lengthcombo.h"
19 #include "QBoxDialog.h"
20 #include "qt_helpers.h"
21 #include "Qt2BC.h"
22
23 #include "lengthcommon.h"
24 #include "lyxrc.h" // to set the default length values
25
26 #include "controllers/ControlBox.h"
27 #include "controllers/helper_funcs.h"
28
29 #include "insets/insetbox.h"
30
31 #include "support/lstrings.h"
32
33 #include <qpushbutton.h>
34 #include <qlineedit.h>
35 #include <qcombobox.h>
36 #include <qbuttongroup.h>
37
38 #include <vector>
39
40 using lyx::support::getStringFromVector;
41 using lyx::support::isStrDbl;
42 using lyx::support::subst;
43 using std::string;
44
45 namespace lyx {
46 namespace frontend {
47
48 typedef QController<ControlBox, QView<QBoxDialog> > base_class;
49
50 QBox::QBox(Dialog & parent)
51         : base_class(parent, _("Box Settings"))
52 {}
53
54
55 void QBox::build_dialog()
56 {
57         dialog_.reset(new QBoxDialog(this));
58
59         // fill the box type choice
60         box_gui_tokens(ids_, gui_names_);
61         for (unsigned int i = 0; i < gui_names_.size(); ++i)
62                 dialog_->typeCO->insertItem(toqstr(gui_names_[i]));
63
64         // add the special units to the height choice
65         // width needs different handling
66         box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
67         for (unsigned int i = 1; i < gui_names_spec_.size(); ++i) {
68                 dialog_->heightUnitsLC->insertItem(toqstr(gui_names_spec_[i]));
69         }
70
71         bcview().addReadOnly(dialog_->typeCO);
72         bcview().addReadOnly(dialog_->innerBoxCO);
73         bcview().addReadOnly(dialog_->valignCO);
74         bcview().addReadOnly(dialog_->ialignCO);
75         bcview().addReadOnly(dialog_->halignCO);
76         bcview().addReadOnly(dialog_->widthED);
77         bcview().addReadOnly(dialog_->heightED);
78         bcview().addReadOnly(dialog_->widthUnitsLC);
79         bcview().addReadOnly(dialog_->heightUnitsLC);
80
81         bcview().setRestore(dialog_->restorePB);
82         bcview().setOK(dialog_->okPB);
83         bcview().setApply(dialog_->applyPB);
84         bcview().setCancel(dialog_->closePB);
85
86         // initialize the length validator
87         addCheckedLineEdit(bcview(), dialog_->widthED, dialog_->widthLA);
88         addCheckedLineEdit(bcview(), dialog_->heightED, dialog_->heightLA);
89 }
90
91
92 void QBox::update_contents()
93 {
94         string type(controller().params().type);
95         for (unsigned int i = 0; i < gui_names_.size(); ++i) {
96                 if (type == ids_[i])
97                         dialog_->typeCO->setCurrentItem(i);
98         }
99
100         // default: minipage
101         unsigned int inner_type = 2;
102         if (!controller().params().inner_box)
103                 // none
104                 inner_type = 0;
105         if (controller().params().use_parbox)
106                 // parbox
107                 inner_type = 1;
108         bool frameless = (controller().params().type == "Frameless");
109         setInnerType(frameless, inner_type);
110
111         char c = controller().params().pos;
112         dialog_->valignCO->setCurrentItem(string("tcb").find(c, 0));
113         c = controller().params().inner_pos;
114         dialog_->ialignCO->setCurrentItem(string("tcbs").find(c, 0));
115         c = controller().params().hor_pos;
116         dialog_->halignCO->setCurrentItem(string("lcrs").find(c, 0));
117
118         bool ibox = controller().params().inner_box;
119         dialog_->valignCO->setEnabled(ibox);
120         dialog_->ialignCO->setEnabled(ibox);
121         dialog_->halignCO->setEnabled(!ibox);
122         setSpecial(ibox);
123
124         LyXLength::UNIT default_unit =
125                 (lyxrc.default_papersize > 3) ? LyXLength::CM : LyXLength::IN;
126
127         lengthToWidgets(dialog_->widthED, dialog_->widthUnitsLC,
128                 (controller().params().width).asString(), default_unit);
129
130         string const special(controller().params().special);
131         if (!special.empty() && special != "none") {
132                 QString spc;
133                 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
134                         if (special == ids_spec_[i])
135                                 spc = toqstr(gui_names_spec_[i].c_str());
136                 }
137                 for (int j = 0; j < dialog_->widthUnitsLC->count(); j++) {
138                         if (dialog_->widthUnitsLC->text(j) == spc)
139                                 dialog_->widthUnitsLC->setCurrentItem(j);
140                 }
141         }
142
143         lengthToWidgets(dialog_->heightED, dialog_->heightUnitsLC,
144                 (controller().params().height).asString(), default_unit);
145
146         string const height_special(controller().params().height_special);
147         if (!height_special.empty() && height_special != "none") {
148                 QString hspc;
149                 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
150                         if (height_special == ids_spec_[i]) {
151                                 hspc = toqstr(gui_names_spec_[i].c_str());
152                         }
153                 }
154                 for (int j = 0; j < dialog_->heightUnitsLC->count(); j++) {
155                         if (dialog_->heightUnitsLC->text(j) == hspc) {
156                                 dialog_->heightUnitsLC->setCurrentItem(j);
157                         }
158                 }
159         }
160
161         dialog_->heightED->setEnabled(ibox);
162         dialog_->heightUnitsLC->setEnabled(ibox);
163 }
164
165
166 void QBox::apply()
167 {
168         controller().params().type =
169                 ids_[dialog_->typeCO->currentItem()];
170
171         controller().params().inner_box =
172                 dialog_->innerBoxCO->currentText() != qt_("None");
173         controller().params().use_parbox =
174                 dialog_->innerBoxCO->currentText() ==  qt_("Parbox");
175
176         controller().params().pos =
177                 "tcb"[dialog_->valignCO->currentItem()];
178         controller().params().inner_pos =
179                 "tcbs"[dialog_->ialignCO->currentItem()];
180         controller().params().hor_pos =
181                 "lcrs"[dialog_->halignCO->currentItem()];
182
183         int i = 0;
184         bool spec = false;
185         QString special = dialog_->widthUnitsLC->currentText();
186         QString value = dialog_->widthED->text();
187         if (special == qt_("Height")) {
188                 i = 1;
189                 spec = true;
190         } else if (special == qt_("Depth")) {
191                 i = 2;
192                 spec = true;
193         } else if (special == qt_("Total Height")) {
194                 i = 3;
195                 spec = true;
196         } else if (special == qt_("Width")) {
197                 i = 4;
198                 spec = true;
199         }
200         // the user might insert a non-special value in the line edit
201         if (isValidLength(fromqstr(value))) {
202                 i = 0;
203                 spec = false;
204         }
205         controller().params().special = ids_spec_[i];
206
207         string width;
208         if (spec) {
209                 width = fromqstr(value);
210                 // beware: bogosity! the unit is simply ignored in this case
211                 width += "in";
212         } else
213                 width = widgetsToLength(dialog_->widthED, dialog_->widthUnitsLC);
214
215         controller().params().width = LyXLength(width);
216
217         i = 0;
218         spec = false;
219         special = dialog_->heightUnitsLC->currentText();
220         value = dialog_->heightED->text();
221         if (special == qt_("Height")) {
222                 i = 1;
223                 spec = true;
224         } else if (special == qt_("Depth")) {
225                 i = 2;
226                 spec = true;
227         } else if (special == qt_("Total Height")) {
228                 i = 3;
229                 spec = true;
230         } else if (special == qt_("Width")) {
231                 i = 4;
232                 spec = true;
233         }
234         // the user might insert a non-special value in the line edit
235         if (isValidLength(fromqstr(value))) {
236                 i = 0;
237                 spec = false;
238         }
239         controller().params().height_special = ids_spec_[i];
240
241         string height;
242         if (spec  && !isValidLength(fromqstr(dialog_->heightED->text()))) {
243                 height = fromqstr(value);
244                 // beware: bogosity! the unit is simply ignored in this case
245                 height += "in";
246         } else
247                 height = widgetsToLength(dialog_->heightED, dialog_->heightUnitsLC);
248
249         controller().params().height = LyXLength(height);
250 }
251
252
253 void QBox::setSpecial(bool ibox)
254 {
255         box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
256         // check if the widget contains the special units
257         int count = dialog_->widthUnitsLC->count();
258         bool has_special = false;
259         for (int i = 0; i < count; i++)
260                 if (dialog_->widthUnitsLC->text(i).contains(qt_("Total Height")) > 0)
261                         has_special = true;
262         // insert 'em if needed...
263         if (!ibox && !has_special) {
264                 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
265                         dialog_->widthUnitsLC->insertItem(toqstr(gui_names_spec_[i]));
266         // ... or remove 'em if needed
267         } else if (ibox && has_special) {
268                 dialog_->widthUnitsLC->clear();
269                 for (int i = 0; i < num_units; i++)
270                         dialog_->widthUnitsLC->insertItem(unit_name_gui[i]);
271         }
272 }
273
274
275 void QBox::setInnerType(bool frameless, int i)
276 {
277         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
278         // we have to remove "none" then and adjust the combo
279         if (frameless) {
280                 dialog_->innerBoxCO->clear();
281                 dialog_->innerBoxCO->insertItem(qt_("Parbox"));
282                 dialog_->innerBoxCO->insertItem(qt_("Minipage"));
283                 dialog_->innerBoxCO->setCurrentItem(i - 1);
284         } else {
285                 if (dialog_->innerBoxCO->count() == 2)
286                         i += 1;
287                 dialog_->innerBoxCO->clear();
288                 dialog_->innerBoxCO->insertItem(qt_("None"));
289                 dialog_->innerBoxCO->insertItem(qt_("Parbox"));
290                 dialog_->innerBoxCO->insertItem(qt_("Minipage"));
291                 dialog_->innerBoxCO->setCurrentItem(i);
292         }
293 }
294
295 } // namespace frontend
296 } // namespace lyx