1// Copyright (C) 2023 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#ifndef QQUICKLAYOUTITEMPROXY_P_H
5#define QQUICKLAYOUTITEMPROXY_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <private/qquickitem_p.h>
19#include <private/qquickrectangle_p.h>
20
21QT_BEGIN_NAMESPACE
22
23class QQuickLayoutItemProxyAttachedData;
24class QQuickLayoutItemProxyPrivate;
25class QQuickLayoutItemProxy : public QQuickItem
26{
27 Q_OBJECT
28
29 Q_PROPERTY(QQuickItem *target READ target WRITE setTarget NOTIFY targetChanged)
30 QML_NAMED_ELEMENT(LayoutItemProxy)
31 QML_ADDED_IN_VERSION(6, 6)
32
33public:
34 QQuickLayoutItemProxy(QQuickItem *parent = nullptr);
35 ~QQuickLayoutItemProxy() override;
36
37 void geometryChange(const QRectF &newGeom, const QRectF &oldGeom) override;
38 void itemChange(ItemChange c, const ItemChangeData &d) override;
39
40 QQuickItem *target() const;
41 void setTarget(QQuickItem *newTarget);
42 Q_INVOKABLE QQuickItem *effectiveTarget() const;
43 void clearTarget();
44
45 void maybeTakeControl();
46public slots:
47 void updatePos();
48
49private slots:
50 // We define some slots to react to changes to the Layout attached properties.
51 // They are all named following the same scheme, which allows us to use a macro.
52#define propertyForwarding(Property) \
53 void target##Property##Changed(); \
54 void proxy##Property##Changed();
55
56 propertyForwarding(MinimumWidth)
57 propertyForwarding(MinimumHeight)
58 propertyForwarding(PreferredWidth)
59 propertyForwarding(PreferredHeight)
60 propertyForwarding(MaximumWidth)
61 propertyForwarding(MaximumHeight)
62 propertyForwarding(FillWidth)
63 propertyForwarding(FillHeight)
64 propertyForwarding(Alignment)
65 propertyForwarding(HorizontalStretchFactor)
66 propertyForwarding(VerticalStretchFactor)
67 propertyForwarding(Margins)
68 propertyForwarding(LeftMargin)
69 propertyForwarding(TopMargin)
70 propertyForwarding(RightMargin)
71 propertyForwarding(BottomMargin)
72#undef propertyForwarding
73
74signals:
75 void targetChanged();
76
77private:
78 Q_DECLARE_PRIVATE(QQuickLayoutItemProxy)
79};
80
81class QQuickLayoutItemProxyPrivate : public QQuickItemPrivate
82{
83 Q_DECLARE_PUBLIC(QQuickLayoutItemProxy)
84
85public:
86 QQuickLayoutItemProxyPrivate();
87
88 // the target of the LayoutItem
89 QQuickItem *target = nullptr;
90
91 // These values are required to know why the Layout property of the proxy changed
92 // If it changed because the target changed we should keep the connection valid
93 // If a Layout property change is not invoked by the target, it was set
94 // explicitly by the application developer and we should disconnect the connection
95 // between target and proxy for this property.
96 unsigned m_expectProxyMinimumWidthChange : 1;
97 unsigned m_expectProxyMinimumHeightChange : 1;
98 unsigned m_expectProxyPreferredWidthChange : 1;
99 unsigned m_expectProxyPreferredHeightChange : 1;
100 unsigned m_expectProxyMaximumWidthChange : 1;
101 unsigned m_expectProxyMaximumHeightChange : 1;
102 unsigned m_expectProxyFillWidthChange : 1;
103 unsigned m_expectProxyFillHeightChange : 1;
104 unsigned m_expectProxyAlignmentChange : 1;
105 unsigned m_expectProxyHorizontalStretchFactorChange : 1;
106 unsigned m_expectProxyVerticalStretchFactorChange : 1;
107 unsigned m_expectProxyMarginsChange : 1;
108 unsigned m_expectProxyLeftMarginChange : 1;
109 unsigned m_expectProxyTopMarginChange : 1;
110 unsigned m_expectProxyRightMarginChange : 1;
111 unsigned m_expectProxyBottomMarginChange : 1;
112
113 friend class QQuickLayoutItemProxy;
114};
115
116class QQuickLayoutItemProxyAttachedData : public QObject
117{
118 Q_OBJECT
119
120 QML_ANONYMOUS
121 Q_PROPERTY(bool proxyHasControl READ proxyHasControl NOTIFY controllingProxyChanged)
122 Q_PROPERTY(QQuickLayoutItemProxy* controllingProxy READ getControllingProxy NOTIFY controllingProxyChanged)
123 Q_PROPERTY(QQmlListProperty<QQuickLayoutItemProxy> proxies READ getProxies NOTIFY proxiesChanged)
124
125public:
126 QQuickLayoutItemProxyAttachedData(QObject *parent);
127 ~QQuickLayoutItemProxyAttachedData() override;
128 void registerProxy(QQuickLayoutItemProxy *proxy);
129 void releaseProxy(QQuickLayoutItemProxy *proxy);
130 bool takeControl(QQuickLayoutItemProxy *proxy);
131 void releaseControl(QQuickLayoutItemProxy *proxy);
132 QQuickLayoutItemProxy *getControllingProxy() const;
133 QQmlListProperty<QQuickLayoutItemProxy> getProxies();
134 bool proxyHasControl() const;
135
136signals:
137 void controlTaken();
138 void controlReleased();
139 void controllingProxyChanged();
140 void proxiesChanged();
141
142private:
143 QList<QQuickLayoutItemProxy*> proxies;
144 QQuickLayoutItemProxy* controllingProxy;
145};
146
147QT_END_NAMESPACE
148
149Q_DECLARE_METATYPE(QQuickLayoutItemProxyAttachedData*);
150
151#endif // QQUICKLAYOUTITEMPROXY_P_H
152

source code of qtdeclarative/src/quicklayouts/qquicklayoutitemproxy_p.h