Line | Source Code | Coverage |
---|
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | class QStackedLayoutPrivate : public QLayoutPrivate | - |
6 | { | - |
7 | inline QStackedLayout* q_func() { return static_cast<QStackedLayout *>(q_ptr); } inline const QStackedLayout* q_func() const { return static_cast<const QStackedLayout *>(q_ptr); } friend class QStackedLayout; | - |
8 | public: | - |
9 | QStackedLayoutPrivate() : index(-1), stackingMode(QStackedLayout::StackOne) {} | - |
10 | QList<QLayoutItem *> list; | - |
11 | int index; | - |
12 | QStackedLayout::StackingMode stackingMode; | - |
13 | }; | - |
14 | QStackedLayout::QStackedLayout() | - |
15 | : QLayout(*new QStackedLayoutPrivate, 0, 0) | - |
16 | { | - |
17 | } | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | QStackedLayout::QStackedLayout(QWidget *parent) | - |
26 | : QLayout(*new QStackedLayoutPrivate, 0, parent) | - |
27 | { | - |
28 | } | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | QStackedLayout::QStackedLayout(QLayout *parentLayout) | - |
35 | : QLayout(*new QStackedLayoutPrivate, parentLayout, 0) | - |
36 | { | - |
37 | } | - |
38 | | - |
39 | | - |
40 | | - |
41 | | - |
42 | | - |
43 | QStackedLayout::~QStackedLayout() | - |
44 | { | - |
45 | QStackedLayoutPrivate * const d = d_func(); | - |
46 | qDeleteAll(d->list); | - |
47 | } | - |
48 | int QStackedLayout::addWidget(QWidget *widget) | - |
49 | { | - |
50 | QStackedLayoutPrivate * const d = d_func(); | - |
51 | return insertWidget(d->list.count(), widget); | - |
52 | } | - |
53 | int QStackedLayout::insertWidget(int index, QWidget *widget) | - |
54 | { | - |
55 | QStackedLayoutPrivate * const d = d_func(); | - |
56 | addChildWidget(widget); | - |
57 | index = qMin(index, d->list.count()); | - |
58 | if (index < 0) | - |
59 | index = d->list.count(); | - |
60 | QWidgetItem *wi = QLayoutPrivate::createWidgetItem(this, widget); | - |
61 | d->list.insert(index, wi); | - |
62 | invalidate(); | - |
63 | if (d->index < 0) { | - |
64 | setCurrentIndex(index); | - |
65 | } else { | - |
66 | if (index <= d->index) | - |
67 | ++d->index; | - |
68 | if (d->stackingMode == StackOne) | - |
69 | widget->hide(); | - |
70 | widget->lower(); | - |
71 | } | - |
72 | return index; | - |
73 | } | - |
74 | | - |
75 | | - |
76 | | - |
77 | | - |
78 | QLayoutItem *QStackedLayout::itemAt(int index) const | - |
79 | { | - |
80 | const QStackedLayoutPrivate * const d = d_func(); | - |
81 | return d->list.value(index); | - |
82 | } | - |
83 | | - |
84 | | - |
85 | | - |
86 | | - |
87 | | - |
88 | class QtFriendlyLayoutWidget : public QWidget | - |
89 | { | - |
90 | public: | - |
91 | inline bool wasDeleted() const { return d_ptr->wasDeleted; } | - |
92 | }; | - |
93 | | - |
94 | static bool qt_wasDeleted(const QWidget *w) { return static_cast<const QtFriendlyLayoutWidget*>(w)->wasDeleted(); } | - |
95 | | - |
96 | | - |
97 | | - |
98 | | - |
99 | | - |
100 | QLayoutItem *QStackedLayout::takeAt(int index) | - |
101 | { | - |
102 | QStackedLayoutPrivate * const d = d_func(); | - |
103 | if (index <0 || index >= d->list.size()) | - |
104 | return 0; | - |
105 | QLayoutItem *item = d->list.takeAt(index); | - |
106 | if (index == d->index) { | - |
107 | d->index = -1; | - |
108 | if ( d->list.count() > 0 ) { | - |
109 | int newIndex = (index == d->list.count()) ? index-1 : index; | - |
110 | setCurrentIndex(newIndex); | - |
111 | } else { | - |
112 | currentChanged(-1); | - |
113 | } | - |
114 | } else if (index < d->index) { | - |
115 | --d->index; | - |
116 | } | - |
117 | widgetRemoved(index); | - |
118 | if (item->widget() && !qt_wasDeleted(item->widget())) | - |
119 | item->widget()->hide(); | - |
120 | return item; | - |
121 | } | - |
122 | void QStackedLayout::setCurrentIndex(int index) | - |
123 | { | - |
124 | QStackedLayoutPrivate * const d = d_func(); | - |
125 | QWidget *prev = currentWidget(); | - |
126 | QWidget *next = widget(index); | - |
127 | if (!next || next == prev) evaluated: !next yes Evaluation Count:6 | yes Evaluation Count:635 |
evaluated: next == prev yes Evaluation Count:355 | yes Evaluation Count:280 |
| 6-635 |
128 | return; executed: return; Execution Count:361 | 361 |
129 | | - |
130 | bool reenableUpdates = false; | - |
131 | QWidget *parent = parentWidget(); | - |
132 | | - |
133 | if (parent && parent->updatesEnabled()) { evaluated: parent yes Evaluation Count:278 | yes Evaluation Count:2 |
partially evaluated: parent->updatesEnabled() yes Evaluation Count:278 | no Evaluation Count:0 |
| 0-278 |
134 | reenableUpdates = true; | - |
135 | parent->setUpdatesEnabled(false); | - |
136 | } executed: } Execution Count:278 | 278 |
137 | | - |
138 | QPointer<QWidget*> fw = parent ? parent->window()->focusWidget() : 0; evaluated: parent yes Evaluation Count:278 | yes Evaluation Count:2 |
| 2-278 |
139 | const bool focusWasOnOldPage = fw && (prev && prev->isAncestorOf(fw)); evaluated: fw yes Evaluation Count:24 | yes Evaluation Count:256 |
evaluated: prev yes Evaluation Count:23 | yes Evaluation Count:1 |
evaluated: prev->isAncestorOf(fw) yes Evaluation Count:7 | yes Evaluation Count:16 |
| 1-256 |
140 | | - |
141 | if (prev) { evaluated: prev yes Evaluation Count:43 | yes Evaluation Count:237 |
| 43-237 |
142 | prev->clearFocus(); | - |
143 | if (d->stackingMode == StackOne) partially evaluated: d->stackingMode == StackOne yes Evaluation Count:43 | no Evaluation Count:0 |
| 0-43 |
144 | prev->hide(); executed: prev->hide(); Execution Count:43 | 43 |
145 | } executed: } Execution Count:43 | 43 |
146 | | - |
147 | d->index = index; | - |
148 | next->raise(); | - |
149 | next->show(); | - |
150 | | - |
151 | | - |
152 | | - |
153 | | - |
154 | if (parent) { evaluated: parent yes Evaluation Count:278 | yes Evaluation Count:2 |
| 2-278 |
155 | if (fw && (prev && prev->isAncestorOf(fw)))focusWasOnOldPage) { evaluated: focusWasOnOldPage yes Evaluation Count:7 | yes Evaluation Count:271 |
| 7-271 |
156 | | - |
157 | if (QWidget *nfw = next->focusWidget()) evaluated: QWidget *nfw = next->focusWidget() yes Evaluation Count:3 | yes Evaluation Count:4 |
| 3-4 |
158 | nfw->setFocus(); executed: nfw->setFocus(); Execution Count:3 | 3 |
159 | else { | - |
160 | | - |
161 | if (QWidget *i = fw;) { partially evaluated: QWidget *i = fw yes Evaluation Count:4 | no Evaluation Count:0 |
| 0-4 |
162 | while ((i = i->nextInFocusChain()) != fw) { evaluated: (i = i->nextInFocusChain()) != fw yes Evaluation Count:51 | yes Evaluation Count:1 |
| 1-51 |
163 | if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus) evaluated: ((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus) yes Evaluation Count:16 | yes Evaluation Count:35 |
| 16-35 |
164 | && !i->focusProxy() && i->isVisibleTo(next) && i->isEnabled() evaluated: !i->focusProxy() yes Evaluation Count:14 | yes Evaluation Count:2 |
evaluated: i->isVisibleTo(next) yes Evaluation Count:5 | yes Evaluation Count:9 |
partially evaluated: i->isEnabled() yes Evaluation Count:5 | no Evaluation Count:0 |
| 0-14 |
165 | && next->isAncestorOf(i)) { evaluated: next->isAncestorOf(i) yes Evaluation Count:3 | yes Evaluation Count:2 |
| 2-3 |
166 | i->setFocus(); | - |
167 | break; executed: break; Execution Count:3 | 3 |
168 | } | - |
169 | } executed: } Execution Count:48 | 48 |
170 | | - |
171 | if (i == fw ) evaluated: i == fw yes Evaluation Count:1 | yes Evaluation Count:3 |
| 1-3 |
172 | next->setFocus(); executed: next->setFocus(); Execution Count:1 | 1 |
173 | } executed: } Execution Count:4 | 4 |
174 | } executed: } Execution Count:4 | 4 |
175 | } | - |
176 | } executed: } Execution Count:278 | 278 |
177 | if (reenableUpdates) evaluated: reenableUpdates yes Evaluation Count:278 | yes Evaluation Count:2 |
| 2-278 |
178 | parent->setUpdatesEnabled(true); executed: parent->setUpdatesEnabled(true); Execution Count:278 | 278 |
179 | currentChanged(index); | - |
180 | } executed: } Execution Count:280 | 280 |
181 | | - |
182 | int QStackedLayout::currentIndex() const | - |
183 | { | - |
184 | const QStackedLayoutPrivate * const d = d_func(); | - |
185 | return d->index; | - |
186 | } | - |
187 | void QStackedLayout::setCurrentWidget(QWidget *widget) | - |
188 | { | - |
189 | int index = indexOf(widget); | - |
190 | if (index == -1) { | - |
191 | QMessageLogger("kernel/qstackedlayout.cpp", 363366, __PRETTY_FUNCTION__).warning("QStackedLayout::setCurrentWidget: Widget %p not contained in stack", widget); | - |
192 | return; | - |
193 | } | - |
194 | setCurrentIndex(index); | - |
195 | } | - |
196 | QWidget *QStackedLayout::currentWidget() const | - |
197 | { | - |
198 | const QStackedLayoutPrivate * const d = d_func(); | - |
199 | return d->index >= 0 ? d->list.at(d->index)->widget() : 0; | - |
200 | } | - |
201 | | - |
202 | | - |
203 | | - |
204 | | - |
205 | | - |
206 | | - |
207 | | - |
208 | QWidget *QStackedLayout::widget(int index) const | - |
209 | { | - |
210 | const QStackedLayoutPrivate * const d = d_func(); | - |
211 | if (index < 0 || index >= d->list.size()) | - |
212 | return 0; | - |
213 | return d->list.at(index)->widget(); | - |
214 | } | - |
215 | | - |
216 | | - |
217 | | - |
218 | | - |
219 | | - |
220 | | - |
221 | | - |
222 | int QStackedLayout::count() const | - |
223 | { | - |
224 | const QStackedLayoutPrivate * const d = d_func(); | - |
225 | return d->list.size(); | - |
226 | } | - |
227 | | - |
228 | | - |
229 | | - |
230 | | - |
231 | | - |
232 | void QStackedLayout::addItem(QLayoutItem *item) | - |
233 | { | - |
234 | QWidget *widget = item->widget(); | - |
235 | if (widget) { | - |
236 | addWidget(widget); | - |
237 | delete item; | - |
238 | } else { | - |
239 | QMessageLogger("kernel/qstackedlayout.cpp", 419422, __PRETTY_FUNCTION__).warning("QStackedLayout::addItem: Only widgets can be added"); | - |
240 | } | - |
241 | } | - |
242 | | - |
243 | | - |
244 | | - |
245 | | - |
246 | QSize QStackedLayout::sizeHint() const | - |
247 | { | - |
248 | const QStackedLayoutPrivate * const d = d_func(); | - |
249 | QSize s(0, 0); | - |
250 | int n = d->list.count(); | - |
251 | | - |
252 | for (int i = 0; i < n; ++i) | - |
253 | if (QWidget *widget = d->list.at(i)->widget()) { | - |
254 | QSize ws(widget->sizeHint()); | - |
255 | if (widget->sizePolicy().horizontalPolicy() == QSizePolicy::Ignored) | - |
256 | ws.setWidth(0); | - |
257 | if (widget->sizePolicy().verticalPolicy() == QSizePolicy::Ignored) | - |
258 | ws.setHeight(0); | - |
259 | s = s.expandedTo(ws); | - |
260 | } | - |
261 | return s; | - |
262 | } | - |
263 | | - |
264 | | - |
265 | | - |
266 | | - |
267 | QSize QStackedLayout::minimumSize() const | - |
268 | { | - |
269 | const QStackedLayoutPrivate * const d = d_func(); | - |
270 | QSize s(0, 0); | - |
271 | int n = d->list.count(); | - |
272 | | - |
273 | for (int i = 0; i < n; ++i) | - |
274 | if (QWidget *widget = d->list.at(i)->widget()) | - |
275 | s = s.expandedTo(qSmartMinSize(widget)); | - |
276 | return s; | - |
277 | } | - |
278 | | - |
279 | | - |
280 | | - |
281 | | - |
282 | void QStackedLayout::setGeometry(const QRect &rect) | - |
283 | { | - |
284 | QStackedLayoutPrivate * const d = d_func(); | - |
285 | switch (d->stackingMode) { | - |
286 | case StackOne: | - |
287 | if (QWidget *widget = currentWidget()) | - |
288 | widget->setGeometry(rect); | - |
289 | break; | - |
290 | case StackAll: | - |
291 | if (const int n = d->list.count()) | - |
292 | for (int i = 0; i < n; ++i) | - |
293 | if (QWidget *widget = d->list.at(i)->widget()) | - |
294 | widget->setGeometry(rect); | - |
295 | break; | - |
296 | } | - |
297 | } | - |
298 | | - |
299 | | - |
300 | | - |
301 | | - |
302 | bool QStackedLayout::hasHeightForWidth() const | - |
303 | { | - |
304 | const int n = count(); | - |
305 | | - |
306 | for (int i = 0; i < n; ++i) { | - |
307 | if (QLayoutItem *item = itemAt(i)) { | - |
308 | if (item->hasHeightForWidth()) | - |
309 | return true; | - |
310 | } | - |
311 | } | - |
312 | return false; | - |
313 | } | - |
314 | | - |
315 | | - |
316 | | - |
317 | | - |
318 | int QStackedLayout::heightForWidth(int width) const | - |
319 | { | - |
320 | const int n = count(); | - |
321 | | - |
322 | int hfw = 0; | - |
323 | for (int i = 0; i < n; ++i) { | - |
324 | if (QLayoutItem *item = itemAt(i)) { | - |
325 | if (QWidget *w = item->widget()) | - |
326 | | - |
327 | | - |
328 | | - |
329 | | - |
330 | | - |
331 | | - |
332 | hfw = qMax(hfw, w->heightForWidth(width)); | - |
333 | } | - |
334 | } | - |
335 | hfw = qMax(hfw, minimumSize().height()); | - |
336 | return hfw; | - |
337 | } | - |
338 | QStackedLayout::StackingMode QStackedLayout::stackingMode() const | - |
339 | { | - |
340 | const QStackedLayoutPrivate * const d = d_func(); | - |
341 | return d->stackingMode; | - |
342 | } | - |
343 | | - |
344 | void QStackedLayout::setStackingMode(StackingMode stackingMode) | - |
345 | { | - |
346 | QStackedLayoutPrivate * const d = d_func(); | - |
347 | if (d->stackingMode == stackingMode) | - |
348 | return; | - |
349 | d->stackingMode = stackingMode; | - |
350 | | - |
351 | const int n = d->list.count(); | - |
352 | if (n == 0) | - |
353 | return; | - |
354 | | - |
355 | switch (d->stackingMode) { | - |
356 | case StackOne: | - |
357 | if (const int idx = currentIndex()) | - |
358 | for (int i = 0; i < n; ++i) | - |
359 | if (QWidget *widget = d->list.at(i)->widget()) | - |
360 | widget->setVisible(i == idx); | - |
361 | break; | - |
362 | case StackAll: { | - |
363 | QRect geometry; | - |
364 | if (const QWidget *widget = currentWidget()) | - |
365 | geometry = widget->geometry(); | - |
366 | for (int i = 0; i < n; ++i) | - |
367 | if (QWidget *widget = d->list.at(i)->widget()) { | - |
368 | if (!geometry.isNull()) | - |
369 | widget->setGeometry(geometry); | - |
370 | widget->setVisible(true); | - |
371 | } | - |
372 | } | - |
373 | break; | - |
374 | } | - |
375 | } | - |
376 | | - |
377 | | - |
378 | | - |
| | |