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