]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
'using namespace std' instead of 'using std::xxx'
[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 "FuncRequest.h"
18 #include "support/gettext.h"
19
20 #include "LengthCombo.h"
21 #include "Length.h"
22 #include "qt_helpers.h"
23 #include "LyXRC.h" // to set the default length values
24 #include "Validator.h"
25
26 #include "insets/InsetBox.h"
27
28 #include "support/lstrings.h"
29
30 #include <QPushButton>
31 #include <QLineEdit>
32 #include <QCloseEvent>
33
34 using namespace std;
35
36 namespace lyx {
37 namespace frontend {
38
39
40 void box_gui_tokens(vector<string> & ids, vector<docstring> & gui_names)
41 {
42         char const * const ids_[] = {
43                 "Frameless", "Boxed", "ovalbox",
44                 "Ovalbox", "Shadowbox", "Shaded", "Doublebox"};
45         size_t const ids_size = sizeof(ids_) / sizeof(char *);
46         ids = vector<string>(ids_, ids_ + ids_size);
47         gui_names.clear();
48         gui_names.push_back(_("No frame"));
49         gui_names.push_back(_("Simple rectangular frame"));
50         gui_names.push_back(_("Oval frame, thin"));
51         gui_names.push_back(_("Oval frame, thick"));
52         gui_names.push_back(_("Drop shadow"));
53         gui_names.push_back(_("Shaded background"));
54         gui_names.push_back(_("Double rectangular frame"));
55 }
56
57
58 void box_gui_tokens_special_length(vector<string> & ids,
59         vector<docstring> & gui_names)
60 {
61         char const * const ids_[] = {
62                 "none", "height", "depth",
63                 "totalheight", "width"};
64         size_t const ids_size = sizeof(ids_) / sizeof(char *);
65         ids = vector<string>(ids_, ids_ + ids_size);
66         gui_names.clear();
67         gui_names.push_back(_("None"));
68         gui_names.push_back(_("Height"));
69         gui_names.push_back(_("Depth"));
70         gui_names.push_back(_("Total Height"));
71         gui_names.push_back(_("Width"));
72 }
73
74
75 GuiBox::GuiBox(GuiView & lv)
76         : GuiDialog(lv, "box"), params_("")
77 {
78         setupUi(this);
79         setViewTitle(_("Box Settings"));
80
81         // fill the box type choice
82         box_gui_tokens(ids_, gui_names_);
83         for (unsigned int i = 0; i < gui_names_.size(); ++i)
84                 typeCO->addItem(toqstr(gui_names_[i]));
85
86         // add the special units to the height choice
87         // width needs different handling
88         box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
89         for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
90                 heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
91
92         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
93         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
94         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
95         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
96
97         connect(widthED, SIGNAL(textChanged(QString)),
98                 this, SLOT(change_adaptor()));
99         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
100                 this, SLOT(change_adaptor()));
101         connect(valignCO, SIGNAL(highlighted(QString)),
102                 this, SLOT(change_adaptor()));
103         connect(heightCB, SIGNAL(stateChanged(int)),
104                 this, SLOT(change_adaptor()));
105         connect(heightED, SIGNAL(textChanged(const QString &)),
106                 this, SLOT(change_adaptor()));
107         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
108                 this, SLOT(change_adaptor()));
109         connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
110         connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
111         connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
112         connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
113         connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
114         connect(innerBoxCO, SIGNAL(activated(const QString&)),
115                 this, SLOT(innerBoxChanged(const QString &)));
116         connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
117         connect(pagebreakCB, SIGNAL(stateChanged(int)),
118                 this, SLOT(pagebreakClicked()));
119
120         heightED->setValidator(unsignedLengthValidator(heightED));
121         widthED->setValidator(unsignedLengthValidator(widthED));
122
123         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
124
125         bc().addReadOnly(typeCO);
126         bc().addReadOnly(innerBoxCO);
127         bc().addReadOnly(valignCO);
128         bc().addReadOnly(ialignCO);
129         bc().addReadOnly(halignCO);
130         bc().addReadOnly(widthED);
131         bc().addReadOnly(widthUnitsLC);
132         bc().addReadOnly(heightCB);
133         bc().addReadOnly(heightED);
134         bc().addReadOnly(heightUnitsLC);
135         bc().addReadOnly(pagebreakCB);
136
137         bc().setRestore(restorePB);
138         bc().setOK(okPB);
139         bc().setApply(applyPB);
140         bc().setCancel(closePB);
141
142         // initialize the length validator
143         bc().addCheckedLineEdit(widthED, widthLA);
144         bc().addCheckedLineEdit(heightED, heightCB);
145 }
146
147
148 void GuiBox::closeEvent(QCloseEvent * e)
149 {
150         slotClose();
151         e->accept();
152 }
153
154
155 void GuiBox::change_adaptor()
156 {
157         changed();
158 }
159
160
161 void GuiBox::innerBoxChanged(const QString & str)
162 {
163         bool const ibox = (str != qt_("None"));
164         valignCO->setEnabled(ibox);
165         ialignCO->setEnabled(ibox);
166         halignCO->setEnabled(!ibox);
167         heightCB->setEnabled(ibox);
168         if (heightCB->checkState() == Qt::Checked && ibox) {
169                 heightED->setEnabled(true);
170                 heightUnitsLC->setEnabled(true);
171         }
172         setSpecial(ibox);
173 }
174
175
176 void GuiBox::typeChanged(int index)
177 {
178         bool const frameless = (index == 0);
179         if (frameless) {
180                 valignCO->setEnabled(true);
181                 ialignCO->setEnabled(true);
182                 halignCO->setEnabled(false);
183                 heightCB->setEnabled(true);
184                 heightED->setEnabled(true);
185                 heightUnitsLC->setEnabled(true);
186                 setSpecial(true);
187         }
188         if (index != 1)
189                 pagebreakCB->setChecked(false);
190         pagebreakCB->setEnabled(index == 1);
191         int itype = innerBoxCO->currentIndex();
192         setInnerType(frameless, itype);
193 }
194
195
196 void GuiBox::restoreClicked()
197 {
198         setInnerType(true, 2);
199         widthED->setText("100");
200         widthUnitsLC->setCurrentItem(Length::PCW);
201         heightCB->setCheckState(Qt::Checked);
202         heightED->setText("1");
203         for (int j = 0; j < heightUnitsLC->count(); j++) {
204                 if (heightUnitsLC->itemText(j) == qt_("Total Height"))
205                         heightUnitsLC->setCurrentItem(j);
206         }
207 }
208
209
210 void GuiBox::pagebreakClicked()
211 {
212         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
213         innerBoxCO->setEnabled(!pbreak);
214         if (pbreak) {
215                 valignCO->setEnabled(false);
216                 ialignCO->setEnabled(false);
217                 halignCO->setEnabled(false);
218                 heightCB->setEnabled(false);
219                 heightED->setEnabled(false);
220                 heightUnitsLC->setEnabled(false);
221                 setSpecial(false);
222         } else
223                 typeChanged(typeCO->currentIndex());
224         change_adaptor();
225 }
226
227 void GuiBox::updateContents()
228 {
229         string type = params_.type;
230         if (type == "Framed") {
231                 pagebreakCB->setChecked(true);
232                 type = "Boxed";
233         } else
234                 pagebreakCB->setChecked(false);
235
236         pagebreakCB->setEnabled(type == "Boxed");
237
238         for (unsigned int i = 0; i < gui_names_.size(); ++i) {
239                 if (type == ids_[i])
240                         typeCO->setCurrentIndex(i);
241         }
242
243         // default: minipage
244         unsigned int inner_type = 2;
245         if (!params_.inner_box)
246                 // none
247                 inner_type = 0;
248         if (params_.use_parbox)
249                 // parbox
250                 inner_type = 1;
251         bool frameless = (params_.type == "Frameless");
252         setInnerType(frameless, inner_type);
253
254         char c = params_.pos;
255         valignCO->setCurrentIndex(string("tcb").find(c, 0));
256         c = params_.inner_pos;
257         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
258         c = params_.hor_pos;
259         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
260
261         bool ibox = params_.inner_box;
262         valignCO->setEnabled(ibox);
263         ialignCO->setEnabled(ibox);
264         halignCO->setEnabled(!ibox);
265         setSpecial(ibox);
266
267         Length::UNIT default_unit =
268                 (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
269
270         lengthToWidgets(widthED, widthUnitsLC,
271                 (params_.width).asString(), default_unit);
272
273         string const special = params_.special;
274         if (!special.empty() && special != "none") {
275                 QString spc;
276                 for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
277                         if (special == ids_spec_[i])
278                                 spc = toqstr(gui_names_spec_[i].c_str());
279                 }
280                 for (int j = 0; j < widthUnitsLC->count(); j++) {
281                         if (widthUnitsLC->itemText(j) == spc)
282                                 widthUnitsLC->setCurrentIndex(j);
283                 }
284         }
285
286         lengthToWidgets(heightED, heightUnitsLC,
287                 (params_.height).asString(), default_unit);
288         
289         string const height_special = params_.height_special;
290         if (!height_special.empty() && height_special != "none") {
291                 QString hspc;
292                 for (unsigned int i = 0; i != gui_names_spec_.size(); i++) {
293                         if (height_special == ids_spec_[i])
294                                 hspc = toqstr(gui_names_spec_[i].c_str());
295                 }
296                 for (int j = 0; j != heightUnitsLC->count(); j++) {
297                         if (heightUnitsLC->itemText(j) == hspc)
298                                 heightUnitsLC->setCurrentIndex(j);
299                 }
300         }
301         // set no optional height when the value is the default "1\height"
302         // (special units like \height are handled as "in",
303         if (height_special == "totalheight" &&  
304                 params_.height == Length("1in"))
305                 heightCB->setCheckState(Qt::Unchecked);
306         else
307                 heightCB->setCheckState(Qt::Checked);
308
309         heightCB->setEnabled(ibox);
310 }
311
312
313 void GuiBox::applyView()
314 {
315         bool pagebreak = pagebreakCB->isChecked();
316         if (pagebreak)
317                 params_.type = "Framed";
318         else
319                 params_.type = ids_[typeCO->currentIndex()];
320
321         params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
322         params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
323
324         params_.pos = "tcb"[valignCO->currentIndex()];
325         params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
326         params_.hor_pos = "lcrs"[halignCO->currentIndex()];
327
328         int i = 0;
329         bool spec = false;
330         QString special = widthUnitsLC->currentText();
331         QString value = widthED->text();
332         if (special == qt_("Height")) {
333                 i = 1;
334                 spec = true;
335         } else if (special == qt_("Depth")) {
336                 i = 2;
337                 spec = true;
338         } else if (special == qt_("Total Height")) {
339                 i = 3;
340                 spec = true;
341         } else if (special == qt_("Width")) {
342                 i = 4;
343                 spec = true;
344         }
345         // the user might insert a non-special value in the line edit
346         if (isValidLength(fromqstr(value))) {
347                 i = 0;
348                 spec = false;
349         }
350         params_.special = ids_spec_[i];
351
352         string width;
353         if (spec) {
354                 width = fromqstr(value);
355                 // beware: bogosity! the unit is simply ignored in this case
356                 width += "in";
357         } else {
358                 width = widgetsToLength(widthED, widthUnitsLC);
359         }
360
361         params_.width = Length(width);
362
363         i = 0;
364         spec = false;
365         special = heightUnitsLC->currentText();
366         value = heightED->text();
367         if (special == qt_("Height")) {
368                 i = 1;
369                 spec = true;
370         } else if (special == qt_("Depth")) {
371                 i = 2;
372                 spec = true;
373         } else if (special == qt_("Total Height")) {
374                 i = 3;
375                 spec = true;
376         } else if (special == qt_("Width")) {
377                 i = 4;
378                 spec = true;
379         }
380         // the user might insert a non-special value in the line edit
381         if (isValidLength(fromqstr(value))) {
382                 i = 0;
383                 spec = false;
384         }
385         params_.height_special = ids_spec_[i];
386
387         string height;
388         if (spec  && !isValidLength(fromqstr(heightED->text()))) {
389                 height = fromqstr(value);
390                 // beware: bogosity! the unit is simply ignored in this case
391                 height += "in";
392         } else
393                 height = widgetsToLength(heightED, heightUnitsLC);
394
395         // the height parameter is omitted in InsetBox.cpp when the value
396         // is "1in" and "Total Height" is used as unit.
397         // 1in + "Total Height" means "1\height" which is the LaTeX default when
398         // no height is given
399         if (heightCB->checkState() == Qt::Checked)
400                 params_.height = Length(height);
401         else {
402                 params_.height = Length("1in");
403                 params_.height_special = ids_spec_[3];
404         }
405 }
406
407
408 void GuiBox::setSpecial(bool ibox)
409 {
410         box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
411         // check if the widget contains the special units
412         int count = widthUnitsLC->count();
413         bool has_special = false;
414         for (int i = 0; i < count; i++)
415                 if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
416                         has_special = true;
417         // insert 'em if needed...
418         if (!ibox && !has_special) {
419                 for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
420                         widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
421         // ... or remove 'em if needed
422         } else if (ibox && has_special) {
423                 widthUnitsLC->clear();
424                 for (int i = 0; i < num_units; i++)
425                         widthUnitsLC->addItem(qt_(unit_name_gui[i]));
426         }
427 }
428
429
430 void GuiBox::setInnerType(bool frameless, int i)
431 {
432         // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
433         // we have to remove "none" then and adjust the combo
434         if (frameless) {
435                 innerBoxCO->clear();
436                 innerBoxCO->addItem(qt_("Parbox"));
437                 innerBoxCO->addItem(qt_("Minipage"));
438                 if (i != 0)
439                         innerBoxCO->setCurrentIndex(i - 1);
440                 else
441                         innerBoxCO->setCurrentIndex(i);
442         } else {
443                 if (innerBoxCO->count() == 2)
444                         ++i;
445                 innerBoxCO->clear();
446                 innerBoxCO->addItem(qt_("None"));
447                 innerBoxCO->addItem(qt_("Parbox"));
448                 innerBoxCO->addItem(qt_("Minipage"));
449                 innerBoxCO->setCurrentIndex(i);
450         }
451 }
452
453 bool GuiBox::initialiseParams(string const & data)
454 {
455         InsetBoxMailer::string2params(data, params_);
456         return true;
457
458 }
459
460
461 void GuiBox::clearParams()
462 {
463         params_ = InsetBoxParams("");
464 }
465
466
467 void GuiBox::dispatchParams()
468 {
469         dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
470 }
471
472
473 Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
474
475
476 } // namespace frontend
477 } // namespace lyx
478
479
480 #include "GuiBox_moc.cpp"