]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiVSpace.cpp
Fix the tab ordering of GuiDocument components.
[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 "Spacing.h"
25 #include "VSpace.h"
26
27 #include "insets/InsetVSpace.h"
28
29 #include "support/gettext.h"
30 #include "support/lstrings.h"
31
32 #include <QCheckBox>
33 #include <QLineEdit>
34 #include <QPushButton>
35 #include <QValidator>
36
37 using namespace std;
38
39 namespace lyx {
40 namespace frontend {
41
42 GuiVSpace::GuiVSpace(QWidget * parent) : InsetParamsWidget(parent)
43 {
44         setupUi(this);
45
46         connect(valueLE, SIGNAL(textChanged(QString)),
47                 this, SIGNAL(changed()));
48         connect(keepCB, SIGNAL(clicked()),
49                 this, SIGNAL(changed()));
50         connect(unitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
51                 this, SIGNAL(changed()));
52
53         connect(spacingCO, SIGNAL(activated(int)),
54                 this, SLOT(enableCustom(int)));
55
56         valueLE->setValidator(unsignedGlueLengthValidator(valueLE));
57
58         // initialize the length validator
59         addCheckedWidget(valueLE, valueL);
60 }
61
62
63 void GuiVSpace::enableCustom(int selection)
64 {
65         bool const enable = selection == 5;
66         valueLE->setEnabled(enable);
67         unitCO->setEnabled(enable);
68 }
69
70
71 static void setWidgetsFromVSpace(VSpace const & space,
72                           QComboBox * spacing,
73                           QLineEdit * value,
74                           LengthCombo * unit,
75                           QCheckBox * keep)
76 {
77         int item = 0;
78         switch (space.kind()) {
79                 case VSpace::DEFSKIP:   item = 0; break;
80                 case VSpace::SMALLSKIP: item = 1; break;
81                 case VSpace::MEDSKIP:   item = 2; break;
82                 case VSpace::BIGSKIP:   item = 3; break;
83                 case VSpace::VFILL:     item = 4; break;
84                 case VSpace::LENGTH:    item = 5; break;
85         }
86         spacing->setCurrentIndex(item);
87         keep->setChecked(space.keep());
88
89         Length::UNIT const default_unit = Length::defaultUnit();
90         bool const custom_vspace = space.kind() == VSpace::LENGTH;
91         if (custom_vspace) {
92                 value->setEnabled(true);
93                 unit->setEnabled(true);
94                 string length = space.length().asString();
95                 lengthToWidgets(value, unit, length, default_unit);
96         } else {
97                 lengthToWidgets(value, unit, "", default_unit);
98                 value->setEnabled(false);
99                 unit->setEnabled(false);
100         }
101 }
102
103
104 static VSpace setVSpaceFromWidgets(int spacing,
105         QLineEdit * value, LengthCombo * unit, bool keep)
106 {
107         VSpace space;
108
109         switch (spacing) {
110                 case 0: space = VSpace(VSpace::DEFSKIP); break;
111                 case 1: space = VSpace(VSpace::SMALLSKIP); break;
112                 case 2: space = VSpace(VSpace::MEDSKIP); break;
113                 case 3: space = VSpace(VSpace::BIGSKIP); break;
114                 case 4: space = VSpace(VSpace::VFILL); break;
115                 case 5: space = VSpace(GlueLength(widgetsToLength(value, unit))); break;
116         }
117
118         space.setKeep(keep);
119         return space;
120 }
121
122
123 docstring GuiVSpace::dialogToParams() const
124 {
125         // If a vspace choice is "Length" but there's no text in
126         // the input field, do not insert a vspace at all.
127         if (spacingCO->currentIndex() == 5 && valueLE->text().isEmpty())
128                 return docstring();
129
130         VSpace const params = setVSpaceFromWidgets(spacingCO->currentIndex(),
131                         valueLE, unitCO, keepCB->isChecked());
132         return from_ascii(InsetVSpace::params2string(params));
133 }
134
135
136 void GuiVSpace::paramsToDialog(Inset const * inset)
137 {
138         InsetVSpace const * vs = static_cast<InsetVSpace const *>(inset);
139         VSpace const & params = vs->space();
140         setWidgetsFromVSpace(params, spacingCO, valueLE, unitCO, keepCB);
141 }
142
143 } // namespace frontend
144 } // namespace lyx
145
146
147 #include "moc_GuiVSpace.cpp"