diff --git a/examples/dataengines/sourcesOnRequest/sourcesOnRequest.cpp b/examples/dataengines/sourcesOnRequest/sourcesOnRequest.cpp index db35a91de..14a23dda0 100644 --- a/examples/dataengines/sourcesOnRequest/sourcesOnRequest.cpp +++ b/examples/dataengines/sourcesOnRequest/sourcesOnRequest.cpp @@ -64,13 +64,12 @@ bool SourcesOnRequestEngine::sourceRequestEvent(const QString &source) // expects. So ALWAYS key the new data by the source string as below: setData(source, "Update Count", 0); - Plasma::DataContainer *s = containerForSource(source); - if (!s->model()) { + if (!model(source)) { 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); + setModel(source, m); } // as we successfully set up the source, return true @@ -96,9 +95,8 @@ bool SourcesOnRequestEngine::updateSourceEvent(const QString &source) const int updateCount = containerForSource(source)->data().value("Update Count").toInt() + 1; setData(source, "Update Count", updateCount); - Plasma::DataContainer *s = containerForSource(source); - QStandardItemModel *m = qobject_cast(s->model()); - if (s->model()) { + QStandardItemModel *m = qobject_cast(model(source)); + if (m) { m->clear(); m->appendRow(new QStandardItem(QString("Item1, update %1").arg(updateCount))); m->appendRow(new QStandardItem(QString("Item2, update %1").arg(updateCount))); diff --git a/src/plasma/dataengine.cpp b/src/plasma/dataengine.cpp index b58723318..194df428d 100644 --- a/src/plasma/dataengine.cpp +++ b/src/plasma/dataengine.cpp @@ -21,6 +21,7 @@ #include "private/dataengine_p.h" #include "private/datacontainer_p.h" +#include #include #include #include @@ -227,6 +228,32 @@ void DataEngine::removeData(const QString &source, const QString &key) } } +void DataEngine::setModel(const QString &source, QAbstractItemModel *model) +{ + if (model) { + setData(source, "HasModel", true); + } else { + removeData(source, "HasModel"); + } + + Plasma::DataContainer *s = containerForSource(source); + + if (s) { + s->setModel(model); + } +} + +QAbstractItemModel *DataEngine::model(const QString &source) +{ + Plasma::DataContainer *s = containerForSource(source); + + if (s) { + return s->model(); + } else { + return 0; + } +} + void DataEngine::addSource(DataContainer *source) { if (d->sources.contains(source->objectName())) { diff --git a/src/plasma/dataengine.h b/src/plasma/dataengine.h index ffe6b6ead..c0e6a9a59 100644 --- a/src/plasma/dataengine.h +++ b/src/plasma/dataengine.h @@ -31,6 +31,8 @@ #include #include +class QAbstractItemModel; + namespace Plasma { @@ -306,6 +308,27 @@ Types::NoAlignment) const; **/ void removeData(const QString &source, const QString &key); + /** + * Associates a model to a data source. If the source + * doesn't exist then it is created. The source will have the key "HasModel" to easily indicate there is a model present. + * + * The ownership of the model is transferred to the DataContainer, + * so the model will be deletd when a new one is set or when the + * DataContainer itself is deleted. As the DataContainer, it will be + * deleted when there won't be any + * visualization associated to this source. + * + * @param source the name of the data source + * @param model the model instance + */ + void setModel(const QString &source, QAbstractItemModel *model); + + /** + * @return The model associated to a source if any. The ownership of the model stays with the DataContainer. + * Returns 0 if there isn't any model associated or if the source doesn't exists. + */ + QAbstractItemModel *model(const QString &source); + /** * Adds an already constructed data source. The DataEngine takes * ownership of the DataContainer object. The objectName of the source