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