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