]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QVSpace.C
Rename .C ==> .cpp for files in src/frontends/qt4, part one
[lyx.git] / src / frontends / qt4 / QVSpace.C
1 /**
2  * \file QVSpace.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 "QVSpace.h"
19 #include "Qt2BC.h"
20
21 #include "CheckedLineEdit.h"
22 #include "LengthCombo.h"
23 #include "qt_helpers.h"
24 #include "Validator.h"
25
26 #include "lyxrc.h" // to set the default length values
27 #include "Spacing.h"
28 #include "vspace.h"
29
30 #include "controllers/ControlVSpace.h"
31 #include "controllers/frontend_helpers.h"
32
33 #include "support/lstrings.h"
34
35 #include <QCheckBox>
36 #include <QCloseEvent>
37 #include <QLineEdit>
38 #include <QPushButton>
39 #include <QValidator>
40
41
42 using std::string;
43
44 namespace lyx {
45 namespace frontend {
46
47
48 /////////////////////////////////////////////////////////////////////
49 //
50 // QVSpaceDialog
51 //
52 /////////////////////////////////////////////////////////////////////
53
54
55 QVSpaceDialog::QVSpaceDialog(QVSpace * form)
56         : form_(form)
57 {
58         setupUi(this);
59
60         connect(okPB, SIGNAL(clicked()), form_, SLOT(slotOK()));
61         connect(applyPB, SIGNAL(clicked()), form_, SLOT(slotApply()));
62         connect(closePB, SIGNAL(clicked()), form_, SLOT(slotClose()));
63
64         connect(spacingCO, SIGNAL(highlighted(const QString &)),
65                 this, SLOT(change_adaptor()));
66         connect(valueLE, SIGNAL(textChanged(const QString &)),
67                 this, SLOT(change_adaptor()));
68         connect(spacingCO, SIGNAL(activated(int)),
69                 this, SLOT(enableCustom(int)));
70         connect(keepCB, SIGNAL(clicked()),
71                 this, SLOT(change_adaptor()));
72         connect(unitCO, SIGNAL(selectionChanged(LyXLength::UNIT)),
73                 this, SLOT(change_adaptor()));
74
75         valueLE->setValidator(unsignedLengthValidator(valueLE));
76 }
77
78
79 void QVSpaceDialog::closeEvent(QCloseEvent * e)
80 {
81         form_->slotWMHide();
82         e->accept();
83 }
84
85
86 void QVSpaceDialog::change_adaptor()
87 {
88         form_->changed();
89 }
90
91
92 void QVSpaceDialog::enableCustom(int selection)
93 {
94         bool const enable = selection == 5;
95         valueLE->setEnabled(enable);
96         unitCO->setEnabled(enable);
97 }
98
99
100 /////////////////////////////////////////////////////////////////////
101 //
102 // QVSpace
103 //
104 /////////////////////////////////////////////////////////////////////
105
106 static void setWidgetsFromVSpace(VSpace const & space,
107                           QComboBox * spacing,
108                           QLineEdit * value,
109                           LengthCombo * unit,
110                           QCheckBox * keep)
111 {
112         int item = 0;
113         switch (space.kind()) {
114         case VSpace::DEFSKIP:
115                 item = 0;
116                 break;
117         case VSpace::SMALLSKIP:
118                 item = 1;
119                 break;
120         case VSpace::MEDSKIP:
121                 item = 2;
122                 break;
123         case VSpace::BIGSKIP:
124                 item = 3;
125                 break;
126         case VSpace::VFILL:
127                 item = 4;
128                 break;
129         case VSpace::LENGTH:
130                 item = 5;
131                 break;
132         }
133         spacing->setCurrentIndex(item);
134         keep->setChecked(space.keep());
135
136         LyXLength::UNIT default_unit =
137                         (lyxrc.default_papersize > 3) ? LyXLength::CM : LyXLength::IN;
138         bool const custom_vspace = space.kind() == VSpace::LENGTH;
139         if (custom_vspace) {
140                 value->setEnabled(true);
141                 unit->setEnabled(true);
142                 string length = space.length().asString();
143                 lengthToWidgets(value, unit, length, default_unit);
144         } else {
145                 lengthToWidgets(value, unit, "", default_unit);
146                 value->setEnabled(false);
147                 unit->setEnabled(false);
148         }
149 }
150
151
152 static VSpace setVSpaceFromWidgets(int spacing,
153                             QLineEdit * value,
154                             LengthCombo * unit,
155                             bool keep)
156 {
157         VSpace space;
158
159         switch (spacing) {
160         case 0:
161                 space = VSpace(VSpace::DEFSKIP);
162                 break;
163         case 1:
164                 space = VSpace(VSpace::SMALLSKIP);
165                 break;
166         case 2:
167                 space = VSpace(VSpace::MEDSKIP);
168                 break;
169         case 3:
170                 space = VSpace(VSpace::BIGSKIP);
171                 break;
172         case 4:
173                 space = VSpace(VSpace::VFILL);
174                 break;
175         case 5:
176                 space = VSpace(LyXGlueLength(widgetsToLength(value, unit)));
177                 break;
178         }
179
180         space.setKeep(keep);
181         return space;
182 }
183
184
185 typedef QController<ControlVSpace, QView<QVSpaceDialog> > VSpaceBase;
186
187 QVSpace::QVSpace(Dialog & parent)
188         : VSpaceBase(parent, _("Vertical Space Settings"))
189 {}
190
191
192 void QVSpace::build_dialog()
193 {
194         // the tabbed folder
195         dialog_.reset(new QVSpaceDialog(this));
196
197         // Manage the ok, apply, restore and cancel/close buttons
198         bcview().setOK(dialog_->okPB);
199         bcview().setApply(dialog_->applyPB);
200         bcview().setCancel(dialog_->closePB);
201
202         // disable for read-only documents
203         bcview().addReadOnly(dialog_->spacingCO);
204         bcview().addReadOnly(dialog_->valueLE);
205         bcview().addReadOnly(dialog_->unitCO);
206         bcview().addReadOnly(dialog_->keepCB);
207
208         // initialize the length validator
209         addCheckedLineEdit(bcview(), dialog_->valueLE, dialog_->valueL);
210
211         // remove the %-items from the unit choice
212         dialog_->unitCO->noPercents();
213 }
214
215
216 void QVSpace::apply()
217 {
218         // spacing
219         // If a vspace choice is "Length" but there's no text in
220         // the input field, do not insert a vspace at all.
221         if (dialog_->spacingCO->currentIndex() == 5
222             && dialog_->valueLE->text().isEmpty())
223                 return;
224
225         VSpace const space =
226                 setVSpaceFromWidgets(dialog_->spacingCO->currentIndex(),
227                                      dialog_->valueLE,
228                                      dialog_->unitCO,
229                                      dialog_->keepCB->isChecked());
230
231         controller().params() = space;
232 }
233
234
235 void QVSpace::update_contents()
236 {
237         setWidgetsFromVSpace(controller().params(),
238                              dialog_->spacingCO,
239                              dialog_->valueLE,
240                              dialog_->unitCO,
241                              dialog_->keepCB);
242 }
243
244 } // namespace frontend
245 } // namespace lyx
246
247
248 #include "QVSpace_moc.cpp"