File indexing completed on 2024-12-01 06:46:26

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 #include <qqmlregistration.h>
0015 
0016 #include "quickcharts_export.h"
0017 
0018 class ChartDataSource;
0019 
0020 /**
0021  * An object that can be used as a grouped property to provide a value range for charts.
0022  *
0023  */
0024 class QUICKCHARTS_EXPORT RangeGroup : public QObject
0025 {
0026     Q_OBJECT
0027     QML_NAMED_ELEMENT(Range)
0028     QML_UNCREATABLE("Grouped Property")
0029 
0030 public:
0031     struct RangeResult {
0032         qreal start = 0.0;
0033         qreal end = 0.0;
0034         qreal distance = 0.0;
0035     };
0036 
0037     explicit RangeGroup(QObject *parent = nullptr);
0038 
0039     /**
0040      * The start of this range.
0041      *
0042      * The default is 0.
0043      */
0044     Q_PROPERTY(qreal from READ from WRITE setFrom NOTIFY fromChanged)
0045     qreal from() const;
0046     void setFrom(qreal from);
0047     Q_SIGNAL void fromChanged();
0048     /**
0049      * The end of this range.
0050      *
0051      * The default is 100.
0052      */
0053     Q_PROPERTY(qreal to READ to WRITE setTo NOTIFY toChanged)
0054     qreal to() const;
0055     void setTo(qreal to);
0056     Q_SIGNAL void toChanged();
0057     /**
0058      * Whether to determine the range based on values of a chart.
0059      *
0060      * If true (the default), `from` and `to` are ignored and instead calculated
0061      * from the minimum and maximum values of a chart's valueSources.
0062      */
0063     Q_PROPERTY(bool automatic READ automatic WRITE setAutomatic NOTIFY automaticChanged)
0064     bool automatic() const;
0065     void setAutomatic(bool newAutomatic);
0066     Q_SIGNAL void automaticChanged();
0067     /**
0068      * The distance between from and to.
0069      */
0070     Q_PROPERTY(qreal distance READ distance NOTIFY rangeChanged)
0071     qreal distance() const;
0072     /**
0073      * The minimum size of the range.
0074      *
0075      * This is mostly relevant when automatic is true. Setting this value will
0076      * ensure that the range will never be smaller than this value. The default
0077      * is `std::numeric_limits<qreal>::min`, which means minimum is disabled.
0078      */
0079     Q_PROPERTY(qreal minimum READ minimum WRITE setMinimum NOTIFY minimumChanged)
0080     qreal minimum() const;
0081     void setMinimum(qreal newMinimum);
0082     Q_SIGNAL void minimumChanged();
0083     /**
0084      * The amount with which the range increases.
0085      *
0086      * The total range will be limited to a multiple of this value. This is
0087      * mostly useful when automatic is true. The default is 0.0, which means do
0088      * not limit the range increment.
0089      */
0090     Q_PROPERTY(qreal increment READ increment WRITE setIncrement NOTIFY incrementChanged)
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 QList<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