2013-09-04 11:48:06 +02:00
|
|
|
/*
|
2020-08-13 21:08:54 +02:00
|
|
|
SPDX-FileCopyrightText: 2011 Aaron Seigo <aseigo@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
2013-09-04 11:48:06 +02:00
|
|
|
|
|
|
|
#include "sourcesOnRequest.h"
|
|
|
|
|
|
|
|
#include <Plasma/DataContainer>
|
|
|
|
|
2013-12-24 14:29:53 +01:00
|
|
|
#include <QStandardItemModel>
|
|
|
|
|
2013-09-04 11:48:06 +02:00
|
|
|
/*
|
|
|
|
This DataEngine shows how to created sources on demand as they are requested
|
|
|
|
and update them on visualization-requested update intervals.
|
2013-12-24 14:29:53 +01:00
|
|
|
It also shows how to bind and arbitrary QAbstractItemModel to a source
|
2013-09-04 11:48:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
SourcesOnRequestEngine::SourcesOnRequestEngine(QObject *parent, const QVariantList &args)
|
|
|
|
: Plasma::DataEngine(parent, args)
|
|
|
|
{
|
|
|
|
// We've passed the constructor's args to our parent class.
|
|
|
|
// We're done for now!
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SourcesOnRequestEngine::sourceRequestEvent(const QString &source)
|
|
|
|
{
|
|
|
|
// When this method is called we can assume that:
|
|
|
|
// * the source does not exist yet
|
|
|
|
// * the source name parameter passed in is not an empty string
|
|
|
|
// * we are free to reject creating the source if we wish
|
|
|
|
|
|
|
|
// We're going to reject any sources that start with the letter 'a'
|
|
|
|
// to demonstrate how to reject a request in a DataEngine.
|
2018-01-26 15:48:27 +01:00
|
|
|
if (source.startsWith(QLatin1Char('a')) || source.startsWith(QLatin1Char('A'))) {
|
2013-09-04 11:48:06 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For every other source, we're going to start an update count for it.
|
|
|
|
// Critically, we create a source before returning that has the exact
|
|
|
|
// same key as the source string. We MUST NOT create a source of a different
|
|
|
|
// name as that will cause unexpected results for the visualization.
|
|
|
|
// In such a case the DataEngine will remain happy and Do The Right Thing(tm)
|
|
|
|
// but the visualization will not get the source connected to it as it
|
|
|
|
// expects. So ALWAYS key the new data by the source string as below:
|
2018-01-26 15:48:27 +01:00
|
|
|
setData(source, QStringLiteral("Update Count"), 0);
|
2013-09-04 11:48:06 +02:00
|
|
|
|
2013-12-30 10:41:14 +01:00
|
|
|
if (!modelForSource(source)) {
|
2013-12-24 14:29:53 +01:00
|
|
|
QStandardItemModel *m = new QStandardItemModel;
|
2018-01-26 15:48:27 +01:00
|
|
|
m->appendRow(new QStandardItem(QStringLiteral("Item1, first update")));
|
|
|
|
m->appendRow(new QStandardItem(QStringLiteral("Item2, first update")));
|
|
|
|
m->appendRow(new QStandardItem(QStringLiteral("Item3, first update")));
|
2013-12-30 10:21:36 +01:00
|
|
|
setModel(source, m);
|
2013-12-24 14:29:53 +01:00
|
|
|
}
|
|
|
|
|
2013-09-04 11:48:06 +02:00
|
|
|
// as we successfully set up the source, return true
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SourcesOnRequestEngine::updateSourceEvent(const QString &source)
|
|
|
|
{
|
|
|
|
// Whenever a visualization has requested an update, such as when passing
|
|
|
|
// an update interval to DataEngine::connectSource, this method will be called.
|
|
|
|
// When this method is called we can assume that:
|
|
|
|
// * the source exists
|
|
|
|
// * it hasn't been updated more frequently than the minimum update interval
|
|
|
|
//
|
|
|
|
// If not data is set in this method, then the update is skipped for the visualiation
|
|
|
|
// and that is just fine.
|
|
|
|
//
|
|
|
|
// We can also set data for other sources here if we wish, but as with
|
|
|
|
// sourceRequestEvent this may not be what the visualization expects. Unlike in
|
|
|
|
// sourceRequestEvent, however, this will result in expected behavior: visualizations
|
|
|
|
// connected to the sources which have setData called for them will be notified
|
|
|
|
// of these changes.
|
2018-01-26 15:48:27 +01:00
|
|
|
const int updateCount = containerForSource(source)->data().value(QStringLiteral("Update Count")).toInt() + 1;
|
|
|
|
setData(source, QStringLiteral("Update Count"), updateCount);
|
2013-12-24 14:29:53 +01:00
|
|
|
|
2013-12-30 10:41:14 +01:00
|
|
|
QStandardItemModel *m = qobject_cast<QStandardItemModel *>(modelForSource(source));
|
2013-12-30 10:21:36 +01:00
|
|
|
if (m) {
|
2013-12-24 15:21:21 +01:00
|
|
|
m->clear();
|
2018-01-26 15:48:27 +01:00
|
|
|
m->appendRow(new QStandardItem(QStringLiteral("Item1, update %1").arg(updateCount)));
|
|
|
|
m->appendRow(new QStandardItem(QStringLiteral("Item2, update %1").arg(updateCount)));
|
|
|
|
m->appendRow(new QStandardItem(QStringLiteral("Item3, update %1").arg(updateCount)));
|
2013-12-24 14:29:53 +01:00
|
|
|
}
|
2013-09-04 11:48:06 +02:00
|
|
|
|
|
|
|
// Since we updated the source immediately here, we need to return true so the DataEngine
|
|
|
|
// knows to continue with the update notification for visualizations.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// export the plugin; use the plugin name and the class name
|
2013-09-04 15:28:15 +02:00
|
|
|
K_EXPORT_PLASMA_DATAENGINE_WITH_JSON(org.kde.examples.sourcesOnRequest, SourcesOnRequestEngine, "plasma-dataengine-example-sourcesOnRequest.json")
|
2013-09-04 11:48:06 +02:00
|
|
|
|
|
|
|
// include the moc file so the build system makes it for us
|
|
|
|
#include "sourcesOnRequest.moc"
|
|
|
|
|