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