]> git.lyx.org Git - lyx.git/blob - src/frontends/qt/GuiVSpace.cpp
Support halfline and fullline also in vspace.
[lyx.git] / src / frontends / qt / 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 == 7;
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:
88                         item = 0;
89                         break;
90                 case VSpace::SMALLSKIP:
91                         item = 1;
92                         break;
93                 case VSpace::MEDSKIP:
94                         item = 2;
95                         break;
96                 case VSpace::BIGSKIP:
97                         item = 3;
98                         break;
99                 case VSpace::HALFLINE:
100                         item = 4;
101                         break;
102                 case VSpace::FULLLINE:
103                         item = 5;
104                         break;
105                 case VSpace::VFILL:
106                         item = 6;
107                         break;
108                 case VSpace::LENGTH:
109                         item = 7;
110                         break;
111         }
112         spacing->setCurrentIndex(item);
113         keep->setChecked(space.keep());
114
115         Length::UNIT const default_unit = Length::defaultUnit();
116         bool const custom_vspace = space.kind() == VSpace::LENGTH;
117         if (custom_vspace) {
118                 value->setEnabled(true);
119                 unit->setEnabled(true);
120                 string length = space.length().asString();
121                 lengthToWidgets(value, unit, length, default_unit);
122         } else {
123                 lengthToWidgets(value, unit, "", default_unit);
124                 value->setEnabled(false);
125                 unit->setEnabled(false);
126         }
127 }
128
129
130 static VSpace setVSpaceFromWidgets(int spacing,
131         QLineEdit * value, LengthCombo * unit, bool keep)
132 {
133         VSpace space;
134
135         switch (spacing) {
136                 case 0:
137                         space = VSpace(VSpace::DEFSKIP);
138                         break;
139                 case 1:
140                         space = VSpace(VSpace::SMALLSKIP);
141                         break;
142                 case 2:
143                         space = VSpace(VSpace::MEDSKIP);
144                         break;
145                 case 3:
146                         space = VSpace(VSpace::BIGSKIP);
147                         break;
148                 case 4:
149                         space = VSpace(VSpace::HALFLINE);
150                         break;
151                 case 5:
152                         space = VSpace(VSpace::FULLLINE);
153                         break;
154                 case 6:
155                         space = VSpace(VSpace::VFILL);
156                         break;
157                 case 7:
158                         space = VSpace(GlueLength(widgetsToLength(value, unit)));
159                         break;
160         }
161
162         space.setKeep(keep);
163         return space;
164 }
165
166
167 docstring GuiVSpace::dialogToParams() const
168 {
169         // If a vspace choice is "Length" but there's no text in
170         // the input field, do not insert a vspace at all.
171         if (spacingCO->currentIndex() == 7 && valueLE->text().isEmpty())
172                 return docstring();
173
174         VSpace const params = setVSpaceFromWidgets(spacingCO->currentIndex(),
175                         valueLE, unitCO, keepCB->isChecked());
176         return from_ascii(InsetVSpace::params2string(params));
177 }
178
179
180 void GuiVSpace::paramsToDialog(Inset const * inset)
181 {
182         InsetVSpace const * vs = static_cast<InsetVSpace const *>(inset);
183         VSpace const & params = vs->space();
184         setWidgetsFromVSpace(params, spacingCO, valueLE, unitCO, keepCB);
185         enableCustom(spacingCO->currentIndex());
186 }
187
188
189 bool GuiVSpace::checkWidgets(bool readonly) const
190 {
191         valueLE->setReadOnly(readonly);
192         keepCB->setEnabled(!readonly);
193
194         if (readonly) {
195                 spacingCO->setEnabled(false);
196                 unitCO->setEnabled(false);
197         } else {
198                 bool const enable = (spacingCO->currentIndex() == 7);
199                 valueLE->setEnabled(enable);
200                 valueL->setEnabled(enable);
201                 unitCO->setEnabled(enable);
202         }
203
204         return InsetParamsWidget::checkWidgets();
205 }
206
207 } // namespace frontend
208 } // namespace lyx
209
210
211 #include "moc_GuiVSpace.cpp"