| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef BARSRENDERER_H |
| 5 | #define BARSRENDERER_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 <QtQuick/private/qquicktext_p.h> |
| 19 | #include <QtCore/QHash> |
| 20 | #include <QtCore/QList> |
| 21 | #include <QtCore/QRectF> |
| 22 | #include <QtGui/QColor> |
| 23 | |
| 24 | QT_BEGIN_NAMESPACE |
| 25 | |
| 26 | class QGraphsView; |
| 27 | class QBarSeries; |
| 28 | class QBarSet; |
| 29 | class QAbstractSeries; |
| 30 | class QQuickTapHandler; |
| 31 | |
| 32 | class BarsRenderer : public QQuickItem |
| 33 | { |
| 34 | Q_OBJECT |
| 35 | public: |
| 36 | BarsRenderer(QGraphsView *graph, bool clipPlotArea); |
| 37 | ~BarsRenderer() override; |
| 38 | |
| 39 | void handlePolish(QBarSeries *series); |
| 40 | void updateSeries(QBarSeries *series); |
| 41 | void afterUpdate(QList<QAbstractSeries *> &cleanupSeries); |
| 42 | void afterPolish(QList<QAbstractSeries *> &cleanupSeries); |
| 43 | bool handleHoverMove(QHoverEvent *event); |
| 44 | |
| 45 | Q_SIGNALS: |
| 46 | |
| 47 | private: |
| 48 | struct BarSelectionRect { |
| 49 | QBarSeries *series = nullptr; |
| 50 | QBarSet *barSet = nullptr; |
| 51 | QList<QRectF> rects; |
| 52 | }; |
| 53 | struct BarSeriesData { |
| 54 | QRectF rect; |
| 55 | QColor color; |
| 56 | QColor borderColor; |
| 57 | QString label; |
| 58 | QColor labelColor; |
| 59 | float value; |
| 60 | float borderWidth; |
| 61 | bool isSelected; |
| 62 | }; |
| 63 | |
| 64 | void updateVerticalBars(QBarSeries *series, qsizetype setCount, qsizetype valuesPerSet); |
| 65 | void updateHorizontalBars(QBarSeries *series, qsizetype setCount, qsizetype valuesPerSet); |
| 66 | QColor getSetColor(QBarSeries *series, QBarSet *set, qsizetype barSerieIndex); |
| 67 | QColor getSetSelectedColor(QBarSeries *series, QBarSet *set); |
| 68 | QColor getSetBorderColor(QBarSeries *series, QBarSet *set, qsizetype barSerieIndex); |
| 69 | qreal getSetBorderWidth(QBarSeries *series, QBarSet *set); |
| 70 | QString generateLabelText(QBarSeries *series, qreal value); |
| 71 | void positionLabelItem(QBarSeries *series, QQuickText *textItem, const BarSeriesData &d); |
| 72 | void updateComponents(QBarSeries *series); |
| 73 | void updateValueLabels(QBarSeries *series); |
| 74 | |
| 75 | void onSingleTapped(QEventPoint eventPoint, Qt::MouseButton button); |
| 76 | void onDoubleTapped(QEventPoint eventPoint, Qt::MouseButton button); |
| 77 | void onPressedChanged(); |
| 78 | |
| 79 | QGraphsView *m_graph = nullptr; |
| 80 | QHash<QBarSeries *, QList<BarSelectionRect>> m_rectNodesInputRects; |
| 81 | QHash<QBarSeries *, QList<QQuickItem *>> m_barItems; |
| 82 | QHash<QBarSeries *, QList<QQuickText *>> m_labelTextItems; |
| 83 | QHash<QBarSeries *, QList<BarSeriesData>> m_seriesData; |
| 84 | |
| 85 | QQuickTapHandler *m_tapHandler = nullptr; |
| 86 | |
| 87 | QBarSeries *m_currentHoverSeries = nullptr; |
| 88 | qsizetype m_colorIndex = -1; |
| 89 | // Margin between bars. |
| 90 | float m_barMargin = 2.0; |
| 91 | }; |
| 92 | |
| 93 | QT_END_NAMESPACE |
| 94 | |
| 95 | #endif // BARSRENDERER_H |
| 96 | |