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