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]
54TableModel::TableModel(QObject *parent)
55 : QAbstractTableModel(parent)
56{
57}
58
59TableModel::TableModel(const QVector<Contact> &contacts, QObject *parent)
60 : QAbstractTableModel(parent),
61 contacts(contacts)
62{
63}
64//! [0]
65
66//! [1]
67int TableModel::rowCount(const QModelIndex &parent) const
68{
69 return parent.isValid() ? 0 : contacts.size();
70}
71
72int TableModel::columnCount(const QModelIndex &parent) const
73{
74 return parent.isValid() ? 0 : 2;
75}
76//! [1]
77
78//! [2]
79QVariant TableModel::data(const QModelIndex &index, int role) const
80{
81 if (!index.isValid())
82 return QVariant();
83
84 if (index.row() >= contacts.size() || index.row() < 0)
85 return QVariant();
86
87 if (role == Qt::DisplayRole) {
88 const auto &contact = contacts.at(i: index.row());
89
90 switch (index.column()) {
91 case 0:
92 return contact.name;
93 case 1:
94 return contact.address;
95 default:
96 break;
97 }
98 }
99 return QVariant();
100}
101//! [2]
102
103//! [3]
104QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
105{
106 if (role != Qt::DisplayRole)
107 return QVariant();
108
109 if (orientation == Qt::Horizontal) {
110 switch (section) {
111 case 0:
112 return tr(s: "Name");
113 case 1:
114 return tr(s: "Address");
115 default:
116 break;
117 }
118 }
119 return QVariant();
120}
121//! [3]
122
123//! [4]
124bool TableModel::insertRows(int position, int rows, const QModelIndex &index)
125{
126 Q_UNUSED(index);
127 beginInsertRows(parent: QModelIndex(), first: position, last: position + rows - 1);
128
129 for (int row = 0; row < rows; ++row)
130 contacts.insert(i: position, t: { .name: QString(), .address: QString() });
131
132 endInsertRows();
133 return true;
134}
135//! [4]
136
137//! [5]
138bool TableModel::removeRows(int position, int rows, const QModelIndex &index)
139{
140 Q_UNUSED(index);
141 beginRemoveRows(parent: QModelIndex(), first: position, last: position + rows - 1);
142
143 for (int row = 0; row < rows; ++row)
144 contacts.removeAt(i: position);
145
146 endRemoveRows();
147 return true;
148}
149//! [5]
150
151//! [6]
152bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
153{
154 if (index.isValid() && role == Qt::EditRole) {
155 const int row = index.row();
156 auto contact = contacts.value(i: row);
157
158 switch (index.column()) {
159 case 0:
160 contact.name = value.toString();
161 break;
162 case 1:
163 contact.address = value.toString();
164 break;
165 default:
166 return false;
167 }
168 contacts.replace(i: row, t: contact);
169 emit dataChanged(topLeft: index, bottomRight: index, roles: {Qt::DisplayRole, Qt::EditRole});
170
171 return true;
172 }
173
174 return false;
175}
176//! [6]
177
178//! [7]
179Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
180{
181 if (!index.isValid())
182 return Qt::ItemIsEnabled;
183
184 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
185}
186//! [7]
187
188//! [8]
189const QVector<Contact> &TableModel::getContacts() const
190{
191 return contacts;
192}
193//! [8]
194

source code of qtbase/examples/widgets/itemviews/addressbook/tablemodel.cpp