| 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)
|
|---|
|
|---|