source: trunk/doc/src/snippets/stringlistmodel/model.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41/*
42 model.cpp
43
44 A simple model that uses a QStringList as its data source.
45*/
46
47#include "model.h"
48
49/*!
50 Returns the number of items in the string list as the number of rows
51 in the model.
52*/
53
54//! [0]
55int StringListModel::rowCount(const QModelIndex &parent) const
56{
57 return stringList.count();
58}
59//! [0]
60
61
62#ifdef 0
63// This represents a read-only version of data(), an early stage in the
64// development of the example leading to an editable StringListModel.
65
66/*!
67 Returns an appropriate value for the requested data.
68 If the view requests an invalid index, an invalid variant is returned.
69 Any valid index that corresponds to a string in the list causes that
70 string to be returned.
71*/
72
73//! [1-data-read-only]
74QVariant StringListModel::data(const QModelIndex &index, int role) const
75{
76 if (!index.isValid())
77 return QVariant();
78
79 if (index.row() >= stringList.size())
80 return QVariant();
81
82 if (role == Qt::DisplayRole)
83 return stringList.at(index.row());
84 else
85 return QVariant();
86}
87//! [1-data-read-only]
88#endif
89
90
91/*!
92 Returns an appropriate value for the requested data.
93 If the view requests an invalid index, an invalid variant is returned.
94 Any valid index that corresponds to a string in the list causes that
95 string to be returned.
96*/
97
98//! [1]
99QVariant StringListModel::data(const QModelIndex &index, int role) const
100{
101 if (!index.isValid())
102 return QVariant();
103
104 if (index.row() >= stringList.size())
105 return QVariant();
106
107 if (role == Qt::DisplayRole || role == Qt::EditRole)
108 return stringList.at(index.row());
109 else
110 return QVariant();
111}
112//! [1]
113
114/*!
115 Returns the appropriate header string depending on the orientation of
116 the header and the section. If anything other than the display role is
117 requested, we return an invalid variant.
118*/
119
120//! [2]
121QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
122 int role) const
123{
124 if (role != Qt::DisplayRole)
125 return QVariant();
126
127 if (orientation == Qt::Horizontal)
128 return QString("Column %1").arg(section);
129 else
130 return QString("Row %1").arg(section);
131}
132//! [2]
133
134/*!
135 Returns an appropriate value for the item's flags. Valid items are
136 enabled, selectable, and editable.
137*/
138
139//! [3]
140Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
141{
142 if (!index.isValid())
143 return Qt::ItemIsEnabled;
144
145 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
146}
147//! [3]
148
149/*!
150 Changes an item in the string list, but only if the following conditions
151 are met:
152
153 * The index supplied is valid.
154 * The index corresponds to an item to be shown in a view.
155 * The role associated with editing text is specified.
156
157 The dataChanged() signal is emitted if the item is changed.
158*/
159
160//! [4]
161bool StringListModel::setData(const QModelIndex &index,
162 const QVariant &value, int role)
163{
164 if (index.isValid() && role == Qt::EditRole) {
165
166 stringList.replace(index.row(), value.toString());
167 emit dataChanged(index, index);
168 return true;
169 }
170//! [4] //! [5]
171 return false;
172}
173//! [5]
174
175/*!
176 Inserts a number of rows into the model at the specified position.
177*/
178
179//! [6]
180bool StringListModel::insertRows(int position, int rows, const QModelIndex &parent)
181{
182 beginInsertRows(QModelIndex(), position, position+rows-1);
183
184 for (int row = 0; row < rows; ++row) {
185 stringList.insert(position, "");
186 }
187
188 endInsertRows();
189 return true;
190//! [6] //! [7]
191}
192//! [7]
193
194/*!
195 Removes a number of rows from the model at the specified position.
196*/
197
198//! [8]
199bool StringListModel::removeRows(int position, int rows, const QModelIndex &parent)
200{
201 beginRemoveRows(QModelIndex(), position, position+rows-1);
202
203 for (int row = 0; row < rows; ++row) {
204 stringList.removeAt(position);
205 }
206
207 endRemoveRows();
208 return true;
209//! [8] //! [9]
210}
211//! [9]
Note: See TracBrowser for help on using the repository browser.