itemmodels/qstringlistmodel.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
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 QStringList as its data source. -
44*/ -
45 -
46#include "qstringlistmodel.h" -
47 -
48#include <QtCore/qvector.h> -
49 -
50#include <algorithm> -
51 -
52#ifndef QT_NO_STRINGLISTMODEL -
53 -
54QT_BEGIN_NAMESPACE -
55 -
56/*! -
57 \class QStringListModel -
58 \inmodule QtCore -
59 \brief The QStringListModel class provides a model that supplies strings to views. -
60 -
61 \ingroup model-view -
62 -
63 QStringListModel is an editable model that can be used for simple -
64 cases where you need to display a number of strings in a view -
65 widget, such as a QListView or a QComboBox. -
66 -
67 The model provides all the standard functions of an editable -
68 model, representing the data in the string list as a model with -
69 one column and a number of rows equal to the number of items in -
70 the list. -
71 -
72 Model indexes corresponding to items are obtained with the -
73 \l{QAbstractListModel::index()}{index()} function, and item flags -
74 are obtained with flags(). Item data is read with the data() -
75 function and written with setData(). The number of rows (and -
76 number of items in the string list) can be found with the -
77 rowCount() function. -
78 -
79 The model can be constructed with an existing string list, or -
80 strings can be set later with the setStringList() convenience -
81 function. Strings can also be inserted in the usual way with the -
82 insertRows() function, and removed with removeRows(). The contents -
83 of the string list can be retrieved with the stringList() -
84 convenience function. -
85 -
86 An example usage of QStringListModel: -
87 -
88 \snippet qstringlistmodel/main.cpp 0 -
89 -
90 \sa QAbstractListModel, QAbstractItemModel, {Model Classes} -
91*/ -
92 -
93/*! -
94 Constructs a string list model with the given \a parent. -
95*/ -
96 -
97QStringListModel::QStringListModel(QObject *parent) -
98 : QAbstractListModel(parent) -
99{ -
100}
executed: }
Execution Count:323
323
101 -
102/*! -
103 Constructs a string list model containing the specified \a strings -
104 with the given \a parent. -
105*/ -
106 -
107QStringListModel::QStringListModel(const QStringList &strings, QObject *parent) -
108 : QAbstractListModel(parent), lst(strings) -
109{ -
110}
executed: }
Execution Count:50
50
111 -
112/*! -
113 Returns the number of rows in the model. This value corresponds to the -
114 number of items in the model's internal string list. -
115 -
116 The optional \a parent argument is in most models used to specify -
117 the parent of the rows to be counted. Because this is a list if a -
118 valid parent is specified, the result will always be 0. -
119 -
120 \sa insertRows(), removeRows(), QAbstractItemModel::rowCount() -
121*/ -
122 -
123int QStringListModel::rowCount(const QModelIndex &parent) const -
124{ -
125 if (parent.isValid())
evaluated: parent.isValid()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:86753
18-86753
126 return 0;
executed: return 0;
Execution Count:18
18
127 -
128 return lst.count();
executed: return lst.count();
Execution Count:86753
86753
129} -
130 -
131/*! -
132 \reimp -
133*/ -
134QModelIndex QStringListModel::sibling(int row, int column, const QModelIndex &idx) const -
135{ -
136 if (!idx.isValid() || column != 0 || row >= lst.count())
evaluated: !idx.isValid()
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:73
partially evaluated: column != 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:73
partially evaluated: row >= lst.count()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:73
0-73
137 return QModelIndex();
executed: return QModelIndex();
Execution Count:5
5
138 -
139 return createIndex(row, 0);
executed: return createIndex(row, 0);
Execution Count:73
73
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 -
151QVariant QStringListModel::data(const QModelIndex &index, int role) const -
152{ -
153 if (index.row() < 0 || index.row() >= lst.size())
evaluated: index.row() < 0
TRUEFALSE
yes
Evaluation Count:4784
yes
Evaluation Count:164920
partially evaluated: index.row() >= lst.size()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:164920
0-164920
154 return QVariant();
executed: return QVariant();
Execution Count:4784
4784
155 -
156 if (role == Qt::DisplayRole || role == Qt::EditRole)
evaluated: role == Qt::DisplayRole
TRUEFALSE
yes
Evaluation Count:21632
yes
Evaluation Count:143288
evaluated: role == Qt::EditRole
TRUEFALSE
yes
Evaluation Count:600
yes
Evaluation Count:142688
600-143288
157 return lst.at(index.row());
executed: return lst.at(index.row());
Execution Count:22232
22232
158 -
159 return QVariant();
executed: return QVariant();
Execution Count:142688
142688
160} -
161 -
162/*! -
163 Returns the flags for the item with the given \a index. -
164 -
165 Valid items are enabled, selectable, editable, drag enabled and drop enabled. -
166 -
167 \sa QAbstractItemModel::flags() -
168*/ -
169 -
170Qt::ItemFlags QStringListModel::flags(const QModelIndex &index) const -
171{ -
172 if (!index.isValid())
evaluated: !index.isValid()
TRUEFALSE
yes
Evaluation Count:18
yes
Evaluation Count:8640
18-8640
173 return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled;
executed: return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled;
Execution Count:18
18
174 -
175 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
executed: return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
Execution Count:8640
8640
176} -
177 -
178/*! -
179 Sets the data for the specified \a role in the item with the given -
180 \a index in the model, to the provided \a value. -
181 -
182 The dataChanged() signal is emitted if the item is changed. -
183 -
184 \sa Qt::ItemDataRole, data() -
185*/ -
186 -
187bool QStringListModel::setData(const QModelIndex &index, const QVariant &value, int role) -
188{ -
189 if (index.row() >= 0 && index.row() < lst.size()
evaluated: index.row() >= 0
TRUEFALSE
yes
Evaluation Count:29
yes
Evaluation Count:35
partially evaluated: index.row() < lst.size()
TRUEFALSE
yes
Evaluation Count:29
no
Evaluation Count:0
0-35
190 && (role == Qt::EditRole || role == Qt::DisplayRole)) {
partially evaluated: role == Qt::EditRole
TRUEFALSE
yes
Evaluation Count:29
no
Evaluation Count:0
never evaluated: role == Qt::DisplayRole
0-29
191 lst.replace(index.row(), value.toString());
executed (the execution status of this line is deduced): lst.replace(index.row(), value.toString());
-
192 emit dataChanged(index, index, QVector<int>() << role);
executed (the execution status of this line is deduced): dataChanged(index, index, QVector<int>() << role);
-
193 return true;
executed: return true;
Execution Count:29
29
194 } -
195 return false;
executed: return false;
Execution Count:35
35
196} -
197 -
198/*! -
199 Inserts \a count rows into the model, beginning at the given \a row. -
200 -
201 The \a parent index of the rows is optional and is only used for -
202 consistency with QAbstractItemModel. By default, a null index is -
203 specified, indicating that the rows are inserted in the top level of -
204 the model. -
205 -
206 \sa QAbstractItemModel::insertRows() -
207*/ -
208 -
209bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent) -
210{ -
211 if (count < 1 || row < 0 || row > rowCount(parent))
evaluated: count < 1
TRUEFALSE
yes
Evaluation Count:16
yes
Evaluation Count:34
evaluated: row < 0
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:30
evaluated: row > rowCount(parent)
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:26
4-34
212 return false;
executed: return false;
Execution Count:24
24
213 -
214 beginInsertRows(QModelIndex(), row, row + count - 1);
executed (the execution status of this line is deduced): beginInsertRows(QModelIndex(), row, row + count - 1);
-
215 -
216 for (int r = 0; r < count; ++r)
evaluated: r < count
TRUEFALSE
yes
Evaluation Count:137
yes
Evaluation Count:26
26-137
217 lst.insert(row, QString());
executed: lst.insert(row, QString());
Execution Count:137
137
218 -
219 endInsertRows();
executed (the execution status of this line is deduced): endInsertRows();
-
220 -
221 return true;
executed: return true;
Execution Count:26
26
222} -
223 -
224/*! -
225 Removes \a count rows from the model, beginning at the given \a row. -
226 -
227 The \a parent index of the rows is optional and is only used for -
228 consistency with QAbstractItemModel. By default, a null index is -
229 specified, indicating that the rows are removed in the top level of -
230 the model. -
231 -
232 \sa QAbstractItemModel::removeRows() -
233*/ -
234 -
235bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent) -
236{ -
237 if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
evaluated: count <= 0
TRUEFALSE
yes
Evaluation Count:16
yes
Evaluation Count:37
evaluated: row < 0
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:31
evaluated: (row + count) > rowCount(parent)
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:22
6-37
238 return false;
executed: return false;
Execution Count:31
31
239 -
240 beginRemoveRows(QModelIndex(), row, row + count - 1);
executed (the execution status of this line is deduced): beginRemoveRows(QModelIndex(), row, row + count - 1);
-
241 -
242 for (int r = 0; r < count; ++r)
evaluated: r < count
TRUEFALSE
yes
Evaluation Count:127
yes
Evaluation Count:22
22-127
243 lst.removeAt(row);
executed: lst.removeAt(row);
Execution Count:127
127
244 -
245 endRemoveRows();
executed (the execution status of this line is deduced): endRemoveRows();
-
246 -
247 return true;
executed: return true;
Execution Count:22
22
248} -
249 -
250static bool ascendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) -
251{ -
252 return s1.first < s2.first;
executed: return s1.first < s2.first;
Execution Count:844
844
253} -
254 -
255static bool decendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) -
256{ -
257 return s1.first > s2.first;
executed: return s1.first > s2.first;
Execution Count:26
26
258} -
259 -
260/*! -
261 \reimp -
262*/ -
263void QStringListModel::sort(int, Qt::SortOrder order) -
264{ -
265 emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
executed (the execution status of this line is deduced): layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
-
266 -
267 QList<QPair<QString, int> > list;
executed (the execution status of this line is deduced): QList<QPair<QString, int> > list;
-
268 for (int i = 0; i < lst.count(); ++i)
evaluated: i < lst.count()
TRUEFALSE
yes
Evaluation Count:347
yes
Evaluation Count:19
19-347
269 list.append(QPair<QString, int>(lst.at(i), i));
executed: list.append(QPair<QString, int>(lst.at(i), i));
Execution Count:347
347
270 -
271 if (order == Qt::AscendingOrder)
evaluated: order == Qt::AscendingOrder
TRUEFALSE
yes
Evaluation Count:15
yes
Evaluation Count:4
4-15
272 std::sort(list.begin(), list.end(), ascendingLessThan);
executed: std::sort(list.begin(), list.end(), ascendingLessThan);
Execution Count:15
15
273 else -
274 std::sort(list.begin(), list.end(), decendingLessThan);
executed: std::sort(list.begin(), list.end(), decendingLessThan);
Execution Count:4
4
275 -
276 lst.clear();
executed (the execution status of this line is deduced): lst.clear();
-
277 QVector<int> forwarding(list.count());
executed (the execution status of this line is deduced): QVector<int> forwarding(list.count());
-
278 for (int i = 0; i < list.count(); ++i) {
evaluated: i < list.count()
TRUEFALSE
yes
Evaluation Count:347
yes
Evaluation Count:19
19-347
279 lst.append(list.at(i).first);
executed (the execution status of this line is deduced): lst.append(list.at(i).first);
-
280 forwarding[list.at(i).second] = i;
executed (the execution status of this line is deduced): forwarding[list.at(i).second] = i;
-
281 }
executed: }
Execution Count:347
347
282 -
283 QModelIndexList oldList = persistentIndexList();
executed (the execution status of this line is deduced): QModelIndexList oldList = persistentIndexList();
-
284 QModelIndexList newList;
executed (the execution status of this line is deduced): QModelIndexList newList;
-
285 for (int i = 0; i < oldList.count(); ++i)
partially evaluated: i < oldList.count()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:19
0-19
286 newList.append(index(forwarding.at(oldList.at(i).row()), 0));
never executed: newList.append(index(forwarding.at(oldList.at(i).row()), 0));
0
287 changePersistentIndexList(oldList, newList);
executed (the execution status of this line is deduced): changePersistentIndexList(oldList, newList);
-
288 -
289 emit layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
executed (the execution status of this line is deduced): layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint);
-
290}
executed: }
Execution Count:19
19
291 -
292/*! -
293 Returns the string list used by the model to store data. -
294*/ -
295QStringList QStringListModel::stringList() const -
296{ -
297 return lst;
executed: return lst;
Execution Count:2514
2514
298} -
299 -
300/*! -
301 Sets the model's internal string list to \a strings. The model will -
302 notify any attached views that its underlying data has changed. -
303 -
304 \sa dataChanged() -
305*/ -
306void QStringListModel::setStringList(const QStringList &strings) -
307{ -
308 emit beginResetModel();
executed (the execution status of this line is deduced): beginResetModel();
-
309 lst = strings;
executed (the execution status of this line is deduced): lst = strings;
-
310 emit endResetModel();
executed (the execution status of this line is deduced): endResetModel();
-
311}
executed: }
Execution Count:829
829
312 -
313/*! -
314 \reimp -
315*/ -
316Qt::DropActions QStringListModel::supportedDropActions() const -
317{ -
318 return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
executed: return QAbstractItemModel::supportedDropActions() | Qt::MoveAction;
Execution Count:19
19
319} -
320 -
321QT_END_NAMESPACE -
322 -
323#endif // QT_NO_STRINGLISTMODEL -
324 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial