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