1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4// W A R N I N G
5// -------------
6//
7// This file is not part of the Qt Chart API. It exists purely as an
8// implementation detail. This header file may change from version to
9// version without notice, or even be removed.
10//
11// We mean it.
12
13#ifndef SCATTERCHARTITEM_H
14#define SCATTERCHARTITEM_H
15
16#include <QtCharts/QChartGlobal>
17#include <private/xychart_p.h>
18#include <QtWidgets/QGraphicsEllipseItem>
19#include <QtGui/QPen>
20#include <QtWidgets/QGraphicsSceneMouseEvent>
21#include <QtCharts/private/qchartglobal_p.h>
22#include <QtCore/qmap.h>
23
24QT_BEGIN_NAMESPACE
25
26class QScatterSeries;
27
28class Q_CHARTS_PRIVATE_EXPORT ScatterChartItem : public XYChart
29{
30 Q_OBJECT
31 Q_INTERFACES(QGraphicsItem)
32public:
33 explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *item = 0);
34
35public:
36 //from QGraphicsItem
37 QRectF boundingRect() const override;
38 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
39
40 void setPen(const QPen &pen);
41 void setBrush(const QBrush &brush);
42
43 void markerSelected(QGraphicsItem *item);
44 void markerHovered(QGraphicsItem *item, bool state);
45 void markerPressed(QGraphicsItem *item);
46 void markerReleased(QGraphicsItem *item);
47 void markerDoubleClicked(QGraphicsItem *item);
48 void handleMarkerMouseReleaseEvent(QGraphicsItem *item);
49
50 void setMousePressed(bool pressed = true) {m_mousePressed = pressed;}
51 bool mousePressed() {return m_mousePressed;}
52
53
54public Q_SLOTS:
55 void handleSeriesUpdated() override;
56
57private:
58 void createPoints(int count);
59 void deletePoints(int count);
60 void resizeMarker(QGraphicsItem *marker, const int size);
61
62protected:
63 void updateGeometry() override;
64 void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
65 void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
66 void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
67 void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
68
69private:
70 QScatterSeries *m_series;
71 QGraphicsItemGroup m_items;
72 bool m_visible;
73 int m_markerShape;
74 QRectF m_rect;
75 QMap<QGraphicsItem *, QPointF> m_markerMap;
76
77 bool m_pointsVisible;
78 bool m_pointLabelsVisible;
79 qreal m_markerSize;
80 QString m_pointLabelsFormat;
81 QFont m_pointLabelsFont;
82 QColor m_pointLabelsColor;
83 bool m_pointLabelsClipping;
84
85 QPointF m_lastHoveredPoint;
86 QPointF m_lastMousePos;
87 bool m_mousePressed;
88};
89
90template <class T>
91class Q_CHARTS_PRIVATE_EXPORT ChartMarker : public T
92{
93public:
94 explicit ChartMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent);
95 explicit ChartMarker(ScatterChartItem *parent);
96
97protected:
98 void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
99 void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
100 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
101 void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
102 void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
103
104private:
105 ScatterChartItem *m_parent;
106};
107
108class Q_CHARTS_PRIVATE_EXPORT RotatedRectangleMarker : public ChartMarker<QGraphicsPolygonItem>
109{
110public:
111 explicit RotatedRectangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent);
112
113 static QPolygonF polygon(qreal x = 0.0, qreal y = 0.0, qreal w = 10.0, qreal h = 10.0);
114};
115
116class Q_CHARTS_PRIVATE_EXPORT TriangleMarker : public ChartMarker<QGraphicsPolygonItem>
117{
118public:
119 explicit TriangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent);
120
121 static QPolygonF polygon(qreal x = 0.0, qreal y = 0.0, qreal w = 10.0, qreal h = 10.0);
122};
123
124class Q_CHARTS_PRIVATE_EXPORT StarMarker : public ChartMarker<QGraphicsPolygonItem>
125{
126public:
127 explicit StarMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent);
128
129 static QPolygonF polygon(qreal x = 0.0, qreal y = 0.0, qreal w = 10.0, qreal h = 10.0);
130};
131
132class Q_CHARTS_PRIVATE_EXPORT PentagonMarker : public ChartMarker<QGraphicsPolygonItem>
133{
134public:
135 explicit PentagonMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent);
136
137 static QPolygonF polygon(qreal x = 0.0, qreal y = 0.0, qreal w = 10.0, qreal h = 10.0);
138};
139
140
141QT_END_NAMESPACE
142
143#endif // SCATTERPRESENTER_H
144

source code of qtcharts/src/charts/scatterchart/scatterchartitem_p.h