]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QVSpace.C
Extracted from r14281
[lyx.git] / src / frontends / qt3 / QVSpace.C
1 /**
2  * \file QVSpace.C
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 "QVSpaceDialog.h"
20 #include "Qt2BC.h"
21
22 #include "checkedwidgets.h"
23 #include "lengthcombo.h"
24 #include "qt_helpers.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/helper_funcs.h"
32
33 #include "support/lstrings.h"
34
35 #include <qcombobox.h>
36 #include <qlineedit.h>
37 #include <qcheckbox.h>
38 #include <qpushbutton.h>
39
40
41 using std::string;
42
43 namespace lyx {
44 namespace frontend {
45
46 namespace {
47
48 void setWidgetsFromVSpace(VSpace const & space,
49                           QComboBox * spacing,
50                           QLineEdit * value,
51                           LengthCombo * unit,
52                           QCheckBox * keep)
53 {
54         int item = 0;
55         switch (space.kind()) {
56         case VSpace::DEFSKIP:
57                 item = 0;
58                 break;
59         case VSpace::SMALLSKIP:
60                 item = 1;
61                 break;
62         case VSpace::MEDSKIP:
63                 item = 2;
64                 break;
65         case VSpace::BIGSKIP:
66                 item = 3;
67                 break;
68         case VSpace::VFILL:
69                 item = 4;
70                 break;
71         case VSpace::LENGTH:
72                 item = 5;
73                 break;
74         }
75         spacing->setCurrentItem(item);
76         keep->setChecked(space.keep());
77
78         LyXLength::UNIT default_unit =
79                         (lyxrc.default_papersize > 3) ? LyXLength::CM : LyXLength::IN;
80         bool const custom_vspace = space.kind() == VSpace::LENGTH;
81         if (custom_vspace) {
82                 value->setEnabled(true);
83                 unit->setEnabled(true);
84                 string length = space.length().asString();
85                 lengthToWidgets(value, unit, length, default_unit);
86         } else {
87                 lengthToWidgets(value, unit, "", default_unit);
88                 value->setEnabled(false);
89                 unit->setEnabled(false);
90         }
91 }
92
93
94 VSpace setVSpaceFromWidgets(int spacing,
95                             QLineEdit * value,
96                             LengthCombo * unit,
97                             bool keep)
98 {
99         VSpace space;
100
101         switch (spacing) {
102         case 0:
103                 space = VSpace(VSpace::DEFSKIP);
104                 break;
105         case 1:
106                 space = VSpace(VSpace::SMALLSKIP);
107                 break;
108         case 2:
109                 space = VSpace(VSpace::MEDSKIP);
110                 break;
111         case 3:
112                 space = VSpace(VSpace::BIGSKIP);
113                 break;
114         case 4:
115                 space = VSpace(VSpace::VFILL);
116                 break;
117         case 5:
118                 space = VSpace(LyXGlueLength(
119                                       widgetsToLength(value, unit)));
120                 break;
121         }
122
123         space.setKeep(keep);
124         return space;
125 }
126
127 } // namespace anon
128
129
130 typedef QController<ControlVSpace, QView<QVSpaceDialog> > base_class;
131
132 QVSpace::QVSpace(Dialog & parent)
133         : base_class(parent, _("Vertical Space Settings"))
134 {}
135
136
137 void QVSpace::build_dialog()
138 {
139         // the tabbed folder
140         dialog_.reset(new QVSpaceDialog(this));
141
142         // Manage the ok, apply, restore and cancel/close buttons
143         bcview().setOK(dialog_->okPB);
144         bcview().setApply(dialog_->applyPB);
145         bcview().setCancel(dialog_->closePB);
146
147         // disable for read-only documents
148         bcview().addReadOnly(dialog_->spacingCO);
149         bcview().addReadOnly(dialog_->valueLE);
150         bcview().addReadOnly(dialog_->unitCO);
151         bcview().addReadOnly(dialog_->keepCB);
152
153         // initialize the length validator
154         addCheckedLineEdit(bcview(), dialog_->valueLE, dialog_->valueL);
155
156         // remove the %-items from the unit choice
157         dialog_->unitCO->noPercents();
158 }
159
160
161 void QVSpace::apply()
162 {
163         // spacing
164         // If a vspace choice is "Length" but there's no text in
165         // the input field, do not insert a vspace at all.
166         if (dialog_->spacingCO->currentItem() == 5
167             && dialog_->valueLE->text().isEmpty())
168                 return;
169
170         VSpace const space =
171                 setVSpaceFromWidgets(dialog_->spacingCO->currentItem(),
172                                      dialog_->valueLE,
173                                      dialog_->unitCO,
174                                      dialog_->keepCB->isChecked());
175
176         controller().params() = space;
177 }
178
179
180 void QVSpace::update_contents()
181 {
182         setWidgetsFromVSpace(controller().params(),
183                              dialog_->spacingCO,
184                              dialog_->valueLE,
185                              dialog_->unitCO,
186                              dialog_->keepCB);
187 }
188
189 } // namespace frontend
190 } // namespace lyx