Warning, file /frameworks/kquickcharts/src/datasource/HistoryProxySource.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 HISTORYPROXYSOURCE_H
0009 #define HISTORYPROXYSOURCE_H
0010 
0011 #include <QTimer>
0012 #include <QVariant>
0013 #include <QVector>
0014 #include <memory>
0015 
0016 #include "ChartDataSource.h"
0017 
0018 /**
0019  * A data source that provides a history of a single item of a different data source.
0020  *
0021  * This data source will monitor a single item of another data source for changes
0022  * and record them, exposing historical values as
0023  */
0024 class HistoryProxySource : public ChartDataSource
0025 {
0026     Q_OBJECT
0027 
0028     /**
0029      * The data source to read data from.
0030      *
0031      * This will use the item at \ref item from the provided source and use that
0032      * to fill the history of this source.
0033      *
0034      * \note Changing this property will clear the existing history.
0035      */
0036     Q_PROPERTY(ChartDataSource *source READ source WRITE setSource NOTIFY sourceChanged)
0037     /**
0038      * The item of the data source to read data from.
0039      *
0040      * This item will be read either when the source's dataChanged has been
0041      * emitted or on an interval if \ref interval has been set.
0042      *
0043      * The default is 0.
0044      *
0045      * \note Changing this property will clear the existing history.
0046      */
0047     Q_PROPERTY(int item READ item WRITE setItem NOTIFY itemChanged)
0048     /**
0049      * The maximum amount of history to keep.
0050      *
0051      * The default is 10.
0052      */
0053     Q_PROPERTY(int maximumHistory READ maximumHistory WRITE setMaximumHistory NOTIFY maximumHistoryChanged)
0054     /**
0055      * The interval, in milliseconds, with which to query the data source.
0056      *
0057      * If set to a value <= 0, a new item will be added whenever \ref source
0058      * changes. Otherwise, source will be sampled every interval milliseconds
0059      * and a new item will be added with whatever value it has at that point,
0060      * even if it did not change.
0061      *
0062      * The default is 0.
0063      */
0064     Q_PROPERTY(int interval READ interval WRITE setInterval NOTIFY intervalChanged)
0065     /**
0066      * The fill mode.
0067      *
0068      * This determines what happens when there is not enough history yet. See
0069      * \ref FillMode for which modes are available.
0070      *
0071      * The default is DoNotFill.
0072      *
0073      * \note Changing this property will clear the existing history.
0074      */
0075     Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged)
0076 
0077 public:
0078     /**
0079      * The different fill modes.
0080      *
0081      * These determine the value returned by \ref itemCount and what happens
0082      * when requesting an item that does not have a value in the history.
0083      */
0084     enum FillMode {
0085         /**
0086          * Do not fill with any items. The source's \ref itemCount will be equal
0087          * to the number of items in the history.
0088          */
0089         DoNotFill,
0090         /**
0091          * Fill with empty values, starting at 0. The source's \ref itemCount
0092          * will be equal to \ref maximumHistory. Items that do not have a value
0093          * in the history will return a default-constructed value based on the
0094          * first item of the source.
0095          */
0096         FillFromStart,
0097         /**
0098          * Fill with empty values, placing partial history at the end. This
0099          * means that the first recorded history item will be shown at position
0100          * `maximumHistory - 1`, the second at `maximumHistory - 2` and so on,
0101          * until \ref maximumHistory is reached, after which items will be
0102          * discarded normally.
0103          */
0104         FillFromEnd
0105     };
0106     Q_ENUM(FillMode)
0107 
0108     explicit HistoryProxySource(QObject *parent = nullptr);
0109 
0110     int itemCount() const override;
0111     QVariant item(int index) const override;
0112     QVariant minimum() const override;
0113     QVariant maximum() const override;
0114 
0115     QVariant first() const override;
0116 
0117     ChartDataSource *source() const;
0118     void setSource(ChartDataSource *newSource);
0119     Q_SIGNAL void sourceChanged();
0120 
0121     int item() const;
0122     void setItem(int newItem);
0123     Q_SIGNAL void itemChanged();
0124 
0125     int maximumHistory() const;
0126     void setMaximumHistory(int maximumHistory);
0127     Q_SIGNAL void maximumHistoryChanged();
0128 
0129     int interval() const;
0130     void setInterval(int newInterval);
0131     Q_SIGNAL void intervalChanged();
0132 
0133     FillMode fillMode() const;
0134     void setFillMode(FillMode newFillMode);
0135     Q_SIGNAL void fillModeChanged();
0136 
0137     /**
0138      * Clear the entire history of this source.
0139      */
0140     Q_INVOKABLE void clear();
0141 
0142 private:
0143     void update();
0144 
0145     ChartDataSource *m_dataSource = nullptr;
0146     int m_item = 0;
0147     int m_maximumHistory = 10;
0148     FillMode m_fillMode = DoNotFill;
0149     std::unique_ptr<QTimer> m_updateTimer;
0150     QVector<QVariant> m_history;
0151 };
0152 
0153 #endif // HISTORYPROXYSOURCE_H