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