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 XYCHART_H
0009 #define XYCHART_H
0010 
0011 #include "Chart.h"
0012 
0013 class RangeGroup;
0014 
0015 /**
0016  * A helper containing the calculated X and Y ranges of a chart.
0017  */
0018 struct ComputedRange {
0019     int startX = 0;
0020     int endX = 0;
0021     int distanceX = 0;
0022     float startY = 0.0;
0023     float endY = 0.0;
0024     float distanceY = 0.0;
0025 };
0026 
0027 bool operator==(const ComputedRange &first, const ComputedRange &second);
0028 
0029 /**
0030  * A base class for Charts that are based on an X/Y grid.
0031  */
0032 class XYChart : public Chart
0033 {
0034     Q_OBJECT
0035     /**
0036      * The range of values on the X axis.
0037      */
0038     Q_PROPERTY(RangeGroup *xRange READ xRange CONSTANT)
0039     /**
0040      * The range of values on the Y axis.
0041      */
0042     Q_PROPERTY(RangeGroup *yRange READ yRange CONSTANT)
0043     /**
0044      * Which direction this chart's X axis runs.
0045      */
0046     Q_PROPERTY(Direction direction READ direction WRITE setDirection NOTIFY directionChanged)
0047     /**
0048      * Whether the values of each value source should be stacked.
0049      *
0050      * When true, Y values will be added on top of each other. The precise
0051      * meaning of this property depends on the specific chart. The default is
0052      * false.
0053      */
0054     Q_PROPERTY(bool stacked READ stacked WRITE setStacked NOTIFY stackedChanged)
0055 
0056 public:
0057     /**
0058      * The direction of values on the X axis.
0059      *
0060      * "Start" is defined as the starting direction of the chart, when using a
0061      * left-to-right language it will be the left side, with a right-to-left
0062      * language it will be right.
0063      */
0064     enum class Direction {
0065         ZeroAtStart, ///< Zero is at the beginning of the chart, values run from begin to end.
0066         ZeroAtEnd ///< Zero is at the end of the chart, values run from end to begin.
0067     };
0068     Q_ENUM(Direction)
0069 
0070     /**
0071      * Constructor
0072      *
0073      * @param parent The QObject parent.
0074      */
0075     explicit XYChart(QQuickItem *parent = nullptr);
0076 
0077     /**
0078      * Destructor
0079      */
0080     ~XYChart() override = default;
0081 
0082     virtual RangeGroup *xRange() const;
0083     virtual RangeGroup *yRange() const;
0084 
0085     virtual XYChart::Direction direction() const;
0086     virtual void setDirection(XYChart::Direction newDirection);
0087     Q_SIGNAL void directionChanged();
0088 
0089     bool stacked() const;
0090     void setStacked(bool newStacked);
0091     Q_SIGNAL void stackedChanged();
0092 
0093     /**
0094      * Get the complete, calculated range for this chart.
0095      */
0096     ComputedRange computedRange() const;
0097     /**
0098      * Emitted whenever the complete range is recalculated.
0099      */
0100     Q_SIGNAL void computedRangeChanged();
0101 
0102 protected:
0103     /**
0104      * Re-calculate the chart's range.
0105      *
0106      * By default, this will make use of the xRange/yRange properties and
0107      * calculate a proper range. This method can be overridden by subclasses if
0108      * some special calculation is needed.
0109      */
0110     virtual void updateComputedRange();
0111 
0112     /**
0113      * Set the computed range.
0114      *
0115      * \param range The new range.
0116      */
0117     void setComputedRange(ComputedRange range);
0118 
0119 private:
0120     RangeGroup *m_xRange = nullptr;
0121     RangeGroup *m_yRange = nullptr;
0122     Direction m_direction = Direction::ZeroAtStart;
0123     bool m_stacked = false;
0124     ComputedRange m_computedRange;
0125 };
0126 
0127 QDebug operator<<(QDebug debug, const ComputedRange &range);
0128 
0129 #endif // XYCHART_H