1/*
2 * This file is part of KQuickCharts
3 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#ifndef MODELSOURCE_H
9#define MODELSOURCE_H
10
11#include <QAbstractItemModel>
12#include <QPointer>
13
14#include "ChartDataSource.h"
15
16/**
17 * A data source that reads data from a QAbstractItemModel.
18 *
19 *
20 */
21class QUICKCHARTS_EXPORT ModelSource : public ChartDataSource
22{
23 Q_OBJECT
24 QML_ELEMENT
25
26public:
27 explicit ModelSource(QObject *parent = nullptr);
28
29 Q_PROPERTY(int role READ role WRITE setRole NOTIFY roleChanged)
30 int role() const;
31 void setRole(int role);
32 Q_SIGNAL void roleChanged();
33
34 Q_PROPERTY(QString roleName READ roleName WRITE setRoleName NOTIFY roleNameChanged)
35 QString roleName() const;
36 void setRoleName(const QString &name);
37 Q_SIGNAL void roleNameChanged();
38
39 Q_PROPERTY(int column READ column WRITE setColumn NOTIFY columnChanged)
40 int column() const;
41 void setColumn(int column);
42 Q_SIGNAL void columnChanged();
43
44 Q_PROPERTY(QAbstractItemModel *model READ model WRITE setModel NOTIFY modelChanged)
45 QAbstractItemModel *model() const;
46 void setModel(QAbstractItemModel *model);
47 Q_SIGNAL void modelChanged();
48
49 Q_PROPERTY(bool indexColumns READ indexColumns WRITE setIndexColumns NOTIFY indexColumnsChanged)
50 bool indexColumns() const;
51 void setIndexColumns(bool index);
52 Q_SIGNAL void indexColumnsChanged();
53
54 int itemCount() const override;
55 QVariant item(int index) const override;
56 QVariant minimum() const override;
57 QVariant maximum() const override;
58
59private:
60 Q_SLOT void onMinimumChanged();
61 Q_SLOT void onMaximumChanged();
62
63 mutable int m_role = -1;
64 QString m_roleName;
65 int m_column = 0;
66 bool m_indexColumns = false;
67 QAbstractItemModel *m_model = nullptr;
68
69 QVariant m_minimum;
70 QVariant m_maximum;
71};
72
73#endif // MODELSOURCE_H
74

source code of kquickcharts/src/datasource/ModelSource.h