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