9#include <QtQml/qqmlinfo.h>
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
76static QQuickStackLayoutAttached *attachedStackLayoutObject(QQuickItem *item,
bool create =
false)
78 return static_cast<QQuickStackLayoutAttached*>(
79 qmlAttachedPropertiesObject<QQuickStackLayout>(item, create));
82QQuickStackLayout::QQuickStackLayout(QQuickItem *parent) :
83 QQuickLayout(*
new QQuickStackLayoutPrivate, parent)
88
89
90
91
92
93
94
95int QQuickStackLayout::count()
const
97 Q_D(
const QQuickStackLayout);
102
103
104
105
106
107
108
109
110int QQuickStackLayout::currentIndex()
const
112 Q_D(
const QQuickStackLayout);
113 return d->currentIndex;
116void QQuickStackLayout::setCurrentIndex(
int index)
118 Q_D(QQuickStackLayout);
119 if (index == d->currentIndex)
122 QQuickItem *prev = itemAt(d->currentIndex);
123 QQuickItem *next = itemAt(index);
124 d->currentIndex = index;
125 d->explicitCurrentIndex =
true;
127 prev->setVisible(
false);
129 next->setVisible(
true);
131 if (isComponentComplete()) {
132 rearrange(QSizeF(width(), height()));
133 emit currentIndexChanged();
139 auto stackLayoutAttached = attachedStackLayoutObject(prev);
140 if (stackLayoutAttached)
141 stackLayoutAttached->setIsCurrentItem(
false);
144 auto stackLayoutAttached = attachedStackLayoutObject(next);
145 if (stackLayoutAttached)
146 stackLayoutAttached->setIsCurrentItem(
true);
150void QQuickStackLayout::componentComplete()
152 QQuickLayout::componentComplete();
156 ensureLayoutItemsUpdated(ApplySizeHints);
158 QQuickItem *par = parentItem();
159 if (qobject_cast<QQuickLayout*>(par))
162 rearrange(QSizeF(width(), height()));
165void QQuickStackLayout::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &value)
167 QQuickLayout::itemChange(change, value);
171 if (change == ItemChildRemovedChange) {
172 QQuickItem *item = value.item;
173 auto stackLayoutAttached = attachedStackLayoutObject(item);
174 if (stackLayoutAttached) {
175 stackLayoutAttached->setLayout(
nullptr);
176 stackLayoutAttached->setIndex(-1);
177 stackLayoutAttached->setIsCurrentItem(
false);
179 m_cachedItemSizeHints.remove(item);
180 childItemsChanged(AdjustCurrentIndex);
182 }
else if (change == ItemChildAddedChange) {
188QSizeF QQuickStackLayout::sizeHint(Qt::SizeHint whichSizeHint)
const
190 Q_D(
const QQuickStackLayout);
191 QSizeF &askingFor = m_cachedSizeHints[whichSizeHint];
192 if (!askingFor.isValid()) {
193 QSizeF &minS = m_cachedSizeHints[Qt::MinimumSize];
194 QSizeF &prefS = m_cachedSizeHints[Qt::PreferredSize];
195 QSizeF &maxS = m_cachedSizeHints[Qt::MaximumSize];
199 maxS = QSizeF(std::numeric_limits<qreal>::infinity(), std::numeric_limits<qreal>::infinity());
201 const int count = itemCount();
202 for (
int i = 0; i < count; ++i) {
203 SizeHints &hints = cachedItemSizeHints(i);
204 minS = minS.expandedTo(hints.min());
205 prefS = prefS.expandedTo(hints.pref());
215int QQuickStackLayout::indexOf(QQuickItem *childItem)
const
219 const auto items = childItems();
220 for (QQuickItem *item : items) {
221 if (shouldIgnoreItem(item))
223 if (childItem == item)
231QQuickStackLayoutAttached *QQuickStackLayout::qmlAttachedProperties(QObject *object)
233 return new QQuickStackLayoutAttached(object);
236QQuickItem *QQuickStackLayout::itemAt(
int index)
const
238 const auto items = childItems();
239 for (QQuickItem *item : items) {
240 if (shouldIgnoreItem(item))
249int QQuickStackLayout::itemCount()
const
252 const auto items = childItems();
253 for (QQuickItem *item : items) {
254 if (shouldIgnoreItem(item))
261void QQuickStackLayout::setAlignment(QQuickItem * , Qt::Alignment )
266void QQuickStackLayout::invalidate(QQuickItem *childItem)
269 SizeHints &hints = m_cachedItemSizeHints[childItem];
270 hints.min() = QSizeF();
271 hints.pref() = QSizeF();
272 hints.max() = QSizeF();
275 for (
int i = 0; i < Qt::NSizeHints; ++i)
276 m_cachedSizeHints[i] = QSizeF();
277 QQuickLayout::invalidate(
this);
279 if (QQuickLayout *parentLayout = qobject_cast<QQuickLayout *>(parentItem()))
280 parentLayout->invalidate(
this);
283void QQuickStackLayout::itemIgnoredChanged(QQuickItem *childItem)
290 invalidate(childItem);
292 if (isItemIgnored(childItem)) {
296 if (QQuickStackLayoutAttached *attached = attachedStackLayoutObject(childItem)) {
297 attached->setIndex(-1);
298 attached->setIsCurrentItem(
false);
300 m_cachedItemSizeHints.remove(childItem);
303 childItemsChanged(AdjustCurrentIndex);
306void QQuickStackLayout::childItemsChanged(AdjustCurrentIndexPolicy adjustCurrentIndexPolicy)
308 Q_D(QQuickStackLayout);
309 const int count = itemCount();
310 const int oldIndex = d->currentIndex;
311 if (!d->explicitCurrentIndex)
312 d->currentIndex = (count > 0 ? 0 : -1);
314 if (adjustCurrentIndexPolicy == AdjustCurrentIndex) {
316
317
318
319
320
321
322
323
324
325
326 if (d->currentIndex < d->count) {
327 for (
int i = 0; i < count; ++i) {
328 QQuickItem *child = itemAt(i);
329 if (child->isVisible()) {
336 if (d->currentIndex != oldIndex)
337 emit currentIndexChanged();
339 if (count != d->count) {
343 for (
int i = 0; i < count; ++i) {
344 QQuickItem *child = itemAt(i);
346 child->setVisible(d->currentIndex == i);
348 auto stackLayoutAttached = attachedStackLayoutObject(child);
349 if (stackLayoutAttached) {
350 stackLayoutAttached->setLayout(
this);
351 stackLayoutAttached->setIndex(i);
352 stackLayoutAttached->setIsCurrentItem(d->currentIndex == i);
357QQuickStackLayout::SizeHints &QQuickStackLayout::cachedItemSizeHints(
int index)
const
359 QQuickItem *item = itemAt(index);
361 SizeHints &hints = m_cachedItemSizeHints[item];
362 if (!hints.min().isValid())
363 QQuickStackLayout::collectItemSizeHints(item, hints.array);
368void QQuickStackLayout::rearrange(
const QSizeF &newSize)
370 Q_D(QQuickStackLayout);
371 if (newSize.isNull() || !newSize.isValid())
374 qCDebug(lcQuickLayouts) <<
"QQuickStackLayout::rearrange";
376 if (d->currentIndex == -1 || d->currentIndex >= m_cachedItemSizeHints.size())
378 QQuickStackLayout::SizeHints &hints = cachedItemSizeHints(d->currentIndex);
379 QQuickItem *item = itemAt(d->currentIndex);
381 item->setPosition(QPointF(0,0));
382 const QSizeF oldSize(item->width(), item->height());
383 const QSizeF effectiveNewSize = newSize.expandedTo(hints.min()).boundedTo(hints.max());
384 item->setSize(effectiveNewSize);
385 if (effectiveNewSize == oldSize)
387 QQuickLayout::rearrange(newSize);
390void QQuickStackLayout::setStretchFactor(QQuickItem * ,
int , Qt::Orientation )
394void QQuickStackLayout::collectItemSizeHints(QQuickItem *item, QSizeF *sizeHints)
396 QQuickLayoutAttached *info =
nullptr;
397 QQuickLayout::effectiveSizeHints_helper(item, sizeHints, &info,
true);
400 if (info->isFillWidthSet() && !info->fillWidth()) {
401 const qreal pref = sizeHints[Qt::PreferredSize].width();
402 sizeHints[Qt::MinimumSize].setWidth(pref);
403 sizeHints[Qt::MaximumSize].setWidth(pref);
406 if (info->isFillHeightSet() && !info->fillHeight()) {
407 const qreal pref = sizeHints[Qt::PreferredSize].height();
408 sizeHints[Qt::MinimumSize].setHeight(pref);
409 sizeHints[Qt::MaximumSize].setHeight(pref);
413bool QQuickStackLayout::shouldIgnoreItem(QQuickItem *item)
const
418 return isItemIgnored(item) || QQuickItemPrivate::get(item)->isTransparentForPositioner();
421void QQuickStackLayout::itemSiblingOrderChanged(QQuickItem *)
425 childItemsChanged(AdjustCurrentIndex);
429QQuickStackLayoutAttached::QQuickStackLayoutAttached(QObject *object)
431 auto item = qobject_cast<QQuickItem*>(object);
433 qmlWarning(object) <<
"StackLayout attached property must be attached to an object deriving from Item";
437 auto stackLayout = qobject_cast<QQuickStackLayout*>(item->parentItem());
444 if (!stackLayout->isComponentComplete()) {
452 const int index = stackLayout->indexOf(item);
453 setLayout(stackLayout);
455 setIsCurrentItem(index >= 0 && stackLayout->currentIndex() == index);
460 stackLayout->childItemsChanged();
464
465
466
467
468
469
470
471
472
473
474int QQuickStackLayoutAttached::index()
const
479void QQuickStackLayoutAttached::setIndex(
int index)
481 if (index == m_index)
489
490
491
492
493
494
495
496
497
498
499bool QQuickStackLayoutAttached::isCurrentItem()
const
501 return m_isCurrentItem;
504void QQuickStackLayoutAttached::setIsCurrentItem(
bool isCurrentItem)
506 if (isCurrentItem == m_isCurrentItem)
509 m_isCurrentItem = isCurrentItem;
510 emit isCurrentItemChanged();
514
515
516
517
518
519
520
521
522
523
524QQuickStackLayout *QQuickStackLayoutAttached::layout()
const
529void QQuickStackLayoutAttached::setLayout(QQuickStackLayout *layout)
531 if (layout == m_layout)
535 emit layoutChanged();
540#include "moc_qquickstacklayout_p.cpp"