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