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 documentation 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 | **
|
---|
44 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
45 | ** Contact: Qt Software Information ([email protected])
|
---|
46 | **
|
---|
47 | ** This file is part of an example program for Qt.
|
---|
48 | ** EDITIONS: NOLIMITS
|
---|
49 | **
|
---|
50 | ****************************************************************************/
|
---|
51 |
|
---|
52 | /*
|
---|
53 | model.cpp
|
---|
54 |
|
---|
55 | A simple model that uses a QStringList as its data source.
|
---|
56 | */
|
---|
57 |
|
---|
58 | #include "model.h"
|
---|
59 |
|
---|
60 | /*!
|
---|
61 | Returns the number of items in the string list as the number of rows
|
---|
62 | in the model.
|
---|
63 | */
|
---|
64 |
|
---|
65 | int StringListModel::rowCount(const QModelIndex &parent) const
|
---|
66 | {
|
---|
67 | return stringList.count();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /*!
|
---|
71 | Returns an appropriate value for the requested data.
|
---|
72 | If the view requests an invalid index, an invalid variant is returned.
|
---|
73 | Any valid index that corresponds to a string in the list causes that
|
---|
74 | string to be returned.
|
---|
75 | */
|
---|
76 |
|
---|
77 | QVariant StringListModel::data(const QModelIndex &index, int role) const
|
---|
78 | {
|
---|
79 | if (!index.isValid())
|
---|
80 | return QVariant();
|
---|
81 |
|
---|
82 | if (index.row() >= stringList.size())
|
---|
83 | return QVariant();
|
---|
84 |
|
---|
85 | if (role == Qt::DisplayRole)
|
---|
86 | return stringList.at(index.row());
|
---|
87 | else
|
---|
88 | return QVariant();
|
---|
89 | }
|
---|
90 |
|
---|
91 | /*!
|
---|
92 | Returns the appropriate header string depending on the orientation of
|
---|
93 | the header and the section. If anything other than the display role is
|
---|
94 | requested, we return an invalid variant.
|
---|
95 | */
|
---|
96 |
|
---|
97 | QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
|
---|
98 | int role) const
|
---|
99 | {
|
---|
100 | if (role != Qt::DisplayRole)
|
---|
101 | return QVariant();
|
---|
102 |
|
---|
103 | if (orientation == Qt::Horizontal)
|
---|
104 | return QString("Column %1").arg(section);
|
---|
105 | else
|
---|
106 | return QString("Row %1").arg(section);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /*!
|
---|
110 | Returns an appropriate value for the item's flags. Valid items are
|
---|
111 | enabled, selectable, and editable.
|
---|
112 | */
|
---|
113 |
|
---|
114 | Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
|
---|
115 | {
|
---|
116 | if (!index.isValid())
|
---|
117 | return Qt::ItemIsEnabled;
|
---|
118 |
|
---|
119 | return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /*!
|
---|
123 | Changes an item in the string list, but only if the following conditions
|
---|
124 | are met:
|
---|
125 |
|
---|
126 | * The index supplied is valid.
|
---|
127 | * The index corresponds to an item to be shown in a view.
|
---|
128 | * The role associated with editing text is specified.
|
---|
129 |
|
---|
130 | The dataChanged() signal is emitted if the item is changed.
|
---|
131 | */
|
---|
132 |
|
---|
133 | bool StringListModel::setData(const QModelIndex &index,
|
---|
134 | const QVariant &value, int role)
|
---|
135 | {
|
---|
136 | if (index.isValid() && role == Qt::EditRole) {
|
---|
137 |
|
---|
138 | stringList.replace(index.row(), value.toString());
|
---|
139 | emit dataChanged(index, index);
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /*!
|
---|
146 | Inserts a number of rows into the model at the specified position.
|
---|
147 | */
|
---|
148 |
|
---|
149 | bool StringListModel::insertRows(int position, int rows, const QModelIndex &parent)
|
---|
150 | {
|
---|
151 | beginInsertRows(QModelIndex(), position, position+rows-1);
|
---|
152 |
|
---|
153 | for (int row = 0; row < rows; ++row) {
|
---|
154 | stringList.insert(position, "");
|
---|
155 | }
|
---|
156 |
|
---|
157 | endInsertRows();
|
---|
158 | return true;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*!
|
---|
162 | Removes a number of rows from the model at the specified position.
|
---|
163 | */
|
---|
164 |
|
---|
165 | bool StringListModel::removeRows(int position, int rows, const QModelIndex &parent)
|
---|
166 | {
|
---|
167 | beginRemoveRows(QModelIndex(), position, position+rows-1);
|
---|
168 |
|
---|
169 | for (int row = 0; row < rows; ++row) {
|
---|
170 | stringList.removeAt(position);
|
---|
171 | }
|
---|
172 |
|
---|
173 | endRemoveRows();
|
---|
174 | return true;
|
---|
175 | }
|
---|