1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or later as published by the Free |
28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
29 | ** the packaging of this file. Please review the following information to |
30 | ** ensure the GNU General Public License version 2.0 requirements will be |
31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
32 | ** |
33 | ** $QT_END_LICENSE$ |
34 | ** |
35 | ****************************************************************************/ |
36 | |
37 | #include "qquickitemgroup_p.h" |
38 | |
39 | #include <QtQuick/private/qquickimplicitsizeitem_p_p.h> |
40 | |
41 | QT_BEGIN_NAMESPACE |
42 | |
43 | QQuickItemGroup::QQuickItemGroup(QQuickItem *parent) |
44 | : QQuickImplicitSizeItem(*(new QQuickImplicitSizeItemPrivate), parent) |
45 | { |
46 | } |
47 | |
48 | QQuickItemGroup::~QQuickItemGroup() |
49 | { |
50 | const auto children = childItems(); |
51 | for (QQuickItem *child : children) |
52 | unwatch(item: child); |
53 | } |
54 | |
55 | void QQuickItemGroup::watch(QQuickItem *item) |
56 | { |
57 | QQuickItemPrivate::get(item)->addItemChangeListener(listener: this, types: QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight); |
58 | } |
59 | |
60 | void QQuickItemGroup::unwatch(QQuickItem *item) |
61 | { |
62 | QQuickItemPrivate::get(item)->removeItemChangeListener(this, types: QQuickItemPrivate::ImplicitWidth | QQuickItemPrivate::ImplicitHeight); |
63 | } |
64 | |
65 | QSizeF QQuickItemGroup::calculateImplicitSize() const |
66 | { |
67 | qreal width = 0; |
68 | qreal height = 0; |
69 | const auto children = childItems(); |
70 | for (QQuickItem *child : children) { |
71 | width = qMax(a: width, b: child->implicitWidth()); |
72 | height = qMax(a: height, b: child->implicitHeight()); |
73 | } |
74 | return QSizeF(width, height); |
75 | } |
76 | |
77 | void QQuickItemGroup::updateImplicitSize() |
78 | { |
79 | QSizeF size = calculateImplicitSize(); |
80 | setImplicitSize(size.width(), size.height()); |
81 | } |
82 | |
83 | void QQuickItemGroup::itemChange(ItemChange change, const ItemChangeData &data) |
84 | { |
85 | QQuickImplicitSizeItem::itemChange(change, data); |
86 | switch (change) { |
87 | case ItemChildAddedChange: |
88 | watch(item: data.item); |
89 | data.item->setSize(QSizeF(width(), height())); |
90 | updateImplicitSize(); |
91 | break; |
92 | case ItemChildRemovedChange: |
93 | unwatch(item: data.item); |
94 | updateImplicitSize(); |
95 | break; |
96 | default: |
97 | break; |
98 | } |
99 | } |
100 | |
101 | void QQuickItemGroup::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) |
102 | { |
103 | QQuickImplicitSizeItem::geometryChanged(newGeometry, oldGeometry); |
104 | |
105 | if (newGeometry.size() != oldGeometry.size()) { |
106 | const auto children = childItems(); |
107 | for (QQuickItem *child : children) |
108 | child->setSize(newGeometry.size()); |
109 | } |
110 | } |
111 | |
112 | void QQuickItemGroup::itemImplicitWidthChanged(QQuickItem *) |
113 | { |
114 | setImplicitWidth(calculateImplicitSize().width()); |
115 | } |
116 | |
117 | void QQuickItemGroup::itemImplicitHeightChanged(QQuickItem *) |
118 | { |
119 | setImplicitHeight(calculateImplicitSize().height()); |
120 | } |
121 | |
122 | QT_END_NAMESPACE |
123 | |