| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QGRAPHSVIEW_H |
| 5 | #define QGRAPHSVIEW_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 <QtQuick/QQuickItem> |
| 18 | #include <QtCore/QList> |
| 19 | #include <QtQml/QQmlListProperty> |
| 20 | #include <QtGraphs/qabstractseries.h> |
| 21 | #include <QtGraphs/qgraphstheme.h> |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class QQuickRectangle; |
| 26 | class QAbstractAxis; |
| 27 | class AxisRenderer; |
| 28 | class BarsRenderer; |
| 29 | class PointRenderer; |
| 30 | class PieRenderer; |
| 31 | class AreaRenderer; |
| 32 | |
| 33 | class Q_GRAPHS_EXPORT QGraphsView : public QQuickItem |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | Q_PROPERTY(QGraphsTheme *theme READ theme WRITE setTheme NOTIFY themeChanged FINAL) |
| 37 | Q_PROPERTY(QQmlListProperty<QObject> seriesList READ seriesList CONSTANT) |
| 38 | Q_PROPERTY(qreal marginTop READ marginTop WRITE setMarginTop NOTIFY marginTopChanged FINAL) |
| 39 | Q_PROPERTY(qreal marginBottom READ marginBottom WRITE setMarginBottom NOTIFY marginBottomChanged FINAL) |
| 40 | Q_PROPERTY(qreal marginLeft READ marginLeft WRITE setMarginLeft NOTIFY marginLeftChanged FINAL) |
| 41 | Q_PROPERTY(qreal marginRight READ marginRight WRITE setMarginRight NOTIFY marginRightChanged FINAL) |
| 42 | |
| 43 | Q_PROPERTY(qreal axisXSmoothing READ axisXSmoothing WRITE setAxisXSmoothing NOTIFY axisXSmoothingChanged FINAL) |
| 44 | Q_PROPERTY(qreal axisYSmoothing READ axisYSmoothing WRITE setAxisYSmoothing NOTIFY axisYSmoothingChanged FINAL) |
| 45 | Q_PROPERTY(qreal gridSmoothing READ gridSmoothing WRITE setGridSmoothing NOTIFY gridSmoothingChanged FINAL) |
| 46 | |
| 47 | Q_PROPERTY(bool shadowVisible READ isShadowVisible WRITE setShadowVisible NOTIFY |
| 48 | shadowVisibleChanged FINAL) |
| 49 | Q_PROPERTY(QColor shadowColor READ shadowColor WRITE setShadowColor NOTIFY shadowColorChanged FINAL) |
| 50 | Q_PROPERTY(qreal shadowBarWidth READ shadowBarWidth WRITE setShadowBarWidth NOTIFY shadowBarWidthChanged FINAL) |
| 51 | Q_PROPERTY(qreal shadowXOffset READ shadowXOffset WRITE setShadowXOffset NOTIFY shadowXOffsetChanged FINAL) |
| 52 | Q_PROPERTY(qreal shadowYOffset READ shadowYOffset WRITE setShadowYOffset NOTIFY shadowYOffsetChanged FINAL) |
| 53 | Q_PROPERTY(qreal shadowSmoothing READ shadowSmoothing WRITE setShadowSmoothing NOTIFY shadowSmoothingChanged FINAL) |
| 54 | |
| 55 | Q_PROPERTY(QAbstractAxis *axisX READ axisX WRITE setAxisX NOTIFY axisXChanged FINAL) |
| 56 | Q_PROPERTY(QAbstractAxis *axisY READ axisY WRITE setAxisY NOTIFY axisYChanged FINAL) |
| 57 | Q_PROPERTY( |
| 58 | Qt::Orientation orientation READ orientation WRITE setOrientation NOTIFY orientationChanged FINAL) |
| 59 | |
| 60 | Q_CLASSINFO("DefaultProperty" , "seriesList" ) |
| 61 | QML_NAMED_ELEMENT(GraphsView) |
| 62 | |
| 63 | public: |
| 64 | explicit QGraphsView(QQuickItem *parent = nullptr); |
| 65 | ~QGraphsView() override; |
| 66 | |
| 67 | Q_INVOKABLE void addSeries(QObject *series); |
| 68 | Q_INVOKABLE void insertSeries(qsizetype index, QObject *series); |
| 69 | Q_INVOKABLE void removeSeries(QObject *series); |
| 70 | Q_INVOKABLE void removeSeries(qsizetype index); |
| 71 | Q_INVOKABLE bool hasSeries(QObject *series); |
| 72 | |
| 73 | QList<QObject *> getSeriesList() const { |
| 74 | return m_seriesList; |
| 75 | } |
| 76 | |
| 77 | QQmlListProperty<QObject> seriesList(); |
| 78 | static void appendSeriesFunc(QQmlListProperty<QObject> *list, QObject *series); |
| 79 | static qsizetype countSeriesFunc(QQmlListProperty<QObject> *list); |
| 80 | static QObject *atSeriesFunc(QQmlListProperty<QObject> *list, qsizetype index); |
| 81 | static void clearSeriesFunc(QQmlListProperty<QObject> *list); |
| 82 | |
| 83 | QGraphsTheme *theme() const; |
| 84 | void setTheme(QGraphsTheme *newTheme); |
| 85 | |
| 86 | qreal marginTop() const; |
| 87 | void setMarginTop(qreal newMarginTop); |
| 88 | |
| 89 | qreal marginBottom() const; |
| 90 | void setMarginBottom(qreal newMarginBottom); |
| 91 | |
| 92 | qreal marginLeft() const; |
| 93 | void setMarginLeft(qreal newMarginLeft); |
| 94 | |
| 95 | qreal marginRight() const; |
| 96 | void setMarginRight(qreal newMarginRight); |
| 97 | |
| 98 | QRectF plotArea() const; |
| 99 | void updatePlotArea(); |
| 100 | void updateAxisAreas(); |
| 101 | |
| 102 | void addAxis(QAbstractAxis *axis); |
| 103 | void removeAxis(QAbstractAxis *axis); |
| 104 | |
| 105 | qsizetype graphSeriesCount() const; |
| 106 | void setGraphSeriesCount(qsizetype count); |
| 107 | |
| 108 | void createBarsRenderer(); |
| 109 | void createAxisRenderer(); |
| 110 | void createPointRenderer(); |
| 111 | void createPieRenderer(); |
| 112 | void createAreaRenderer(); |
| 113 | |
| 114 | qreal axisXSmoothing() const; |
| 115 | void setAxisXSmoothing(qreal smoothing); |
| 116 | qreal axisYSmoothing() const; |
| 117 | void setAxisYSmoothing(qreal smoothing); |
| 118 | qreal gridSmoothing() const; |
| 119 | void setGridSmoothing(qreal smoothing); |
| 120 | |
| 121 | bool isShadowVisible() const; |
| 122 | void setShadowVisible(bool newShadowVisibility); |
| 123 | QColor shadowColor() const; |
| 124 | void setShadowColor(QColor newShadowColor); |
| 125 | qreal shadowBarWidth() const; |
| 126 | void setShadowBarWidth(qreal newShadowBarWidth); |
| 127 | qreal shadowXOffset() const; |
| 128 | void setShadowXOffset(qreal newShadowXOffset); |
| 129 | qreal shadowYOffset() const; |
| 130 | void setShadowYOffset(qreal newShadowYOffset); |
| 131 | qreal shadowSmoothing() const; |
| 132 | void setShadowSmoothing(qreal smoothing); |
| 133 | |
| 134 | QAbstractAxis *axisX() const; |
| 135 | void setAxisX(QAbstractAxis *axis); |
| 136 | |
| 137 | QAbstractAxis *axisY() const; |
| 138 | void setAxisY(QAbstractAxis *axis); |
| 139 | |
| 140 | Qt::Orientation orientation() const; |
| 141 | void setOrientation(Qt::Orientation newOrientation); |
| 142 | |
| 143 | protected: |
| 144 | void handleHoverEnter(const QString &seriesName, QPointF position, QPointF value); |
| 145 | void handleHoverExit(const QString &seriesName, QPointF position); |
| 146 | void handleHover(const QString &seriesName, QPointF position, QPointF value); |
| 147 | void updateComponentSizes(); |
| 148 | void componentComplete() override; |
| 149 | void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
| 150 | void mouseMoveEvent(QMouseEvent *event) override; |
| 151 | void mousePressEvent(QMouseEvent *event) override; |
| 152 | void mouseReleaseEvent(QMouseEvent *event) override; |
| 153 | void hoverMoveEvent(QHoverEvent *event) override; |
| 154 | QSGNode *updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *updatePaintNodeData) override; |
| 155 | void updatePolish() override; |
| 156 | |
| 157 | Q_SIGNALS: |
| 158 | void themeChanged(); |
| 159 | void marginTopChanged(); |
| 160 | void marginBottomChanged(); |
| 161 | void marginLeftChanged(); |
| 162 | void marginRightChanged(); |
| 163 | void hoverEnter(const QString &seriesName, QPointF position, QPointF value); |
| 164 | void hoverExit(const QString &seriesName, QPointF position); |
| 165 | void hover(const QString &seriesName, QPointF position, QPointF value); |
| 166 | |
| 167 | void axisXSmoothingChanged(); |
| 168 | void axisYSmoothingChanged(); |
| 169 | void gridSmoothingChanged(); |
| 170 | |
| 171 | void shadowVisibleChanged(); |
| 172 | void shadowColorChanged(); |
| 173 | void shadowBarWidthChanged(); |
| 174 | void shadowXOffsetChanged(); |
| 175 | void shadowYOffsetChanged(); |
| 176 | void shadowSmoothingChanged(); |
| 177 | |
| 178 | void axisXChanged(); |
| 179 | void axisYChanged(); |
| 180 | |
| 181 | void orientationChanged(); |
| 182 | |
| 183 | private: |
| 184 | friend class AxisRenderer; |
| 185 | friend class BarsRenderer; |
| 186 | friend class PointRenderer; |
| 187 | friend class AreaRenderer; |
| 188 | friend class QAbstractAxis; |
| 189 | |
| 190 | void polishAndUpdate(); |
| 191 | int getSeriesRendererIndex(QAbstractSeries *series); |
| 192 | |
| 193 | static constexpr qreal m_defaultAxisTickersWidth = 15; |
| 194 | static constexpr qreal m_defaultAxisTickersHeight = 15; |
| 195 | static constexpr qreal m_defaultAxisLabelsWidth = 40; |
| 196 | static constexpr qreal m_defaultAxisLabelsHeight = 25; |
| 197 | static constexpr qreal m_defaultAxisXLabelsMargin = 0; |
| 198 | static constexpr qreal m_defaultAxisYLabelsMargin = 5; |
| 199 | |
| 200 | AxisRenderer *m_axisRenderer = nullptr; |
| 201 | BarsRenderer *m_barsRenderer = nullptr; |
| 202 | PointRenderer *m_pointRenderer = nullptr; |
| 203 | PieRenderer *m_pieRenderer = nullptr; |
| 204 | AreaRenderer *m_areaRenderer = nullptr; |
| 205 | QList<QObject *> m_seriesList; |
| 206 | QHash<int, QList<QAbstractSeries *>> m_cleanupSeriesList; |
| 207 | QQuickRectangle *m_backgroundRectangle = nullptr; |
| 208 | |
| 209 | QAbstractAxis *m_axisX = nullptr; |
| 210 | QAbstractAxis *m_axisY = nullptr; |
| 211 | Qt::Orientation m_orientation = Qt::Orientation::Vertical; |
| 212 | |
| 213 | QGraphsTheme *m_theme = nullptr; |
| 214 | QGraphsTheme *m_defaultTheme = nullptr; |
| 215 | |
| 216 | qsizetype m_graphSeriesCount = 0; |
| 217 | |
| 218 | qreal m_marginTop = 20; |
| 219 | qreal m_marginBottom = 20; |
| 220 | qreal m_marginLeft = 20; |
| 221 | qreal m_marginRight = 20; |
| 222 | QRectF m_plotArea; |
| 223 | // Areas of axis |
| 224 | QRectF m_xAxisArea; |
| 225 | QRectF m_yAxisArea; |
| 226 | // Areas of axis tickers |
| 227 | QRectF m_xAxisTickersArea; |
| 228 | QRectF m_yAxisTickersArea; |
| 229 | // Areas of axis labels |
| 230 | QRectF m_xAxisLabelsArea; |
| 231 | QRectF m_yAxisLabelsArea; |
| 232 | // Note: Add properties for these |
| 233 | qreal m_axisTickersWidth = m_defaultAxisTickersWidth; |
| 234 | qreal m_axisTickersHeight = m_defaultAxisTickersHeight; |
| 235 | qreal m_axisLabelsWidth = m_defaultAxisLabelsWidth; |
| 236 | qreal m_axisLabelsHeight = m_defaultAxisLabelsHeight; |
| 237 | qreal m_axisXLabelsMargin = m_defaultAxisXLabelsMargin; |
| 238 | qreal m_axisYLabelsMargin = m_defaultAxisYLabelsMargin; |
| 239 | // Calculated based the the above |
| 240 | qreal m_axisWidth = 0; |
| 241 | qreal m_axisHeight = 0; |
| 242 | |
| 243 | int m_hoverCount = 0; |
| 244 | |
| 245 | qreal m_axisXSmoothing = 1.0; |
| 246 | qreal m_axisYSmoothing = 1.0; |
| 247 | qreal m_gridSmoothing = 1.0; |
| 248 | |
| 249 | bool m_isShadowVisible = false; |
| 250 | QColor m_shadowColor = QColorConstants::Black; |
| 251 | qreal m_shadowBarWidth = 2.0; |
| 252 | qreal m_shadowXOffset = 0.0; |
| 253 | qreal m_shadowYOffset = 0.0; |
| 254 | qreal m_shadowSmoothing = 4.0; |
| 255 | }; |
| 256 | |
| 257 | QT_END_NAMESPACE |
| 258 | |
| 259 | #endif |
| 260 | |