File indexing completed on 2024-10-06 03:40: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 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 QUICKCHARTS_EXPORT XYChart : public Chart
0033 {
0034     Q_OBJECT
0035     QML_ELEMENT
0036     QML_UNCREATABLE("Base Class")
0037 
0038 public:
0039     /**
0040      * The direction of values on the X axis.
0041      *
0042      * "Start" is defined as the starting direction of the chart, when using a
0043      * left-to-right language it will be the left side, with a right-to-left
0044      * language it will be right.
0045      */
0046     enum class Direction {
0047         ZeroAtStart, ///< Zero is at the beginning of the chart, values run from begin to end.
0048         ZeroAtEnd ///< Zero is at the end of the chart, values run from end to begin.
0049     };
0050     Q_ENUM(Direction)
0051 
0052     /**
0053      * Constructor
0054      *
0055      * @param parent The QObject parent.
0056      */
0057     explicit XYChart(QQuickItem *parent = nullptr);
0058 
0059     /**
0060      * Destructor
0061      */
0062     ~XYChart() override = default;
0063 
0064     /**
0065      * The range of values on the X axis.
0066      */
0067     Q_PROPERTY(RangeGroup *xRange READ xRange CONSTANT)
0068     virtual RangeGroup *xRange() const;
0069     /**
0070      * The range of values on the Y axis.
0071      */
0072     Q_PROPERTY(RangeGroup *yRange READ yRange CONSTANT)
0073     virtual RangeGroup *yRange() const;
0074     /**
0075      * Which direction this chart's X axis runs.
0076      */
0077     Q_PROPERTY(Direction direction READ direction WRITE setDirection NOTIFY directionChanged)
0078     virtual XYChart::Direction direction() const;
0079     virtual void setDirection(XYChart::Direction newDirection);
0080     Q_SIGNAL void directionChanged();
0081     /**
0082      * Whether the values of each value source should be stacked.
0083      *
0084      * When true, Y values will be added on top of each other. The precise
0085      * meaning of this property depends on the specific chart. The default is
0086      * false.
0087      */
0088     Q_PROPERTY(bool stacked READ stacked WRITE setStacked NOTIFY stackedChanged)
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