| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquickitemgroup_p.h" |
| 5 | |
| 6 | #include <QtQuick/private/qquickimplicitsizeitem_p_p.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | QQuickItemGroup::QQuickItemGroup(QQuickItem *parent) |
| 11 | : QQuickImplicitSizeItem(*(new QQuickImplicitSizeItemPrivate), parent) |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | QQuickItemGroup::~QQuickItemGroup() |
| 16 | { |
| 17 | const auto children = childItems(); |
| 18 | for (QQuickItem *child : children) |
| 19 | unwatch(item: child); |
| 20 | } |
| 21 | |
| 22 | void QQuickItemGroup::watch(QQuickItem *item) |
| 23 | { |
| 24 | QQuickItemPrivate::get(item)->addItemChangeListener(listener: this, types: QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight); |
| 25 | } |
| 26 | |
| 27 | void QQuickItemGroup::unwatch(QQuickItem *item) |
| 28 | { |
| 29 | QQuickItemPrivate::get(item)->removeItemChangeListener(this, types: QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight); |
| 30 | } |
| 31 | |
| 32 | QSizeF QQuickItemGroup::calculateImplicitSize() const |
| 33 | { |
| 34 | qreal width = 0; |
| 35 | qreal height = 0; |
| 36 | const auto children = childItems(); |
| 37 | for (QQuickItem *child : children) { |
| 38 | width = qMax(a: width, b: child->implicitWidth()); |
| 39 | height = qMax(a: height, b: child->implicitHeight()); |
| 40 | } |
| 41 | return QSizeF(width, height); |
| 42 | } |
| 43 | |
| 44 | void QQuickItemGroup::updateImplicitSize() |
| 45 | { |
| 46 | QSizeF size = calculateImplicitSize(); |
| 47 | setImplicitSize(size.width(), size.height()); |
| 48 | } |
| 49 | |
| 50 | void QQuickItemGroup::itemChange(ItemChange change, const ItemChangeData &data) |
| 51 | { |
| 52 | QQuickImplicitSizeItem::itemChange(change, data); |
| 53 | switch (change) { |
| 54 | case ItemChildAddedChange: |
| 55 | watch(item: data.item); |
| 56 | data.item->setSize(QSizeF(width(), height())); |
| 57 | updateImplicitSize(); |
| 58 | break; |
| 59 | case ItemChildRemovedChange: |
| 60 | unwatch(item: data.item); |
| 61 | updateImplicitSize(); |
| 62 | break; |
| 63 | default: |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void QQuickItemGroup::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) |
| 69 | { |
| 70 | QQuickImplicitSizeItem::geometryChange(newGeometry, oldGeometry); |
| 71 | |
| 72 | if (newGeometry.size() != oldGeometry.size()) { |
| 73 | const auto children = childItems(); |
| 74 | for (QQuickItem *child : children) |
| 75 | child->setSize(newGeometry.size()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void QQuickItemGroup::itemImplicitWidthChanged(QQuickItem *) |
| 80 | { |
| 81 | setImplicitWidth(calculateImplicitSize().width()); |
| 82 | } |
| 83 | |
| 84 | void QQuickItemGroup::itemImplicitHeightChanged(QQuickItem *) |
| 85 | { |
| 86 | setImplicitHeight(calculateImplicitSize().height()); |
| 87 | } |
| 88 | |
| 89 | QT_END_NAMESPACE |
| 90 | |
| 91 | #include "moc_qquickitemgroup_p.cpp" |
| 92 | |