]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiVSpace.cpp
cosmetics
[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 "ControlVSpace.h"
21 #include "LengthCombo.h"
22 #include "qt_helpers.h"
23 #include "Validator.h"
24
25 #include "LyXRC.h" // to set the default length values
26 #include "Spacing.h"
27 #include "VSpace.h"
28
29 #include "ControlVSpace.h"
30 #include "frontend_helpers.h"
31
32 #include "support/lstrings.h"
33
34 #include <QCheckBox>
35 #include <QCloseEvent>
36 #include <QLineEdit>
37 #include <QPushButton>
38 #include <QValidator>
39
40 using std::string;
41
42
43 namespace lyx {
44 namespace frontend {
45
46 GuiVSpaceDialog::GuiVSpaceDialog(LyXView & lv)
47         : GuiDialog(lv, "vspace")
48 {
49         setupUi(this);
50         setViewTitle(_("Vertical Space Settings"));
51         setController(new ControlVSpace(*this));
52
53         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
54         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
55         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
56
57         connect(spacingCO, SIGNAL(highlighted(const QString &)),
58                 this, SLOT(change_adaptor()));
59         connect(valueLE, SIGNAL(textChanged(const QString &)),
60                 this, SLOT(change_adaptor()));
61         connect(spacingCO, SIGNAL(activated(int)),
62                 this, SLOT(enableCustom(int)));
63         connect(keepCB, SIGNAL(clicked()),
64                 this, SLOT(change_adaptor()));
65         connect(unitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
66                 this, SLOT(change_adaptor()));
67
68         valueLE->setValidator(unsignedLengthValidator(valueLE));
69
70         // Manage the ok, apply, restore and cancel/close buttons
71         bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
72         bc().setOK(okPB);
73         bc().setApply(applyPB);
74         bc().setCancel(closePB);
75
76         // disable for read-only documents
77         bc().addReadOnly(spacingCO);
78         bc().addReadOnly(valueLE);
79         bc().addReadOnly(unitCO);
80         bc().addReadOnly(keepCB);
81
82         // initialize the length validator
83         bc().addCheckedLineEdit(valueLE, valueL);
84
85         // remove the %-items from the unit choice
86         unitCO->noPercents();
87 }
88
89
90 ControlVSpace & GuiVSpaceDialog::controller()
91 {
92         return static_cast<ControlVSpace &>(GuiDialog::controller());
93 }
94
95
96 void GuiVSpaceDialog::closeEvent(QCloseEvent * e)
97 {
98         slotClose();
99         e->accept();
100 }
101
102
103 void GuiVSpaceDialog::change_adaptor()
104 {
105         changed();
106 }
107
108
109 void GuiVSpaceDialog::enableCustom(int selection)
110 {
111         bool const enable = selection == 5;
112         valueLE->setEnabled(enable);
113         unitCO->setEnabled(enable);
114 }
115
116
117 static void setWidgetsFromVSpace(VSpace const & space,
118                           QComboBox * spacing,
119                           QLineEdit * value,
120                           LengthCombo * unit,
121                           QCheckBox * keep)
122 {
123         int item = 0;
124         switch (space.kind()) {
125                 case VSpace::DEFSKIP:   item = 0; break;
126                 case VSpace::SMALLSKIP: item = 1; break;
127                 case VSpace::MEDSKIP:   item = 2; break;
128                 case VSpace::BIGSKIP:   item = 3; break;
129                 case VSpace::VFILL:     item = 4; break;
130                 case VSpace::LENGTH:    item = 5; break;
131         }
132         spacing->setCurrentIndex(item);
133         keep->setChecked(space.keep());
134
135         Length::UNIT default_unit =
136                         (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
137         bool const custom_vspace = space.kind() == VSpace::LENGTH;
138         if (custom_vspace) {
139                 value->setEnabled(true);
140                 unit->setEnabled(true);
141                 string length = space.length().asString();
142                 lengthToWidgets(value, unit, length, default_unit);
143         } else {
144                 lengthToWidgets(value, unit, "", default_unit);
145                 value->setEnabled(false);
146                 unit->setEnabled(false);
147         }
148 }
149
150
151 static VSpace setVSpaceFromWidgets(int spacing,
152         QLineEdit * value, LengthCombo * unit, bool keep)
153 {
154         VSpace space;
155
156         switch (spacing) {
157                 case 0: space = VSpace(VSpace::DEFSKIP); break;
158                 case 1: space = VSpace(VSpace::SMALLSKIP); break;
159                 case 2: space = VSpace(VSpace::MEDSKIP); break;
160                 case 3: space = VSpace(VSpace::BIGSKIP); break;
161                 case 4: space = VSpace(VSpace::VFILL); break;
162                 case 5: space = VSpace(GlueLength(widgetsToLength(value, unit))); break;
163         }
164
165         space.setKeep(keep);
166         return space;
167 }
168
169
170 void GuiVSpaceDialog::applyView()
171 {
172         // If a vspace choice is "Length" but there's no text in
173         // the input field, do not insert a vspace at all.
174         if (spacingCO->currentIndex() == 5 && valueLE->text().isEmpty())
175                 return;
176
177         VSpace const space = setVSpaceFromWidgets(spacingCO->currentIndex(),
178                         valueLE, unitCO, keepCB->isChecked()); 
179
180         controller().params() = space;
181 }
182
183
184 void GuiVSpaceDialog::updateContents()
185 {
186         setWidgetsFromVSpace(controller().params(),
187                 spacingCO, valueLE, unitCO, keepCB);
188 }
189
190 } // namespace frontend
191 } // namespace lyx
192
193
194 #include "GuiVSpace_moc.cpp"