@@ -1,189 +1,194 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | #include "scatterchartitem_p.h" |
|
22 | 22 | #include "qscatterseries.h" |
|
23 | 23 | #include "qscatterseries_p.h" |
|
24 | 24 | #include "chartpresenter_p.h" |
|
25 | 25 | #include <QPainter> |
|
26 | 26 | #include <QGraphicsScene> |
|
27 | 27 | #include <QDebug> |
|
28 | 28 | #include <QGraphicsSceneMouseEvent> |
|
29 | 29 | |
|
30 | 30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | 31 | |
|
32 | 32 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) |
|
33 | 33 | : XYChart(series, presenter), |
|
34 | 34 | QGraphicsItem(presenter ? presenter->rootItem() : 0), |
|
35 | 35 | m_series(series), |
|
36 | 36 | m_items(this), |
|
37 | 37 | m_visible(true), |
|
38 | 38 | m_shape(QScatterSeries::MarkerShapeRectangle), |
|
39 | 39 | m_size(15) |
|
40 | 40 | { |
|
41 | 41 | QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
42 | 42 | QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); |
|
43 | 43 | QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); |
|
44 | 44 | |
|
45 | 45 | setZValue(ChartPresenter::ScatterSeriesZValue); |
|
46 | 46 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
47 | 47 | |
|
48 | 48 | handleUpdated(); |
|
49 | 49 | |
|
50 | 50 | m_items.setHandlesChildEvents(false); |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | QRectF ScatterChartItem::boundingRect() const |
|
54 | 54 | { |
|
55 | 55 | return m_rect; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void ScatterChartItem::createPoints(int count) |
|
59 | 59 | { |
|
60 | 60 | for (int i = 0; i < count; ++i) { |
|
61 | 61 | |
|
62 | 62 | QGraphicsItem *item = 0; |
|
63 | 63 | |
|
64 | 64 | switch (m_shape) { |
|
65 | 65 | case QScatterSeries::MarkerShapeCircle: { |
|
66 | 66 | item = new CircleMarker(0, 0, m_size, m_size, this); |
|
67 | 67 | const QRectF &rect = item->boundingRect(); |
|
68 | 68 | item->setPos(-rect.width() / 2, -rect.height() / 2); |
|
69 | 69 | break; |
|
70 | 70 | } |
|
71 | 71 | case QScatterSeries::MarkerShapeRectangle: |
|
72 | 72 | item = new RectangleMarker(0, 0, m_size, m_size, this); |
|
73 | 73 | item->setPos(-m_size / 2, -m_size / 2); |
|
74 | 74 | break; |
|
75 | 75 | default: |
|
76 | 76 | qWarning() << "Unsupported marker type"; |
|
77 | 77 | break; |
|
78 | 78 | } |
|
79 | 79 | m_items.addToGroup(item); |
|
80 | 80 | } |
|
81 | 81 | } |
|
82 | 82 | |
|
83 | 83 | void ScatterChartItem::deletePoints(int count) |
|
84 | 84 | { |
|
85 | 85 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
86 | 86 | |
|
87 | 87 | for (int i = 0; i < count; ++i) { |
|
88 | 88 | QGraphicsItem *item = items.takeLast(); |
|
89 | 89 | m_markerMap.remove(item); |
|
90 | 90 | delete(item); |
|
91 | 91 | } |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | void ScatterChartItem::markerSelected(QGraphicsItem *marker) |
|
95 | 95 | { |
|
96 | 96 | emit XYChart::clicked(calculateDomainPoint(m_markerMap[marker])); |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | void ScatterChartItem::markerHovered(QGraphicsItem *marker, bool state) | |
|
100 | { | |
|
101 | emit XYChart::hovered(calculateDomainPoint(m_markerMap[marker]), state); | |
|
102 | } | |
|
103 | ||
|
99 | 104 | void ScatterChartItem::updateGeometry() |
|
100 | 105 | { |
|
101 | 106 | |
|
102 | 107 | const QVector<QPointF>& points = geometryPoints(); |
|
103 | 108 | |
|
104 | 109 | if (points.size() == 0) { |
|
105 | 110 | deletePoints(m_items.childItems().count()); |
|
106 | 111 | return; |
|
107 | 112 | } |
|
108 | 113 | |
|
109 | 114 | int diff = m_items.childItems().size() - points.size(); |
|
110 | 115 | |
|
111 | 116 | if (diff > 0) |
|
112 | 117 | deletePoints(diff); |
|
113 | 118 | else if (diff < 0) |
|
114 | 119 | createPoints(-diff); |
|
115 | 120 | |
|
116 | 121 | if (diff != 0) |
|
117 | 122 | handleUpdated(); |
|
118 | 123 | |
|
119 | 124 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
120 | 125 | |
|
121 | 126 | for (int i = 0; i < points.size(); i++) { |
|
122 | 127 | QGraphicsItem *item = items.at(i); |
|
123 | 128 | const QPointF &point = points.at(i); |
|
124 | 129 | const QRectF &rect = item->boundingRect(); |
|
125 | 130 | m_markerMap[item] = point; |
|
126 | 131 | item->setPos(point.x() - rect.width() / 2, point.y() - rect.height() / 2); |
|
127 | 132 | if (!m_visible || !clipRect().contains(point)) |
|
128 | 133 | item->setVisible(false); |
|
129 | 134 | else |
|
130 | 135 | item->setVisible(true); |
|
131 | 136 | } |
|
132 | 137 | |
|
133 | 138 | prepareGeometryChange(); |
|
134 | 139 | m_rect = clipRect(); |
|
135 | 140 | setPos(origin()); |
|
136 | 141 | } |
|
137 | 142 | |
|
138 | 143 | void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
139 | 144 | { |
|
140 | 145 | Q_UNUSED(painter) |
|
141 | 146 | Q_UNUSED(option) |
|
142 | 147 | Q_UNUSED(widget) |
|
143 | 148 | } |
|
144 | 149 | |
|
145 | 150 | void ScatterChartItem::setPen(const QPen &pen) |
|
146 | 151 | { |
|
147 | 152 | foreach (QGraphicsItem *item , m_items.childItems()) |
|
148 | 153 | static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen); |
|
149 | 154 | } |
|
150 | 155 | |
|
151 | 156 | void ScatterChartItem::setBrush(const QBrush &brush) |
|
152 | 157 | { |
|
153 | 158 | foreach (QGraphicsItem *item , m_items.childItems()) |
|
154 | 159 | static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush); |
|
155 | 160 | } |
|
156 | 161 | |
|
157 | 162 | void ScatterChartItem::handleUpdated() |
|
158 | 163 | { |
|
159 | 164 | int count = m_items.childItems().count(); |
|
160 | 165 | |
|
161 | 166 | if (count == 0) |
|
162 | 167 | return; |
|
163 | 168 | |
|
164 | 169 | bool recreate = m_visible != m_series->isVisible() |
|
165 | 170 | || m_size != m_series->markerSize() |
|
166 | 171 | || m_shape != m_series->markerShape(); |
|
167 | 172 | |
|
168 | 173 | m_visible = m_series->isVisible(); |
|
169 | 174 | m_size = m_series->markerSize(); |
|
170 | 175 | m_shape = m_series->markerShape(); |
|
171 | 176 | setOpacity(m_series->opacity()); |
|
172 | 177 | |
|
173 | 178 | if (recreate) { |
|
174 | 179 | // TODO: optimize handleUpdate to recreate points only in case shape changed |
|
175 | 180 | deletePoints(count); |
|
176 | 181 | createPoints(count); |
|
177 | 182 | |
|
178 | 183 | // Updating geometry is now safe, because it won't call handleUpdated unless it creates/deletes points |
|
179 | 184 | updateGeometry(); |
|
180 | 185 | } |
|
181 | 186 | |
|
182 | 187 | setPen(m_series->pen()); |
|
183 | 188 | setBrush(m_series->brush()); |
|
184 | 189 | update(); |
|
185 | 190 | } |
|
186 | 191 | |
|
187 | 192 | #include "moc_scatterchartitem_p.cpp" |
|
188 | 193 | |
|
189 | 194 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,123 +1,146 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 Digia Plc |
|
4 | 4 | ** All rights reserved. |
|
5 | 5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
6 | 6 | ** |
|
7 | 7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
12 | 12 | ** Software or, alternatively, in accordance with the terms contained in |
|
13 | 13 | ** a written agreement between you and Digia. |
|
14 | 14 | ** |
|
15 | 15 | ** If you have questions regarding the use of this file, please use |
|
16 | 16 | ** contact form at http://qt.digia.com |
|
17 | 17 | ** $QT_END_LICENSE$ |
|
18 | 18 | ** |
|
19 | 19 | ****************************************************************************/ |
|
20 | 20 | |
|
21 | 21 | // W A R N I N G |
|
22 | 22 | // ------------- |
|
23 | 23 | // |
|
24 | 24 | // This file is not part of the QtCommercial Chart API. It exists purely as an |
|
25 | 25 | // implementation detail. This header file may change from version to |
|
26 | 26 | // version without notice, or even be removed. |
|
27 | 27 | // |
|
28 | 28 | // We mean it. |
|
29 | 29 | |
|
30 | 30 | #ifndef SCATTERCHARTITEM_H |
|
31 | 31 | #define SCATTERCHARTITEM_H |
|
32 | 32 | |
|
33 | 33 | #include "qchartglobal.h" |
|
34 | 34 | #include "xychart_p.h" |
|
35 | 35 | #include <QGraphicsEllipseItem> |
|
36 | 36 | #include <QPen> |
|
37 | 37 | |
|
38 | 38 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
39 | 39 | |
|
40 | 40 | class QScatterSeries; |
|
41 | 41 | |
|
42 | 42 | class ScatterChartItem : public XYChart, public QGraphicsItem |
|
43 | 43 | { |
|
44 | 44 | Q_OBJECT |
|
45 | 45 | Q_INTERFACES(QGraphicsItem) |
|
46 | 46 | public: |
|
47 | 47 | explicit ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter); |
|
48 | 48 | |
|
49 | 49 | public: |
|
50 | 50 | //from QGraphicsItem |
|
51 | 51 | QRectF boundingRect() const; |
|
52 | 52 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
53 | 53 | |
|
54 | 54 | void setPen(const QPen &pen); |
|
55 | 55 | void setBrush(const QBrush &brush); |
|
56 | 56 | |
|
57 | 57 | void markerSelected(QGraphicsItem *item); |
|
58 | void markerHovered(QGraphicsItem *item, bool state); | |
|
58 | 59 | |
|
59 | 60 | public Q_SLOTS: |
|
60 | 61 | void handleUpdated(); |
|
61 | 62 | |
|
62 | 63 | private: |
|
63 | 64 | void createPoints(int count); |
|
64 | 65 | void deletePoints(int count); |
|
65 | 66 | |
|
66 | 67 | protected: |
|
67 | 68 | void updateGeometry(); |
|
68 | 69 | |
|
69 | 70 | private: |
|
70 | 71 | QScatterSeries *m_series; |
|
71 | 72 | QGraphicsItemGroup m_items; |
|
72 | 73 | bool m_visible; |
|
73 | 74 | int m_shape; |
|
74 | 75 | int m_size; |
|
75 | 76 | QRectF m_rect; |
|
76 | 77 | QMap<QGraphicsItem *, QPointF> m_markerMap; |
|
77 | 78 | }; |
|
78 | 79 | |
|
79 | 80 | class CircleMarker: public QGraphicsEllipseItem |
|
80 | 81 | { |
|
81 | 82 | |
|
82 | 83 | public: |
|
83 | 84 | CircleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent) |
|
84 | 85 | : QGraphicsEllipseItem(x, y, w, h, parent), |
|
85 | 86 | m_parent(parent) |
|
86 | 87 | { |
|
88 | setAcceptHoverEvents(true); | |
|
87 | 89 | } |
|
88 | 90 | |
|
89 | 91 | protected: |
|
90 | 92 | void mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
91 | 93 | { |
|
92 | 94 | m_parent->markerSelected(this); |
|
93 | 95 | QGraphicsEllipseItem::mousePressEvent(event); |
|
94 | 96 | } |
|
97 | void hoverEnterEvent(QGraphicsSceneHoverEvent *event) | |
|
98 | { | |
|
99 | m_parent->markerHovered(this, true); | |
|
100 | QGraphicsEllipseItem::hoverEnterEvent(event); | |
|
101 | } | |
|
102 | void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | |
|
103 | { | |
|
104 | m_parent->markerHovered(this, false); | |
|
105 | QGraphicsEllipseItem::hoverLeaveEvent(event); | |
|
106 | } | |
|
95 | 107 | |
|
96 | 108 | private: |
|
97 | 109 | ScatterChartItem *m_parent; |
|
98 | 110 | }; |
|
99 | 111 | |
|
100 | 112 | class RectangleMarker: public QGraphicsRectItem |
|
101 | 113 | { |
|
102 | 114 | |
|
103 | 115 | public: |
|
104 | 116 | RectangleMarker(qreal x, qreal y, qreal w, qreal h, ScatterChartItem *parent) |
|
105 | 117 | : QGraphicsRectItem(x, y, w, h, parent), |
|
106 | 118 | m_parent(parent) |
|
107 | 119 | { |
|
120 | setAcceptHoverEvents(true); | |
|
108 | 121 | } |
|
109 | 122 | |
|
110 | 123 | protected: |
|
111 | 124 | void mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
112 | 125 | { |
|
113 | 126 | m_parent->markerSelected(this); |
|
114 | 127 | QGraphicsRectItem::mousePressEvent(event); |
|
115 | 128 | } |
|
129 | void hoverEnterEvent(QGraphicsSceneHoverEvent *event) | |
|
130 | { | |
|
131 | m_parent->markerHovered(this, true); | |
|
132 | QGraphicsRectItem::hoverEnterEvent(event); | |
|
133 | } | |
|
134 | void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | |
|
135 | { | |
|
136 | m_parent->markerHovered(this, false); | |
|
137 | QGraphicsRectItem::hoverLeaveEvent(event); | |
|
138 | } | |
|
116 | 139 | |
|
117 | 140 | private: |
|
118 | 141 | ScatterChartItem *m_parent; |
|
119 | 142 | }; |
|
120 | 143 | |
|
121 | 144 | QTCOMMERCIALCHART_END_NAMESPACE |
|
122 | 145 | |
|
123 | 146 | #endif // SCATTERPRESENTER_H |
General Comments 0
You need to be logged in to leave comments.
Login now