| 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 <QtGui/QColor> | 
| 35 |  | 
| 36 | const QString categories[] = {"Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Nov" , "Dec" }; | 
| 37 |  | 
| 38 | CustomTableModel::CustomTableModel(QObject *parent) : | 
| 39 |     QAbstractTableModel(parent) | 
| 40 | { | 
| 41 |     m_columnCount = 6; | 
| 42 |     m_rowCount = 5; | 
| 43 |     QVector<qreal>* dataVec_Jan = new QVector<qreal>(m_rowCount); | 
| 44 |     dataVec_Jan->insert(i: 0, t: 3.0); | 
| 45 |     dataVec_Jan->insert(i: 1, t: 4.0); | 
| 46 |     dataVec_Jan->insert(i: 2, t: 4.4); | 
| 47 |     dataVec_Jan->insert(i: 3, t: 6.0); | 
| 48 |     dataVec_Jan->insert(i: 4, t: 7.0); | 
| 49 |     m_data.append(t: dataVec_Jan); | 
| 50 |  | 
| 51 |     QVector<qreal>* dataVec_Feb = new QVector<qreal>(m_rowCount); | 
| 52 |     dataVec_Feb->insert(i: 0, t: 5.0); | 
| 53 |     dataVec_Feb->insert(i: 1, t: 6.0); | 
| 54 |     dataVec_Feb->insert(i: 2, t: 7.5); | 
| 55 |     dataVec_Feb->insert(i: 3, t: 8.0); | 
| 56 |     dataVec_Feb->insert(i: 4, t: 12.0); | 
| 57 |     m_data.append(t: dataVec_Feb); | 
| 58 |  | 
| 59 |     QVector<qreal>* dataVec_Mar = new QVector<qreal>(m_rowCount); | 
| 60 |     dataVec_Mar->insert(i: 0, t: 3.0); | 
| 61 |     dataVec_Mar->insert(i: 1, t: 4.0); | 
| 62 |     dataVec_Mar->insert(i: 2, t: 5.7); | 
| 63 |     dataVec_Mar->insert(i: 3, t: 8.0); | 
| 64 |     dataVec_Mar->insert(i: 4, t: 9.0); | 
| 65 |     m_data.append(t: dataVec_Mar); | 
| 66 |  | 
| 67 |     QVector<qreal>* dataVec_Apr = new QVector<qreal>(m_rowCount); | 
| 68 |     dataVec_Apr->insert(i: 0, t: 5.0); | 
| 69 |     dataVec_Apr->insert(i: 1, t: 6.0); | 
| 70 |     dataVec_Apr->insert(i: 2, t: 6.8); | 
| 71 |     dataVec_Apr->insert(i: 3, t: 7.0); | 
| 72 |     dataVec_Apr->insert(i: 4, t: 8.0); | 
| 73 |     m_data.append(t: dataVec_Apr); | 
| 74 |  | 
| 75 |     QVector<qreal>* dataVec_May = new QVector<qreal>(m_rowCount); | 
| 76 |     dataVec_May->insert(i: 0, t: 4.0); | 
| 77 |     dataVec_May->insert(i: 1, t: 5.0); | 
| 78 |     dataVec_May->insert(i: 2, t: 5.2); | 
| 79 |     dataVec_May->insert(i: 3, t: 6.0); | 
| 80 |     dataVec_May->insert(i: 4, t: 7.0); | 
| 81 |     m_data.append(t: dataVec_May); | 
| 82 |  | 
| 83 |     QVector<qreal>* dataVec_Jun = new QVector<qreal>(m_rowCount); | 
| 84 |     dataVec_Jun->insert(i: 0, t: 4.0); | 
| 85 |     dataVec_Jun->insert(i: 1, t: 7.0); | 
| 86 |     dataVec_Jun->insert(i: 2, t: 8.2); | 
| 87 |     dataVec_Jun->insert(i: 3, t: 9.0); | 
| 88 |     dataVec_Jun->insert(i: 4, t: 10.0); | 
| 89 |     m_data.append(t: dataVec_Jun); | 
| 90 | } | 
| 91 |  | 
| 92 | CustomTableModel::~CustomTableModel() | 
| 93 | { | 
| 94 |     qDeleteAll(c: m_data); | 
| 95 | } | 
| 96 |  | 
| 97 | int CustomTableModel::rowCount(const QModelIndex &parent) const | 
| 98 | { | 
| 99 |     Q_UNUSED(parent) | 
| 100 |     return m_rowCount; | 
| 101 | } | 
| 102 |  | 
| 103 | int CustomTableModel::columnCount(const QModelIndex &parent) const | 
| 104 | { | 
| 105 |     Q_UNUSED(parent) | 
| 106 |     return m_data.count(); | 
| 107 | } | 
| 108 |  | 
| 109 | QVariant CustomTableModel::(int section, Qt::Orientation orientation, int role) const | 
| 110 | { | 
| 111 |     if (role != Qt::DisplayRole) | 
| 112 |         return QVariant(); | 
| 113 |  | 
| 114 |     if (orientation == Qt::Horizontal) | 
| 115 |         return categories[section]; | 
| 116 |     else | 
| 117 |         return QString("%1" ).arg(a: section + 1); | 
| 118 | } | 
| 119 |  | 
| 120 | QVariant CustomTableModel::data(const QModelIndex &index, int role) const | 
| 121 | { | 
| 122 |     if (role == Qt::DisplayRole) { | 
| 123 |         return m_data[index.column()]->at(i: index.row()); | 
| 124 |     } else if (role == Qt::EditRole) { | 
| 125 |         return m_data[index.column()]->at(i: index.row()); | 
| 126 |     } else if (role == Qt::BackgroundRole) { | 
| 127 |         QRect rect; | 
| 128 |         foreach (rect, m_mapping) | 
| 129 |         if (rect.contains(ax: index.column(), ay: index.row())) | 
| 130 |             return QColor(m_mapping.key(avalue: rect)); | 
| 131 |  | 
| 132 |         // cell not mapped return white color | 
| 133 |         return QColor(Qt::white); | 
| 134 |     } | 
| 135 |     return QVariant(); | 
| 136 | } | 
| 137 |  | 
| 138 | bool CustomTableModel::setData(const QModelIndex &index, const QVariant &value, int role) | 
| 139 | { | 
| 140 |     if (index.isValid() && role == Qt::EditRole) { | 
| 141 |         m_data[index.column()]->replace(i: index.row(), t: value.toDouble()); | 
| 142 |         emit dataChanged(topLeft: index, bottomRight: index); | 
| 143 |         return true; | 
| 144 |     } | 
| 145 |     return false; | 
| 146 | } | 
| 147 |  | 
| 148 | Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const | 
| 149 | { | 
| 150 |     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable; | 
| 151 | } | 
| 152 |  | 
| 153 | void CustomTableModel::addMapping(QString color, QRect area) | 
| 154 | { | 
| 155 |     m_mapping.insertMulti(key: color, value: area); | 
| 156 | } | 
| 157 |  |