add DataEngine::setModel

This commit is contained in:
Marco Martin 2013-12-30 10:21:36 +01:00
parent f692b5aa74
commit dea7942533
3 changed files with 54 additions and 6 deletions

View File

@ -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<QStandardItemModel *>(s->model());
if (s->model()) {
QStandardItemModel *m = qobject_cast<QStandardItemModel *>(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)));

View File

@ -21,6 +21,7 @@
#include "private/dataengine_p.h"
#include "private/datacontainer_p.h"
#include <QAbstractItemModel>
#include <QQueue>
#include <QTimer>
#include <QTime>
@ -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())) {

View File

@ -31,6 +31,8 @@
#include <plasma/plasma.h>
#include <plasma/service.h>
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