Disregard milliseconds in QTime::secsTo().
[profile/ivi/qtbase.git] / src / widgets / doc / snippets / reading-selections / model.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
21 **     the names of its contributors may be used to endorse or promote
22 **     products derived from this software without specific prior written
23 **     permission.
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
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
55 TableModel::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
75 int 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
85 int 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
98 QVariant 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
115 QVariant 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
132 Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
133 {
134     if (!index.isValid())
135         return Qt::ItemIsEnabled;
136
137     return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
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
150 bool 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
165 bool 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
187 bool TableModel::insertColumns(int position, int columns,
188                                const QModelIndex &parent)
189 {
190     int rows = rowCount();
191     beginInsertColumns(parent, position, position + columns - 1);
192
193     for (int row = 0; row < rows; ++row) {
194         for (int column = position; column < columns; ++column) {
195             rowList[row].insert(position, "");
196         }
197     }
198
199     endInsertColumns();
200     return true;
201 }
202
203 /*!
204     Removes a number of rows from the model at the specified position.
205 */
206
207 bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
208 {
209     beginRemoveRows(parent, position, position + rows - 1);
210
211     for (int row = 0; row < rows; ++row) {
212         rowList.removeAt(position);
213     }
214
215     endRemoveRows();
216     return true;
217 }
218
219 /*!
220     Removes a number of columns from the model at the specified position.
221     Each row is shortened by the number of columns specified.
222 */
223
224 bool TableModel::removeColumns(int position, int columns,
225                                const QModelIndex &parent)
226 {
227     int rows = rowCount();
228     beginRemoveColumns(parent, position, position + columns - 1);
229
230     for (int row = 0; row < rows; ++row) {
231         for (int column = 0; column < columns; ++column) {
232             rowList[row].removeAt(position);
233         }
234     }
235
236     endRemoveColumns();
237     return true;
238 }