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