From 124139c2bfe1f4e373f61ff201943756d2f5a3aa Mon Sep 17 00:00:00 2001 From: Marco Martin Date: Tue, 24 Dec 2013 15:21:21 +0100 Subject: [PATCH] working models, add an example applet --- examples/applets/CMakeLists.txt | 1 + .../dataenginemodel/contents/ui/main.qml | 55 +++++++++++++++++++ .../applets/dataenginemodel/metadata.desktop | 20 +++++++ .../sourcesOnRequest/sourcesOnRequest.cpp | 12 ++-- src/declarativeimports/core/datasource.cpp | 10 ++++ src/declarativeimports/core/datasource.h | 11 +++- src/plasma/datacontainer.cpp | 4 ++ src/plasma/dataengine.cpp | 5 ++ 8 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 examples/applets/dataenginemodel/contents/ui/main.qml create mode 100644 examples/applets/dataenginemodel/metadata.desktop diff --git a/examples/applets/CMakeLists.txt b/examples/applets/CMakeLists.txt index ff9498720..56916fd24 100644 --- a/examples/applets/CMakeLists.txt +++ b/examples/applets/CMakeLists.txt @@ -1,5 +1,6 @@ plasma_install_package(config org.kde.example.configuration) +plasma_install_package(config org.kde.example.dataenginemodel) plasma_install_package(notes org.kde.example.notes) plasma_install_package(widgetgallery org.kde.example.widgetgallery) plasma_install_package(qmltasks org.kde.example.tasks) diff --git a/examples/applets/dataenginemodel/contents/ui/main.qml b/examples/applets/dataenginemodel/contents/ui/main.qml new file mode 100644 index 000000000..d64e71f48 --- /dev/null +++ b/examples/applets/dataenginemodel/contents/ui/main.qml @@ -0,0 +1,55 @@ +// -*- coding: iso-8859-1 -*- +/* + * Copyright 2012 Marco Martin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details + * + * You should have received a copy of the GNU Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import QtQuick 2.0 +import org.kde.plasma.core 2.0 as PlasmaCore +import org.kde.plasma.components 2.0 as PlasmaComponents +import org.kde.plasma.extras 2.0 as PlasmaExtras + +Column { + width: 500 + height: 500 + property int minimumWidth: 200 + property int minimumHeight: 300 + + PlasmaCore.DataSource { + id: source + dataEngine: "org.kde.examples.sourcesOnRequest" + interval: 1000 + connectedSources: "test" + } + + PlasmaComponents.Label { + text: source.data.test["Update Count"] + } + PlasmaExtras.ScrollArea { + anchors { + left: parent.left + right: parent.right + } + height: 500 + ListView { + model: source.models.test + delegate: PlasmaComponents.Label { + text: model.display + } + } + } +} diff --git a/examples/applets/dataenginemodel/metadata.desktop b/examples/applets/dataenginemodel/metadata.desktop new file mode 100644 index 000000000..73e30a6c5 --- /dev/null +++ b/examples/applets/dataenginemodel/metadata.desktop @@ -0,0 +1,20 @@ +[Desktop Entry] +Comment=Example applet that shows how to use Models embeeded in DataEngines +Encoding=UTF-8 +Keywords= +Name=Dataengine model +Type=Service +Icon= +X-KDE-ParentApp= +X-KDE-PluginInfo-Author=Marco Martin +X-KDE-PluginInfo-Category=Miscellaneous +X-KDE-PluginInfo-Email=mart@kde.org +X-KDE-PluginInfo-License=GPL +X-KDE-PluginInfo-Name=org.kde.examples.dataenginemodel +X-KDE-PluginInfo-Version= +X-KDE-PluginInfo-Website= +X-KDE-ServiceTypes=Plasma/Applet +X-Plasma-API=declarativeappletscript + +X-Plasma-MainScript=ui/main.qml +X-Plasma-RemoteLocation= diff --git a/examples/dataengines/sourcesOnRequest/sourcesOnRequest.cpp b/examples/dataengines/sourcesOnRequest/sourcesOnRequest.cpp index 68a251e95..db35a91de 100644 --- a/examples/dataengines/sourcesOnRequest/sourcesOnRequest.cpp +++ b/examples/dataengines/sourcesOnRequest/sourcesOnRequest.cpp @@ -97,12 +97,12 @@ bool SourcesOnRequestEngine::updateSourceEvent(const QString &source) setData(source, "Update Count", updateCount); Plasma::DataContainer *s = containerForSource(source); - if (!s->model()) { - QStandardItemModel *m = new QStandardItemModel; - m->appendRow(new QStandardItem("Item1, update " + updateCount)); - m->appendRow(new QStandardItem("Item2, update " + updateCount)); - m->appendRow(new QStandardItem("Item3, update " + updateCount)); - s->setModel(m); + QStandardItemModel *m = qobject_cast(s->model()); + if (s->model()) { + m->clear(); + m->appendRow(new QStandardItem(QString("Item1, update %1").arg(updateCount))); + m->appendRow(new QStandardItem(QString("Item2, update %1").arg(updateCount))); + m->appendRow(new QStandardItem(QString("Item3, update %1").arg(updateCount))); } // Since we updated the source immediately here, we need to return true so the DataEngine diff --git a/src/declarativeimports/core/datasource.cpp b/src/declarativeimports/core/datasource.cpp index b44ce109f..64cd38f5b 100644 --- a/src/declarativeimports/core/datasource.cpp +++ b/src/declarativeimports/core/datasource.cpp @@ -148,6 +148,16 @@ void DataSource::dataUpdated(const QString &sourceName, const Plasma::DataEngine } } +void DataSource::modelChanged(const QString &sourceName, QAbstractItemModel *model) +{ + m_models[sourceName] = QVariant::fromValue(model); + connect(model, &QObject::destroyed, [=]() { + m_models.remove(sourceName); + emit modelsChanged(); + }); + emit modelsChanged(); +} + void DataSource::removeSource(const QString &source) { m_data.remove(source); diff --git a/src/declarativeimports/core/datasource.h b/src/declarativeimports/core/datasource.h index 01c613a17..16b11b3bd 100644 --- a/src/declarativeimports/core/datasource.h +++ b/src/declarativeimports/core/datasource.h @@ -89,11 +89,17 @@ public: /** * All the data fetched by this dataengine. - * This is a map of maps. At the first level, there are the source names, at the secons, they keys set by the DataEngine + * This is a map of maps. At the first level, there are the source names, at the second, they keys set by the DataEngine */ Q_PROPERTY(QVariantMap data READ data NOTIFY dataChanged); QVariantMap data() const {return m_data;} + /** + * All the models associated to this DataEngine, indexed by source + */ + Q_PROPERTY(QVariantMap models READ models NOTIFY modelsChanged); + QVariantMap models() const {return m_models;} + /** * @returns a Plasma::Service given a source name * @arg QString source source name we want a service of @@ -112,6 +118,7 @@ public: protected Q_SLOTS: void dataUpdated(const QString &sourceName, const Plasma::DataEngine::Data &data); + void modelChanged(const QString &sourceName, QAbstractItemModel *model); void removeSource(const QString &source); void setupData(); @@ -124,6 +131,7 @@ Q_SIGNALS: void intervalChanged(); void engineChanged(); void dataChanged(); + void modelsChanged(); void connectedSourcesChanged(); void sourcesChanged(); @@ -132,6 +140,7 @@ private: int m_interval; QString m_engine; QVariantMap m_data; + QVariantMap m_models; Plasma::DataEngine* m_dataEngine; Plasma::DataEngineConsumer* m_dataEngineConsumer; QStringList m_connectedSources; diff --git a/src/plasma/datacontainer.cpp b/src/plasma/datacontainer.cpp index 8891d8964..218c8627d 100644 --- a/src/plasma/datacontainer.cpp +++ b/src/plasma/datacontainer.cpp @@ -68,6 +68,10 @@ void DataContainer::setData(const QString &key, const QVariant &value) void DataContainer::setModel(QAbstractItemModel *model) { + if (d->model.data() == model) { + return; + } + if (d->model) { d->model.data()->deleteLater(); } diff --git a/src/plasma/dataengine.cpp b/src/plasma/dataengine.cpp index a56448229..b58723318 100644 --- a/src/plasma/dataengine.cpp +++ b/src/plasma/dataengine.cpp @@ -521,6 +521,11 @@ void DataEnginePrivate::connectSource(DataContainer *s, QObject *visualization, QMetaObject::invokeMethod(visualization, "dataUpdated", Q_ARG(QString, s->objectName()), Q_ARG(Plasma::DataEngine::Data, s->data())); + if (s->d->model) { + QMetaObject::invokeMethod(visualization, "modelChanged", + Q_ARG(QString, s->objectName()), + Q_ARG(QAbstractItemModel *, s->d->model.data())); + } s->d->dirty = false; } }