a crazy experiment: if one of the dataitems is a QVariantList of QVariantMaps (like many dataengines, like rss, microblog, opendesktop...)

make it available as a QAbstractModel, so will be possible to categorize it
(considering to put it behind a qsortfilterproxymodel to make it easy to do filtering)

svn path=/trunk/KDE/kdebase/runtime/; revision=1189920
This commit is contained in:
Marco Martin 2010-10-26 12:01:59 +00:00
parent 711676009b
commit 7ee1ebf5ac
5 changed files with 212 additions and 2 deletions

View File

@ -4,6 +4,7 @@ set(corebindings_SRCS
corebindingsplugin.cpp
dataengineconsumer.cpp
theme.cpp
datamodel.cpp
datasource.cpp
svgitem.cpp
framesvgitem.cpp

View File

@ -0,0 +1,116 @@
/*
* Copyright 2009 by Alan Alpert <alan.alpert@nokia.com>
* Copyright 2010 by Ménard Alexis <menard@kde.org>
* Copyright 2010 by 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.
*/
#include "datamodel.h"
#include <KDebug>
namespace Plasma
{
DataModel::DataModel(QObject* parent)
: QAbstractItemModel(parent)
{
setObjectName("DataModel");
}
DataModel::~DataModel()
{
}
void DataModel::setItems(const QVariantList &list)
{
emit modelAboutToBeReset();
//convert to vector, so data() will be O(1)
m_items = list.toVector();
int role = Qt::UserRole;
m_roleNames.clear();
foreach (QString roleName, list.first().value<QVariantMap>().keys()) {
++role;
m_roleNames[role] = roleName.toLatin1();
}
setRoleNames(m_roleNames);
//make the declarative view reload everything,
//would be nice an incremental update but is not possible
emit modelReset();
}
QVariant DataModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.column() > 0 ||
index.row() < 0 || index.row() >= m_items.count()){
return QVariant();
}
return m_items.value(index.row()).value<QVariantMap>().value(m_roleNames.value(role));
}
QVariant DataModel::headerData(int section, Qt::Orientation orientation, int role) const
{
Q_UNUSED(section)
Q_UNUSED(orientation)
Q_UNUSED(role)
return QVariant();
}
QModelIndex DataModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid() || column > 0 || row < 0 || row >= m_items.count()) {
return QModelIndex();
}
return createIndex(row, column, 0);
}
QModelIndex DataModel::parent(const QModelIndex &child) const
{
Q_UNUSED(child)
return QModelIndex();
}
int DataModel::rowCount(const QModelIndex &parent) const
{
//this is not a tree
//TODO: make it possible some day?
if (parent.isValid()) {
return 0;
}
return m_items.count();
}
int DataModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}
return 1;
}
}
#include "datamodel.moc"

View File

@ -0,0 +1,60 @@
/*
* Copyright 2009 by Alan Alpert <alan.alpert@nokia.com>
* Copyright 2010 by Ménard Alexis <menard@kde.org>
* Copyright 2010 by 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.
*/
#ifndef DATAMODEL_H
#define DATAMODEL_H
#include <QAbstractItemModel>
#include <QVector>
namespace Plasma
{
class DataModel : public QAbstractItemModel
{
Q_OBJECT
public:
DataModel(QObject* parent=0);
~DataModel();
void setItems(const QVariantList &list);
//Reimplemented
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &child) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
Q_SIGNALS:
void modelAboutToBeReset();
void modelReset();
private:
QVector<QVariant> m_items;
QHash<int, QByteArray> m_roleNames;
};
}
#endif

View File

@ -20,6 +20,8 @@
*/
#include "datasource_p.h"
#include "datamodel.h"
#include "qdeclarativeengine.h"
#include "qdeclarativecontext.h"
@ -46,6 +48,11 @@ DataSource::DataSource(QObject* parent)
this, SLOT(setupData()));
}
Plasma::DataModel *DataSource::modelFromKey(const QString &key)
{
return qobject_cast<DataModel*>(qvariant_cast<QObject*>(m_data->value(key.toLatin1())));
}
void DataSource::setSource(const QString &s)
{
if (s == m_source) {
@ -92,7 +99,25 @@ void DataSource::dataUpdated(const QString &sourceName, const Plasma::DataEngine
// Properties in QML must start lowercase.
QString ourKey = key.toLower();
if (data.value(key).canConvert<QVariantList>() && data.value(key).value<QVariantList>().first().canConvert<QVariantMap>()) {
DataModel *model = modelFromKey(ourKey);
if (model) {
model->removeRows(0, model->rowCount());
} else {
model = new DataModel(m_data);
m_data->insert(ourKey.toLatin1(), qVariantFromValue(static_cast<QObject*>(model)));
}
model->setItems(data.value(key).value<QVariantList>());
} else {
DataModel *model = modelFromKey(ourKey);
if (model) {
model->deleteLater();
}
m_data->insert(ourKey.toLatin1(), data.value(key));
}
newKeys << ourKey;
}
@ -101,6 +126,10 @@ void DataSource::dataUpdated(const QString &sourceName, const Plasma::DataEngine
//FIXME: pretty utterly inefficient
foreach (const QString &key, m_keys) {
if (!newKeys.contains(key)) {
DataModel *model = modelFromKey(key);
if (model) {
model->deleteLater();
}
m_data->insert(key.toLatin1(), QVariant());
}
}

View File

@ -34,9 +34,11 @@
class QDeclarativePropertyMap;
namespace Plasma
{
class DataEngine;
class DataModel;
class DataSource : public QObject, DataEngineConsumer
{
@ -84,8 +86,10 @@ namespace Plasma
void dataChanged();
void sourcesChanged();
private:
protected:
inline DataModel *modelFromKey(const QString &key);
private:
QString m_id;
int m_interval;
QString m_source;