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