]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiWrap.cpp
Support for command argument placement after the workarea argument
[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 "support/gettext.h"
21 #include "support/lstrings.h"
22
23 #include <QLineEdit>
24 #include <QPushButton>
25
26 using namespace std;
27
28 namespace lyx {
29 namespace frontend {
30
31 GuiWrap::GuiWrap(GuiView & lv)
32         : GuiDialog(lv, "wrap", qt_("Wrap Float Settings"))
33 {
34         setupUi(this);
35
36         connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
37         connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
38         connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
39         connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
40
41         connect(valignCO, SIGNAL(highlighted(QString)),
42                 this, SLOT(change_adaptor()));
43         connect(floatCB, SIGNAL(stateChanged(int)),
44                 this, SLOT(change_adaptor()));
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(overhangCB, SIGNAL(stateChanged(int)),
50                 this, SLOT(change_adaptor()));
51         connect(overhangED, SIGNAL(textChanged(QString)),
52                 this, SLOT(change_adaptor()));
53         connect(overhangUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
54                 this, SLOT(change_adaptor()));
55         connect(linesCB, SIGNAL(stateChanged(int)),
56                 this, SLOT(change_adaptor()));
57         connect(linesSB, SIGNAL(valueChanged(int)),
58                 this, SLOT(change_adaptor()));
59
60         widthED->setValidator(unsignedLengthValidator(widthED));
61         // FIXME:
62         // overhang can be negative, but the unsignedLengthValidator allows this
63         overhangED->setValidator(unsignedLengthValidator(overhangED));
64
65         bc().setPolicy(ButtonPolicy::NoRepeatedApplyReadOnlyPolicy);
66         bc().setRestore(restorePB);
67         bc().setOK(okPB);
68         bc().setApply(applyPB);
69         bc().setCancel(closePB);
70
71         bc().addReadOnly(valignCO);
72         bc().addReadOnly(floatCB);
73         bc().addReadOnly(widthED);
74         bc().addReadOnly(widthUnitLC);
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         bool floatOn = false;
121         if (floatCB->checkState() == Qt::Checked)
122                 floatOn = true;
123         switch (valignCO->currentIndex()) {
124         case 0:
125                 if (floatOn)
126                         params_.placement = "O";
127                 else
128                         params_.placement = "o";
129                 break;
130         case 1:
131                 if (floatOn)
132                         params_.placement = "I";
133                 else
134                         params_.placement = "i";
135                 break;
136         case 2:
137                 if (floatOn)
138                         params_.placement = "L";
139                 else
140                         params_.placement = "l";
141                 break;
142         case 3:
143                 if (floatOn)
144                         params_.placement = "R";
145                 else
146                         params_.placement = "r";
147                 break;
148         }
149 }
150
151
152 void GuiWrap::paramsToDialog(InsetWrapParams const & params)
153 {
154         // 0pt is a legal width now, it yields a
155         // wrapfloat just wide enough for the contents.
156         Length len_w = params.width;
157         widthED->setText(QString::number(len_w.value()));
158         widthUnitLC->setCurrentItem(len_w.unit());
159
160         Length len_o(params.overhang);
161         overhangED->setText(QString::number(len_o.value()));
162         overhangUnitLC->setCurrentItem(len_o.unit());
163         if (len_o.value() == 0)
164                 overhangCB->setCheckState(Qt::Unchecked);
165         else
166                 overhangCB->setCheckState(Qt::Checked);
167
168         linesSB->setValue(params.lines);
169         if (params.lines == 0)
170                 linesCB->setCheckState(Qt::Unchecked);
171         else
172                 linesCB->setCheckState(Qt::Checked);
173
174         int item = 0;
175         if (params.placement == "i" || params.placement == "I")
176                 item = 1;
177         else if (params.placement == "l" || params.placement == "L")
178                 item = 2;
179         else if (params.placement == "r" || params.placement == "R")
180                 item = 3;
181
182         valignCO->setCurrentIndex(item);
183
184         if (params.placement == "O" || params.placement == "I"
185                 || params.placement == "L" || params.placement == "R")
186                 floatCB->setCheckState(Qt::Checked);
187 }
188
189
190 bool GuiWrap::initialiseParams(string const & data)
191 {
192         InsetWrap::string2params(data, params_);
193         paramsToDialog(params_);
194         return true;
195 }
196
197
198 void GuiWrap::clearParams()
199 {
200         params_ = InsetWrapParams();
201 }
202
203
204 void GuiWrap::dispatchParams()
205 {
206         string const lfun = InsetWrap::params2string(params_);
207         dispatch(FuncRequest(getLfun(), lfun));
208 }
209
210
211 Dialog * createGuiWrap(GuiView & lv) { return new GuiWrap(lv); }
212
213
214 } // namespace frontend
215 } // namespace lyx
216
217
218 #include "moc_GuiWrap.cpp"