Warning, file /libraries/kqtquickcharts/src/chartmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  Copyright 2014  Sebastian Gottfried <sebastiangottfried@web.de>
0003  *
0004  *  This library is free software; you can redistribute it and/or
0005  *  modify it under the terms of the GNU Lesser General Public
0006  *  License as published by the Free Software Foundation; either
0007  *  version 2.1 of the License, or (at your option) version 3, or any
0008  *  later version accepted by the membership of KDE e.V. (or its
0009  *  successor approved by the membership of KDE e.V.), which shall
0010  *  act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  *  This library is distributed in the hope that it will be useful,
0013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  *  Lesser General Public License for more details.
0016  *
0017  *  You should have received a copy of the GNU Lesser General Public
0018  */
0019 
0020 #include "chartmodel.h"
0021 #include <limits.h>
0022 #include "record.h"
0023 
0024 ChartModel::ChartModel(QObject* parent) :
0025     QAbstractTableModel(parent),
0026     m_columns(0)
0027 {
0028 }
0029 
0030 QQmlListProperty<Record> ChartModel::records()
0031 {
0032     return QQmlListProperty<Record>(this, nullptr, &ChartModel::appendRecord, &ChartModel::countRecords, &ChartModel::recordAt, &ChartModel::clearRecords);
0033 }
0034 
0035 int ChartModel::columns() const
0036 {
0037     return m_columns;
0038 }
0039 
0040 void ChartModel::setColumns(int columns)
0041 {
0042     if (columns != m_columns)
0043     {
0044         beginResetModel();
0045         m_columns = columns;
0046         endResetModel();
0047         emit columnsChanged();
0048     }
0049 }
0050 
0051 int ChartModel::rows() const
0052 {
0053     return m_records.count();
0054 }
0055 
0056 qreal ChartModel::value(int row, int column) const
0057 {
0058     if (row >= m_records.count())
0059         return std::numeric_limits<double>::quiet_NaN();;
0060     return m_records.at(row)->value(column);
0061 }
0062 
0063 void ChartModel::appendRecord()
0064 {
0065     insertRecord(rows(), new Record());
0066 }
0067 
0068 void ChartModel::insertRecord(int row)
0069 {
0070     insertRecord(row, new Record());
0071 }
0072 
0073 void ChartModel::removeRecord(int row)
0074 {
0075     beginRemoveRows(QModelIndex(), row, row);
0076     Record* record = m_records.at(row);
0077     record->disconnect(this);
0078     m_records.removeAt(row);
0079     record->deleteLater();
0080     endRemoveRows();
0081     emit rowsChanged();
0082 }
0083 
0084 void ChartModel::setValue(int row, int column, qreal value)
0085 {
0086     while (row >= m_records.count())
0087     {
0088         appendRecord();
0089     }
0090     m_records.at(row)->setValue(column, value);
0091 }
0092 
0093 int ChartModel::rowCount(const QModelIndex& parent) const
0094 {
0095     if (parent.isValid())
0096         return 0;
0097     return m_records.count();
0098 }
0099 
0100 int ChartModel::columnCount(const QModelIndex& parent) const
0101 {
0102     if (parent.isValid())
0103         return 0;
0104     return m_columns;
0105 }
0106 
0107 QVariant ChartModel::data(const QModelIndex& index, int role) const
0108 {
0109     if (index.parent().isValid())
0110         return QVariant();
0111     if (role != Qt::DisplayRole)
0112         return QVariant();
0113     return QVariant(value(index.row(), index.column()));
0114 }
0115 
0116 void ChartModel::onRecordChanged(Record* record)
0117 {
0118     const int row = m_records.indexOf(record);
0119     emit dataChanged(index(row, 0), index(row, columns() - 1));
0120     emit recordChanged(row);
0121 }
0122 
0123 void ChartModel::insertRecord(int row, Record *record)
0124 {
0125     beginInsertRows(QModelIndex(), row, row);
0126     record->setParent(this);
0127     connect(record, &Record::valuesChanged, this, &ChartModel::onRecordChanged);
0128     m_records.insert(row, record);
0129     endInsertRows();
0130     emit rowsChanged();
0131 }
0132 
0133 void ChartModel::appendRecord(QQmlListProperty<Record>* list, Record* record)
0134 {
0135     ChartModel* chartModel = qobject_cast<ChartModel*>(list->object);
0136     if (chartModel)
0137     {
0138         chartModel->insertRecord(chartModel->rows(), record);
0139     }
0140 }
0141 
0142 int ChartModel::countRecords(QQmlListProperty<Record>* list)
0143 {
0144     ChartModel* chartModel = qobject_cast<ChartModel*>(list->object);
0145     if (chartModel)
0146     {
0147         return chartModel->m_records.count();
0148     }
0149     return -1;
0150 }
0151 
0152 Record* ChartModel::recordAt(QQmlListProperty<Record>* list, int index)
0153 {
0154     ChartModel* chartModel = qobject_cast<ChartModel*>(list->object);
0155     if (chartModel)
0156     {
0157         return chartModel->m_records.at(index);
0158     }
0159     return nullptr;
0160 }
0161 
0162 void ChartModel::clearRecords(QQmlListProperty<Record>* list)
0163 {
0164     ChartModel* chartModel = qobject_cast<ChartModel*>(list->object);
0165     if (chartModel)
0166     {
0167         qDeleteAll(chartModel->m_records);
0168         chartModel->m_records.clear();
0169         emit chartModel->rowsChanged();
0170     }
0171 }