|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the documentation of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 /* |
|
43 model.cpp |
|
44 |
|
45 A simple model that uses a QVector as its data source. |
|
46 */ |
|
47 |
|
48 #include "model.h" |
|
49 |
|
50 /*! |
|
51 Returns the number of items in the string list as the number of rows |
|
52 in the model. |
|
53 */ |
|
54 |
|
55 int LinearModel::rowCount(const QModelIndex &parent) const |
|
56 { |
|
57 Q_USING(parent); |
|
58 |
|
59 return values.count(); |
|
60 } |
|
61 |
|
62 /* |
|
63 Returns an appropriate value for the requested data. |
|
64 If the view requests an invalid index, an invalid variant is returned. |
|
65 If a header is requested then we just return the column or row number, |
|
66 depending on the orientation of the header. |
|
67 Any valid index that corresponds to a string in the list causes that |
|
68 string to be returned. |
|
69 */ |
|
70 |
|
71 /*! |
|
72 Returns a model index for other component to use when referencing the |
|
73 item specified by the given row, column, and type. The parent index |
|
74 is ignored. |
|
75 */ |
|
76 |
|
77 QModelIndex LinearModel::index(int row, int column, const QModelIndex &parent) const |
|
78 { |
|
79 if (parent == QModelIndex() && row >= 0 && row < rowCount() |
|
80 && column == 0) |
|
81 return createIndex(row, column, 0); |
|
82 else |
|
83 return QModelIndex(); |
|
84 } |
|
85 |
|
86 QVariant LinearModel::data(const QModelIndex &index, int role) const |
|
87 { |
|
88 Q_UNUSED(role); |
|
89 |
|
90 if (!index.isValid()) |
|
91 return QVariant(); |
|
92 |
|
93 return values.at(index.row()); |
|
94 } |
|
95 |
|
96 /*! |
|
97 Returns Qt::ItemIsEditable so that all items in the vector can be edited. |
|
98 */ |
|
99 |
|
100 Qt::ItemFlags LinearModel::flags(const QModelIndex &index) const |
|
101 { |
|
102 // all items in the model are editable |
|
103 return QAbstractListModel::flags(index) | Qt::ItemIsEditable; |
|
104 } |
|
105 |
|
106 /*! |
|
107 Changes an item in the string list, but only if the following conditions |
|
108 are met: |
|
109 |
|
110 * The index supplied is valid. |
|
111 * The index corresponds to an item to be shown in a view. |
|
112 * The role associated with editing text is specified. |
|
113 |
|
114 The dataChanged() signal is emitted if the item is changed. |
|
115 */ |
|
116 |
|
117 bool LinearModel::setData(const QModelIndex &index, |
|
118 const QVariant &value, int role) |
|
119 { |
|
120 if (!index.isValid() || role != Qt::EditRole) |
|
121 return false; |
|
122 values.replace(index.row(), value.toInt()); |
|
123 emit dataChanged(index, index); |
|
124 return true; |
|
125 } |
|
126 |
|
127 /*! |
|
128 Inserts a number of rows into the model at the specified position. |
|
129 */ |
|
130 |
|
131 bool LinearModel::insertRows(int position, int rows, const QModelIndex &parent) |
|
132 { |
|
133 beginInsertRows(parent, position, position + rows - 1); |
|
134 |
|
135 values.insert(position, rows, 0); |
|
136 |
|
137 endInsertRows(); |
|
138 return true; |
|
139 } |
|
140 |
|
141 /*! |
|
142 Removes a number of rows from the model at the specified position. |
|
143 */ |
|
144 |
|
145 bool LinearModel::removeRows(int position, int rows, const QModelIndex &parent) |
|
146 { |
|
147 beginRemoveRows(QModelIndex(), position, position+rows-1); |
|
148 |
|
149 values.remove(position, rows); |
|
150 |
|
151 endRemoveRows(); |
|
152 return true; |
|
153 } |