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