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 | **
|
---|
43 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
44 | ** All rights reserved.
|
---|
45 | ** Contact: Nokia Corporation ([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 | */
|
---|
|
---|