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