]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiVSpace.cpp
Add missing initialization
[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         // Set up a signed glue length validator
57         LengthValidator * v = new LengthValidator(valueLE);
58         v->setBottom(GlueLength());
59         valueLE->setValidator(v);
60
61         // initialize the length validator
62         addCheckedWidget(valueLE, valueL);
63         enableCustom(spacingCO->currentIndex());
64 }
65
66
67 void GuiVSpace::enableCustom(int selection)
68 {
69         bool const enable = selection == 5;
70         valueLE->setEnabled(enable);
71         if (enable)
72                 valueLE->setFocus();
73         valueL->setEnabled(enable);
74         unitCO->setEnabled(enable);
75         changed();
76 }
77
78
79 static void setWidgetsFromVSpace(VSpace const & space,
80                           QComboBox * spacing,
81                           QLineEdit * value,
82                           LengthCombo * unit,
83                           QCheckBox * keep)
84 {
85         int item = 0;
86         switch (space.kind()) {
87                 case VSpace::DEFSKIP:   item = 0; break;
88                 case VSpace::SMALLSKIP: item = 1; break;
89                 case VSpace::MEDSKIP:   item = 2; break;
90                 case VSpace::BIGSKIP:   item = 3; break;
91                 case VSpace::VFILL:     item = 4; break;
92                 case VSpace::LENGTH:    item = 5; break;
93         }
94         spacing->setCurrentIndex(item);
95         keep->setChecked(space.keep());
96
97         Length::UNIT const default_unit = Length::defaultUnit();
98         bool const custom_vspace = space.kind() == VSpace::LENGTH;
99         if (custom_vspace) {
100                 value->setEnabled(true);
101                 unit->setEnabled(true);
102                 string length = space.length().asString();
103                 lengthToWidgets(value, unit, length, default_unit);
104         } else {
105                 lengthToWidgets(value, unit, "", default_unit);
106                 value->setEnabled(false);
107                 unit->setEnabled(false);
108         }
109 }
110
111
112 static VSpace setVSpaceFromWidgets(int spacing,
113         QLineEdit * value, LengthCombo * unit, bool keep)
114 {
115         VSpace space;
116
117         switch (spacing) {
118                 case 0: space = VSpace(VSpace::DEFSKIP); break;
119                 case 1: space = VSpace(VSpace::SMALLSKIP); break;
120                 case 2: space = VSpace(VSpace::MEDSKIP); break;
121                 case 3: space = VSpace(VSpace::BIGSKIP); break;
122                 case 4: space = VSpace(VSpace::VFILL); break;
123                 case 5: space = VSpace(GlueLength(widgetsToLength(value, unit))); break;
124         }
125
126         space.setKeep(keep);
127         return space;
128 }
129
130
131 docstring GuiVSpace::dialogToParams() const
132 {
133         // If a vspace choice is "Length" but there's no text in
134         // the input field, do not insert a vspace at all.
135         if (spacingCO->currentIndex() == 5 && valueLE->text().isEmpty())
136                 return docstring();
137
138         VSpace const params = setVSpaceFromWidgets(spacingCO->currentIndex(),
139                         valueLE, unitCO, keepCB->isChecked());
140         return from_ascii(InsetVSpace::params2string(params));
141 }
142
143
144 void GuiVSpace::paramsToDialog(Inset const * inset)
145 {
146         InsetVSpace const * vs = static_cast<InsetVSpace const *>(inset);
147         VSpace const & params = vs->space();
148         setWidgetsFromVSpace(params, spacingCO, valueLE, unitCO, keepCB);
149         enableCustom(spacingCO->currentIndex());
150 }
151
152
153 bool GuiVSpace::checkWidgets(bool readonly) const
154 {
155         valueLE->setReadOnly(readonly);
156         keepCB->setEnabled(!readonly);
157
158         if (readonly) {
159                 spacingCO->setEnabled(false);
160                 unitCO->setEnabled(false);
161         } else {
162                 bool const enable = (spacingCO->currentIndex() == 5);
163                 valueLE->setEnabled(enable);
164                 valueL->setEnabled(enable);
165                 unitCO->setEnabled(enable);
166         }
167
168         return InsetParamsWidget::checkWidgets();
169 }
170
171 } // namespace frontend
172 } // namespace lyx
173
174
175 #include "moc_GuiVSpace.cpp"