From ea5123cb6b30d5ab083ee3821269537b1d31b9f0 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Sun, 5 May 2013 00:21:21 +0200 Subject: reduce the number of qdeclarativeengine instances --- libsowatch/declarativewatchlet.cpp | 34 +++++++++++++++++++++------------- libsowatch/declarativewatchlet.h | 5 ++++- libsowatch/watchlet.h | 8 ++++---- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/libsowatch/declarativewatchlet.cpp b/libsowatch/declarativewatchlet.cpp index 32ff8e8..7e3ac53 100644 --- a/libsowatch/declarativewatchlet.cpp +++ b/libsowatch/declarativewatchlet.cpp @@ -31,20 +31,28 @@ DeclarativeWatchlet::DeclarativeWatchlet(WatchServer* server, const QString& id) _registered = true; } - // TODO: Share a single engine per watch server instead of this. - _engine = new QDeclarativeEngine(this); -#if !defined(QT_NO_DEBUG) - QString qmlDir = QDir::current().absoluteFilePath(SOWATCH_QML_DIR); - qDebug() << "Using debug QML import path: " << qmlDir; - _engine->addImportPath(SOWATCH_QML_DIR); -#else - _engine->addImportPath(SOWATCH_QML_DIR); -#endif + // A dynamic property on the WatchServer object is used to share a single + // DeclarativeEngine amongst all DeclarativeWatchlet instances. + QVariant serverEngine = server->property("declarativeEngine"); + if (!serverEngine.isValid()) { + // Create the shared engine + qDebug() << "Starting QDeclarativeEngine"; + _engine = new QDeclarativeEngine(server); + _engine->addImportPath(SOWATCH_QML_DIR); + + // Set context properties that are shared by all watchlets here + _engine->rootContext()->setContextProperty("notifications", + const_cast(server->notifications())); + + server->setProperty("declarativeEngine", QVariant::fromValue(_engine)); + } else { + _engine = serverEngine.value(); + } + + _context = new QDeclarativeContext(_engine, this); _wrapper = new DeclarativeWatchWrapper(server, server->watch(), this); - _engine->rootContext()->setContextProperty("watch", _wrapper); - _engine->rootContext()->setContextProperty("notifications", - const_cast(server->notifications())); + _context->setContextProperty("watch", _wrapper); } DeclarativeWatchlet::~DeclarativeWatchlet() @@ -140,7 +148,7 @@ void DeclarativeWatchlet::handleComponentStatus(QDeclarativeComponent::Status st /* Nothing to do */ break; case QDeclarativeComponent::Ready: - obj = _component->create(); + obj = _component->create(_context); if (_component->isError()) { qWarning() << "QML has errors found while creating:"; qWarning() << _component->errors(); diff --git a/libsowatch/declarativewatchlet.h b/libsowatch/declarativewatchlet.h index 087387a..28b16a8 100644 --- a/libsowatch/declarativewatchlet.h +++ b/libsowatch/declarativewatchlet.h @@ -17,7 +17,7 @@ class SOWATCH_EXPORT DeclarativeWatchlet : public GraphicsWatchlet { Q_OBJECT public: - explicit DeclarativeWatchlet(WatchServer* server, const QString& id); + DeclarativeWatchlet(WatchServer* server, const QString& id); ~DeclarativeWatchlet(); void setSource(const QUrl& url); @@ -35,6 +35,7 @@ private: static bool _registered; QDeclarativeEngine* _engine; + QDeclarativeContext *_context; QDeclarativeComponent* _component; QDeclarativeItem* _item; DeclarativeWatchWrapper* _wrapper; @@ -45,4 +46,6 @@ private slots: } +Q_DECLARE_METATYPE(QDeclarativeEngine*) + #endif // SOWATCH_DECLARATIVEWATCHLET_H diff --git a/libsowatch/watchlet.h b/libsowatch/watchlet.h index f442086..66ec874 100644 --- a/libsowatch/watchlet.h +++ b/libsowatch/watchlet.h @@ -14,11 +14,11 @@ class SOWATCH_EXPORT Watchlet : public QObject { Q_OBJECT Q_PROPERTY(QString id READ id CONSTANT) - Q_PROPERTY(bool isActive READ isActive NOTIFY activeChanged) + Q_PROPERTY(bool active READ isActive NOTIFY activeChanged) public: - explicit Watchlet(WatchServer *server, const QString& id); - virtual ~Watchlet(); + Watchlet(WatchServer *server, const QString& id); + ~Watchlet(); WatchServer* server(); Watch* watch(); @@ -26,7 +26,7 @@ public: const WatchServer* server() const; const Watch* watch() const; - Q_INVOKABLE QString id() const; + QString id() const; bool isActive() const; signals: -- cgit v1.2.3