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 MODELHISTORYSOURCE_H
9 #define MODELHISTORYSOURCE_H
10 
11 #include "ModelSource.h"
12 #include "quickcharts_export.h"
13 #include <QTimer>
14 #include <memory>
15 
16 #if QUICKCHARTS_BUILD_DEPRECATED_SINCE(5, 78)
17 
18 /**
19  * A data source that watches a QAbstractItemModel cell and provides the history of that cell as data.
20  *
21  * \deprecated Since 5.78, use HistoryProxySource instead with a ModelSource as
22  * inner source.
23  */
24 class ModelHistorySource : public ModelSource
25 {
26     Q_OBJECT
27     /**
28      * The row to read data from.
29      *
30      * The items of this source will be the values of that row, using the column
31      * and role from ModelSource.
32      *
33      * The default is 0.
34      */
35     Q_PROPERTY(int row READ row WRITE setRow NOTIFY rowChanged)
36     /**
37      * The maximum amount of history to keep.
38      *
39      * The default is 10.
40      */
41     Q_PROPERTY(int maximumHistory READ maximumHistory WRITE setMaximumHistory NOTIFY maximumHistoryChanged)
42     /**
43      * The interval, in milliseconds, with which to query the model.
44      *
45      * If not set or set to a value < 0, a new item will be added whenever the
46      * underlying model changes. Otherwise, the source will sample the
47      * underlying model every interval milliseconds and add a new item with
48      * whatever value the model has at that point - even if it did not change.
49      *
50      * The default is 0.
51      */
52     Q_PROPERTY(int interval READ interval WRITE setInterval NOTIFY intervalChanged)
53 
54 public:
55     explicit ModelHistorySource(QObject *parent = nullptr);
56 
57     virtual int itemCount() const override;
58     virtual QVariant item(int index) const override;
59     virtual QVariant minimum() const override;
60     virtual QVariant maximum() const override;
61 
62     int row() const;
63     void setRow(int row);
64     Q_SIGNAL void rowChanged();
65 
66     int maximumHistory() const;
67     void setMaximumHistory(int maximumHistory);
68     Q_SIGNAL void maximumHistoryChanged();
69 
70     int interval() const;
71     void setInterval(int newInterval);
72     Q_SIGNAL void intervalChanged();
73 
74     Q_INVOKABLE void clear();
75 
76 private:
77     void onModelChanged();
78     void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
79 
80     int m_row = 0;
81     int m_maximumHistory = 10;
82     QVariantList m_history;
83     std::unique_ptr<QTimer> m_updateTimer;
84 };
85 
86 #endif // QUICKCHARTS_BUILD_DEPRECATED_SINCE
87 
88 #endif // MODELHISTORYSOURCE_H
89