[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
[2] | 11 | **
|
---|
[846] | 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.
|
---|
[2] | 25 | **
|
---|
[846] | 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."
|
---|
[2] | 37 | ** $QT_END_LICENSE$
|
---|
| 38 | **
|
---|
| 39 | ****************************************************************************/
|
---|
| 40 |
|
---|
| 41 | #include "tablemodel.h"
|
---|
| 42 |
|
---|
| 43 | //! [0]
|
---|
| 44 | TableModel::TableModel(QObject *parent)
|
---|
| 45 | : QAbstractTableModel(parent)
|
---|
| 46 | {
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | TableModel::TableModel(QList< QPair<QString, QString> > pairs, QObject *parent)
|
---|
| 50 | : QAbstractTableModel(parent)
|
---|
| 51 | {
|
---|
| 52 | listOfPairs=pairs;
|
---|
| 53 | }
|
---|
| 54 | //! [0]
|
---|
| 55 |
|
---|
| 56 | //! [1]
|
---|
| 57 | int TableModel::rowCount(const QModelIndex &parent) const
|
---|
| 58 | {
|
---|
| 59 | Q_UNUSED(parent);
|
---|
| 60 | return listOfPairs.size();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | int TableModel::columnCount(const QModelIndex &parent) const
|
---|
| 64 | {
|
---|
| 65 | Q_UNUSED(parent);
|
---|
| 66 | return 2;
|
---|
| 67 | }
|
---|
| 68 | //! [1]
|
---|
| 69 |
|
---|
| 70 | //! [2]
|
---|
| 71 | QVariant TableModel::data(const QModelIndex &index, int role) const
|
---|
| 72 | {
|
---|
| 73 | if (!index.isValid())
|
---|
| 74 | return QVariant();
|
---|
[846] | 75 |
|
---|
[2] | 76 | if (index.row() >= listOfPairs.size() || index.row() < 0)
|
---|
| 77 | return QVariant();
|
---|
[846] | 78 |
|
---|
[2] | 79 | if (role == Qt::DisplayRole) {
|
---|
| 80 | QPair<QString, QString> pair = listOfPairs.at(index.row());
|
---|
[846] | 81 |
|
---|
[2] | 82 | if (index.column() == 0)
|
---|
| 83 | return pair.first;
|
---|
| 84 | else if (index.column() == 1)
|
---|
| 85 | return pair.second;
|
---|
| 86 | }
|
---|
| 87 | return QVariant();
|
---|
| 88 | }
|
---|
| 89 | //! [2]
|
---|
| 90 |
|
---|
| 91 | //! [3]
|
---|
| 92 | QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
|
---|
| 93 | {
|
---|
| 94 | if (role != Qt::DisplayRole)
|
---|
| 95 | return QVariant();
|
---|
[846] | 96 |
|
---|
[2] | 97 | if (orientation == Qt::Horizontal) {
|
---|
| 98 | switch (section) {
|
---|
| 99 | case 0:
|
---|
| 100 | return tr("Name");
|
---|
[846] | 101 |
|
---|
[2] | 102 | case 1:
|
---|
| 103 | return tr("Address");
|
---|
[846] | 104 |
|
---|
[2] | 105 | default:
|
---|
| 106 | return QVariant();
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | return QVariant();
|
---|
| 110 | }
|
---|
| 111 | //! [3]
|
---|
| 112 |
|
---|
| 113 | //! [4]
|
---|
| 114 | bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
|
---|
| 115 | {
|
---|
| 116 | Q_UNUSED(index);
|
---|
| 117 | beginInsertRows(QModelIndex(), position, position+rows-1);
|
---|
[846] | 118 |
|
---|
[2] | 119 | for (int row=0; row < rows; row++) {
|
---|
| 120 | QPair<QString, QString> pair(" ", " ");
|
---|
| 121 | listOfPairs.insert(position, pair);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | endInsertRows();
|
---|
| 125 | return true;
|
---|
| 126 | }
|
---|
| 127 | //! [4]
|
---|
| 128 |
|
---|
| 129 | //! [5]
|
---|
| 130 | bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
|
---|
| 131 | {
|
---|
[846] | 132 | Q_UNUSED(index);
|
---|
[2] | 133 | beginRemoveRows(QModelIndex(), position, position+rows-1);
|
---|
[846] | 134 |
|
---|
[2] | 135 | for (int row=0; row < rows; ++row) {
|
---|
| 136 | listOfPairs.removeAt(position);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | endRemoveRows();
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 | //! [5]
|
---|
| 143 |
|
---|
| 144 | //! [6]
|
---|
| 145 | bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
---|
| 146 | {
|
---|
[846] | 147 | if (index.isValid() && role == Qt::EditRole) {
|
---|
| 148 | int row = index.row();
|
---|
| 149 |
|
---|
| 150 | QPair<QString, QString> p = listOfPairs.value(row);
|
---|
| 151 |
|
---|
| 152 | if (index.column() == 0)
|
---|
| 153 | p.first = value.toString();
|
---|
| 154 | else if (index.column() == 1)
|
---|
| 155 | p.second = value.toString();
|
---|
[2] | 156 | else
|
---|
| 157 | return false;
|
---|
[846] | 158 |
|
---|
[2] | 159 | listOfPairs.replace(row, p);
|
---|
[846] | 160 | emit(dataChanged(index, index));
|
---|
| 161 |
|
---|
[2] | 162 | return true;
|
---|
[846] | 163 | }
|
---|
[2] | 164 |
|
---|
[846] | 165 | return false;
|
---|
[2] | 166 | }
|
---|
| 167 | //! [6]
|
---|
| 168 |
|
---|
| 169 | //! [7]
|
---|
| 170 | Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
|
---|
| 171 | {
|
---|
| 172 | if (!index.isValid())
|
---|
| 173 | return Qt::ItemIsEnabled;
|
---|
[846] | 174 |
|
---|
[2] | 175 | return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
|
---|
| 176 | }
|
---|
| 177 | //! [7]
|
---|
| 178 |
|
---|
| 179 | //! [8]
|
---|
| 180 | QList< QPair<QString, QString> > TableModel::getList()
|
---|
| 181 | {
|
---|
| 182 | return listOfPairs;
|
---|
| 183 | }
|
---|
| 184 | //! [8]
|
---|