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