| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "tablemodel.h" |
| 52 | |
| 53 | //! [0] |
| 54 | TableModel::TableModel(QObject *parent) |
| 55 | : QAbstractTableModel(parent) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | TableModel::TableModel(const QList<Contact> &contacts, QObject *parent) |
| 60 | : QAbstractTableModel(parent), contacts(contacts) |
| 61 | { |
| 62 | } |
| 63 | //! [0] |
| 64 | |
| 65 | //! [1] |
| 66 | int TableModel::rowCount(const QModelIndex &parent) const |
| 67 | { |
| 68 | return parent.isValid() ? 0 : contacts.size(); |
| 69 | } |
| 70 | |
| 71 | int TableModel::columnCount(const QModelIndex &parent) const |
| 72 | { |
| 73 | return parent.isValid() ? 0 : 2; |
| 74 | } |
| 75 | //! [1] |
| 76 | |
| 77 | //! [2] |
| 78 | QVariant TableModel::data(const QModelIndex &index, int role) const |
| 79 | { |
| 80 | if (!index.isValid()) |
| 81 | return QVariant(); |
| 82 | |
| 83 | if (index.row() >= contacts.size() || index.row() < 0) |
| 84 | return QVariant(); |
| 85 | |
| 86 | if (role == Qt::DisplayRole) { |
| 87 | const auto &contact = contacts.at(index.row()); |
| 88 | |
| 89 | switch (index.column()) { |
| 90 | case 0: |
| 91 | return contact.name; |
| 92 | case 1: |
| 93 | return contact.address; |
| 94 | default: |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | return QVariant(); |
| 99 | } |
| 100 | //! [2] |
| 101 | |
| 102 | //! [3] |
| 103 | QVariant TableModel::(int section, Qt::Orientation orientation, int role) const |
| 104 | { |
| 105 | if (role != Qt::DisplayRole) |
| 106 | return QVariant(); |
| 107 | |
| 108 | if (orientation == Qt::Horizontal) { |
| 109 | switch (section) { |
| 110 | case 0: |
| 111 | return tr("Name" ); |
| 112 | case 1: |
| 113 | return tr("Address" ); |
| 114 | default: |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | return QVariant(); |
| 119 | } |
| 120 | //! [3] |
| 121 | |
| 122 | //! [4] |
| 123 | bool TableModel::insertRows(int position, int rows, const QModelIndex &index) |
| 124 | { |
| 125 | Q_UNUSED(index); |
| 126 | beginInsertRows(QModelIndex(), position, position + rows - 1); |
| 127 | |
| 128 | for (int row = 0; row < rows; ++row) |
| 129 | contacts.insert(position, { QString(), QString() }); |
| 130 | |
| 131 | endInsertRows(); |
| 132 | return true; |
| 133 | } |
| 134 | //! [4] |
| 135 | |
| 136 | //! [5] |
| 137 | bool TableModel::removeRows(int position, int rows, const QModelIndex &index) |
| 138 | { |
| 139 | Q_UNUSED(index); |
| 140 | beginRemoveRows(QModelIndex(), position, position + rows - 1); |
| 141 | |
| 142 | for (int row = 0; row < rows; ++row) |
| 143 | contacts.removeAt(position); |
| 144 | |
| 145 | endRemoveRows(); |
| 146 | return true; |
| 147 | } |
| 148 | //! [5] |
| 149 | |
| 150 | //! [6] |
| 151 | bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role) |
| 152 | { |
| 153 | if (index.isValid() && role == Qt::EditRole) { |
| 154 | const int row = index.row(); |
| 155 | auto contact = contacts.value(row); |
| 156 | |
| 157 | switch (index.column()) { |
| 158 | case 0: |
| 159 | contact.name = value.toString(); |
| 160 | break; |
| 161 | case 1: |
| 162 | contact.address = value.toString(); |
| 163 | break; |
| 164 | default: |
| 165 | return false; |
| 166 | } |
| 167 | contacts.replace(row, contact); |
| 168 | emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole}); |
| 169 | |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | //! [6] |
| 176 | |
| 177 | //! [7] |
| 178 | Qt::ItemFlags TableModel::flags(const QModelIndex &index) const |
| 179 | { |
| 180 | if (!index.isValid()) |
| 181 | return Qt::ItemIsEnabled; |
| 182 | |
| 183 | return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; |
| 184 | } |
| 185 | //! [7] |
| 186 | |
| 187 | //! [8] |
| 188 | const QList<Contact> &TableModel::getContacts() const |
| 189 | { |
| 190 | return contacts; |
| 191 | } |
| 192 | //! [8] |
| 193 | |