]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiVSpace.cpp
* fix spelling in comments to please John.
[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 "FuncRequest.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(GuiView & lv)
43         : GuiDialog(lv, "vspace", qt_("Vertical Space Settings"))
44 {
45         setupUi(this);
46
47         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
48         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
49         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
50
51         connect(spacingCO, SIGNAL(highlighted(QString)),
52                 this, SLOT(change_adaptor()));
53         connect(valueLE, SIGNAL(textChanged(QString)),
54                 this, SLOT(change_adaptor()));
55         connect(spacingCO, SIGNAL(activated(int)),
56                 this, SLOT(enableCustom(int)));
57         connect(keepCB, SIGNAL(clicked()),
58                 this, SLOT(change_adaptor()));
59         connect(unitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
60                 this, SLOT(change_adaptor()));
61
62         valueLE->setValidator(unsignedGlueLengthValidator(valueLE));
63
64         // Manage the ok, apply, restore and cancel/close buttons
65         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
66         bc().setOK(okPB);
67         bc().setApply(applyPB);
68         bc().setCancel(closePB);
69
70         // disable for read-only documents
71         bc().addReadOnly(spacingCO);
72         bc().addReadOnly(valueLE);
73         bc().addReadOnly(unitCO);
74         bc().addReadOnly(keepCB);
75
76         // initialize the length validator
77         bc().addCheckedLineEdit(valueLE, valueL);
78 }
79
80
81 void GuiVSpace::change_adaptor()
82 {
83         changed();
84 }
85
86
87 void GuiVSpace::enableCustom(int selection)
88 {
89         bool const enable = selection == 5;
90         valueLE->setEnabled(enable);
91         unitCO->setEnabled(enable);
92 }
93
94
95 static void setWidgetsFromVSpace(VSpace const & space,
96                           QComboBox * spacing,
97                           QLineEdit * value,
98                           LengthCombo * unit,
99                           QCheckBox * keep)
100 {
101         int item = 0;
102         switch (space.kind()) {
103                 case VSpace::DEFSKIP:   item = 0; break;
104                 case VSpace::SMALLSKIP: item = 1; break;
105                 case VSpace::MEDSKIP:   item = 2; break;
106                 case VSpace::BIGSKIP:   item = 3; break;
107                 case VSpace::VFILL:     item = 4; break;
108                 case VSpace::LENGTH:    item = 5; 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 void GuiVSpace::applyView()
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;
153
154         params_ = setVSpaceFromWidgets(spacingCO->currentIndex(),
155                         valueLE, unitCO, keepCB->isChecked()); 
156 }
157
158
159 void GuiVSpace::updateContents()
160 {
161         setWidgetsFromVSpace(params_, spacingCO, valueLE, unitCO, keepCB);
162 }
163
164
165 bool GuiVSpace::initialiseParams(string const & data)
166 {
167         InsetVSpace::string2params(data, params_);
168         setButtonsValid(true);
169         return true;
170 }
171
172
173 void GuiVSpace::clearParams()
174 {
175         params_ = VSpace();
176 }
177
178
179 void GuiVSpace::dispatchParams()
180 {
181         dispatch(FuncRequest(getLfun(), InsetVSpace::params2string(params_)));
182 }
183
184
185 Dialog * createGuiVSpace(GuiView & lv) { return new GuiVSpace(lv); }
186
187
188 } // namespace frontend
189 } // namespace lyx
190
191
192 #include "moc_GuiVSpace.cpp"