From aa1c0fd3146b4ed055d181c99d52463afa6bedbb Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Fri, 16 Sep 2011 17:47:24 +0200 Subject: Initial import --- declarativewatchlet.cpp | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 declarativewatchlet.cpp (limited to 'declarativewatchlet.cpp') diff --git a/declarativewatchlet.cpp b/declarativewatchlet.cpp new file mode 100644 index 0000000..1b9c836 --- /dev/null +++ b/declarativewatchlet.cpp @@ -0,0 +1,94 @@ +#include +#include +#include "watchserver.h" +#include "declarativewatchwrapper.h" +#include "declarativewatchlet.h" + +using namespace sowatch; + +bool DeclarativeWatchlet::_registered = false; + +DeclarativeWatchlet::DeclarativeWatchlet(WatchServer* server, const QString& id) : + GraphicsWatchlet(server, id), + _engine(0), + _component(0), + _item(0), + _wrapper(0) +{ + setScene(new QGraphicsScene(this)); + + if (!_registered) { + qmlRegisterUncreatableType("com.javispedro.sowatch", 1, 0, + "Watch", "Watch is only available via attached properties"); + _registered = true; + } + + _engine = new QDeclarativeEngine(this); + _wrapper = new DeclarativeWatchWrapper(server->watch(), this); + + _engine->rootContext()->setContextProperty("watch", _wrapper); +} + +void DeclarativeWatchlet::setSource(const QUrl &url) +{ + if (_item) { + scene()->removeItem(_item); + delete _item; + _item = 0; + } + if (_component) { + delete _component; + _component = 0; + } + if (!url.isEmpty()) { + _component = new QDeclarativeComponent(_engine, url, this); + connect(_component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), + SLOT(handleComponentStatus(QDeclarativeComponent::Status))); + if (!_component->isLoading()) { + /* No signals are going to be generated for this. */ + handleComponentStatus(_component->status()); + } else { + connect(_component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), + this, SLOT(handleComponentStatus(QDeclarativeComponent::Status))); + } + } +} + +void DeclarativeWatchlet::activate() +{ + _wrapper->activate(); + _scene->update(); +} + +void DeclarativeWatchlet::deactivate() +{ + _wrapper->deactivate(); +} + +void DeclarativeWatchlet::handleComponentStatus(QDeclarativeComponent::Status status) +{ + QObject *obj; + disconnect(_component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), + this, SLOT(handleComponentStatus(QDeclarativeComponent::Status))); + switch (status) { + case QDeclarativeComponent::Null: + case QDeclarativeComponent::Loading: + /* Nothing to do */ + break; + case QDeclarativeComponent::Ready: + obj = _component->create(); + if (_component->isError()) { + qWarning() << "QML has instantation errors:"; + qWarning() << _component->errors(); + return; + } + Q_ASSERT(_item == 0); + _item = qobject_cast(obj); + scene()->addItem(_item); + break; + case QDeclarativeComponent::Error: + qWarning() << "QML has errors:"; + qWarning() << _component->errors(); + break; + } +} -- cgit v1.2.3