Loading...
Searching...
No Matches
qvariantlistmodel.cpp
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/*
A simple model that uses a QVariantList as its data source.
*/
// LightDM currently is Qt4 compatible, and so doesn't define setRoleNames.
// To use the same method of setting role name that it does, we
// set our compatibility to Qt4 here too.
#define QT_DISABLE_DEPRECATED_BEFORE QT_VERSION_CHECK(4, 0, 0)
#include "qvariantlistmodel.h"
#include <QtCore/qvector.h>
QVariantListModel::QVariantListModel(QObject *parent) :
QAbstractListModel(parent)
{
this->roles = QAbstractListModel::roleNames();
this->roles[Qt::DisplayRole] = "modelData";
}
QVariantListModel::QVariantListModel(const QVariantList &list, QObject *parent) :
QAbstractListModel(parent), lst(list)
{
this->roles = QAbstractListModel::roleNames();
this->roles[Qt::DisplayRole] = "modelData";
}
QVariantListModel::~QVariantListModel() {
}
QHash<int, QByteArray> QVariantListModel::roleNames() const
{
return this->roles;
}
int QVariantListModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return lst.count();
}
QModelIndex QVariantListModel::sibling(int row, int column,
const QModelIndex &idx) const
{
if (!idx.isValid() || column != 0 || row >= lst.count())
return QModelIndex();
return createIndex(row, 0);
}
QVariant QVariantListModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= lst.size())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
return lst.at(index.row());
return QVariant();
}
bool QVariantListModel::setData(const QModelIndex &index, const QVariant &value,
int role)
{
if (index.row() >= 0 && index.row() < lst.size()
&& (role == Qt::EditRole || role == Qt::DisplayRole))
{
lst.replace(index.row(), value);
dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
bool QVariantListModel::insertRows(int row, int count,
const QModelIndex &parent)
{
if (count < 1 || row < 0 || row > rowCount(parent))
return false;
beginInsertRows(QModelIndex(), row, row + count - 1);
for (int r = 0; r < count; ++r)
lst.insert(row, QVariant());
endInsertRows();
return true;
}
bool QVariantListModel::removeRows(int row, int count,
const QModelIndex &parent)
{
if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
return false;
beginRemoveRows(QModelIndex(), row, row + count - 1);
for (int r = 0; r < count; ++r)
lst.removeAt(row);
endRemoveRows();
return true;
}
QVariantList QVariantListModel::variantList() const
{
return lst;
}
void QVariantListModel::setVariantList(const QVariantList &list)
{
int size = lst.size();
bool sameSize = list.size() == size;
if (!sameSize)
{
beginResetModel();
}
lst = list;
if (!sameSize)
{
endResetModel();
} else
{
dataChanged(QAbstractListModel::index(0),
QAbstractListModel::index(size - 1));
}
}
QVariantListModel::roleNames
QHash< int, QByteArray > roleNames() const override
Definition qvariantlistmodel.cpp:120
QVariantListModel::sibling
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
Definition qvariantlistmodel.cpp:136
QVariantListModel::QVariantListModel
QVariantListModel(QObject *parent=0)
Definition qvariantlistmodel.cpp:87
QVariantListModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition qvariantlistmodel.cpp:174
QVariantListModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition qvariantlistmodel.cpp:154
QVariantListModel::removeRows
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition qvariantlistmodel.cpp:225
QVariantListModel::variantList
QVariantList variantList() const
Definition qvariantlistmodel.cpp:244
QVariantListModel::setVariantList
void setVariantList(const QVariantList &list)
Definition qvariantlistmodel.cpp:255
QVariantListModel::insertRows
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
Definition qvariantlistmodel.cpp:198