Lomiri
Loading...
Searching...
No Matches
qvariantlistmodel.cpp
1/****************************************************************************
2**
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*
43 A simple model that uses a QVariantList as its data source.
44 */
45
46// LightDM currently is Qt4 compatible, and so doesn't define setRoleNames.
47// To use the same method of setting role name that it does, we
48// set our compatibility to Qt4 here too.
49#define QT_DISABLE_DEPRECATED_BEFORE QT_VERSION_CHECK(4, 0, 0)
50
51#include "qvariantlistmodel.h"
52
53#include <QtCore/qvector.h>
54
88 QAbstractListModel(parent)
89{
90 QHash<int, QByteArray> roles(roleNames());
91 roles[Qt::DisplayRole] = "modelData";
92 setRoleNames(roles);
93}
94
100QVariantListModel::QVariantListModel(const QVariantList &list, QObject *parent) :
101 QAbstractListModel(parent), lst(list)
102{
103 QHash<int, QByteArray> roles(roleNames());
104 roles[Qt::DisplayRole] = "modelData";
105 setRoleNames(roles);
106}
107
108QVariantListModel::~QVariantListModel() {
109}
110
122int QVariantListModel::rowCount(const QModelIndex &parent) const
123{
124 if (parent.isValid())
125 return 0;
126
127 return lst.count();
128}
129
133QModelIndex QVariantListModel::sibling(int row, int column,
134 const QModelIndex &idx) const
135{
136 if (!idx.isValid() || column != 0 || row >= lst.count())
137 return QModelIndex();
138
139 return createIndex(row, 0);
140}
141
151QVariant QVariantListModel::data(const QModelIndex &index, int role) const
152{
153 if (index.row() < 0 || index.row() >= lst.size())
154 return QVariant();
155
156 if (role == Qt::DisplayRole || role == Qt::EditRole)
157 return lst.at(index.row());
158
159 return QVariant();
160}
161
171bool QVariantListModel::setData(const QModelIndex &index, const QVariant &value,
172 int role)
173{
174 if (index.row() >= 0 && index.row() < lst.size()
175 && (role == Qt::EditRole || role == Qt::DisplayRole))
176 {
177 lst.replace(index.row(), value);
178 dataChanged(index, index, QVector<int>() << role);
179 return true;
180 }
181 return false;
182}
183
195bool QVariantListModel::insertRows(int row, int count,
196 const QModelIndex &parent)
197{
198 if (count < 1 || row < 0 || row > rowCount(parent))
199 return false;
200
201 beginInsertRows(QModelIndex(), row, row + count - 1);
202
203 for (int r = 0; r < count; ++r)
204 lst.insert(row, QVariant());
205
206 endInsertRows();
207
208 return true;
209}
210
222bool QVariantListModel::removeRows(int row, int count,
223 const QModelIndex &parent)
224{
225 if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
226 return false;
227
228 beginRemoveRows(QModelIndex(), row, row + count - 1);
229
230 for (int r = 0; r < count; ++r)
231 lst.removeAt(row);
232
233 endRemoveRows();
234
235 return true;
236}
237
242{
243 return lst;
244}
245
252void QVariantListModel::setVariantList(const QVariantList &list)
253{
254 int size = lst.size();
255 bool sameSize = list.size() == size;
256 if (!sameSize)
257 {
258 beginResetModel();
259 }
260 lst = list;
261 if (!sameSize)
262 {
263 endResetModel();
264 } else
265 {
266 dataChanged(QAbstractListModel::index(0),
267 QAbstractListModel::index(size - 1));
268 }
269}
QModelIndex sibling(int row, int column, const QModelIndex &idx) const override
QVariantListModel(QObject *parent=0)
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
QVariantList variantList() const
void setVariantList(const QVariantList &list)
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override