]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiVSpace.cpp
Do not try to validate custom value in VSpace dialog if it is disabled
[lyx.git] / src / frontends / qt4 / GuiVSpace.cpp
1 /**
2  * \file GuiVSpace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Jürgen Vigna
8  * \author Rob Lahaye
9  * \author Angus Leeming
10  * \author Edwin Leuven
11  * \author Jürgen Spitzmüller
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "GuiVSpace.h"
19
20 #include "LengthCombo.h"
21 #include "qt_helpers.h"
22 #include "Validator.h"
23
24 #include "Spacing.h"
25 #include "VSpace.h"
26
27 #include "insets/InsetVSpace.h"
28
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31
32 #include <QCheckBox>
33 #include <QLineEdit>
34 #include <QPushButton>
35 #include <QValidator>
36
37 using namespace std;
38
39 namespace lyx {
40 namespace frontend {
41
42 GuiVSpace::GuiVSpace(QWidget * parent) : InsetParamsWidget(parent)
43 {
44         setupUi(this);
45
46         connect(valueLE, SIGNAL(textChanged(QString)),
47                 this, SIGNAL(changed()));
48         connect(keepCB, SIGNAL(clicked()),
49                 this, SIGNAL(changed()));
50         connect(unitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
51                 this, SIGNAL(changed()));
52
53         connect(spacingCO, SIGNAL(activated(int)),
54                 this, SLOT(enableCustom(int)));
55
56         valueLE->setValidator(unsignedGlueLengthValidator(valueLE));
57
58         // initialize the length validator
59         addCheckedWidget(valueLE, valueL);
60         enableCustom(spacingCO->currentIndex());
61 }
62
63
64 void GuiVSpace::enableCustom(int selection)
65 {
66         bool const enable = selection == 5;
67         valueLE->setEnabled(enable);
68         valueL->setEnabled(enable);
69         unitCO->setEnabled(enable);
70         changed();
71 }
72
73
74 static void setWidgetsFromVSpace(VSpace const & space,
75                           QComboBox * spacing,
76                           QLineEdit * value,
77                           LengthCombo * unit,
78                           QCheckBox * keep)
79 {
80         int item = 0;
81         switch (space.kind()) {
82                 case VSpace::DEFSKIP:   item = 0; break;
83                 case VSpace::SMALLSKIP: item = 1; break;
84                 case VSpace::MEDSKIP:   item = 2; break;
85                 case VSpace::BIGSKIP:   item = 3; break;
86                 case VSpace::VFILL:     item = 4; break;
87                 case VSpace::LENGTH:    item = 5; break;
88         }
89         spacing->setCurrentIndex(item);
90         keep->setChecked(space.keep());
91
92         Length::UNIT const default_unit = Length::defaultUnit();
93         bool const custom_vspace = space.kind() == VSpace::LENGTH;
94         if (custom_vspace) {
95                 value->setEnabled(true);
96                 unit->setEnabled(true);
97                 string length = space.length().asString();
98                 lengthToWidgets(value, unit, length, default_unit);
99         } else {
100                 lengthToWidgets(value, unit, "", default_unit);
101                 value->setEnabled(false);
102                 unit->setEnabled(false);
103         }
104 }
105
106
107 static VSpace setVSpaceFromWidgets(int spacing,
108         QLineEdit * value, LengthCombo * unit, bool keep)
109 {
110         VSpace space;
111
112         switch (spacing) {
113                 case 0: space = VSpace(VSpace::DEFSKIP); break;
114                 case 1: space = VSpace(VSpace::SMALLSKIP); break;
115                 case 2: space = VSpace(VSpace::MEDSKIP); break;
116                 case 3: space = VSpace(VSpace::BIGSKIP); break;
117                 case 4: space = VSpace(VSpace::VFILL); break;
118                 case 5: space = VSpace(GlueLength(widgetsToLength(value, unit))); break;
119         }
120
121         space.setKeep(keep);
122         return space;
123 }
124
125
126 docstring GuiVSpace::dialogToParams() const
127 {
128         // If a vspace choice is "Length" but there's no text in
129         // the input field, do not insert a vspace at all.
130         if (spacingCO->currentIndex() == 5 && valueLE->text().isEmpty())
131                 return docstring();
132
133         VSpace const params = setVSpaceFromWidgets(spacingCO->currentIndex(),
134                         valueLE, unitCO, keepCB->isChecked());
135         return from_ascii(InsetVSpace::params2string(params));
136 }
137
138
139 void GuiVSpace::paramsToDialog(Inset const * inset)
140 {
141         InsetVSpace const * vs = static_cast<InsetVSpace const *>(inset);
142         VSpace const & params = vs->space();
143         setWidgetsFromVSpace(params, spacingCO, valueLE, unitCO, keepCB);
144 }
145
146 } // namespace frontend
147 } // namespace lyx
148
149
150 #include "moc_GuiVSpace.cpp"