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