~mhr3/unity8/fix-1297246

« back to all changes in this revision

Viewing changes to plugins/Utils/qvariantlistmodel.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
55
/*!
 
56
 \class QVariantListModel
 
57
 \brief The QVariantListModel class provides a model that supplies variants to views.
 
58
 
 
59
 QVariantListModel is an editable model that can be used for simple
 
60
 cases where you need to display a number of variants in a view.
 
61
 
 
62
 The model provides all the standard functions of an editable
 
63
 model, representing the data in the variant list as a model with
 
64
 one column and a number of rows equal to the number of items in
 
65
 the list.
 
66
 
 
67
 Model indexes corresponding to items are obtained with the
 
68
 \l{QAbstractListModel::index()}{index()} function.  Item data is
 
69
 read with the data() function and written with setData().
 
70
 The number of rows (and number of items in the variant list)
 
71
 can be found with the rowCount() function.
 
72
 
 
73
 The model can be constructed with an existing variant list, or
 
74
 variants can be set later with the setVariantList() convenience
 
75
 function. Variants can also be inserted in the usual way with the
 
76
 insertRows() function, and removed with removeRows(). The contents
 
77
 of the variant list can be retrieved with the variantList()
 
78
 convenience function.
 
79
 
 
80
 \sa QAbstractListModel, QAbstractItemModel, {Model Classes}
 
81
 */
 
82
 
 
83
/*!
 
84
 Constructs a variant list model with the given \a parent.
 
85
 */
 
86
 
 
87
QVariantListModel::QVariantListModel(QObject *parent) :
 
88
        QAbstractListModel(parent)
 
89
{
 
90
    QHash<int, QByteArray> roles(roleNames());
 
91
    roles[Qt::DisplayRole] = "modelData";
 
92
    setRoleNames(roles);
 
93
}
 
94
 
 
95
/*!
 
96
 Constructs a variant list model containing the specified \a list
 
97
 with the given \a parent.
 
98
 */
 
99
 
 
100
QVariantListModel::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
 
 
108
QVariantListModel::~QVariantListModel() {
 
109
}
 
110
 
 
111
/*!
 
112
 Returns the number of rows in the model. This value corresponds to the
 
113
 number of items in the model's internal variant list.
 
114
 
 
115
 The optional \a parent argument is in most models used to specify
 
116
 the parent of the rows to be counted. Because this is a list if a
 
117
 valid parent is specified, the result will always be 0.
 
118
 
 
119
 \sa insertRows(), removeRows(), QAbstractItemModel::rowCount()
 
120
 */
 
121
 
 
122
int QVariantListModel::rowCount(const QModelIndex &parent) const
 
123
{
 
124
    if (parent.isValid())
 
125
        return 0;
 
126
 
 
127
    return lst.count();
 
128
}
 
129
 
 
130
/*!
 
131
 \reimp
 
132
 */
 
133
QModelIndex 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
 
 
142
/*!
 
143
 Returns data for the specified \a role, from the item with the
 
144
 given \a index.
 
145
 
 
146
 If the view requests an invalid index, an invalid variant is returned.
 
147
 
 
148
 \sa setData()
 
149
 */
 
150
 
 
151
QVariant 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
 
 
162
/*!
 
163
 Sets the data for the specified \a role in the item with the given
 
164
 \a index in the model, to the provided \a value.
 
165
 
 
166
 The dataChanged() signal is emitted if the item is changed.
 
167
 
 
168
 \sa Qt::ItemDataRole, data()
 
169
 */
 
170
 
 
171
bool 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
 
 
184
/*!
 
185
 Inserts \a count rows into the model, beginning at the given \a row.
 
186
 
 
187
 The \a parent index of the rows is optional and is only used for
 
188
 consistency with QAbstractItemModel. By default, a null index is
 
189
 specified, indicating that the rows are inserted in the top level of
 
190
 the model.
 
191
 
 
192
 \sa QAbstractItemModel::insertRows()
 
193
 */
 
194
 
 
195
bool 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
 
 
211
/*!
 
212
 Removes \a count rows from the model, beginning at the given \a row.
 
213
 
 
214
 The \a parent index of the rows is optional and is only used for
 
215
 consistency with QAbstractItemModel. By default, a null index is
 
216
 specified, indicating that the rows are removed in the top level of
 
217
 the model.
 
218
 
 
219
 \sa QAbstractItemModel::removeRows()
 
220
 */
 
221
 
 
222
bool 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
 
 
238
/*!
 
239
 Returns the variant list used by the model to store data.
 
240
 */
 
241
QVariantList QVariantListModel::variantList() const
 
242
{
 
243
    return lst;
 
244
}
 
245
 
 
246
/*!
 
247
 Sets the model's internal variant list to \a list. The model will
 
248
 notify any attached views that its underlying data has changed.
 
249
 
 
250
 \sa dataChanged()
 
251
 */
 
252
void 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
}