plasma-framework/declarativeimports/core/datamodel.cpp

117 lines
2.8 KiB
C++
Raw Normal View History

/*
* 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"