| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses |
| 5 |
|
| 6 |
#include <QtCharts/QCandlestickSet> |
| 7 |
#include <QtCore/QRect> |
| 8 |
#include <QtCore/QList> |
| 9 |
#include <QtGui/QColor> |
| 10 |
#include "customtablemodel.h" |
| 11 |
|
| 12 |
CustomTableModel::CustomTableModel(QObject *parent) |
| 13 |
: QAbstractTableModel(parent) |
| 14 |
{ |
| 15 |
m_categories.append(QStringLiteral("Timestamp")); |
| 16 |
m_categories.append(QStringLiteral("Open")); |
| 17 |
m_categories.append(QStringLiteral("High")); |
| 18 |
m_categories.append(QStringLiteral("Low")); |
| 19 |
m_categories.append(QStringLiteral("Close")); |
| 20 |
} |
| 21 |
|
| 22 |
CustomTableModel::~CustomTableModel() |
| 23 |
{ |
| 24 |
qDeleteAll(m_data); |
| 25 |
} |
| 26 |
|
| 27 |
int CustomTableModel::rowCount(const QModelIndex &parent) const |
| 28 |
{ |
| 29 |
Q_UNUSED(parent); |
| 30 |
|
| 31 |
return m_data.count(); |
| 32 |
} |
| 33 |
|
| 34 |
int CustomTableModel::columnCount(const QModelIndex &parent) const |
| 35 |
{ |
| 36 |
Q_UNUSED(parent); |
| 37 |
|
| 38 |
return m_categories.count(); |
| 39 |
} |
| 40 |
|
| 41 |
QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 42 |
{ |
| 43 |
if (role != Qt::DisplayRole) |
| 44 |
return QVariant(); |
| 45 |
|
| 46 |
if (orientation == Qt::Horizontal) |
| 47 |
return m_categories[section]; |
| 48 |
else |
| 49 |
return QStringLiteral("%1").arg(section); |
| 50 |
} |
| 51 |
|
| 52 |
bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role) |
| 53 |
{ |
| 54 |
if (index.isValid() && role == Qt::EditRole) { |
| 55 |
m_data[index.row()]->replace(index.column(), value.toDouble()); |
| 56 |
emit dataChanged(index, index); |
| 57 |
|
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
QVariant CustomTableModel::data(const QModelIndex &index, int role) const |
| 65 |
{ |
| 66 |
switch (role) { |
| 67 |
case Qt::DisplayRole: |
| 68 |
// fall through |
| 69 |
case Qt::EditRole: |
| 70 |
return m_data[index.row()]->at(index.column()); |
| 71 |
case Qt::BackgroundRole: |
| 72 |
foreach (QRect rect, m_mapping) { |
| 73 |
if (rect.contains(index.column(), index.row())) |
| 74 |
return QColor(m_mapping.key(rect)); |
| 75 |
} |
| 76 |
// cell is not mapped, return white color |
| 77 |
return QColor(Qt::white); |
| 78 |
default: |
| 79 |
return QVariant(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const |
| 84 |
{ |
| 85 |
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; |
| 86 |
} |
| 87 |
|
| 88 |
bool CustomTableModel::insertRows(int row, int count, const QModelIndex &parent) |
| 89 |
{ |
| 90 |
beginInsertRows(parent, row, row + count - 1); |
| 91 |
m_data.append(new QList<qreal>(columnCount())); |
| 92 |
endInsertRows(); |
| 93 |
|
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
bool CustomTableModel::removeRows(int row, int count, const QModelIndex &parent) |
| 98 |
{ |
| 99 |
beginRemoveRows(parent, row, row + count - 1); |
| 100 |
for (int i = row + count; i >= row; --i) |
| 101 |
m_data.removeAt(i); |
| 102 |
endRemoveRows(); |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
void CustomTableModel::addRow(QCandlestickSet *set) |
| 108 |
{ |
| 109 |
bool changed = insertRows(m_data.count(), 1); |
| 110 |
|
| 111 |
if (changed) { |
| 112 |
QList<qreal> *row = m_data.last(); |
| 113 |
row->insert(0, set->timestamp()); |
| 114 |
row->insert(1, set->open()); |
| 115 |
row->insert(2, set->high()); |
| 116 |
row->insert(3, set->low()); |
| 117 |
row->insert(4, set->close()); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
void CustomTableModel::clearRows() |
| 122 |
{ |
| 123 |
bool changed = removeRows(0, m_data.count()); |
| 124 |
if (changed) |
| 125 |
m_data.clear(); |
| 126 |
} |
| 127 |
|
| 128 |
void CustomTableModel::addMapping(QString color, QRect area) |
| 129 |
{ |
| 130 |
m_mapping.insert(color, area); |
| 131 |
} |
| 132 |
|
| 133 |
void CustomTableModel::clearMapping() |
| 134 |
{ |
| 135 |
m_mapping.clear(); |
| 136 |
} |