| 1 | // SPDX-FileCopyrightText: 2024 Carl Schwan <carl@carlschwan.eu> |
| 2 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <QJSValue> |
| 7 | #include <QQuickAttachedPropertyPropagator> |
| 8 | #include <QQuickItem> |
| 9 | #include <qqmlregistration.h> |
| 10 | |
| 11 | /*! |
| 12 | * \qmltype PageStack |
| 13 | * \inqmlmodule org.kde.kirigami.layouts |
| 14 | * |
| 15 | * \brief This attached property makes possible to access from anywhere the |
| 16 | * page stack this page was pushed into. |
| 17 | * |
| 18 | * It can be an instance of PageRow or |
| 19 | * a StackView from QtQuick Controls. |
| 20 | * |
| 21 | * \qml |
| 22 | * Kirigami.Page { |
| 23 | * id: root |
| 24 | * |
| 25 | * Button { |
| 26 | * text: "Push Page" |
| 27 | * onClicked: Kirigami.PageStack.push(Qt.resolvedurl("AnotherPage")); |
| 28 | * } |
| 29 | * } |
| 30 | * \endqml |
| 31 | * |
| 32 | * \since 6.10 |
| 33 | */ |
| 34 | class PageStackAttached : public QQuickAttachedPropertyPropagator |
| 35 | { |
| 36 | Q_OBJECT |
| 37 | QML_NAMED_ELEMENT(PageStack) |
| 38 | QML_ATTACHED(PageStackAttached) |
| 39 | QML_UNCREATABLE("" ) |
| 40 | |
| 41 | /*! |
| 42 | * \qmlattachedproperty Item PageStack::pageStack |
| 43 | * |
| 44 | * This property holds the pageStack where this page was pushed. |
| 45 | * |
| 46 | * It will point to the proper instance in the parent hierarchy |
| 47 | * and normally is not necessary to explicitly write it. |
| 48 | * |
| 49 | * Write on this property only if it's desired this attached |
| 50 | * property and those of all the children to point to a different |
| 51 | * PageRow or StackView |
| 52 | */ |
| 53 | Q_PROPERTY(QQuickItem *pageStack READ pageStack WRITE setPageStack NOTIFY pageStackChanged) |
| 54 | |
| 55 | public: |
| 56 | explicit PageStackAttached(QObject *parent); |
| 57 | |
| 58 | QQuickItem *pageStack() const; |
| 59 | void setPageStack(QQuickItem *pageStack); |
| 60 | |
| 61 | /*! |
| 62 | * \qmlattachedmethod void PageStack::push(variant page, object properties) |
| 63 | */ |
| 64 | Q_INVOKABLE void push(const QVariant &page, const QVariantMap &properties = QVariantMap()); |
| 65 | |
| 66 | /*! |
| 67 | * \qmlattachedmethod void PageStack::replace(variant page, object properties) |
| 68 | */ |
| 69 | Q_INVOKABLE void replace(const QVariant &page, const QVariantMap &properties = QVariantMap()); |
| 70 | |
| 71 | /*! |
| 72 | * \qmlattachedmethod void PageStack::pop(variant page) |
| 73 | */ |
| 74 | Q_INVOKABLE void pop(const QVariant &page = QVariant()); |
| 75 | |
| 76 | /*! |
| 77 | * \qmlattachedmethod void PageStack::clear() |
| 78 | */ |
| 79 | Q_INVOKABLE void clear(); |
| 80 | |
| 81 | static PageStackAttached *qmlAttachedProperties(QObject *object); |
| 82 | |
| 83 | protected: |
| 84 | bool hasStackCapabilities(QQuickItem *candidate); |
| 85 | void propagatePageStack(QQuickItem *pageStack); |
| 86 | void attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent) override; |
| 87 | |
| 88 | Q_SIGNALS: |
| 89 | void pageStackChanged(); |
| 90 | |
| 91 | private: |
| 92 | QPointer<QQuickItem> m_pageStack; |
| 93 | QPointer<QQuickItem> m_parentItem; |
| 94 | bool m_customStack = false; |
| 95 | }; |
| 96 | |