1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 //  W A R N I N G
31 //  -------------
32 //
33 // This file is not part of the Qt Chart API.  It exists purely as an
34 // implementation detail.  This header file may change from version to
35 // version without notice, or even be removed.
36 //
37 // We mean it.
38 
39 #ifndef SCATTERCHARTITEM_H
40 #define SCATTERCHARTITEM_H
41 
42 #include <QtCharts/QChartGlobal>
43 #include <private/xychart_p.h>
44 #include <QtWidgets/QGraphicsEllipseItem>
45 #include <QtGui/QPen>
46 #include <QtWidgets/QGraphicsSceneMouseEvent>
47 #include <QtCharts/private/qchartglobal_p.h>
48 
49 QT_CHARTS_BEGIN_NAMESPACE
50 
51 class QScatterSeries;
52 
53 class Q_CHARTS_PRIVATE_EXPORT ScatterChartItem : public XYChart
54 {
55     Q_OBJECT
56     Q_INTERFACES(QGraphicsItem)
57 public:
58     explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *item = 0);
59 
60 public:
61     //from QGraphicsItem
62     QRectF boundingRect() const;
63     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
64 
65     void setPen(const QPen &pen);
66     void setBrush(const QBrush &brush);
67 
68     void markerSelected(QGraphicsItem *item);
69     void markerHovered(QGraphicsItem *item, bool state);
70     void markerPressed(QGraphicsItem *item);
71     void markerReleased(QGraphicsItem *item);
72     void markerDoubleClicked(QGraphicsItem *item);
73 
74     void setMousePressed(bool pressed = true) {m_mousePressed = pressed;}
mousePressed()75     bool mousePressed() {return m_mousePressed;}
76 
77 
78 public Q_SLOTS:
79     void handleUpdated();
80 
81 private:
82     void createPoints(int count);
83     void deletePoints(int count);
84 
85 protected:
86     void updateGeometry();
87 
88 private:
89     QScatterSeries *m_series;
90     QGraphicsItemGroup m_items;
91     bool m_visible;
92     int m_shape;
93     int m_size;
94     QRectF m_rect;
95     QMap<QGraphicsItem *, QPointF> m_markerMap;
96 
97     bool m_pointLabelsVisible;
98     QString m_pointLabelsFormat;
99     QFont m_pointLabelsFont;
100     QColor m_pointLabelsColor;
101     bool m_pointLabelsClipping;
102 
103     bool m_mousePressed;
104 };
105 
106 class Q_CHARTS_PRIVATE_EXPORT CircleMarker: public QGraphicsEllipseItem
107 {
108 
109 public:
CircleMarker(qreal x,qreal y,qreal w,qreal h,ScatterChartItem * parent)110     CircleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent)
111         : QGraphicsEllipseItem(x, y, w, h, parent),
112           m_parent(parent)
113     {
114         setAcceptHoverEvents(true);
115         setFlag(QGraphicsItem::ItemIsSelectable);
116     }
117 
118 protected:
mousePressEvent(QGraphicsSceneMouseEvent * event)119     void mousePressEvent(QGraphicsSceneMouseEvent *event)
120     {
121         QGraphicsEllipseItem::mousePressEvent(event);
122         m_parent->markerPressed(this);
123         m_parent->setMousePressed();
124     }
hoverEnterEvent(QGraphicsSceneHoverEvent * event)125     void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
126     {
127         QGraphicsEllipseItem::hoverEnterEvent(event);
128         m_parent->markerHovered(this, true);
129     }
hoverLeaveEvent(QGraphicsSceneHoverEvent * event)130     void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
131     {
132         QGraphicsEllipseItem::hoverLeaveEvent(event);
133         m_parent->markerHovered(this, false);
134     }
mouseReleaseEvent(QGraphicsSceneMouseEvent * event)135     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
136     {
137         QGraphicsEllipseItem::mouseReleaseEvent(event);
138         m_parent->markerReleased(this);
139         if (m_parent->mousePressed())
140             m_parent->markerSelected(this);
141         m_parent->setMousePressed(false);
142     }
mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)143     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
144     {
145         QGraphicsEllipseItem::mouseDoubleClickEvent(event);
146         m_parent->markerDoubleClicked(this);
147     }
148 
149 private:
150     ScatterChartItem *m_parent;
151 };
152 
153 class Q_CHARTS_PRIVATE_EXPORT RectangleMarker: public QGraphicsRectItem
154 {
155 
156 public:
RectangleMarker(qreal x,qreal y,qreal w,qreal h,ScatterChartItem * parent)157     RectangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent)
158         : QGraphicsRectItem(x, y, w, h, parent),
159           m_parent(parent)
160     {
161         setAcceptHoverEvents(true);
162         setFlag(QGraphicsItem::ItemIsSelectable);
163     }
164 
165 protected:
mousePressEvent(QGraphicsSceneMouseEvent * event)166     void mousePressEvent(QGraphicsSceneMouseEvent *event)
167     {
168         QGraphicsRectItem::mousePressEvent(event);
169         m_parent->markerPressed(this);
170         m_parent->setMousePressed();
171     }
hoverEnterEvent(QGraphicsSceneHoverEvent * event)172     void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
173     {
174         QGraphicsRectItem::hoverEnterEvent(event);
175         m_parent->markerHovered(this, true);
176     }
hoverLeaveEvent(QGraphicsSceneHoverEvent * event)177     void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
178     {
179         QGraphicsRectItem::hoverLeaveEvent(event);
180         m_parent->markerHovered(this, false);
181     }
mouseReleaseEvent(QGraphicsSceneMouseEvent * event)182     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
183     {
184         QGraphicsRectItem::mouseReleaseEvent(event);
185         m_parent->markerReleased(this);
186         if (m_parent->mousePressed())
187             m_parent->markerSelected(this);
188         m_parent->setMousePressed(false);
189     }
mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)190     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
191     {
192         QGraphicsRectItem::mouseDoubleClickEvent(event);
193         m_parent->markerDoubleClicked(this);
194     }
195 
196 private:
197     ScatterChartItem *m_parent;
198 };
199 
200 QT_CHARTS_END_NAMESPACE
201 
202 #endif // SCATTERPRESENTER_H
203