source: trunk/doc/src/snippets/sharedtablemodel/model.cpp@ 1168

Last change on this file since 1168 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: 6.8 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 Provides a table model for use in various examples.
45*/
46
47#include <QtGui>
48
49#include "model.h"
50
51/*!
52 Constructs a table model with at least one row and one column.
53*/
54
55TableModel::TableModel(int rows, int columns, QObject *parent)
56 : QAbstractTableModel(parent)
57{
58 QStringList newList;
59
60 for (int column = 0; column < qMax(1, columns); ++column) {
61 newList.append("");
62 }
63
64 for (int row = 0; row < qMax(1, rows); ++row) {
65 rowList.append(newList);
66 }
67}
68
69
70/*!
71 Returns the number of items in the row list as the number of rows
72 in the model.
73*/
74
75int TableModel::rowCount(const QModelIndex &/*parent*/) const
76{
77 return rowList.size();
78}
79
80/*!
81 Returns the number of items in the first list item as the number of
82 columns in the model. All rows should have the same number of columns.
83*/
84
85int TableModel::columnCount(const QModelIndex &/*parent*/) const
86{
87 return rowList[0].size();
88}
89
90/*!
91 Returns an appropriate value for the requested data.
92 If the view requests an invalid index, an invalid variant is returned.
93 Any valid index that corresponds to a string in the list causes that
94 string to be returned for the display role; otherwise an invalid variant
95 is returned.
96*/
97
98QVariant TableModel::data(const QModelIndex &index, int role) const
99{
100 if (!index.isValid())
101 return QVariant();
102
103 if (role == Qt::DisplayRole)
104 return rowList[index.row()][index.column()];
105 else
106 return QVariant();
107}
108
109/*!
110 Returns the appropriate header string depending on the orientation of
111 the header and the section. If anything other than the display role is
112 requested, we return an invalid variant.
113*/
114
115QVariant TableModel::headerData(int section, Qt::Orientation orientation,
116 int role) const
117{
118 if (role != Qt::DisplayRole)
119 return QVariant();
120
121 if (orientation == Qt::Horizontal)
122 return QString("Column %1").arg(section);
123 else
124 return QString("Row %1").arg(section);
125}
126
127/*!
128 Returns an appropriate value for the item's flags. Valid items are
129 enabled, selectable, and editable.
130*/
131
132Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
133{
134 if (!index.isValid())
135 return Qt::ItemIsEnabled;
136
137 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
138}
139
140/*!
141 Changes an item in the model, but only if the following conditions
142 are met:
143
144 * The index supplied is valid.
145 * The role associated with editing text is specified.
146
147 The dataChanged() signal is emitted if the item is changed.
148*/
149
150bool TableModel::setData(const QModelIndex &index,
151 const QVariant &value, int role)
152{
153 if (!index.isValid() || role != Qt::EditRole)
154 return false;
155
156 rowList[index.row()][index.column()] = value.toString();
157 emit dataChanged(index, index);
158 return true;
159}
160
161/*!
162 Inserts a number of rows into the model at the specified position.
163*/
164
165bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
166{
167 int columns = columnCount();
168 beginInsertRows(parent, position, position + rows - 1);
169
170 for (int row = 0; row < rows; ++row) {
171 QStringList items;
172 for (int column = 0; column < columns; ++column)
173 items.append("");
174 rowList.insert(position, items);
175 }
176
177 endInsertRows();
178 return true;
179}
180
181/*!
182 Inserts a number of columns into the model at the specified position.
183 Each entry in the list is extended in turn with the required number of
184 empty strings.
185*/
186
187bool TableModel::insertColumns(int position, int columns, const QModelIndex &parent)
188{
189 int rows = rowCount();
190 beginInsertColumns(parent, position, position + columns - 1);
191
192 for (int row = 0; row < rows; ++row) {
193 for (int column = position; column < columns; ++column) {
194 rowList[row].insert(position, "");
195 }
196 }
197
198 endInsertColumns();
199 return true;
200}
201
202/*!
203 Removes a number of rows from the model at the specified position.
204*/
205
206bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
207{
208 beginRemoveRows(parent, position, position + rows - 1);
209
210 for (int row = 0; row < rows; ++row) {
211 rowList.removeAt(position);
212 }
213
214 endRemoveRows();
215 return true;
216}
217
218/*!
219 Removes a number of columns from the model at the specified position.
220 Each row is shortened by the number of columns specified.
221*/
222
223bool TableModel::removeColumns(int position, int columns, const QModelIndex &parent)
224{
225 int rows = rowCount();
226 beginRemoveColumns(parent, position, position + columns - 1);
227
228 for (int row = 0; row < rows; ++row) {
229 for (int column = 0; column < columns; ++column) {
230 rowList[row].removeAt(position);
231 }
232 }
233
234 endRemoveColumns();
235 return true;
236}
Note: See TracBrowser for help on using the repository browser.