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/QTime>
33#include <QtCore/QRect>
34#include <QtCore/QRandomGenerator>
35#include <QtGui/QColor>
36
37CustomTableModel::CustomTableModel(QObject *parent) :
38 QAbstractTableModel(parent)
39{
40 m_columnCount = 6;
41 m_rowCount = 12;
42
43 // m_data
44 for (int i = 0; i < m_rowCount; i++) {
45 QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
46 for (int k = 0; k < dataVec->size(); k++) {
47 if (k % 2 == 0)
48 dataVec->replace(i: k, t: i * 50 + QRandomGenerator::global()->bounded(highest: 20));
49 else
50 dataVec->replace(i: k, t: QRandomGenerator::global()->bounded(highest: 100));
51 }
52 m_data.append(t: dataVec);
53 }
54}
55
56CustomTableModel::~CustomTableModel()
57{
58 qDeleteAll(c: m_data);
59}
60
61int CustomTableModel::rowCount(const QModelIndex &parent) const
62{
63 Q_UNUSED(parent)
64 return m_data.count();
65}
66
67int CustomTableModel::columnCount(const QModelIndex &parent) const
68{
69 Q_UNUSED(parent)
70 return m_columnCount;
71}
72
73QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
74{
75 if (role != Qt::DisplayRole)
76 return QVariant();
77
78 if (orientation == Qt::Horizontal)
79 return QString("201%1").arg(a: section);
80 else
81 return QString("%1").arg(a: section + 1);
82}
83
84QVariant CustomTableModel::data(const QModelIndex &index, int role) const
85{
86 if (role == Qt::DisplayRole) {
87 return m_data[index.row()]->at(i: index.column());
88 } else if (role == Qt::EditRole) {
89 return m_data[index.row()]->at(i: index.column());
90 } else if (role == Qt::BackgroundRole) {
91 for (const QRect &rect : m_mapping) {
92 if (rect.contains(ax: index.column(), ay: index.row()))
93 return QColor(m_mapping.key(avalue: rect));
94 }
95
96 // cell not mapped return white color
97 return QColor(Qt::white);
98 }
99 return QVariant();
100}
101
102bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
103{
104 if (index.isValid() && role == Qt::EditRole) {
105 m_data[index.row()]->replace(i: index.column(), t: value.toDouble());
106 emit dataChanged(topLeft: index, bottomRight: index);
107 return true;
108 }
109 return false;
110}
111
112Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const
113{
114 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
115}
116
117void CustomTableModel::addMapping(QString color, QRect area)
118{
119 m_mapping.insertMulti(key: color, value: area);
120}
121

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