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