diff --git a/src/pagerouter.h b/src/pagerouter.h --- a/src/pagerouter.h +++ b/src/pagerouter.h @@ -439,22 +439,24 @@ QPointer m_router; QVariant m_data; + void findParent(); + friend class PageRouter; public: PageRouter* router() const { return m_router; }; QVariant data() const; bool isCurrent() const; /// @see PageRouter::navigateToRoute() - Q_INVOKABLE void navigateToRoute(QJSValue route) { m_router->navigateToRoute(route); }; + Q_INVOKABLE void navigateToRoute(QJSValue route); /// @see PageRouter::routeActive() - Q_INVOKABLE bool routeActive(QJSValue route) { return m_router->routeActive(route); }; + Q_INVOKABLE bool routeActive(QJSValue route); /// @see PageRouter::pushRoute() - Q_INVOKABLE void pushRoute(QJSValue route) { m_router->pushRoute(route); }; + Q_INVOKABLE void pushRoute(QJSValue route); /// @see PageRouter::popRoute() - Q_INVOKABLE void popRoute() { m_router->popRoute(); }; + Q_INVOKABLE void popRoute(); // @see PageRouter::bringToView() - Q_INVOKABLE void bringToView(QJSValue route) { m_router->bringToView(route); }; + Q_INVOKABLE void bringToView(QJSValue route); Q_SIGNALS: void routerChanged(); diff --git a/src/pagerouter.cpp b/src/pagerouter.cpp --- a/src/pagerouter.cpp +++ b/src/pagerouter.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "pagerouter.h" ParsedRoute parseRoute(QJSValue value) @@ -181,6 +182,8 @@ if (routesCacheForKey(route.name)) { m_cachedRoutes << clone; } + auto attached = qobject_cast(qmlAttachedPropertiesObject(item, true)); + attached->m_router = this; component->completeCreate(); m_pageStack->addItem(qobject_cast(item)); m_pageStack->setCurrentIndex(m_currentRoutes.length()-1); @@ -334,39 +337,73 @@ PageRouterAttached* PageRouter::qmlAttachedProperties(QObject *object) { auto attached = new PageRouterAttached(object); - auto asItem = qobject_cast(object); - auto seekParent = [](QObject *seek) -> PageRouter* { - auto pointer = seek; - while (pointer != nullptr) { - auto casted = qobject_cast(pointer); - if (casted != nullptr) { - return casted; - } - pointer = pointer->parent(); - } - return nullptr; - }; - if (asItem) { - while (asItem != nullptr) { - auto parent = seekParent(asItem); - if (parent != nullptr) { - attached->m_router = parent; - connect(parent, &PageRouter::currentIndexChanged, attached, &PageRouterAttached::isCurrentChanged); - break; - } - asItem = asItem->parentItem(); + return attached; +} + +void PageRouterAttached::findParent() +{ + QQuickItem *parent = qobject_cast(this->parent()); + while (parent != nullptr) { + auto attached = qobject_cast(qmlAttachedPropertiesObject(parent, false)); + if (attached != nullptr && attached->m_router != nullptr) { + m_router = attached->m_router; + Q_EMIT routerChanged(); + Q_EMIT dataChanged(); + Q_EMIT isCurrentChanged(); + break; } + parent = parent->parentItem(); + } +} + +void PageRouterAttached::navigateToRoute(QJSValue route) +{ + if (m_router) { + m_router->navigateToRoute(route); } else { - auto parent = seekParent(object); - if (parent != nullptr) { - attached->m_router = parent; - connect(parent, &PageRouter::currentIndexChanged, attached, &PageRouterAttached::isCurrentChanged); - } + qCritical() << "PageRouterAttached does not have a parent PageRouter"; + return; } - if (attached->m_router.isNull()) { - qCritical() << "PageRouterAttached could not find a parent PageRouter"; +} + +bool PageRouterAttached::routeActive(QJSValue route) +{ + if (m_router) { + return m_router->routeActive(route); + } else { + qCritical() << "PageRouterAttached does not have a parent PageRouter"; + return false; + } +} + +void PageRouterAttached::pushRoute(QJSValue route) +{ + if (m_router) { + m_router->pushRoute(route); + } else { + qCritical() << "PageRouterAttached does not have a parent PageRouter"; + return; + } +} + +void PageRouterAttached::popRoute() +{ + if (m_router) { + m_router->popRoute(); + } else { + qCritical() << "PageRouterAttached does not have a parent PageRouter"; + return; + } +} + +void PageRouterAttached::bringToView(QJSValue route) +{ + if (m_router) { + m_router->bringToView(route); + } else { + qCritical() << "PageRouterAttached does not have a parent PageRouter"; + return; } - return attached; } QVariant PageRouterAttached::data() const @@ -402,4 +439,16 @@ return ret; } -PageRouterAttached::PageRouterAttached(QObject *parent) : QObject(parent) {} \ No newline at end of file +PageRouterAttached::PageRouterAttached(QObject *parent) : QObject(parent) +{ + findParent(); + auto item = qobject_cast(parent); + if (item != nullptr) { + connect(item, &QQuickItem::windowChanged, this, [this]() { + findParent(); + }); + connect(item, &QQuickItem::parentChanged, this, [this]() { + findParent(); + }); + } +} \ No newline at end of file