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 <QtCharts/QCandlestickSet>
31#include <QtCore/QRect>
32#include <QtCore/QVector>
33#include <QtGui/QColor>
34#include "customtablemodel.h"
35
36CustomTableModel::CustomTableModel(QObject *parent)
37 : QAbstractTableModel(parent)
38{
39 m_categories.append(QStringLiteral("Timestamp"));
40 m_categories.append(QStringLiteral("Open"));
41 m_categories.append(QStringLiteral("High"));
42 m_categories.append(QStringLiteral("Low"));
43 m_categories.append(QStringLiteral("Close"));
44}
45
46CustomTableModel::~CustomTableModel()
47{
48 qDeleteAll(c: m_data);
49}
50
51int CustomTableModel::rowCount(const QModelIndex &parent) const
52{
53 Q_UNUSED(parent)
54
55 return m_data.count();
56}
57
58int CustomTableModel::columnCount(const QModelIndex &parent) const
59{
60 Q_UNUSED(parent)
61
62 return m_categories.count();
63}
64
65QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
66{
67 if (role != Qt::DisplayRole)
68 return QVariant();
69
70 if (orientation == Qt::Horizontal)
71 return m_categories[section];
72 else
73 return QStringLiteral("%1").arg(a: section);
74}
75
76bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
77{
78 if (index.isValid() && role == Qt::EditRole) {
79 m_data[index.row()]->replace(i: index.column(), t: value.toDouble());
80 emit dataChanged(topLeft: index, bottomRight: index);
81
82 return true;
83 }
84
85 return false;
86}
87
88QVariant CustomTableModel::data(const QModelIndex &index, int role) const
89{
90 switch (role) {
91 case Qt::DisplayRole:
92 // fall through
93 case Qt::EditRole:
94 return m_data[index.row()]->at(i: index.column());
95 case Qt::BackgroundRole:
96 foreach (QRect rect, m_mapping) {
97 if (rect.contains(ax: index.column(), ay: index.row()))
98 return QColor(m_mapping.key(avalue: rect));
99 }
100 // cell is not mapped, return white color
101 return QColor(Qt::white);
102 default:
103 return QVariant();
104 }
105}
106
107Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const
108{
109 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
110}
111
112bool CustomTableModel::insertRows(int row, int count, const QModelIndex &parent)
113{
114 beginInsertRows(parent, first: row, last: row + count - 1);
115 m_data.append(t: new QVector<qreal>(columnCount()));
116 endInsertRows();
117
118 return true;
119}
120
121bool CustomTableModel::removeRows(int row, int count, const QModelIndex &parent)
122{
123 beginRemoveRows(parent, first: row, last: row + count - 1);
124 for (int i = row + count; i >= row; --i)
125 m_data.removeAt(i);
126 endRemoveRows();
127
128 return true;
129}
130
131void CustomTableModel::addRow(QCandlestickSet *set)
132{
133 bool changed = insertRows(row: m_data.count(), count: 1);
134
135 if (changed) {
136 QVector<qreal> *row = m_data.last();
137 row->insert(i: 0, t: set->timestamp());
138 row->insert(i: 1, t: set->open());
139 row->insert(i: 2, t: set->high());
140 row->insert(i: 3, t: set->low());
141 row->insert(i: 4, t: set->close());
142 }
143}
144
145void CustomTableModel::clearRows()
146{
147 bool changed = removeRows(row: 0, count: m_data.count());
148 if (changed)
149 m_data.clear();
150}
151
152void CustomTableModel::addMapping(QString color, QRect area)
153{
154 m_mapping.insertMulti(key: color, value: area);
155}
156
157void CustomTableModel::clearMapping()
158{
159 m_mapping.clear();
160}
161

source code of qtcharts/tests/manual/candlesticktester/customtablemodel.cpp