]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiBox.cpp
Add a function to translate QStrings
[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 "LengthCombo.h"
18 #include "Length.h"
19 #include "qt_helpers.h"
20 #include "Validator.h"
21
22 #include "insets/InsetBox.h"
23
24 #include "support/gettext.h"
25 #include "support/foreach.h"
26 #include "support/lstrings.h"
27
28 #include <QPushButton>
29 #include <QLineEdit>
30
31 #ifdef IN
32 #undef IN
33 #endif
34
35 using namespace std;
36
37
38 namespace lyx {
39 namespace frontend {
40
41 static QStringList boxGuiIds()
42 {
43         return QStringList()
44                 << "Frameless" << "Boxed"
45                 << "ovalbox" << "Ovalbox"
46                 << "Shadowbox" << "Shaded"
47                 << "Doublebox";
48 }
49
50
51 static QStringList boxGuiNames()
52 {
53         return QStringList()
54                 << qt_("No frame") << qt_("Simple rectangular frame")
55                 << qt_("Oval frame, thin") << qt_("Oval frame, thick")
56                 << qt_("Drop shadow") << qt_("Shaded background")
57                 << qt_("Double rectangular frame");
58 }
59
60
61 static QStringList boxGuiSpecialLengthIds()
62 {
63         return QStringList() << "height" << "depth"
64                 << "totalheight" << "width";
65 }
66
67
68 static QStringList boxGuiSpecialLengthNames()
69 {
70         return QStringList() << qt_("Height") << qt_("Depth")
71                 << qt_("Total Height") << qt_("Width");
72 }
73
74
75 GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
76 {
77         setupUi(this);
78
79         // fill the box type choice
80         ids_ = boxGuiIds();
81         gui_names_ = boxGuiNames();
82         for (int i = 0; i != ids_.size(); ++i)
83                 typeCO->addItem(gui_names_[i], ids_[i]);
84
85         // add the special units to the height choice
86         // width needs different handling
87         ids_spec_ = boxGuiSpecialLengthIds();
88         gui_names_spec_ = boxGuiSpecialLengthNames();
89         for (int i = 0; i != ids_spec_.size(); ++i)
90                 heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
91
92         connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
93         connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
94                 this, SIGNAL(changed()));
95         connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
96         connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
97         connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
98                 this, SIGNAL(changed()));
99         connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
100         connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
101
102         heightED->setValidator(unsignedLengthValidator(heightED));
103         widthED->setValidator(unsignedLengthValidator(widthED));
104
105         // initialize the length validator
106         addCheckedWidget(widthED, widthCB);
107         addCheckedWidget(heightED, heightCB);
108
109         initDialog();
110 }
111
112
113 void GuiBox::on_innerBoxCO_activated(int /* index */)
114 {
115         QString itype =
116                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
117         // handle parbox and minipage the same way
118         bool const ibox =
119                 (itype != "none"
120                  && itype != "makebox");
121         QString const outer =
122                 typeCO->itemData(typeCO->currentIndex()).toString();
123         valignCO->setEnabled(ibox);
124         ialignCO->setEnabled(ibox);
125         if (heightCB->isChecked() && !ibox)
126                 heightCB->setChecked(false);
127         heightCB->setEnabled(ibox);
128         // the width can only be selected for makebox or framebox
129         widthCB->setEnabled(itype == "makebox" 
130                             || (outer == "Boxed" && itype == "none"));
131         widthCB->setChecked(itype != "none" && !widthCB->isEnabled());
132         // except for frameless and boxed, the width cannot be specified if
133         // there is no inner box
134         bool const width_enabled =
135                 ibox || outer == "Frameless" || outer == "Boxed";
136         // enable if width_enabled, except if checkbaox is active but unset
137         widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
138         widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
139         if (!widthCB->isChecked() && widthCB->isEnabled()) {
140                 widthED->setEnabled(false);
141                 widthUnitsLC->setEnabled(false);
142         }
143         // halign is only allowed without inner box and if a width is used and if
144         // pagebreak is not used
145         halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
146                              && ((!ibox && outer == "Boxed") || itype == "makebox"));
147         // pagebreak is only allowed for Boxed without inner box
148         pagebreakCB->setEnabled(!ibox && outer == "Boxed");
149         setSpecial(ibox);
150         changed();
151 }
152
153
154 void GuiBox::on_typeCO_activated(int index)
155 {
156         QString const type =
157                 typeCO->itemData(index).toString();
158         bool const frameless = (type == "Frameless");
159         QString itype =
160                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
161         setInnerType(frameless, itype);
162         // refresh itype because it might have been changed in setInnerType
163         itype =
164                 innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
165         // handle parbox and minipage the same way
166         bool const ibox =
167                 (itype != "none"
168                  && itype != "makebox");
169         if (frameless && itype != "makebox") {
170                 valignCO->setEnabled(ibox);
171                 ialignCO->setEnabled(ibox);
172                 if (heightCB->isChecked() && !ibox)
173                         heightCB->setChecked(false);
174                 heightCB->setEnabled(ibox);
175                 setSpecial(ibox);
176         }
177         // the width can only be selected for makebox or framebox
178         widthCB->setEnabled(itype == "makebox" 
179                             || (type == "Boxed" && itype == "none"));
180         widthCB->setChecked(itype != "none" && !widthCB->isEnabled());
181         // except for frameless and boxed, the width cannot be specified if
182         // there is no inner box
183         bool const width_enabled = 
184                 itype != "none" || frameless || type == "Boxed";
185         // enable if width_enabled, except if checkbaox is active but unset
186         widthED->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
187         widthUnitsLC->setEnabled(width_enabled || (widthCB->isEnabled() && widthCB->isChecked()));
188         if (!widthCB->isChecked() && widthCB->isEnabled()) {
189                 widthED->setEnabled(false);
190                 widthUnitsLC->setEnabled(false);
191         }
192         // halign is only allowed without inner box and if a width is used and if
193         // pagebreak is not used
194         halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
195                              && ((itype == "none" && type == "Boxed") || itype == "makebox"));
196         // pagebreak is only allowed for Boxed without inner box
197         pagebreakCB->setEnabled(type == "Boxed" && itype == "none");
198         if (type != "Boxed")
199                 pagebreakCB->setChecked(false);
200         changed();
201 }
202
203
204 void GuiBox::initDialog()
205 {
206         setInnerType(true, toqstr("minipage"));
207         widthED->setText("100");
208         widthCB->setChecked(true);
209         widthCB->setEnabled(false);
210         widthUnitsLC->setCurrentItem(Length::PCW);
211         heightED->setText("1");
212         heightUnitsLC->setCurrentItem("totalheight");
213 }
214
215
216 void GuiBox::on_widthCB_stateChanged(int)
217 {
218         if (widthCB->isEnabled()) {
219                 widthED->setEnabled(widthCB->isChecked());
220                 widthUnitsLC->setEnabled(widthCB->isChecked());
221                 halignCO->setEnabled(widthCB->isChecked());
222         }
223         changed();
224 }
225
226
227 void GuiBox::on_heightCB_stateChanged(int state)
228 {
229         bool const enable = (innerBoxCO->currentText() != qt_("None"))
230                 && (state == Qt::Checked);
231         heightED->setEnabled(enable);
232         heightUnitsLC->setEnabled(enable);
233         changed();
234 }
235
236
237 void GuiBox::on_pagebreakCB_stateChanged()
238 {
239         bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
240         innerBoxCO->setEnabled(!pbreak);
241         widthCB->setEnabled(!pbreak);
242         if (pbreak)
243                 widthCB->setChecked(!pbreak);
244         widthED->setEnabled(!pbreak);
245         widthUnitsLC->setEnabled(!pbreak);
246         if (!pbreak) {
247                 on_typeCO_activated(typeCO->currentIndex());
248                 return;
249         }
250         valignCO->setEnabled(false);
251         ialignCO->setEnabled(false);
252         halignCO->setEnabled(false);
253         heightCB->setEnabled(false);
254         heightED->setEnabled(false);
255         heightUnitsLC->setEnabled(false);
256         setSpecial(false);
257         changed();
258 }
259
260
261 void GuiBox::paramsToDialog(Inset const * inset)
262 {
263         InsetBox const * box = static_cast<InsetBox const *>(inset);
264         InsetBoxParams const & params = box->params();
265         QString type = toqstr(params.type);
266         if (type == "Framed") {
267                 pagebreakCB->setChecked(true);
268                 type = "Boxed";
269         } else {
270                 pagebreakCB->setChecked(false);
271         }
272
273         typeCO->setCurrentIndex(typeCO->findData(type));
274
275         // default: minipage
276         QString inner_type = "minipage";
277         if (!params.inner_box)
278                 inner_type = "none";
279         if (params.use_parbox)
280                 inner_type = "parbox";
281         if (params.use_makebox)
282                 inner_type = "makebox";
283         bool const frameless = (params.type == "Frameless");
284         setInnerType(frameless, inner_type);
285
286         char c = params.pos;
287         valignCO->setCurrentIndex(string("tcb").find(c, 0));
288         c = params.inner_pos;
289         ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
290         c = params.hor_pos;
291         halignCO->setCurrentIndex(string("lcrs").find(c, 0));
292
293         bool ibox = (params.inner_box && !params.use_makebox);
294         valignCO->setEnabled(ibox);
295         ialignCO->setEnabled(ibox);
296         setSpecial(ibox);
297
298         // halign is only allowed without inner box and if a width is used and if
299         // pagebreak is not used
300         halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
301                              && ((!ibox && type == "Boxed") || params.use_makebox));
302         // pagebreak is only allowed for Boxed without inner box
303         pagebreakCB->setEnabled(!ibox && type == "Boxed");
304
305         Length::UNIT const default_unit = Length::defaultUnit();
306
307         // the width can only be selected for makebox or framebox
308         widthCB->setEnabled(inner_type == "makebox" 
309                             || (type == "Boxed" && !ibox && !pagebreakCB->isChecked()));
310         // "-999col%" is the code for no width
311         if ((params.width).asString() == "-999col%")
312                 widthCB->setCheckState(Qt::Unchecked);
313         else {
314                 if (widthCB->isEnabled())
315                         widthCB->setChecked(true);
316                 lengthToWidgets(widthED, widthUnitsLC,
317                         (params.width).asString(), default_unit);
318                 QString const special = toqstr(params.special);
319                 if (!special.isEmpty() && special != "none")
320                         widthUnitsLC->setCurrentItem(special);
321         }
322
323         widthED->setEnabled(widthCB->isChecked());
324         widthUnitsLC->setEnabled(widthCB->isChecked());
325
326         lengthToWidgets(heightED, heightUnitsLC,
327                 (params.height).asString(), default_unit);
328         
329         QString const height_special = toqstr(params.height_special);
330         if (!height_special.isEmpty() && height_special != "none")
331                 heightUnitsLC->setCurrentItem(height_special);
332         // set no optional height if the value is the default "1\height"
333         // (special units like \height are handled as "in",
334         // FIXME: this is a very bad UI, this check box should be disabled in
335         // this case, not forced to 'unchecked' state.
336         if (height_special == "totalheight" && params.height == Length("1in"))
337                 heightCB->setCheckState(Qt::Unchecked);
338         else
339                 heightCB->setCheckState(Qt::Checked);
340
341         heightCB->setEnabled(ibox);
342 }
343
344
345 docstring GuiBox::dialogToParams() const
346 {
347         bool const pagebreak =
348                 pagebreakCB->isEnabled() && pagebreakCB->isChecked();
349         string box_type;
350         if (pagebreak)
351                 box_type = "Framed";
352         else
353                 box_type = fromqstr(typeCO->itemData(
354                                 typeCO->currentIndex()).toString());
355
356         InsetBoxParams params(box_type);
357         params.inner_box =
358                 (!pagebreak && innerBoxCO->currentText() != qt_("None"));
359         params.use_parbox =
360                 (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
361         params.use_makebox =
362                 (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
363
364         params.pos = "tcb"[valignCO->currentIndex()];
365         params.inner_pos = "tcbs"[ialignCO->currentIndex()];
366         params.hor_pos = "lcrs"[halignCO->currentIndex()];
367
368         QString unit =
369                 widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
370         QString value = widthED->text();
371
372         if (widthCB->isChecked()) {
373          if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
374                  params.special = fromqstr(unit);
375                  // Note: the unit is simply ignored in this case
376                  params.width = Length(value.toDouble(), Length::IN);
377          } else {
378                  params.special = "none";
379                  params.width = Length(widgetsToLength(widthED, widthUnitsLC));
380          }
381         } else {
382                 if (widthCB->isEnabled()) {
383                         // use the code "-999col%" for the case that no width was selected
384                         params.special = "none";
385                         params.width = Length("-999col%");
386                 }
387         }
388
389         // the height parameter is omitted if the value
390         // is "1in" and "Total Height" is used as unit.
391         // 1in + "Total Height" means "1\height" which is the LaTeX default
392         // if no height is given
393         if (heightCB->checkState() == Qt::Unchecked) {
394                 params.height = Length("1in");
395                 params.height_special = "totalheight";
396         } else {
397                 unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
398                 value = heightED->text();
399                 if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
400                         params.height_special = fromqstr(unit);
401                         // Note: the unit is simply ignored in this case
402                         params.height = Length(value.toDouble(), Length::IN);
403                 } else {
404                         params.height_special = "none";
405                         params.height =
406                                 Length(widgetsToLength(heightED, heightUnitsLC));
407                 }
408         }
409         return from_ascii(InsetBox::params2string(params));
410 }
411
412
413 void GuiBox::setSpecial(bool ibox)
414 {
415         QString const last_item =
416                 widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
417
418         // check if the widget contains the special units
419         bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
420         // insert 'em if needed...
421         if (!ibox && !has_special) {
422                 for (int i = 1; i < ids_spec_.size(); ++i)
423                         widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
424         // ... or remove 'em if needed
425         } else if (ibox && has_special) {
426                 for (int i = 1; i < ids_spec_.size(); ++i) {
427                         int n = widthUnitsLC->findData(ids_spec_[i]);
428                         if (n != -1)
429                                 widthUnitsLC->removeItem(n);
430                 }
431         }
432         // restore selected text, if possible
433         widthUnitsLC->setCurrentItem(last_item);
434 }
435
436
437 void GuiBox::setInnerType(bool frameless, QString const & type)
438 {
439         // with "frameless" boxes, inner box is mandatory
440         // (i.e. is the actual box)
441         // we have to remove "none" then and adjust the combo
442         innerBoxCO->clear();
443         if (!frameless)
444                 innerBoxCO->addItem(qt_("None"), toqstr("none"));
445         else
446                 innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
447         innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
448         innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
449         int i = (innerBoxCO->findData(type) != -1)
450                 ? innerBoxCO->findData(type) : 0;
451         innerBoxCO->setCurrentIndex(i);
452 }
453
454 } // namespace frontend
455 } // namespace lyx
456
457
458 #include "moc_GuiBox.cpp"