| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef AXISRENDERER_H |
| 5 | #define AXISRENDERER_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the QtGraphs API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | |
| 17 | #include <QQuickItem> |
| 18 | #include <QRectF> |
| 19 | #include <QList> |
| 20 | #include <QList> |
| 21 | #include <QtQuick/private/qquicktext_p.h> |
| 22 | #include <private/axisgrid_p.h> |
| 23 | #include <private/axisticker_p.h> |
| 24 | #include <private/axisline_p.h> |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QAbstractAxis; |
| 29 | class QGraphsView; |
| 30 | class QBarCategoryAxis; |
| 31 | class QValueAxis; |
| 32 | class QGraphsTheme; |
| 33 | class QDateTimeAxis; |
| 34 | class QQuickDragHandler; |
| 35 | class QAbstractSeries; |
| 36 | |
| 37 | class AxisRenderer : public QQuickItem |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | public: |
| 41 | AxisRenderer(QQuickItem *parent = nullptr); |
| 42 | ~AxisRenderer() override; |
| 43 | |
| 44 | void handlePolish(); |
| 45 | void updateAxis(); |
| 46 | void updateAxisTickers(); |
| 47 | void updateAxisTickersShadow(); |
| 48 | void updateAxisGrid(); |
| 49 | void updateAxisGridShadow(); |
| 50 | void updateAxisTitles(); |
| 51 | void initialize(); |
| 52 | |
| 53 | bool handleWheel(QWheelEvent *event); |
| 54 | void handlePinchScale(qreal delta); |
| 55 | void handlePinchGrab(QPointingDevice::GrabTransition transition, QEventPoint point); |
| 56 | |
| 57 | Q_SIGNALS: |
| 58 | |
| 59 | private: |
| 60 | friend class QGraphsView; |
| 61 | friend class BarsRenderer; |
| 62 | friend class LinesRenderer; |
| 63 | friend class PointRenderer; |
| 64 | friend class AreaRenderer; |
| 65 | |
| 66 | struct AxisProperties { |
| 67 | qreal x = 0; |
| 68 | qreal y = 0; |
| 69 | |
| 70 | QAbstractAxis *axis = nullptr; |
| 71 | |
| 72 | QList<QQuickItem *> textItems; |
| 73 | QQuickText *title = nullptr; |
| 74 | AxisTicker *ticker = nullptr; |
| 75 | AxisLine *line = nullptr; |
| 76 | AxisTicker *tickerShadow = nullptr; |
| 77 | AxisLine *lineShadow = nullptr; |
| 78 | |
| 79 | // Max value |
| 80 | double maxValue = 20; |
| 81 | // Min value |
| 82 | double minValue = 0; |
| 83 | // Values range, so m_axisVerticalMaxValue - m_axisVerticalMinValue |
| 84 | double valueRange = 0; |
| 85 | double valueRangeZoomless = 0; |
| 86 | // How much each major value step is |
| 87 | double valueStep = 1.0; |
| 88 | // px between major ticks |
| 89 | double stepPx = 0; |
| 90 | // Ticks movement, between -m_axisHorizontalStepPx .. m_axisHorizontalStepPx. |
| 91 | double displacement = 0; |
| 92 | // The value of smallest label |
| 93 | double minLabel = 0; |
| 94 | double subGridScale = 0.5; |
| 95 | }; |
| 96 | |
| 97 | #ifdef USE_BARGRAPH |
| 98 | void updateBarXAxisLabels(AxisProperties &ax, const QRectF rect); |
| 99 | void updateBarYAxisLabels(AxisProperties &ax, const QRectF rect); |
| 100 | #endif |
| 101 | void updateValueYAxisLabels(AxisProperties &ax, const QRectF rect); |
| 102 | void updateValueXAxisLabels(AxisProperties &ax, const QRectF rect); |
| 103 | void updateDateTimeYAxisLabels(AxisProperties &ax, const QRectF rect); |
| 104 | void updateDateTimeXAxisLabels(AxisProperties &ax, const QRectF rect); |
| 105 | |
| 106 | void onTranslationChanged(QVector2D delta); |
| 107 | void onGrabChanged(QPointingDevice::GrabTransition transition, QEventPoint point); |
| 108 | |
| 109 | double getValueStepsFromRange(double range); |
| 110 | int getValueDecimalsFromRange(double range); |
| 111 | void setLabelTextProperties(QQuickItem *item, const QString &text, bool xAxis, |
| 112 | QQuickText::HAlignment hAlign = QQuickText::HAlignment::AlignHCenter, |
| 113 | QQuickText::VAlignment vAlign = QQuickText::VAlignment::AlignVCenter, |
| 114 | Qt::TextElideMode elide = Qt::ElideNone); |
| 115 | void updateAxisLabelItems(QList<QQuickItem *> &textItems, qsizetype neededSize, |
| 116 | QQmlComponent *component); |
| 117 | |
| 118 | QVector2D windowToAxisCoords(QVector2D coords); |
| 119 | bool zoom(qreal delta); |
| 120 | |
| 121 | const AxisProperties &getAxisX(QAbstractSeries *series) const; |
| 122 | const AxisProperties &getAxisY(QAbstractSeries *series) const; |
| 123 | |
| 124 | QGraphsView *m_graph = nullptr; |
| 125 | QGraphsTheme *theme(); |
| 126 | bool m_initialized = false; |
| 127 | bool m_wasVertical = false; |
| 128 | |
| 129 | QVector<AxisProperties> m_axes1; |
| 130 | QVector<AxisProperties> m_axes2; |
| 131 | QVector<AxisProperties> *m_horzAxes = &m_axes1; |
| 132 | QVector<AxisProperties> *m_vertAxes = &m_axes2; |
| 133 | |
| 134 | AxisGrid *m_axisGrid = nullptr; |
| 135 | AxisGrid *m_axisGridShadow = nullptr; |
| 136 | |
| 137 | bool m_gridHorizontalLinesVisible = true; |
| 138 | bool m_gridVerticalLinesVisible = true; |
| 139 | bool m_gridHorizontalSubLinesVisible = false; |
| 140 | bool m_gridVerticalSubLinesVisible = false; |
| 141 | |
| 142 | QQuickDragHandler *m_dragHandler = nullptr; |
| 143 | |
| 144 | struct DragState |
| 145 | { |
| 146 | bool dragging = false; |
| 147 | QVector2D touchPositionAtPress; |
| 148 | QVector2D panAtPress; |
| 149 | QVector2D delta; |
| 150 | }; |
| 151 | |
| 152 | DragState m_dragState; |
| 153 | }; |
| 154 | |
| 155 | QT_END_NAMESPACE |
| 156 | |
| 157 | #endif // AXISRENDERER_H |
| 158 | |