]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/FloatPlacement.cpp
Some cleanup for the Floating class.
[lyx.git] / src / frontends / qt4 / FloatPlacement.cpp
1 /**
2  * \file FloatPlacement.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "FloatPlacement.h"
15 #include "qt_helpers.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "FloatList.h"
20
21 #include "insets/InsetFloat.h"
22 #include "support/lstrings.h"
23
24 using namespace std;
25 using namespace lyx::support;
26
27
28 namespace lyx {
29
30 namespace frontend {
31
32 FloatPlacement::FloatPlacement(bool show_options, QWidget * parent)
33         : InsetParamsWidget(parent), float_list_(0)
34 {
35         setupUi(this);
36
37         connect(floatTypeCO, SIGNAL(activated(int)), this, SLOT(changedSlot()));
38         connect(topCB, SIGNAL(clicked()), this, SLOT(changedSlot()));
39         connect(bottomCB, SIGNAL(clicked()), this, SLOT(changedSlot()));
40         connect(pageCB, SIGNAL(clicked()), this, SLOT(changedSlot()));
41         connect(herepossiblyCB, SIGNAL(clicked()), this, SLOT(changedSlot()));
42         connect(heredefinitelyCB, SIGNAL(clicked()), this, SLOT(changedSlot()));
43         connect(ignoreCB, SIGNAL(clicked()), this, SLOT(changedSlot()));
44         connect(spanCB, SIGNAL(clicked()), this, SLOT(changedSlot()));
45         connect(sidewaysCB, SIGNAL(clicked()), this, SLOT(changedSlot()));
46
47         floatTypeTitle->setVisible(show_options);
48         floatTypeCO->setVisible(show_options);
49         spanCB->setVisible(show_options);
50         sidewaysCB->setVisible(show_options);
51 }
52
53
54 docstring FloatPlacement::dialogToParams() const
55 {
56         InsetFloatParams params;
57         params.type = fromqstr(floatTypeCO->itemData(floatTypeCO->currentIndex()).toString());
58         params.placement = get(params.wide, params.sideways);
59         return from_ascii(InsetFloat::params2string(params));
60 }
61
62
63 void FloatPlacement::useWide()
64 {
65         spanCB->show();
66 }
67
68
69 void FloatPlacement::useSideways()
70 {
71         sidewaysCB->show();
72 }
73
74
75 void FloatPlacement::set(string const & placement)
76 {
77         bool def_placement = false;
78         bool top = false;
79         bool bottom = false;
80         bool page = false;
81         bool here = false;
82         bool force = false;
83         bool here_definitely = false;
84
85         if (placement.empty()) {
86                 def_placement = true;
87         } else if (contains(placement, 'H')) {
88                 here_definitely = true;
89         } else {
90                 if (contains(placement, '!')) {
91                         force = true;
92                 }
93                 if (contains(placement, 't')) {
94                         top = true;
95                 }
96                 if (contains(placement, 'b')) {
97                         bottom = true;
98                 }
99                 if (contains(placement, 'p')) {
100                         page = true;
101                 }
102                 if (contains(placement, 'h')) {
103                         here = true;
104                 }
105         }
106
107         defaultsCB->setChecked(def_placement);
108         topCB->setChecked(top);
109         bottomCB->setChecked(bottom);
110         pageCB->setChecked(page);
111         herepossiblyCB->setChecked(here);
112         ignoreCB->setChecked(force);
113         heredefinitelyCB->setChecked(here_definitely);
114         checkAllowed();
115 }
116
117
118 void FloatPlacement::initFloatTypeCO(FloatList const & floats)
119 {
120         if (float_list_ == &floats)
121                 return;
122
123         float_list_ = &floats;
124         floatTypeCO->clear();
125         FloatList::const_iterator it = floats.begin();
126         FloatList::const_iterator const end = floats.end();
127         for (; it != end; ++it) {
128                 floatTypeCO->addItem(qt_(it->second.name()),
129                                      toqstr(it->second.floattype()));
130         }
131 }
132
133
134 void FloatPlacement::paramsToDialog(Inset const * inset)
135 {
136         InsetFloat const * fl = static_cast<InsetFloat const *>(inset);
137         InsetFloatParams const & params = fl->params();
138
139         BufferParams const & bp = fl->buffer().params();
140         initFloatTypeCO(bp.documentClass().floats());
141
142         int const item = floatTypeCO->findData(toqstr(params.type));
143         floatTypeCO->setCurrentIndex(item);
144
145         set(params.placement);
146
147         standardfloat_ = (params.type == "figure"
148                 || params.type == "table");
149
150         if (params.wide) {
151                 herepossiblyCB->setChecked(false);
152                 heredefinitelyCB->setChecked(false);
153                 bottomCB->setChecked(false);
154         }
155
156         spanCB->setChecked(params.wide);
157         sidewaysCB->setChecked(params.sideways);
158         // the package rotfloat only has *-versions for figure and table
159         spanCB->setEnabled(!params.sideways || standardfloat_);
160         checkAllowed();
161 }
162
163
164 string const FloatPlacement::get(bool & wide, bool & sideways) const
165 {
166         wide = spanCB->isChecked();
167         sideways = sidewaysCB->isChecked();
168
169         return get();
170 }
171
172
173 string const FloatPlacement::get() const
174 {
175         string placement;
176
177         if (defaultsCB->isChecked())
178                 return placement;
179
180         if (heredefinitelyCB->isChecked()) {
181                 placement += 'H';
182         } else {
183                 if (ignoreCB->isChecked()) {
184                         placement += '!';
185                 }
186                 if (topCB->isChecked()) {
187                         placement += 't';
188                 }
189                 if (bottomCB->isChecked()) {
190                         placement += 'b';
191                 }
192                 if (pageCB->isChecked()) {
193                         placement += 'p';
194                 }
195                 if (herepossiblyCB->isChecked()) {
196                         placement += 'h';
197                 }
198         }
199         return placement;
200 }
201
202
203 void FloatPlacement::on_defaultsCB_stateChanged(int state)
204 {
205         checkAllowed();
206         if (state == Qt::Checked)
207                 return;
208         if (topCB->isChecked() || bottomCB->isChecked()
209            || pageCB->isChecked() || herepossiblyCB->isChecked()
210            || heredefinitelyCB->isChecked() || ignoreCB->isChecked())
211                 changed();
212 }
213
214
215 void FloatPlacement::changedSlot()
216 {
217         checkAllowed();
218         changed();
219 }
220
221
222 void FloatPlacement::checkAllowed()
223 {
224         bool const defaults = defaultsCB->isChecked();
225         bool const ignore = topCB->isChecked() || bottomCB->isChecked()
226                       || pageCB->isChecked() || herepossiblyCB->isChecked();
227         bool const heredefinitely = heredefinitelyCB->isChecked();
228
229         // float or document dialog?
230         if (spanCB->isVisible()) {
231                 bool const span = spanCB->isChecked();
232                 bool const sideways = sidewaysCB->isChecked();
233                 defaultsCB->setEnabled(!sideways);
234                 topCB->setEnabled(!sideways && !defaults && !heredefinitely);
235                 bottomCB->setEnabled(!sideways && !defaults && !span && !heredefinitely);
236                 pageCB->setEnabled(!sideways && !defaults && !heredefinitely);
237                 herepossiblyCB->setEnabled(!sideways && !defaults && !span && !heredefinitely);
238                 heredefinitelyCB->setEnabled(!sideways && !defaults && !span);
239                 ignoreCB->setEnabled(!sideways && !defaults && ignore && !heredefinitely);
240                 spanCB->setEnabled(!sideways || standardfloat_);
241         } else {
242                 topCB->setEnabled(!defaults && !heredefinitely);
243                 bottomCB->setEnabled(!defaults && !heredefinitely);
244                 pageCB->setEnabled(!defaults && !heredefinitely);
245                 herepossiblyCB->setEnabled(!defaults && !heredefinitely);
246                 heredefinitelyCB->setEnabled(!defaults);
247                 ignoreCB->setEnabled(!defaults && ignore && !heredefinitely);
248         }
249 }
250
251 } // namespace frontend
252 } // namespace lyx
253
254 #include "moc_FloatPlacement.cpp"