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