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