File indexing completed on 2024-04-14 14:27:08

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 RANGEGROUP_H
0009 #define RANGEGROUP_H
0010 
0011 #include <functional>
0012 
0013 #include <QObject>
0014 
0015 class ChartDataSource;
0016 
0017 /**
0018  * An object that can be used as a grouped property to provide a value range for charts.
0019  *
0020  */
0021 class RangeGroup : public QObject
0022 {
0023     Q_OBJECT
0024     /**
0025      * The start of this range.
0026      *
0027      * The default is 0.
0028      */
0029     Q_PROPERTY(qreal from READ from WRITE setFrom NOTIFY fromChanged)
0030     /**
0031      * The end of this range.
0032      *
0033      * The default is 100.
0034      */
0035     Q_PROPERTY(qreal to READ to WRITE setTo NOTIFY toChanged)
0036     /**
0037      * Whether to determine the range based on values of a chart.
0038      *
0039      * If true (the default), `from` and `to` are ignored and instead calculated
0040      * from the minimum and maximum values of a chart's valueSources.
0041      */
0042     Q_PROPERTY(bool automatic READ automatic WRITE setAutomatic NOTIFY automaticChanged)
0043     /**
0044      * The distance between from and to.
0045      */
0046     Q_PROPERTY(qreal distance READ distance NOTIFY rangeChanged)
0047     /**
0048      * The minimum size of the range.
0049      *
0050      * This is mostly relevant when automatic is true. Setting this value will
0051      * ensure that the range will never be smaller than this value. The default
0052      * is `std::numeric_limits<qreal>::min`, which means minimum is disabled.
0053      */
0054     Q_PROPERTY(qreal minimum READ minimum WRITE setMinimum NOTIFY minimumChanged)
0055     /**
0056      * The amount with which the range increases.
0057      *
0058      * The total range will be limited to a multiple of this value. This is
0059      * mostly useful when automatic is true. The default is 0.0, which means do
0060      * not limit the range increment.
0061      */
0062     Q_PROPERTY(qreal increment READ increment WRITE setIncrement NOTIFY incrementChanged)
0063 
0064 public:
0065     struct RangeResult {
0066         qreal start = 0.0;
0067         qreal end = 0.0;
0068         qreal distance = 0.0;
0069     };
0070 
0071     explicit RangeGroup(QObject *parent = nullptr);
0072 
0073     qreal from() const;
0074     void setFrom(qreal from);
0075     Q_SIGNAL void fromChanged();
0076 
0077     qreal to() const;
0078     void setTo(qreal to);
0079     Q_SIGNAL void toChanged();
0080 
0081     bool automatic() const;
0082     void setAutomatic(bool newAutomatic);
0083     Q_SIGNAL void automaticChanged();
0084 
0085     qreal distance() const;
0086 
0087     qreal minimum() const;
0088     void setMinimum(qreal newMinimum);
0089     Q_SIGNAL void minimumChanged();
0090 
0091     qreal increment() const;
0092     void setIncrement(qreal newIncrement);
0093     Q_SIGNAL void incrementChanged();
0094 
0095     bool isValid() const;
0096 
0097     Q_SIGNAL void rangeChanged();
0098 
0099     RangeResult calculateRange(const QVector<ChartDataSource *> &sources,
0100                                std::function<qreal(ChartDataSource *)> minimumCallback,
0101                                std::function<qreal(ChartDataSource *)> maximumCallback);
0102 
0103 private:
0104     qreal m_from = 0.0;
0105     qreal m_to = 100.0;
0106     bool m_automatic = true;
0107     qreal m_minimum = std::numeric_limits<qreal>::min();
0108     qreal m_increment = 0.0;
0109 };
0110 
0111 #endif // RANGEGROUP_H