| 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 "customtablemodel.h" |
| 7 |
#include <QtCore/QList> |
| 8 |
#include <QtCore/QTime> |
| 9 |
#include <QtCore/QRect> |
| 10 |
#include <QtGui/QColor> |
| 11 |
|
| 12 |
const QString categories[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Nov", "Dec"}; |
| 13 |
|
| 14 |
CustomTableModel::CustomTableModel(QObject *parent) : |
| 15 |
QAbstractTableModel(parent) |
| 16 |
{ |
| 17 |
m_columnCount = 6; |
| 18 |
m_rowCount = 5; |
| 19 |
QList<qreal> *dataVec_Jan = new QList<qreal>(m_rowCount); |
| 20 |
dataVec_Jan->insert(0, 3.0); |
| 21 |
dataVec_Jan->insert(1, 4.0); |
| 22 |
dataVec_Jan->insert(2, 4.4); |
| 23 |
dataVec_Jan->insert(3, 6.0); |
| 24 |
dataVec_Jan->insert(4, 7.0); |
| 25 |
m_data.append(dataVec_Jan); |
| 26 |
|
| 27 |
QList<qreal> *dataVec_Feb = new QList<qreal>(m_rowCount); |
| 28 |
dataVec_Feb->insert(0, 5.0); |
| 29 |
dataVec_Feb->insert(1, 6.0); |
| 30 |
dataVec_Feb->insert(2, 7.5); |
| 31 |
dataVec_Feb->insert(3, 8.0); |
| 32 |
dataVec_Feb->insert(4, 12.0); |
| 33 |
m_data.append(dataVec_Feb); |
| 34 |
|
| 35 |
QList<qreal> *dataVec_Mar = new QList<qreal>(m_rowCount); |
| 36 |
dataVec_Mar->insert(0, 3.0); |
| 37 |
dataVec_Mar->insert(1, 4.0); |
| 38 |
dataVec_Mar->insert(2, 5.7); |
| 39 |
dataVec_Mar->insert(3, 8.0); |
| 40 |
dataVec_Mar->insert(4, 9.0); |
| 41 |
m_data.append(dataVec_Mar); |
| 42 |
|
| 43 |
QList<qreal> *dataVec_Apr = new QList<qreal>(m_rowCount); |
| 44 |
dataVec_Apr->insert(0, 5.0); |
| 45 |
dataVec_Apr->insert(1, 6.0); |
| 46 |
dataVec_Apr->insert(2, 6.8); |
| 47 |
dataVec_Apr->insert(3, 7.0); |
| 48 |
dataVec_Apr->insert(4, 8.0); |
| 49 |
m_data.append(dataVec_Apr); |
| 50 |
|
| 51 |
QList<qreal> *dataVec_May = new QList<qreal>(m_rowCount); |
| 52 |
dataVec_May->insert(0, 4.0); |
| 53 |
dataVec_May->insert(1, 5.0); |
| 54 |
dataVec_May->insert(2, 5.2); |
| 55 |
dataVec_May->insert(3, 6.0); |
| 56 |
dataVec_May->insert(4, 7.0); |
| 57 |
m_data.append(dataVec_May); |
| 58 |
|
| 59 |
QList<qreal> *dataVec_Jun = new QList<qreal>(m_rowCount); |
| 60 |
dataVec_Jun->insert(0, 4.0); |
| 61 |
dataVec_Jun->insert(1, 7.0); |
| 62 |
dataVec_Jun->insert(2, 8.2); |
| 63 |
dataVec_Jun->insert(3, 9.0); |
| 64 |
dataVec_Jun->insert(4, 10.0); |
| 65 |
m_data.append(dataVec_Jun); |
| 66 |
} |
| 67 |
|
| 68 |
CustomTableModel::~CustomTableModel() |
| 69 |
{ |
| 70 |
qDeleteAll(m_data); |
| 71 |
} |
| 72 |
|
| 73 |
int CustomTableModel::rowCount(const QModelIndex &parent) const |
| 74 |
{ |
| 75 |
Q_UNUSED(parent); |
| 76 |
return m_rowCount; |
| 77 |
} |
| 78 |
|
| 79 |
int CustomTableModel::columnCount(const QModelIndex &parent) const |
| 80 |
{ |
| 81 |
Q_UNUSED(parent); |
| 82 |
return m_data.count(); |
| 83 |
} |
| 84 |
|
| 85 |
QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 86 |
{ |
| 87 |
if (role != Qt::DisplayRole) |
| 88 |
return QVariant(); |
| 89 |
|
| 90 |
if (orientation == Qt::Horizontal) |
| 91 |
return categories[section]; |
| 92 |
else |
| 93 |
return QString("%1").arg(section + 1); |
| 94 |
} |
| 95 |
|
| 96 |
QVariant CustomTableModel::data(const QModelIndex &index, int role) const |
| 97 |
{ |
| 98 |
if (role == Qt::DisplayRole) { |
| 99 |
return m_data[index.column()]->at(index.row()); |
| 100 |
} else if (role == Qt::EditRole) { |
| 101 |
return m_data[index.column()]->at(index.row()); |
| 102 |
} else if (role == Qt::BackgroundRole) { |
| 103 |
QRect rect; |
| 104 |
foreach (rect, m_mapping) |
| 105 |
if (rect.contains(index.column(), index.row())) |
| 106 |
return QColor(m_mapping.key(rect)); |
| 107 |
|
| 108 |
// cell not mapped return white color |
| 109 |
return QColor(Qt::white); |
| 110 |
} |
| 111 |
return QVariant(); |
| 112 |
} |
| 113 |
|
| 114 |
bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role) |
| 115 |
{ |
| 116 |
if (index.isValid() && role == Qt::EditRole) { |
| 117 |
m_data[index.column()]->replace(index.row(), value.toDouble()); |
| 118 |
emit dataChanged(index, index); |
| 119 |
return true; |
| 120 |
} |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const |
| 125 |
{ |
| 126 |
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; |
| 127 |
} |
| 128 |
|
| 129 |
void CustomTableModel::addMapping(QString color, QRect area) |
| 130 |
{ |
| 131 |
m_mapping.insert(color, area); |
| 132 |
} |