diff --git a/example/applet-example-data/exampleapplet.cpp b/example/applet-example-data/exampleapplet.cpp index 41aed9198..7421f128a 100644 --- a/example/applet-example-data/exampleapplet.cpp +++ b/example/applet-example-data/exampleapplet.cpp @@ -5,6 +5,7 @@ #include "exampleapplet.h" #include "pluginfactory.h" +#include "appletitem.h" ExampleApplet::ExampleApplet(QObject *parent) : DApplet(parent) @@ -18,6 +19,17 @@ QString ExampleApplet::mainText() const return m_mainText; } +void ExampleApplet::init() +{ + DApplet::init(); + + DAppletItem *root = qobject_cast(rootObject()); + Q_ASSERT(root); + + m_mainText = QString("%1 - w:%2;h:%3").arg(m_mainText).arg(root->width()).arg(root->height()); + Q_EMIT mainTextChanged(); +} + D_APPLET_CLASS(ExampleApplet) #include "exampleapplet.moc" diff --git a/example/applet-example-data/exampleapplet.h b/example/applet-example-data/exampleapplet.h index 50179f9a9..9e67829c8 100644 --- a/example/applet-example-data/exampleapplet.h +++ b/example/applet-example-data/exampleapplet.h @@ -11,11 +11,16 @@ DS_USE_NAMESPACE class ExampleApplet : public DApplet { Q_OBJECT - Q_PROPERTY(QString mainText READ mainText CONSTANT) + Q_PROPERTY(QString mainText READ mainText NOTIFY mainTextChanged CONSTANT) public: explicit ExampleApplet(QObject *parent = nullptr); QString mainText() const; + + virtual void init() override; + +Q_SIGNALS: + void mainTextChanged(); private: QString m_mainText; }; diff --git a/example/panel-example/examplepanel.cpp b/example/panel-example/examplepanel.cpp index 38c423c32..cc918d142 100644 --- a/example/panel-example/examplepanel.cpp +++ b/example/panel-example/examplepanel.cpp @@ -6,7 +6,6 @@ #include "pluginfactory.h" - ExamplePanel::ExamplePanel(QObject *parent) : DPanel(parent) { @@ -20,6 +19,8 @@ void ExamplePanel::load() void ExamplePanel::init() { DPanel::init(); + Q_ASSERT(rootObject()); + Q_ASSERT(window()); } D_APPLET_CLASS(ExamplePanel) diff --git a/frame/applet.cpp b/frame/applet.cpp index e67052707..419cb9b5a 100644 --- a/frame/applet.cpp +++ b/frame/applet.cpp @@ -48,6 +48,12 @@ DPluginMetaData DApplet::pluginMetaData() const return d->m_metaData; } +QObject *DApplet::rootObject() const +{ + D_DC(DApplet); + return d->m_rootObject; +} + void DApplet::init() { } diff --git a/frame/applet.h b/frame/applet.h index 149f1c693..f3926e2cc 100644 --- a/frame/applet.h +++ b/frame/applet.h @@ -14,18 +14,22 @@ DS_BEGIN_NAMESPACE /** * @brief 插件项,单个插件实例 */ +class DAppletItem; class DAppletPrivate; class DS_SHARE DApplet : public QObject, public DTK_CORE_NAMESPACE::DObject { Q_OBJECT Q_PROPERTY(QString pluginId READ pluginId CONSTANT FINAL) + Q_PROPERTY(QObject *rootObject READ rootObject NOTIFY rootObjectChanged) D_DECLARE_PRIVATE(DApplet) + friend class DAppletItem; public: explicit DApplet(QObject *parent = nullptr); virtual ~DApplet() override; void setMetaData(const DPluginMetaData &metaData); QString pluginId() const; + QObject *rootObject() const; DPluginMetaData pluginMetaData() const; @@ -33,6 +37,9 @@ class DS_SHARE DApplet : public QObject, public DTK_CORE_NAMESPACE::DObject virtual void load(); +Q_SIGNALS: + void rootObjectChanged(); + protected: explicit DApplet(DAppletPrivate &dd, QObject *parent = nullptr); }; diff --git a/frame/appletitem.cpp b/frame/appletitem.cpp index baa98cc36..7c658ad78 100644 --- a/frame/appletitem.cpp +++ b/frame/appletitem.cpp @@ -4,6 +4,7 @@ #include "appletitem.h" #include "private/appletitem_p.h" +#include "private/applet_p.h" #include "applet.h" #include "qmlengine.h" @@ -41,7 +42,7 @@ DAppletItem *DAppletItem::itemForApplet(DApplet *applet) if (it != g_appletItems.constEnd()) return it.value(); - QScopedPointer engine(new DQmlEngine(applet, applet)); + std::unique_ptr engine(new DQmlEngine(applet, applet)); auto rootObject = engine->beginCreate(); if (!rootObject) { @@ -55,10 +56,11 @@ DAppletItem *DAppletItem::itemForApplet(DApplet *applet) } item->d_func()->m_applet = applet; - item->d_func()->m_engine = engine.take(); + item->d_func()->m_engine = engine.release(); g_appletItems[applet] = item; item->d_func()->m_engine->completeCreate(); + applet->d_func()->setRootObject(item); return item; } diff --git a/frame/panel.cpp b/frame/panel.cpp index ce8727e49..14b0fc90a 100644 --- a/frame/panel.cpp +++ b/frame/panel.cpp @@ -29,7 +29,7 @@ DPanel::~DPanel() QQuickWindow *DPanel::window() const { D_DC(DPanel); - return d->m_window; + return qobject_cast(d->m_rootObject); } void DPanel::load() @@ -51,8 +51,8 @@ void DPanel::init() auto window = qobject_cast(rootObject); if (window) { - d->m_window = window; - d->m_window->setProperty("_ds_window_applet", QVariant::fromValue(applet)); + applet->d_func()->setRootObject(window); + d->m_rootObject->setProperty("_ds_window_applet", QVariant::fromValue(applet)); } DContainment::init(); diff --git a/frame/private/applet_p.h b/frame/private/applet_p.h index dfdccb24a..95f5b39ad 100644 --- a/frame/private/applet_p.h +++ b/frame/private/applet_p.h @@ -22,6 +22,15 @@ class DAppletPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate } DPluginMetaData m_metaData; + QObject *m_rootObject{nullptr}; + void setRootObject(QObject *root) + { + if (m_rootObject == root) + return; + m_rootObject = root; + D_Q(DApplet); + Q_EMIT q->rootObjectChanged(); + } D_DECLARE_PUBLIC(DApplet); }; diff --git a/frame/private/panel_p.h b/frame/private/panel_p.h index bfbbde690..018e2c015 100644 --- a/frame/private/panel_p.h +++ b/frame/private/panel_p.h @@ -25,8 +25,6 @@ class DPanelPrivate : public DContainmentPrivate void initDciSearchPaths(); - QQuickWindow *m_window = nullptr; - D_DECLARE_PUBLIC(DPanel) }; diff --git a/frame/qmlengine.cpp b/frame/qmlengine.cpp index 988679d3b..9b346e5db 100644 --- a/frame/qmlengine.cpp +++ b/frame/qmlengine.cpp @@ -77,7 +77,7 @@ DQmlEngine::~DQmlEngine() QObject *DQmlEngine::beginCreate() { D_D(DQmlEngine); - QScopedPointer component(new QQmlComponent(engine(), this)); + std::unique_ptr component(new QQmlComponent(engine(), this)); const QString url = d->appletUrl(); if (url.isEmpty()) return nullptr; @@ -91,7 +91,7 @@ QObject *DQmlEngine::beginCreate() auto object = component->beginCreate(context); d->m_context = context; d->m_rootObject = object; - d->m_component = component.take(); + d->m_component = component.release(); return object; }