]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiWrap.cpp
14bf05c06e182afad058f78b2e7a75b705d426da
[lyx.git] / src / frontends / qt4 / GuiWrap.cpp
1 /**
2  * \file GuiWrap.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Spitzmüller
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiWrap.h"
14
15 #include "LengthCombo.h"
16 #include "qt_helpers.h"
17 #include "Validator.h"
18 #include "FuncRequest.h"
19
20 #include "insets/InsetWrap.h"
21
22 #include "support/gettext.h"
23 #include "support/lstrings.h"
24
25 #include <QLineEdit>
26 #include <QCloseEvent>
27 #include <QPushButton>
28
29 using namespace std;
30
31 namespace lyx {
32 namespace frontend {
33
34 GuiWrap::GuiWrap(GuiView & lv)
35         : GuiDialog(lv, "wrap", qt_("Wrap Float Settings"))
36 {
37         setupUi(this);
38
39         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
40         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
41         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
42         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
43
44         connect(widthED, SIGNAL(textChanged(QString)),
45                 this, SLOT(change_adaptor()));
46         connect(widthUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
47                 this, SLOT(change_adaptor()));
48         connect(valignCO, SIGNAL(highlighted(QString)),
49                 this, SLOT(change_adaptor()));
50         connect(overhangCB, SIGNAL(stateChanged(int)),
51                 this, SLOT(change_adaptor()));
52         connect(overhangED, SIGNAL(textChanged(QString)),
53                 this, SLOT(change_adaptor()));
54         connect(overhangUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
55                 this, SLOT(change_adaptor()));
56         connect(linesCB, SIGNAL(stateChanged(int)),
57                 this, SLOT(change_adaptor()));
58         connect(linesSB, SIGNAL(valueChanged(int)),
59                 this, SLOT(change_adaptor()));
60
61         widthED->setValidator(unsignedLengthValidator(widthED));
62         // FIXME:
63         // overhang can be negative, but the unsignedLengthValidator allows this
64         overhangED->setValidator(unsignedLengthValidator(overhangED));
65
66         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
67         bc().setRestore(restorePB);
68         bc().setOK(okPB);
69         bc().setApply(applyPB);
70         bc().setCancel(closePB);
71
72         bc().addReadOnly(widthED);
73         bc().addReadOnly(widthUnitLC);
74         bc().addReadOnly(valignCO);
75         bc().addReadOnly(overhangCB);
76         bc().addReadOnly(overhangED);
77         bc().addReadOnly(overhangUnitLC);
78         bc().addReadOnly(linesCB);
79         bc().addReadOnly(linesSB);
80
81         // initialize the length validator
82         bc().addCheckedLineEdit(widthED, widthLA);
83         bc().addCheckedLineEdit(overhangED, overhangCB);
84 }
85
86
87 void GuiWrap::change_adaptor()
88 {
89         changed();
90 }
91
92
93 void GuiWrap::applyView()
94 {
95         double const width_value = widthED->text().toDouble();
96         Length::UNIT widthUnit = widthUnitLC->currentLengthItem();
97         if (widthED->text().isEmpty())
98                 widthUnit = Length::UNIT_NONE;
99         double const overhang_value = overhangED->text().toDouble();
100         Length::UNIT overhangUnit = overhangUnitLC->currentLengthItem();
101         if (overhangED->text().isEmpty())
102                 overhangUnit = Length::UNIT_NONE;
103         
104         params_.width = Length(width_value, widthUnit);
105
106         if (overhangCB->checkState() == Qt::Checked)
107                 params_.overhang = Length(overhang_value, overhangUnit);
108         else
109                 // when value is "0" the option is not set in the LaTeX-output
110                 // in InsetWrap.cpp
111                 params_.overhang = Length("0in");
112
113         if (linesCB->checkState() == Qt::Checked)
114                 params_.lines = linesSB->value();
115         else
116                 // when value is "0" the option is not set in the LaTeX-output
117                 // in InsetWrap.cpp
118                 params_.lines = 0;
119
120         switch (valignCO->currentIndex()) {
121         case 0:
122                 params_.placement = "o";
123                 break;
124         case 1:
125                 params_.placement = "i";
126                 break;
127         case 2:
128                 params_.placement = "l";
129                 break;
130         case 3:
131                 params_.placement = "r";
132                 break;
133         }
134 }
135
136
137 void GuiWrap::updateContents()
138 {
139         // 0pt is a legal width now, it yields a
140         // wrapfloat just wide enough for the contents.
141         Length len_w = params_.width;
142         widthED->setText(QString::number(len_w.value()));
143         widthUnitLC->setCurrentItem(len_w.unit());
144
145         Length len_o(params_.overhang);
146         overhangED->setText(QString::number(len_o.value()));
147         overhangUnitLC->setCurrentItem(len_o.unit());
148         if (len_o.value() == 0)
149                 overhangCB->setCheckState(Qt::Unchecked);
150         else
151                 overhangCB->setCheckState(Qt::Checked);
152
153         linesSB->setValue(params_.lines);
154         if (params_.lines == 0)
155                 linesCB->setCheckState(Qt::Unchecked);
156         else
157                 linesCB->setCheckState(Qt::Checked);
158
159         int item = 0;
160         if (params_.placement == "i")
161                 item = 1;
162         else if (params_.placement == "l")
163                 item = 2;
164         else if (params_.placement == "r")
165                 item = 3;
166
167         valignCO->setCurrentIndex(item);
168 }
169
170
171 bool GuiWrap::initialiseParams(string const & data)
172 {
173         InsetWrapMailer::string2params(data, params_);
174         return true;
175 }
176
177
178 void GuiWrap::clearParams()
179 {
180         params_ = InsetWrapParams();
181 }
182
183
184 void GuiWrap::dispatchParams()
185 {
186         string const lfun = InsetWrapMailer::params2string(params_);
187         dispatch(FuncRequest(getLfun(), lfun));
188 }
189
190
191 Dialog * createGuiWrap(GuiView & lv) { return new GuiWrap(lv); }
192
193
194 } // namespace frontend
195 } // namespace lyx
196
197
198 #include "GuiWrap_moc.cpp"