File indexing completed on 2024-04-21 15:02:53

0001 /*
0002  * This file is part of KQuickCharts
0003  * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 #ifndef VALUEHISTORYSOURCE_H
0009 #define VALUEHISTORYSOURCE_H
0010 
0011 #include <QTimer>
0012 #include <QVariant>
0013 #include <QVector>
0014 #include <memory>
0015 
0016 #include "ChartDataSource.h"
0017 #include "quickcharts_export.h"
0018 
0019 #if QUICKCHARTS_BUILD_DEPRECATED_SINCE(5, 78)
0020 
0021 /**
0022  * A data source that provides a history of a single value.
0023  *
0024  * \deprecated Since 5.78, use HistoryProxySource instead with a
0025  * SingleValueSource as inner source.
0026  */
0027 class ValueHistorySource : public ChartDataSource
0028 {
0029     Q_OBJECT
0030     /**
0031      * The value to track.
0032      *
0033      * Depending on the value of \property interval , changing this will add a
0034      * new item to the source.
0035      */
0036     Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY dataChanged)
0037     /**
0038      * The maximum amount of history to keep.
0039      *
0040      * The default is 10.
0041      */
0042     Q_PROPERTY(int maximumHistory READ maximumHistory WRITE setMaximumHistory NOTIFY maximumHistoryChanged)
0043     /**
0044      * The interval, in milliseconds, with which to query the value.
0045      *
0046      * If not set or set to a value < 0, a new item will be added whenever value
0047      * changes. Otherwise, the source will sample the value every interval
0048      * milliseconds and add a new item with whatever value it has at that point
0049      * - even if it did not change.
0050      *
0051      * The default is 0.
0052      */
0053     Q_PROPERTY(int interval READ interval WRITE setInterval NOTIFY intervalChanged)
0054 
0055 public:
0056     explicit ValueHistorySource(QObject *parent = nullptr);
0057 
0058     int itemCount() const override;
0059     QVariant item(int index) const override;
0060     QVariant minimum() const override;
0061     QVariant maximum() const override;
0062 
0063     QVariant value() const;
0064     void setValue(const QVariant &value);
0065 
0066     int maximumHistory() const;
0067     void setMaximumHistory(int maximumHistory);
0068     Q_SIGNAL void maximumHistoryChanged();
0069 
0070     int interval() const;
0071     void setInterval(int newInterval);
0072     Q_SIGNAL void intervalChanged();
0073 
0074     Q_INVOKABLE void clear();
0075 
0076 private:
0077     QVariant m_value;
0078     int m_maximumHistory = 10;
0079     std::unique_ptr<QTimer> m_updateTimer;
0080     QVector<QVariant> m_history;
0081 };
0082 
0083 #endif // QUICKCHARTS_BUILD_DEPRECATED_SINCE
0084 
0085 #endif // VALUEHISTORYSOURCE_H