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