File: customtablemodel.cpp

package info (click to toggle)
qtcharts-opensource-src 5.15.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 13,112 kB
  • sloc: cpp: 66,609; ansic: 221; makefile: 23
file content (160 lines) | stat: -rw-r--r-- 4,585 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCharts/QCandlestickSet>
#include <QtCore/QRect>
#include <QtCore/QVector>
#include <QtGui/QColor>
#include "customtablemodel.h"

CustomTableModel::CustomTableModel(QObject *parent)
    : QAbstractTableModel(parent)
{
    m_categories.append(QStringLiteral("Timestamp"));
    m_categories.append(QStringLiteral("Open"));
    m_categories.append(QStringLiteral("High"));
    m_categories.append(QStringLiteral("Low"));
    m_categories.append(QStringLiteral("Close"));
}

CustomTableModel::~CustomTableModel()
{
    qDeleteAll(m_data);
}

int CustomTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    return m_data.count();
}

int CustomTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    return m_categories.count();
}

QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal)
        return m_categories[section];
    else
        return QStringLiteral("%1").arg(section);
}

bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole) {
        m_data[index.row()]->replace(index.column(), value.toDouble());
        emit dataChanged(index, index);

        return true;
    }

    return false;
}

QVariant CustomTableModel::data(const QModelIndex &index, int role) const
{
    switch (role) {
    case Qt::DisplayRole:
        // fall through
    case Qt::EditRole:
        return m_data[index.row()]->at(index.column());
    case Qt::BackgroundRole:
        foreach (QRect rect, m_mapping) {
            if (rect.contains(index.column(), index.row()))
                return QColor(m_mapping.key(rect));
        }
        // cell is not mapped, return white color
        return QColor(Qt::white);
    default:
        return QVariant();
    }
}

Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const
{
    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

bool CustomTableModel::insertRows(int row, int count, const QModelIndex &parent)
{
    beginInsertRows(parent, row, row + count - 1);
    m_data.append(new QVector<qreal>(columnCount()));
    endInsertRows();

    return true;
}

bool CustomTableModel::removeRows(int row, int count, const QModelIndex &parent)
{
    beginRemoveRows(parent, row, row + count - 1);
    for (int i = row + count; i >= row; --i)
        m_data.removeAt(i);
    endRemoveRows();

    return true;
}

void CustomTableModel::addRow(QCandlestickSet *set)
{
    bool changed = insertRows(m_data.count(), 1);

    if (changed) {
        QVector<qreal> *row = m_data.last();
        row->insert(0, set->timestamp());
        row->insert(1, set->open());
        row->insert(2, set->high());
        row->insert(3, set->low());
        row->insert(4, set->close());
    }
}

void CustomTableModel::clearRows()
{
    bool changed = removeRows(0, m_data.count());
    if (changed)
        m_data.clear();
}

void CustomTableModel::addMapping(QString color, QRect area)
{
    m_mapping.insertMulti(color, area);
}

void CustomTableModel::clearMapping()
{
    m_mapping.clear();
}