make use of the model embedding

This commit is contained in:
Marco Martin 2013-12-24 14:29:53 +01:00
parent 05a62e5652
commit c318acb242
3 changed files with 33 additions and 1 deletions

View File

@ -27,9 +27,12 @@
#include <Plasma/DataContainer> #include <Plasma/DataContainer>
#include <QStandardItemModel>
/* /*
This DataEngine shows how to created sources on demand as they are requested This DataEngine shows how to created sources on demand as they are requested
and update them on visualization-requested update intervals. and update them on visualization-requested update intervals.
It also shows how to bind and arbitrary QAbstractItemModel to a source
*/ */
SourcesOnRequestEngine::SourcesOnRequestEngine(QObject *parent, const QVariantList &args) SourcesOnRequestEngine::SourcesOnRequestEngine(QObject *parent, const QVariantList &args)
@ -61,6 +64,15 @@ bool SourcesOnRequestEngine::sourceRequestEvent(const QString &source)
// expects. So ALWAYS key the new data by the source string as below: // expects. So ALWAYS key the new data by the source string as below:
setData(source, "Update Count", 0); setData(source, "Update Count", 0);
Plasma::DataContainer *s = containerForSource(source);
if (!s->model()) {
QStandardItemModel *m = new QStandardItemModel;
m->appendRow(new QStandardItem("Item1, first update"));
m->appendRow(new QStandardItem("Item2, first update"));
m->appendRow(new QStandardItem("Item3, first update"));
s->setModel(m);
}
// as we successfully set up the source, return true // as we successfully set up the source, return true
return true; return true;
} }
@ -81,7 +93,17 @@ bool SourcesOnRequestEngine::updateSourceEvent(const QString &source)
// sourceRequestEvent, however, this will result in expected behavior: visualizations // sourceRequestEvent, however, this will result in expected behavior: visualizations
// connected to the sources which have setData called for them will be notified // connected to the sources which have setData called for them will be notified
// of these changes. // of these changes.
setData(source, "Update Count", containerForSource(source)->data().value("Update Count").toInt() + 1); const int updateCount = containerForSource(source)->data().value("Update Count").toInt() + 1;
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);
}
// Since we updated the source immediately here, we need to return true so the DataEngine // Since we updated the source immediately here, we need to return true so the DataEngine
// knows to continue with the update notification for visualizations. // knows to continue with the update notification for visualizations.

View File

@ -77,6 +77,11 @@ void DataContainer::setModel(QAbstractItemModel *model)
emit modelChanged(objectName(), model); emit modelChanged(objectName(), model);
} }
QAbstractItemModel *DataContainer::model()
{
return d->model.data();
}
void DataContainer::removeAllData() void DataContainer::removeAllData()
{ {
if (d->data.isEmpty()) { if (d->data.isEmpty()) {

View File

@ -121,6 +121,11 @@ class PLASMA_EXPORT DataContainer : public QObject
*/ */
void setModel(QAbstractItemModel *model); void setModel(QAbstractItemModel *model);
/**
* @return the model owned by this DataSource
*/
QAbstractItemModel *model();
/** /**
* @return true if the visualization is currently connected * @return true if the visualization is currently connected
*/ */