Skip to content

Commit

Permalink
feat: support to access AppletItem for Applet
Browse files Browse the repository at this point in the history
  Add rootObject property to export AppletItem.
  • Loading branch information
18202781743 committed Nov 7, 2023
1 parent 9756c40 commit a6795bd
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 11 deletions.
12 changes: 12 additions & 0 deletions example/applet-example-data/exampleapplet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "exampleapplet.h"

#include "pluginfactory.h"
#include "appletitem.h"

ExampleApplet::ExampleApplet(QObject *parent)
: DApplet(parent)
Expand All @@ -18,6 +19,17 @@ QString ExampleApplet::mainText() const
return m_mainText;
}

void ExampleApplet::init()
{
DApplet::init();

DAppletItem *root = qobject_cast<DAppletItem *>(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"
7 changes: 6 additions & 1 deletion example/applet-example-data/exampleapplet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
3 changes: 2 additions & 1 deletion example/panel-example/examplepanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include "pluginfactory.h"


ExamplePanel::ExamplePanel(QObject *parent)
: DPanel(parent)
{
Expand All @@ -20,6 +19,8 @@ void ExamplePanel::load()
void ExamplePanel::init()
{
DPanel::init();
Q_ASSERT(rootObject());
Q_ASSERT(window());
}

D_APPLET_CLASS(ExamplePanel)
Expand Down
6 changes: 6 additions & 0 deletions frame/applet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand Down
7 changes: 7 additions & 0 deletions frame/applet.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,32 @@ 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;

virtual void init();

virtual void load();

Q_SIGNALS:
void rootObjectChanged();

protected:
explicit DApplet(DAppletPrivate &dd, QObject *parent = nullptr);
};
Expand Down
6 changes: 4 additions & 2 deletions frame/appletitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "appletitem.h"
#include "private/appletitem_p.h"
#include "private/applet_p.h"
#include "applet.h"
#include "qmlengine.h"

Expand Down Expand Up @@ -41,7 +42,7 @@ DAppletItem *DAppletItem::itemForApplet(DApplet *applet)
if (it != g_appletItems.constEnd())
return it.value();

QScopedPointer<DQmlEngine> engine(new DQmlEngine(applet, applet));
std::unique_ptr<DQmlEngine> engine(new DQmlEngine(applet, applet));

auto rootObject = engine->beginCreate();
if (!rootObject) {
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions frame/panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ DPanel::~DPanel()
QQuickWindow *DPanel::window() const
{
D_DC(DPanel);
return d->m_window;
return qobject_cast<QQuickWindow *>(d->m_rootObject);
}

void DPanel::load()
Expand All @@ -51,8 +51,8 @@ void DPanel::init()

auto window = qobject_cast<QQuickWindow *>(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();
Expand Down
9 changes: 9 additions & 0 deletions frame/private/applet_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
2 changes: 0 additions & 2 deletions frame/private/panel_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class DPanelPrivate : public DContainmentPrivate

void initDciSearchPaths();

QQuickWindow *m_window = nullptr;

D_DECLARE_PUBLIC(DPanel)
};

Expand Down
4 changes: 2 additions & 2 deletions frame/qmlengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ DQmlEngine::~DQmlEngine()
QObject *DQmlEngine::beginCreate()
{
D_D(DQmlEngine);
QScopedPointer<QQmlComponent> component(new QQmlComponent(engine(), this));
std::unique_ptr<QQmlComponent> component(new QQmlComponent(engine(), this));
const QString url = d->appletUrl();
if (url.isEmpty())
return nullptr;
Expand All @@ -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;
}

Expand Down

0 comments on commit a6795bd

Please sign in to comment.