Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/itemmodels/qstringlistmodel.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
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 The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free | - | ||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
25 | ** | - | ||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
29 | ** | - | ||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
31 | ** | - | ||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||
33 | - | |||||||||||||||||||
34 | /* | - | ||||||||||||||||||
35 | A simple model that uses a QStringList as its data source. | - | ||||||||||||||||||
36 | */ | - | ||||||||||||||||||
37 | - | |||||||||||||||||||
38 | #include "qstringlistmodel.h" | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | #include <QtCore/qvector.h> | - | ||||||||||||||||||
41 | - | |||||||||||||||||||
42 | #include <algorithm> | - | ||||||||||||||||||
43 | - | |||||||||||||||||||
44 | #ifndef QT_NO_STRINGLISTMODEL | - | ||||||||||||||||||
45 | - | |||||||||||||||||||
46 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
47 | - | |||||||||||||||||||
48 | /*! | - | ||||||||||||||||||
49 | \class QStringListModel | - | ||||||||||||||||||
50 | \inmodule QtCore | - | ||||||||||||||||||
51 | \brief The QStringListModel class provides a model that supplies strings to views. | - | ||||||||||||||||||
52 | - | |||||||||||||||||||
53 | \ingroup model-view | - | ||||||||||||||||||
54 | - | |||||||||||||||||||
55 | QStringListModel is an editable model that can be used for simple | - | ||||||||||||||||||
56 | cases where you need to display a number of strings in a view | - | ||||||||||||||||||
57 | widget, such as a QListView or a QComboBox. | - | ||||||||||||||||||
58 | - | |||||||||||||||||||
59 | The model provides all the standard functions of an editable | - | ||||||||||||||||||
60 | model, representing the data in the string list as a model with | - | ||||||||||||||||||
61 | one column and a number of rows equal to the number of items in | - | ||||||||||||||||||
62 | the list. | - | ||||||||||||||||||
63 | - | |||||||||||||||||||
64 | Model indexes corresponding to items are obtained with the | - | ||||||||||||||||||
65 | \l{QAbstractListModel::index()}{index()} function, and item flags | - | ||||||||||||||||||
66 | are obtained with flags(). Item data is read with the data() | - | ||||||||||||||||||
67 | function and written with setData(). The number of rows (and | - | ||||||||||||||||||
68 | number of items in the string list) can be found with the | - | ||||||||||||||||||
69 | rowCount() function. | - | ||||||||||||||||||
70 | - | |||||||||||||||||||
71 | The model can be constructed with an existing string list, or | - | ||||||||||||||||||
72 | strings can be set later with the setStringList() convenience | - | ||||||||||||||||||
73 | function. Strings can also be inserted in the usual way with the | - | ||||||||||||||||||
74 | insertRows() function, and removed with removeRows(). The contents | - | ||||||||||||||||||
75 | of the string list can be retrieved with the stringList() | - | ||||||||||||||||||
76 | convenience function. | - | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | An example usage of QStringListModel: | - | ||||||||||||||||||
79 | - | |||||||||||||||||||
80 | \snippet qstringlistmodel/main.cpp 0 | - | ||||||||||||||||||
81 | - | |||||||||||||||||||
82 | \sa QAbstractListModel, QAbstractItemModel, {Model Classes} | - | ||||||||||||||||||
83 | */ | - | ||||||||||||||||||
84 | - | |||||||||||||||||||
85 | /*! | - | ||||||||||||||||||
86 | Constructs a string list model with the given \a parent. | - | ||||||||||||||||||
87 | */ | - | ||||||||||||||||||
88 | - | |||||||||||||||||||
89 | QStringListModel::QStringListModel(QObject *parent) | - | ||||||||||||||||||
90 | : QAbstractListModel(parent) | - | ||||||||||||||||||
91 | { | - | ||||||||||||||||||
92 | } executed 283 times by 13 tests: end of block Executed by:
| 283 | ||||||||||||||||||
93 | - | |||||||||||||||||||
94 | /*! | - | ||||||||||||||||||
95 | Constructs a string list model containing the specified \a strings | - | ||||||||||||||||||
96 | with the given \a parent. | - | ||||||||||||||||||
97 | */ | - | ||||||||||||||||||
98 | - | |||||||||||||||||||
99 | QStringListModel::QStringListModel(const QStringList &strings, QObject *parent) | - | ||||||||||||||||||
100 | : QAbstractListModel(parent), lst(strings) | - | ||||||||||||||||||
101 | { | - | ||||||||||||||||||
102 | } executed 67 times by 14 tests: end of block Executed by:
| 67 | ||||||||||||||||||
103 | - | |||||||||||||||||||
104 | /*! | - | ||||||||||||||||||
105 | Returns the number of rows in the model. This value corresponds to the | - | ||||||||||||||||||
106 | number of items in the model's internal string list. | - | ||||||||||||||||||
107 | - | |||||||||||||||||||
108 | The optional \a parent argument is in most models used to specify | - | ||||||||||||||||||
109 | the parent of the rows to be counted. Because this is a list if a | - | ||||||||||||||||||
110 | valid parent is specified, the result will always be 0. | - | ||||||||||||||||||
111 | - | |||||||||||||||||||
112 | \sa insertRows(), removeRows(), QAbstractItemModel::rowCount() | - | ||||||||||||||||||
113 | */ | - | ||||||||||||||||||
114 | - | |||||||||||||||||||
115 | int QStringListModel::rowCount(const QModelIndex &parent) const | - | ||||||||||||||||||
116 | { | - | ||||||||||||||||||
117 | if (parent.isValid())
| 18-93345 | ||||||||||||||||||
118 | return 0; executed 18 times by 3 tests: return 0; Executed by:
| 18 | ||||||||||||||||||
119 | - | |||||||||||||||||||
120 | return lst.count(); executed 93345 times by 20 tests: return lst.count(); Executed by:
| 93345 | ||||||||||||||||||
121 | } | - | ||||||||||||||||||
122 | - | |||||||||||||||||||
123 | /*! | - | ||||||||||||||||||
124 | \reimp | - | ||||||||||||||||||
125 | */ | - | ||||||||||||||||||
126 | QModelIndex QStringListModel::sibling(int row, int column, const QModelIndex &idx) const | - | ||||||||||||||||||
127 | { | - | ||||||||||||||||||
128 | if (!idx.isValid() || column != 0 || row >= lst.count())
| 0-73 | ||||||||||||||||||
129 | return QModelIndex(); executed 5 times by 2 tests: return QModelIndex(); Executed by:
| 5 | ||||||||||||||||||
130 | - | |||||||||||||||||||
131 | return createIndex(row, 0); executed 73 times by 3 tests: return createIndex(row, 0); Executed by:
| 73 | ||||||||||||||||||
132 | } | - | ||||||||||||||||||
133 | - | |||||||||||||||||||
134 | /*! | - | ||||||||||||||||||
135 | Returns data for the specified \a role, from the item with the | - | ||||||||||||||||||
136 | given \a index. | - | ||||||||||||||||||
137 | - | |||||||||||||||||||
138 | If the view requests an invalid index, an invalid variant is returned. | - | ||||||||||||||||||
139 | - | |||||||||||||||||||
140 | \sa setData() | - | ||||||||||||||||||
141 | */ | - | ||||||||||||||||||
142 | - | |||||||||||||||||||
143 | QVariant QStringListModel::data(const QModelIndex &index, int role) const | - | ||||||||||||||||||
144 | { | - | ||||||||||||||||||
145 | if (index.row() < 0 || index.row() >= lst.size())
| 0-133254 | ||||||||||||||||||
146 | return QVariant(); executed 4737 times by 6 tests: return QVariant(); Executed by:
| 4737 | ||||||||||||||||||
147 | - | |||||||||||||||||||
148 | if (role == Qt::DisplayRole || role == Qt::EditRole)
| 4188-113222 | ||||||||||||||||||
149 | return lst.at(index.row()); executed 24220 times by 17 tests: return lst.at(index.row()); Executed by:
| 24220 | ||||||||||||||||||
150 | - | |||||||||||||||||||
151 | return QVariant(); executed 109034 times by 14 tests: return QVariant(); Executed by:
| 109034 | ||||||||||||||||||
152 | } | - | ||||||||||||||||||
153 | - | |||||||||||||||||||
154 | /*! | - | ||||||||||||||||||
155 | Returns the flags for the item with the given \a index. | - | ||||||||||||||||||
156 | - | |||||||||||||||||||
157 | Valid items are enabled, selectable, editable, drag enabled and drop enabled. | - | ||||||||||||||||||
158 | - | |||||||||||||||||||
159 | \sa QAbstractItemModel::flags() | - | ||||||||||||||||||
160 | */ | - | ||||||||||||||||||
161 | - | |||||||||||||||||||
162 | Qt::ItemFlags QStringListModel::flags(const QModelIndex &index) const | - | ||||||||||||||||||
163 | { | - | ||||||||||||||||||
164 | if (!index.isValid())
| 18-10586 | ||||||||||||||||||
165 | return QAbstractListModel::flags(index) | Qt::ItemIsDropEnabled; executed 18 times by 3 tests: return QAbstractListModel::flags(index) | Qt::ItemIsDropEnabled; Executed by:
| 18 | ||||||||||||||||||
166 | - | |||||||||||||||||||
167 | return QAbstractListModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; executed 10586 times by 13 tests: return QAbstractListModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; Executed by:
| 10586 | ||||||||||||||||||
168 | } | - | ||||||||||||||||||
169 | - | |||||||||||||||||||
170 | /*! | - | ||||||||||||||||||
171 | Sets the data for the specified \a role in the item with the given | - | ||||||||||||||||||
172 | \a index in the model, to the provided \a value. | - | ||||||||||||||||||
173 | - | |||||||||||||||||||
174 | The dataChanged() signal is emitted if the item is changed. | - | ||||||||||||||||||
175 | - | |||||||||||||||||||
176 | \sa Qt::ItemDataRole, data() | - | ||||||||||||||||||
177 | */ | - | ||||||||||||||||||
178 | - | |||||||||||||||||||
179 | bool QStringListModel::setData(const QModelIndex &index, const QVariant &value, int role) | - | ||||||||||||||||||
180 | { | - | ||||||||||||||||||
181 | if (index.row() >= 0 && index.row() < lst.size()
| 0-37 | ||||||||||||||||||
182 | && (role == Qt::EditRole || role == Qt::DisplayRole)) {
| 0-34 | ||||||||||||||||||
183 | lst.replace(index.row(), value.toString()); | - | ||||||||||||||||||
184 | QVector<int> roles; | - | ||||||||||||||||||
185 | roles.reserve(2); | - | ||||||||||||||||||
186 | roles.append(Qt::DisplayRole); | - | ||||||||||||||||||
187 | roles.append(Qt::EditRole); | - | ||||||||||||||||||
188 | emit dataChanged(index, index, roles); | - | ||||||||||||||||||
189 | // once Q_COMPILER_UNIFORM_INIT can be used, change to: | - | ||||||||||||||||||
190 | // emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); | - | ||||||||||||||||||
191 | return true; executed 37 times by 4 tests: return true; Executed by:
| 37 | ||||||||||||||||||
192 | } | - | ||||||||||||||||||
193 | return false; executed 35 times by 3 tests: return false; Executed by:
| 35 | ||||||||||||||||||
194 | } | - | ||||||||||||||||||
195 | - | |||||||||||||||||||
196 | /*! | - | ||||||||||||||||||
197 | Inserts \a count rows into the model, beginning at the given \a row. | - | ||||||||||||||||||
198 | - | |||||||||||||||||||
199 | The \a parent index of the rows is optional and is only used for | - | ||||||||||||||||||
200 | consistency with QAbstractItemModel. By default, a null index is | - | ||||||||||||||||||
201 | specified, indicating that the rows are inserted in the top level of | - | ||||||||||||||||||
202 | the model. | - | ||||||||||||||||||
203 | - | |||||||||||||||||||
204 | \sa QAbstractItemModel::insertRows() | - | ||||||||||||||||||
205 | */ | - | ||||||||||||||||||
206 | - | |||||||||||||||||||
207 | bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent) | - | ||||||||||||||||||
208 | { | - | ||||||||||||||||||
209 | if (count < 1 || row < 0 || row > rowCount(parent))
| 4-37 | ||||||||||||||||||
210 | return false; executed 24 times by 1 test: return false; Executed by:
| 24 | ||||||||||||||||||
211 | - | |||||||||||||||||||
212 | beginInsertRows(QModelIndex(), row, row + count - 1); | - | ||||||||||||||||||
213 | - | |||||||||||||||||||
214 | for (int r = 0; r < count; ++r)
| 29-140 | ||||||||||||||||||
215 | lst.insert(row, QString()); executed 140 times by 7 tests: lst.insert(row, QString()); Executed by:
| 140 | ||||||||||||||||||
216 | - | |||||||||||||||||||
217 | endInsertRows(); | - | ||||||||||||||||||
218 | - | |||||||||||||||||||
219 | return true; executed 29 times by 7 tests: return true; Executed by:
| 29 | ||||||||||||||||||
220 | } | - | ||||||||||||||||||
221 | - | |||||||||||||||||||
222 | /*! | - | ||||||||||||||||||
223 | Removes \a count rows from the model, beginning at the given \a row. | - | ||||||||||||||||||
224 | - | |||||||||||||||||||
225 | The \a parent index of the rows is optional and is only used for | - | ||||||||||||||||||
226 | consistency with QAbstractItemModel. By default, a null index is | - | ||||||||||||||||||
227 | specified, indicating that the rows are removed in the top level of | - | ||||||||||||||||||
228 | the model. | - | ||||||||||||||||||
229 | - | |||||||||||||||||||
230 | \sa QAbstractItemModel::removeRows() | - | ||||||||||||||||||
231 | */ | - | ||||||||||||||||||
232 | - | |||||||||||||||||||
233 | bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent) | - | ||||||||||||||||||
234 | { | - | ||||||||||||||||||
235 | if (count <= 0 || row < 0 || (row + count) > rowCount(parent))
| 6-39 | ||||||||||||||||||
236 | return false; executed 31 times by 2 tests: return false; Executed by:
| 31 | ||||||||||||||||||
237 | - | |||||||||||||||||||
238 | beginRemoveRows(QModelIndex(), row, row + count - 1); | - | ||||||||||||||||||
239 | - | |||||||||||||||||||
240 | for (int r = 0; r < count; ++r)
| 24-129 | ||||||||||||||||||
241 | lst.removeAt(row); executed 129 times by 5 tests: lst.removeAt(row); Executed by:
| 129 | ||||||||||||||||||
242 | - | |||||||||||||||||||
243 | endRemoveRows(); | - | ||||||||||||||||||
244 | - | |||||||||||||||||||
245 | return true; executed 24 times by 5 tests: return true; Executed by:
| 24 | ||||||||||||||||||
246 | } | - | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | static bool ascendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) | - | ||||||||||||||||||
249 | { | - | ||||||||||||||||||
250 | return s1.first < s2.first; executed 952 times by 2 tests: return s1.first < s2.first; Executed by:
| 952 | ||||||||||||||||||
251 | } | - | ||||||||||||||||||
252 | - | |||||||||||||||||||
253 | static bool decendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) | - | ||||||||||||||||||
254 | { | - | ||||||||||||||||||
255 | return s1.first > s2.first; executed 26 times by 1 test: return s1.first > s2.first; Executed by:
| 26 | ||||||||||||||||||
256 | } | - | ||||||||||||||||||
257 | - | |||||||||||||||||||
258 | /*! | - | ||||||||||||||||||
259 | \reimp | - | ||||||||||||||||||
260 | */ | - | ||||||||||||||||||
261 | void QStringListModel::sort(int, Qt::SortOrder order) | - | ||||||||||||||||||
262 | { | - | ||||||||||||||||||
263 | emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint); | - | ||||||||||||||||||
264 | - | |||||||||||||||||||
265 | QList<QPair<QString, int> > list; | - | ||||||||||||||||||
266 | const int lstCount = lst.count(); | - | ||||||||||||||||||
267 | list.reserve(lstCount); | - | ||||||||||||||||||
268 | for (int i = 0; i < lstCount; ++i)
| 19-347 | ||||||||||||||||||
269 | list.append(QPair<QString, int>(lst.at(i), i)); executed 347 times by 2 tests: list.append(QPair<QString, int>(lst.at(i), i)); Executed by:
| 347 | ||||||||||||||||||
270 | - | |||||||||||||||||||
271 | if (order == Qt::AscendingOrder)
| 4-15 | ||||||||||||||||||
272 | std::sort(list.begin(), list.end(), ascendingLessThan); executed 15 times by 2 tests: std::sort(list.begin(), list.end(), ascendingLessThan); Executed by:
| 15 | ||||||||||||||||||
273 | else | - | ||||||||||||||||||
274 | std::sort(list.begin(), list.end(), decendingLessThan); executed 4 times by 1 test: std::sort(list.begin(), list.end(), decendingLessThan); Executed by:
| 4 | ||||||||||||||||||
275 | - | |||||||||||||||||||
276 | lst.clear(); | - | ||||||||||||||||||
277 | QVector<int> forwarding(list.count()); | - | ||||||||||||||||||
278 | for (int i = 0; i < list.count(); ++i) {
| 19-347 | ||||||||||||||||||
279 | lst.append(list.at(i).first); | - | ||||||||||||||||||
280 | forwarding[list.at(i).second] = i; | - | ||||||||||||||||||
281 | } executed 347 times by 2 tests: end of block Executed by:
| 347 | ||||||||||||||||||
282 | - | |||||||||||||||||||
283 | QModelIndexList oldList = persistentIndexList(); | - | ||||||||||||||||||
284 | QModelIndexList newList; | - | ||||||||||||||||||
285 | const int numOldIndexes = oldList.count(); | - | ||||||||||||||||||
286 | newList.reserve(numOldIndexes); | - | ||||||||||||||||||
287 | for (int i = 0; i < numOldIndexes; ++i)
| 0-19 | ||||||||||||||||||
288 | newList.append(index(forwarding.at(oldList.at(i).row()), 0)); never executed: newList.append(index(forwarding.at(oldList.at(i).row()), 0)); | 0 | ||||||||||||||||||
289 | changePersistentIndexList(oldList, newList); | - | ||||||||||||||||||
290 | - | |||||||||||||||||||
291 | emit layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint); | - | ||||||||||||||||||
292 | } executed 19 times by 2 tests: end of block Executed by:
| 19 | ||||||||||||||||||
293 | - | |||||||||||||||||||
294 | /*! | - | ||||||||||||||||||
295 | Returns the string list used by the model to store data. | - | ||||||||||||||||||
296 | */ | - | ||||||||||||||||||
297 | QStringList QStringListModel::stringList() const | - | ||||||||||||||||||
298 | { | - | ||||||||||||||||||
299 | return lst; executed 1455 times by 2 tests: return lst; Executed by:
| 1455 | ||||||||||||||||||
300 | } | - | ||||||||||||||||||
301 | - | |||||||||||||||||||
302 | /*! | - | ||||||||||||||||||
303 | Sets the model's internal string list to \a strings. The model will | - | ||||||||||||||||||
304 | notify any attached views that its underlying data has changed. | - | ||||||||||||||||||
305 | - | |||||||||||||||||||
306 | \sa dataChanged() | - | ||||||||||||||||||
307 | */ | - | ||||||||||||||||||
308 | void QStringListModel::setStringList(const QStringList &strings) | - | ||||||||||||||||||
309 | { | - | ||||||||||||||||||
310 | beginResetModel(); | - | ||||||||||||||||||
311 | lst = strings; | - | ||||||||||||||||||
312 | endResetModel(); | - | ||||||||||||||||||
313 | } executed 618 times by 11 tests: end of block Executed by:
| 618 | ||||||||||||||||||
314 | - | |||||||||||||||||||
315 | /*! | - | ||||||||||||||||||
316 | \reimp | - | ||||||||||||||||||
317 | */ | - | ||||||||||||||||||
318 | Qt::DropActions QStringListModel::supportedDropActions() const | - | ||||||||||||||||||
319 | { | - | ||||||||||||||||||
320 | return QAbstractItemModel::supportedDropActions() | Qt::MoveAction; executed 19 times by 4 tests: return QAbstractItemModel::supportedDropActions() | Qt::MoveAction; Executed by:
| 19 | ||||||||||||||||||
321 | } | - | ||||||||||||||||||
322 | - | |||||||||||||||||||
323 | QT_END_NAMESPACE | - | ||||||||||||||||||
324 | - | |||||||||||||||||||
325 | #endif // QT_NO_STRINGLISTMODEL | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |