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 Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
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** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include "customtablemodel.h"
31#include <QtCore/QVector>
32#include <QtCore/QRandomGenerator>
33#include <QtCore/QRect>
34#include <QtGui/QColor>
35
36CustomTableModel::CustomTableModel(QObject *parent) :
37 QAbstractTableModel(parent)
38{
39 m_columnCount = 4;
40 m_rowCount = 15;
41
42 // m_data
43 for (int i = 0; i < m_rowCount; i++) {
44 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
45 for (int k = 0; k < dataVec->size(); k++) {
46 if (k % 2 == 0)
47 dataVec->replace(i: k, t: i * 50 + QRandomGenerator::global()->bounded(highest: 20));
48 else
49 dataVec->replace(i: k, t: QRandomGenerator::global()->bounded(highest: 100));
50 }
51 m_data.append(t: dataVec);
52 }
53}
54
55int CustomTableModel::rowCount(const QModelIndex &parent) const
56{
57 Q_UNUSED(parent)
58 return m_data.count();
59}
60
61int CustomTableModel::columnCount(const QModelIndex &parent) const
62{
63 Q_UNUSED(parent)
64 return m_columnCount;
65}
66
67QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
68{
69 if (role != Qt::DisplayRole)
70 return QVariant();
71
72 if (orientation == Qt::Horizontal) {
73 if (section % 2 == 0)
74 return "x";
75 else
76 return "y";
77 } else {
78 return QString("%1").arg(a: section + 1);
79 }
80}
81
82QVariant CustomTableModel::data(const QModelIndex &index, int role) const
83{
84 if (role == Qt::DisplayRole) {
85 return m_data[index.row()]->at(i: index.column());
86 } else if (role == Qt::EditRole) {
87 return m_data[index.row()]->at(i: index.column());
88 } else if (role == Qt::BackgroundRole) {
89 for (const QRect &rect : m_mapping) {
90 if (rect.contains(ax: index.column(), ay: index.row()))
91 return QColor(m_mapping.key(avalue: rect));
92 }
93 // cell not mapped return white color
94 return QColor(Qt::white);
95 }
96 return QVariant();
97}
98
99bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
100{
101 if (index.isValid() && role == Qt::EditRole) {
102 m_data[index.row()]->replace(i: index.column(), t: value.toDouble());
103 emit dataChanged(topLeft: index, bottomRight: index);
104 return true;
105 }
106 return false;
107}
108
109Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const
110{
111 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
112}
113
114void CustomTableModel::addMapping(QString color, QRect area)
115{
116 m_mapping.insertMulti(key: color, value: area);
117}
118

source code of qtcharts/examples/charts/modeldata/customtablemodel.cpp