working models, add an example applet

This commit is contained in:
Marco Martin 2013-12-24 15:21:21 +01:00
parent c318acb242
commit 124139c2bf
8 changed files with 111 additions and 7 deletions

View File

@ -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)

View File

@ -0,0 +1,55 @@
// -*- coding: iso-8859-1 -*-
/*
* Copyright 2012 Marco Martin <mart@kde.org>
*
* 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
}
}
}
}

View File

@ -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=

View File

@ -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<QStandardItemModel *>(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

View File

@ -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);

View File

@ -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;

View File

@ -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();
}

View File

@ -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;
}
}