[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[651] | 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 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 | **
|
---|
[561] | 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.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | /*
|
---|
| 43 | model.cpp
|
---|
| 44 |
|
---|
| 45 | Provides a table model for use in various examples.
|
---|
| 46 | */
|
---|
| 47 |
|
---|
| 48 | #include <QtGui>
|
---|
| 49 |
|
---|
| 50 | #include "model.h"
|
---|
| 51 |
|
---|
| 52 | /*!
|
---|
| 53 | Constructs a table model with at least one row and one column.
|
---|
| 54 | */
|
---|
| 55 |
|
---|
| 56 | TableModel::TableModel(int rows, int columns, QObject *parent)
|
---|
| 57 | : QAbstractTableModel(parent)
|
---|
| 58 | {
|
---|
| 59 | QStringList newList;
|
---|
| 60 |
|
---|
| 61 | for (int column = 0; column < qMax(1, columns); ++column) {
|
---|
| 62 | newList.append("");
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | for (int row = 0; row < qMax(1, rows); ++row) {
|
---|
| 66 | rowList.append(newList);
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | /*!
|
---|
| 72 | Returns the number of items in the row list as the number of rows
|
---|
| 73 | in the model.
|
---|
| 74 | */
|
---|
| 75 |
|
---|
| 76 | int TableModel::rowCount(const QModelIndex &/*parent*/) const
|
---|
| 77 | {
|
---|
| 78 | return rowList.size();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /*!
|
---|
| 82 | Returns the number of items in the first list item as the number of
|
---|
| 83 | columns in the model. All rows should have the same number of columns.
|
---|
| 84 | */
|
---|
| 85 |
|
---|
| 86 | int TableModel::columnCount(const QModelIndex &/*parent*/) const
|
---|
| 87 | {
|
---|
| 88 | return rowList[0].size();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /*!
|
---|
| 92 | Returns an appropriate value for the requested data.
|
---|
| 93 | If the view requests an invalid index, an invalid variant is returned.
|
---|
| 94 | Any valid index that corresponds to a string in the list causes that
|
---|
| 95 | string to be returned for the display role; otherwise an invalid variant
|
---|
| 96 | is returned.
|
---|
| 97 | */
|
---|
| 98 |
|
---|
| 99 | QVariant TableModel::data(const QModelIndex &index, int role) const
|
---|
| 100 | {
|
---|
| 101 | if (!index.isValid())
|
---|
| 102 | return QVariant();
|
---|
| 103 |
|
---|
| 104 | if (role == Qt::DisplayRole)
|
---|
| 105 | return rowList[index.row()][index.column()];
|
---|
| 106 | else
|
---|
| 107 | return QVariant();
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /*!
|
---|
| 111 | Returns the appropriate header string depending on the orientation of
|
---|
| 112 | the header and the section. If anything other than the display role is
|
---|
| 113 | requested, we return an invalid variant.
|
---|
| 114 | */
|
---|
| 115 |
|
---|
| 116 | QVariant TableModel::headerData(int section, Qt::Orientation orientation,
|
---|
| 117 | int role) const
|
---|
| 118 | {
|
---|
| 119 | if (role != Qt::DisplayRole)
|
---|
| 120 | return QVariant();
|
---|
| 121 |
|
---|
| 122 | if (orientation == Qt::Horizontal)
|
---|
| 123 | return QString("Column %1").arg(section);
|
---|
| 124 | else
|
---|
| 125 | return QString("Row %1").arg(section);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /*!
|
---|
| 129 | Returns an appropriate value for the item's flags. Valid items are
|
---|
| 130 | enabled, selectable, and editable.
|
---|
| 131 | */
|
---|
| 132 |
|
---|
| 133 | Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
|
---|
| 134 | {
|
---|
| 135 | if (!index.isValid())
|
---|
| 136 | return Qt::ItemIsEnabled;
|
---|
| 137 |
|
---|
| 138 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /*!
|
---|
| 142 | Changes an item in the model, but only if the following conditions
|
---|
| 143 | are met:
|
---|
| 144 |
|
---|
| 145 | * The index supplied is valid.
|
---|
| 146 | * The role associated with editing text is specified.
|
---|
| 147 |
|
---|
| 148 | The dataChanged() signal is emitted if the item is changed.
|
---|
| 149 | */
|
---|
| 150 |
|
---|
| 151 | bool TableModel::setData(const QModelIndex &index,
|
---|
| 152 | const QVariant &value, int role)
|
---|
| 153 | {
|
---|
| 154 | if (!index.isValid() || role != Qt::EditRole)
|
---|
| 155 | return false;
|
---|
| 156 |
|
---|
| 157 | rowList[index.row()][index.column()] = value.toString();
|
---|
| 158 | emit dataChanged(index, index);
|
---|
| 159 | return true;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /*!
|
---|
| 163 | Inserts a number of rows into the model at the specified position.
|
---|
| 164 | */
|
---|
| 165 |
|
---|
| 166 | bool TableModel::insertRows(int position, int rows, const QModelIndex &parent)
|
---|
| 167 | {
|
---|
| 168 | int columns = columnCount();
|
---|
| 169 | beginInsertRows(parent, position, position + rows - 1);
|
---|
| 170 |
|
---|
| 171 | for (int row = 0; row < rows; ++row) {
|
---|
| 172 | QStringList items;
|
---|
| 173 | for (int column = 0; column < columns; ++column)
|
---|
| 174 | items.append("");
|
---|
| 175 | rowList.insert(position, items);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | endInsertRows();
|
---|
| 179 | return true;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /*!
|
---|
| 183 | Inserts a number of columns into the model at the specified position.
|
---|
| 184 | Each entry in the list is extended in turn with the required number of
|
---|
| 185 | empty strings.
|
---|
| 186 | */
|
---|
| 187 |
|
---|
| 188 | bool TableModel::insertColumns(int position, int columns,
|
---|
| 189 | const QModelIndex &parent)
|
---|
| 190 | {
|
---|
| 191 | int rows = rowCount();
|
---|
| 192 | beginInsertColumns(parent, position, position + columns - 1);
|
---|
| 193 |
|
---|
| 194 | for (int row = 0; row < rows; ++row) {
|
---|
| 195 | for (int column = position; column < columns; ++column) {
|
---|
| 196 | rowList[row].insert(position, "");
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | endInsertColumns();
|
---|
| 201 | return true;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /*!
|
---|
| 205 | Removes a number of rows from the model at the specified position.
|
---|
| 206 | */
|
---|
| 207 |
|
---|
| 208 | bool TableModel::removeRows(int position, int rows, const QModelIndex &parent)
|
---|
| 209 | {
|
---|
| 210 | beginRemoveRows(parent, position, position + rows - 1);
|
---|
| 211 |
|
---|
| 212 | for (int row = 0; row < rows; ++row) {
|
---|
| 213 | rowList.removeAt(position);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | endRemoveRows();
|
---|
| 217 | return true;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /*!
|
---|
| 221 | Removes a number of columns from the model at the specified position.
|
---|
| 222 | Each row is shortened by the number of columns specified.
|
---|
| 223 | */
|
---|
| 224 |
|
---|
| 225 | bool TableModel::removeColumns(int position, int columns,
|
---|
| 226 | const QModelIndex &parent)
|
---|
| 227 | {
|
---|
| 228 | int rows = rowCount();
|
---|
| 229 | beginRemoveColumns(parent, position, position + columns - 1);
|
---|
| 230 |
|
---|
| 231 | for (int row = 0; row < rows; ++row) {
|
---|
| 232 | for (int column = 0; column < columns; ++column) {
|
---|
| 233 | rowList[row].removeAt(position);
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | endRemoveColumns();
|
---|
| 238 | return true;
|
---|
| 239 | }
|
---|