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