| Line | Source Code | Coverage |
|---|
| 1 | /**************************************************************************** | - |
| | ** | |
| | ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). | |
| | ** Contact: http://www.qt-project.org/legal | |
| | ** | |
| | ** This file is part of the QtGui module of the Qt Toolkit. | |
| | ** | |
| | ** $QT_BEGIN_LICENSE:LGPL$ | |
| | ** Commercial License Usage | |
| | ** Licensees holding valid commercial Qt licenses may use this file in | |
| | ** accordance with the commercial license agreement provided with the | |
| | ** Software or, alternatively, in accordance with the terms contained in | |
| | ** a written agreement between you and Digia. For licensing terms and | |
| | ** conditions see http://qt.digia.com/licensing. For further information | |
| | ** use the contact form at http://qt.digia.com/contact-us. | |
| | ** | |
| | ** GNU Lesser General Public License Usage | |
| | ** Alternatively, this file may be used under the terms of the GNU Lesser | |
| | ** General Public License version 2.1 as published by the Free Software | |
| | ** Foundation and appearing in the file LICENSE.LGPL included in the | |
| | ** packaging of this file. Please review the following information to | |
| | ** ensure the GNU Lesser General Public License version 2.1 requirements | |
| | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | |
| | ** | |
| | ** In addition, as a special exception, Digia gives you certain additional | |
| | ** rights. These rights are described in the Digia Qt LGPL Exception | |
| | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | |
| | ** | |
| | ** GNU General Public License Usage | |
| | ** Alternatively, this file may be used under the terms of the GNU | |
| | ** General Public License version 3.0 as published by the Free Software | |
| | ** Foundation and appearing in the file LICENSE.GPL included in the | |
| | ** packaging of this file. Please review the following information to | |
| | ** ensure the GNU General Public License version 3.0 requirements will be | |
| | ** met: http://www.gnu.org/copyleft/gpl.html. | |
| | ** | |
| | ** | |
| | ** $QT_END_LICENSE$ | |
| | ** | |
| | ****************************************************************************/**************************************************************************** | |
| 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 | | - |
| 54 | QT_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 | | - |
| 97 | QStringListModel::QStringListModel(QObject *parent) | - |
| 98 | : QAbstractListModel(parent) | - |
| 99 | { | - |
| 100 | } | - |
| 101 | | - |
| 102 | /*! | - |
| 103 | Constructs a string list model containing the specified \a strings | - |
| 104 | with the given \a parent. | - |
| 105 | */ | - |
| 106 | | - |
| 107 | QStringListModel::QStringListModel(const QStringList &strings, QObject *parent) | - |
| 108 | : QAbstractListModel(parent), lst(strings) | - |
| 109 | { | - |
| 110 | } | - |
| 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 | | - |
| 123 | int QStringListModel::rowCount(const QModelIndex &parent) const | - |
| 124 | { | - |
| 125 | if (parent.isValid()) | - |
| 126 | return 0; | - |
| 127 | | - |
| 128 | return lst.count(); | - |
| 129 | } | - |
| 130 | | - |
| 131 | /*! | - |
| 132 | \reimp | - |
| 133 | */ | - |
| 134 | QModelIndex QStringListModel::sibling(int row, int column, 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 QStringListModel::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 | 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 | | - |
| 170 | Qt::ItemFlags QStringListModel::flags(const QModelIndex &index) const | - |
| 171 | { | - |
| 172 | if (!index.isValid()) | - |
| 173 | return QAbstractItemModel::flags(index) | Qt::ItemIsDropEnabled; | - |
| 174 | | - |
| 175 | return QAbstractItemModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; | - |
| 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 | | - |
| 187 | bool QStringListModel::setData(const QModelIndex &index, const QVariant &value, int role) | - |
| 188 | { | - |
| 189 | if (index.row() >= 0 && index.row() < lst.size() evaluated: index.row() >= 0| yes Evaluation Count:29 | yes Evaluation Count:35 |
partially evaluated: index.row() < lst.size()| yes Evaluation Count:29 | no Evaluation Count:0 |
| 0-35 |
| 190 | && (role == Qt::EditRole || role == Qt::DisplayRole)) { partially evaluated: role == Qt::EditRole| 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 | | - |
| 209 | bool QStringListModel::insertRows(int row, int count, const QModelIndex &parent) | - |
| 210 | { | - |
| 211 | if (count < 1 || row < 0 || row > rowCount(parent)) | - |
| 212 | return false; | - |
| 213 | | - |
| 214 | beginInsertRows(QModelIndex(), row, row + count - 1); | - |
| 215 | | - |
| 216 | for (int r = 0; r < count; ++r) | - |
| 217 | lst.insert(row, QString()); | - |
| 218 | | - |
| 219 | endInsertRows(); | - |
| 220 | | - |
| 221 | return true; | - |
| 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 | | - |
| 235 | bool QStringListModel::removeRows(int row, int count, const QModelIndex &parent) | - |
| 236 | { | - |
| 237 | if (count <= 0 || row < 0 || (row + count) > rowCount(parent)) | - |
| 238 | return false; | - |
| 239 | | - |
| 240 | beginRemoveRows(QModelIndex(), row, row + count - 1); | - |
| 241 | | - |
| 242 | for (int r = 0; r < count; ++r) | - |
| 243 | lst.removeAt(row); | - |
| 244 | | - |
| 245 | endRemoveRows(); | - |
| 246 | | - |
| 247 | return true; | - |
| 248 | } | - |
| 249 | | - |
| 250 | static bool ascendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) | - |
| 251 | { | - |
| 252 | return s1.first < s2.first; | - |
| 253 | } | - |
| 254 | | - |
| 255 | static bool decendingLessThan(const QPair<QString, int> &s1, const QPair<QString, int> &s2) | - |
| 256 | { | - |
| 257 | return s1.first > s2.first; | - |
| 258 | } | - |
| 259 | | - |
| 260 | /*! | - |
| 261 | \reimp | - |
| 262 | */ | - |
| 263 | void QStringListModel::sort(int, Qt::SortOrder order) | - |
| 264 | { | - |
| 265 | emit layoutAboutToBeChanged(QList<QPersistentModelIndex>(), VerticalSortHint); | - |
| 266 | | - |
| 267 | QList<QPair<QString, int> > list; | - |
| 268 | for (int i = 0; i < lst.count(); ++i) | - |
| 269 | list.append(QPair<QString, int>(lst.at(i), i)); | - |
| 270 | | - |
| 271 | if (order == Qt::AscendingOrder) | - |
| 272 | std::sort(list.begin(), list.end(), ascendingLessThan); | - |
| 273 | else | - |
| 274 | std::sort(list.begin(), list.end(), decendingLessThan); | - |
| 275 | | - |
| 276 | lst.clear(); | - |
| 277 | QVector<int> forwarding(list.count()); | - |
| 278 | for (int i = 0; i < list.count(); ++i) { | - |
| 279 | lst.append(list.at(i).first); | - |
| 280 | forwarding[list.at(i).second] = i; | - |
| 281 | } | - |
| 282 | | - |
| 283 | QModelIndexList oldList = persistentIndexList(); | - |
| 284 | QModelIndexList newList; | - |
| 285 | for (int i = 0; i < oldList.count(); ++i) | - |
| 286 | newList.append(index(forwarding.at(oldList.at(i).row()), 0)); | - |
| 287 | changePersistentIndexList(oldList, newList); | - |
| 288 | | - |
| 289 | emit layoutChanged(QList<QPersistentModelIndex>(), VerticalSortHint); | - |
| 290 | } | - |
| 291 | | - |
| 292 | /*! | - |
| 293 | Returns the string list used by the model to store data. | - |
| 294 | */ | - |
| 295 | QStringList QStringListModel::stringList() const | - |
| 296 | { | - |
| 297 | return lst; | - |
| 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 | */ | - |
| 306 | void QStringListModel::setStringList(const QStringList &strings) | - |
| 307 | { | - |
| 308 | emit beginResetModel(); | - |
| 309 | lst = strings; | - |
| 310 | emit endResetModel(); | - |
| 311 | } | - |
| 312 | | - |
| 313 | /*! | - |
| 314 | \reimp | - |
| 315 | */ | - |
| 316 | Qt::DropActions QStringListModel::supportedDropActions() const | - |
| 317 | { | - |
| 318 | return QAbstractItemModel::supportedDropActions() | Qt::MoveAction; | - |
| 319 | } | - |
| 320 | | - |
| 321 | QT_END_NAMESPACE | - |
| 322 | | - |
| 323 | #endif // QT_NO_STRINGLISTMODEL | - |
| 324 | | - |
| | |