@@ -1,455 +1,454 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2015 The Qt Company Ltd |
|
3 | ** Copyright (C) 2015 The Qt Company Ltd | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to The Qt Company, please use contact form at http://qt.io |
|
5 | ** For any questions to The Qt Company, please use contact form at http://qt.io | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Charts module. |
|
7 | ** This file is part of the Qt Charts module. | |
8 | ** |
|
8 | ** | |
9 | ** Licensees holding valid commercial license for Qt may use this file in |
|
9 | ** Licensees holding valid commercial license for Qt may use this file in | |
10 | ** accordance with the Qt License Agreement provided with the Software |
|
10 | ** accordance with the Qt License Agreement provided with the Software | |
11 | ** or, alternatively, in accordance with the terms contained in a written |
|
11 | ** or, alternatively, in accordance with the terms contained in a written | |
12 | ** agreement between you and The Qt Company. |
|
12 | ** agreement between you and The Qt Company. | |
13 | ** |
|
13 | ** | |
14 | ** If you have questions regarding the use of this file, please use |
|
14 | ** If you have questions regarding the use of this file, please use | |
15 | ** contact form at http://qt.io |
|
15 | ** contact form at http://qt.io | |
16 | ** |
|
16 | ** | |
17 | ****************************************************************************/ |
|
17 | ****************************************************************************/ | |
18 |
|
18 | |||
19 | #include <private/linechartitem_p.h> |
|
19 | #include <private/linechartitem_p.h> | |
20 | #include <QtCharts/QLineSeries> |
|
20 | #include <QtCharts/QLineSeries> | |
21 | #include <private/qlineseries_p.h> |
|
21 | #include <private/qlineseries_p.h> | |
22 | #include <private/chartpresenter_p.h> |
|
22 | #include <private/chartpresenter_p.h> | |
23 | #include <private/polardomain_p.h> |
|
23 | #include <private/polardomain_p.h> | |
24 | #include <private/chartthememanager_p.h> |
|
24 | #include <private/chartthememanager_p.h> | |
25 | #include <private/charttheme_p.h> |
|
25 | #include <private/charttheme_p.h> | |
26 | #include <QtGui/QPainter> |
|
26 | #include <QtGui/QPainter> | |
27 | #include <QtWidgets/QGraphicsSceneMouseEvent> |
|
27 | #include <QtWidgets/QGraphicsSceneMouseEvent> | |
28 |
|
28 | |||
29 | QT_CHARTS_BEGIN_NAMESPACE |
|
29 | QT_CHARTS_BEGIN_NAMESPACE | |
30 |
|
30 | |||
31 | const qreal mouseEventMinWidth(12); |
|
31 | const qreal mouseEventMinWidth(12); | |
32 |
|
32 | |||
33 | LineChartItem::LineChartItem(QLineSeries *series, QGraphicsItem *item) |
|
33 | LineChartItem::LineChartItem(QLineSeries *series, QGraphicsItem *item) | |
34 | : XYChart(series,item), |
|
34 | : XYChart(series,item), | |
35 | m_series(series), |
|
35 | m_series(series), | |
36 | m_pointsVisible(false), |
|
36 | m_pointsVisible(false), | |
37 | m_chartType(QChart::ChartTypeUndefined), |
|
37 | m_chartType(QChart::ChartTypeUndefined), | |
38 | m_pointLabelsVisible(false), |
|
38 | m_pointLabelsVisible(false), | |
39 | m_pointLabelsFormat(series->pointLabelsFormat()), |
|
39 | m_pointLabelsFormat(series->pointLabelsFormat()), | |
40 | m_pointLabelsFont(series->pointLabelsFont()), |
|
40 | m_pointLabelsFont(series->pointLabelsFont()), | |
41 | m_pointLabelsColor(series->pointLabelsColor()), |
|
41 | m_pointLabelsColor(series->pointLabelsColor()), | |
42 | m_pointLabelsClipping(true), |
|
42 | m_pointLabelsClipping(true), | |
43 | m_mousePressed(false) |
|
43 | m_mousePressed(false) | |
44 | { |
|
44 | { | |
45 | setAcceptHoverEvents(true); |
|
45 | setAcceptHoverEvents(true); | |
46 | setFlag(QGraphicsItem::ItemIsSelectable); |
|
46 | setFlag(QGraphicsItem::ItemIsSelectable); | |
47 | setZValue(ChartPresenter::LineChartZValue); |
|
47 | setZValue(ChartPresenter::LineChartZValue); | |
48 | QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
48 | QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | |
49 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); |
|
49 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | |
50 | QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); |
|
50 | QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); | |
51 | QObject::connect(series, SIGNAL(pointLabelsFormatChanged(QString)), |
|
51 | QObject::connect(series, SIGNAL(pointLabelsFormatChanged(QString)), | |
52 | this, SLOT(handleUpdated())); |
|
52 | this, SLOT(handleUpdated())); | |
53 | QObject::connect(series, SIGNAL(pointLabelsVisibilityChanged(bool)), |
|
53 | QObject::connect(series, SIGNAL(pointLabelsVisibilityChanged(bool)), | |
54 | this, SLOT(handleUpdated())); |
|
54 | this, SLOT(handleUpdated())); | |
55 | QObject::connect(series, SIGNAL(pointLabelsFontChanged(QFont)), this, SLOT(handleUpdated())); |
|
55 | QObject::connect(series, SIGNAL(pointLabelsFontChanged(QFont)), this, SLOT(handleUpdated())); | |
56 | QObject::connect(series, SIGNAL(pointLabelsColorChanged(QColor)), this, SLOT(handleUpdated())); |
|
56 | QObject::connect(series, SIGNAL(pointLabelsColorChanged(QColor)), this, SLOT(handleUpdated())); | |
57 | QObject::connect(series, SIGNAL(pointLabelsClippingChanged(bool)), this, SLOT(handleUpdated())); |
|
57 | QObject::connect(series, SIGNAL(pointLabelsClippingChanged(bool)), this, SLOT(handleUpdated())); | |
58 | handleUpdated(); |
|
58 | handleUpdated(); | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 | QRectF LineChartItem::boundingRect() const |
|
61 | QRectF LineChartItem::boundingRect() const | |
62 | { |
|
62 | { | |
63 | return m_rect; |
|
63 | return m_rect; | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | QPainterPath LineChartItem::shape() const |
|
66 | QPainterPath LineChartItem::shape() const | |
67 | { |
|
67 | { | |
68 | return m_shapePath; |
|
68 | return m_shapePath; | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | void LineChartItem::updateGeometry() |
|
71 | void LineChartItem::updateGeometry() | |
72 | { |
|
72 | { | |
73 | static const QRectF dummyRect = QRectF(0.0, 0.0, 0.001, 0.001); |
|
|||
74 | if (m_series->useOpenGL()) { |
|
73 | if (m_series->useOpenGL()) { | |
75 | // Fake a miniscule region, so we trigger changed signal. |
|
74 | if (!m_rect.isEmpty()) { | |
76 | if (m_rect.width() != dummyRect.width()) { |
|
|||
77 | prepareGeometryChange(); |
|
75 | prepareGeometryChange(); | |
78 | m_rect = dummyRect; |
|
76 | // Changed signal seems to trigger even with empty region | |
|
77 | m_rect = QRectF(); | |||
79 | } |
|
78 | } | |
80 | update(); |
|
79 | update(); | |
81 | return; |
|
80 | return; | |
82 | } |
|
81 | } | |
83 |
|
82 | |||
84 | // Store the points to a local variable so that the old line gets properly cleared |
|
83 | // Store the points to a local variable so that the old line gets properly cleared | |
85 | // when animation starts. |
|
84 | // when animation starts. | |
86 | m_linePoints = geometryPoints(); |
|
85 | m_linePoints = geometryPoints(); | |
87 | const QVector<QPointF> &points = m_linePoints; |
|
86 | const QVector<QPointF> &points = m_linePoints; | |
88 |
|
87 | |||
89 | if (points.size() == 0) { |
|
88 | if (points.size() == 0) { | |
90 | prepareGeometryChange(); |
|
89 | prepareGeometryChange(); | |
91 | m_fullPath = QPainterPath(); |
|
90 | m_fullPath = QPainterPath(); | |
92 | m_linePath = QPainterPath(); |
|
91 | m_linePath = QPainterPath(); | |
93 | m_rect = QRect(); |
|
92 | m_rect = QRect(); | |
94 | return; |
|
93 | return; | |
95 | } |
|
94 | } | |
96 |
|
95 | |||
97 | QPainterPath linePath; |
|
96 | QPainterPath linePath; | |
98 | QPainterPath fullPath; |
|
97 | QPainterPath fullPath; | |
99 | // Use worst case scenario to determine required margin. |
|
98 | // Use worst case scenario to determine required margin. | |
100 | qreal margin = m_linePen.width() * 1.42; |
|
99 | qreal margin = m_linePen.width() * 1.42; | |
101 |
|
100 | |||
102 | // Area series use component line series that aren't necessarily added to the chart themselves, |
|
101 | // Area series use component line series that aren't necessarily added to the chart themselves, | |
103 | // so check if chart type is forced before trying to obtain it from the chart. |
|
102 | // so check if chart type is forced before trying to obtain it from the chart. | |
104 | QChart::ChartType chartType = m_chartType; |
|
103 | QChart::ChartType chartType = m_chartType; | |
105 | if (chartType == QChart::ChartTypeUndefined) |
|
104 | if (chartType == QChart::ChartTypeUndefined) | |
106 | chartType = m_series->chart()->chartType(); |
|
105 | chartType = m_series->chart()->chartType(); | |
107 |
|
106 | |||
108 | // For polar charts, we need special handling for angular (horizontal) |
|
107 | // For polar charts, we need special handling for angular (horizontal) | |
109 | // points that are off-grid. |
|
108 | // points that are off-grid. | |
110 | if (chartType == QChart::ChartTypePolar) { |
|
109 | if (chartType == QChart::ChartTypePolar) { | |
111 | QPainterPath linePathLeft; |
|
110 | QPainterPath linePathLeft; | |
112 | QPainterPath linePathRight; |
|
111 | QPainterPath linePathRight; | |
113 | QPainterPath *currentSegmentPath = 0; |
|
112 | QPainterPath *currentSegmentPath = 0; | |
114 | QPainterPath *previousSegmentPath = 0; |
|
113 | QPainterPath *previousSegmentPath = 0; | |
115 | qreal minX = domain()->minX(); |
|
114 | qreal minX = domain()->minX(); | |
116 | qreal maxX = domain()->maxX(); |
|
115 | qreal maxX = domain()->maxX(); | |
117 | qreal minY = domain()->minY(); |
|
116 | qreal minY = domain()->minY(); | |
118 | QPointF currentSeriesPoint = m_series->at(0); |
|
117 | QPointF currentSeriesPoint = m_series->at(0); | |
119 | QPointF currentGeometryPoint = points.at(0); |
|
118 | QPointF currentGeometryPoint = points.at(0); | |
120 | QPointF previousGeometryPoint = points.at(0); |
|
119 | QPointF previousGeometryPoint = points.at(0); | |
121 | int size = m_linePen.width(); |
|
120 | int size = m_linePen.width(); | |
122 | bool pointOffGrid = false; |
|
121 | bool pointOffGrid = false; | |
123 | bool previousPointWasOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX); |
|
122 | bool previousPointWasOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX); | |
124 |
|
123 | |||
125 | qreal domainRadius = domain()->size().height() / 2.0; |
|
124 | qreal domainRadius = domain()->size().height() / 2.0; | |
126 | const QPointF centerPoint(domainRadius, domainRadius); |
|
125 | const QPointF centerPoint(domainRadius, domainRadius); | |
127 |
|
126 | |||
128 | if (!previousPointWasOffGrid) { |
|
127 | if (!previousPointWasOffGrid) { | |
129 | fullPath.moveTo(points.at(0)); |
|
128 | fullPath.moveTo(points.at(0)); | |
130 | if (m_pointsVisible && currentSeriesPoint.y() >= minY) { |
|
129 | if (m_pointsVisible && currentSeriesPoint.y() >= minY) { | |
131 | // Do not draw ellipses for points below minimum Y. |
|
130 | // Do not draw ellipses for points below minimum Y. | |
132 | linePath.addEllipse(points.at(0), size, size); |
|
131 | linePath.addEllipse(points.at(0), size, size); | |
133 | fullPath.addEllipse(points.at(0), size, size); |
|
132 | fullPath.addEllipse(points.at(0), size, size); | |
134 | linePath.moveTo(points.at(0)); |
|
133 | linePath.moveTo(points.at(0)); | |
135 | fullPath.moveTo(points.at(0)); |
|
134 | fullPath.moveTo(points.at(0)); | |
136 | } |
|
135 | } | |
137 | } |
|
136 | } | |
138 |
|
137 | |||
139 | qreal leftMarginLine = centerPoint.x() - margin; |
|
138 | qreal leftMarginLine = centerPoint.x() - margin; | |
140 | qreal rightMarginLine = centerPoint.x() + margin; |
|
139 | qreal rightMarginLine = centerPoint.x() + margin; | |
141 | qreal horizontal = centerPoint.y(); |
|
140 | qreal horizontal = centerPoint.y(); | |
142 |
|
141 | |||
143 | // See ScatterChartItem::updateGeometry() for explanation why seriesLastIndex is needed |
|
142 | // See ScatterChartItem::updateGeometry() for explanation why seriesLastIndex is needed | |
144 | const int seriesLastIndex = m_series->count() - 1; |
|
143 | const int seriesLastIndex = m_series->count() - 1; | |
145 |
|
144 | |||
146 | for (int i = 1; i < points.size(); i++) { |
|
145 | for (int i = 1; i < points.size(); i++) { | |
147 | // Interpolating line fragments would be ugly when thick pen is used, |
|
146 | // Interpolating line fragments would be ugly when thick pen is used, | |
148 | // so we work around it by utilizing three separate |
|
147 | // so we work around it by utilizing three separate | |
149 | // paths for line segments and clip those with custom regions at paint time. |
|
148 | // paths for line segments and clip those with custom regions at paint time. | |
150 | // "Right" path contains segments that cross the axis line with visible point on the |
|
149 | // "Right" path contains segments that cross the axis line with visible point on the | |
151 | // right side of the axis line, as well as segments that have one point within the margin |
|
150 | // right side of the axis line, as well as segments that have one point within the margin | |
152 | // on the right side of the axis line and another point on the right side of the chart. |
|
151 | // on the right side of the axis line and another point on the right side of the chart. | |
153 | // "Left" path contains points with similarly on the left side. |
|
152 | // "Left" path contains points with similarly on the left side. | |
154 | // "Full" path contains rest of the points. |
|
153 | // "Full" path contains rest of the points. | |
155 | // This doesn't yield perfect results always. E.g. when segment covers more than 90 |
|
154 | // This doesn't yield perfect results always. E.g. when segment covers more than 90 | |
156 | // degrees and both of the points are within the margin, one in the top half and one in the |
|
155 | // degrees and both of the points are within the margin, one in the top half and one in the | |
157 | // bottom half of the chart, the bottom one gets clipped incorrectly. |
|
156 | // bottom half of the chart, the bottom one gets clipped incorrectly. | |
158 | // However, this should be rare occurrence in any sensible chart. |
|
157 | // However, this should be rare occurrence in any sensible chart. | |
159 | currentSeriesPoint = m_series->at(qMin(seriesLastIndex, i)); |
|
158 | currentSeriesPoint = m_series->at(qMin(seriesLastIndex, i)); | |
160 | currentGeometryPoint = points.at(i); |
|
159 | currentGeometryPoint = points.at(i); | |
161 | pointOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX); |
|
160 | pointOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX); | |
162 |
|
161 | |||
163 | // Draw something unless both off-grid |
|
162 | // Draw something unless both off-grid | |
164 | if (!pointOffGrid || !previousPointWasOffGrid) { |
|
163 | if (!pointOffGrid || !previousPointWasOffGrid) { | |
165 | QPointF intersectionPoint; |
|
164 | QPointF intersectionPoint; | |
166 | qreal y; |
|
165 | qreal y; | |
167 | if (pointOffGrid != previousPointWasOffGrid) { |
|
166 | if (pointOffGrid != previousPointWasOffGrid) { | |
168 | if (currentGeometryPoint.x() == previousGeometryPoint.x()) { |
|
167 | if (currentGeometryPoint.x() == previousGeometryPoint.x()) { | |
169 | y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) / 2.0; |
|
168 | y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) / 2.0; | |
170 | } else { |
|
169 | } else { | |
171 | qreal ratio = (centerPoint.x() - currentGeometryPoint.x()) / (currentGeometryPoint.x() - previousGeometryPoint.x()); |
|
170 | qreal ratio = (centerPoint.x() - currentGeometryPoint.x()) / (currentGeometryPoint.x() - previousGeometryPoint.x()); | |
172 | y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) * ratio; |
|
171 | y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) * ratio; | |
173 | } |
|
172 | } | |
174 | intersectionPoint = QPointF(centerPoint.x(), y); |
|
173 | intersectionPoint = QPointF(centerPoint.x(), y); | |
175 | } |
|
174 | } | |
176 |
|
175 | |||
177 | bool dummyOk; // We know points are ok, but this is needed |
|
176 | bool dummyOk; // We know points are ok, but this is needed | |
178 | qreal currentAngle = 0; |
|
177 | qreal currentAngle = 0; | |
179 | qreal previousAngle = 0; |
|
178 | qreal previousAngle = 0; | |
180 | if (const PolarDomain *pd = qobject_cast<const PolarDomain *>(domain())) { |
|
179 | if (const PolarDomain *pd = qobject_cast<const PolarDomain *>(domain())) { | |
181 | currentAngle = pd->toAngularCoordinate(currentSeriesPoint.x(), dummyOk); |
|
180 | currentAngle = pd->toAngularCoordinate(currentSeriesPoint.x(), dummyOk); | |
182 | previousAngle = pd->toAngularCoordinate(m_series->at(i - 1).x(), dummyOk); |
|
181 | previousAngle = pd->toAngularCoordinate(m_series->at(i - 1).x(), dummyOk); | |
183 | } else { |
|
182 | } else { | |
184 | qWarning() << Q_FUNC_INFO << "Unexpected domain: " << domain(); |
|
183 | qWarning() << Q_FUNC_INFO << "Unexpected domain: " << domain(); | |
185 | } |
|
184 | } | |
186 | if ((qAbs(currentAngle - previousAngle) > 180.0)) { |
|
185 | if ((qAbs(currentAngle - previousAngle) > 180.0)) { | |
187 | // If the angle between two points is over 180 degrees (half X range), |
|
186 | // If the angle between two points is over 180 degrees (half X range), | |
188 | // any direct segment between them becomes meaningless. |
|
187 | // any direct segment between them becomes meaningless. | |
189 | // In this case two line segments are drawn instead, from previous |
|
188 | // In this case two line segments are drawn instead, from previous | |
190 | // point to the center and from center to current point. |
|
189 | // point to the center and from center to current point. | |
191 | if ((previousAngle < 0.0 || (previousAngle <= 180.0 && previousGeometryPoint.x() < rightMarginLine)) |
|
190 | if ((previousAngle < 0.0 || (previousAngle <= 180.0 && previousGeometryPoint.x() < rightMarginLine)) | |
192 | && previousGeometryPoint.y() < horizontal) { |
|
191 | && previousGeometryPoint.y() < horizontal) { | |
193 | currentSegmentPath = &linePathRight; |
|
192 | currentSegmentPath = &linePathRight; | |
194 | } else if ((previousAngle > 360.0 || (previousAngle > 180.0 && previousGeometryPoint.x() > leftMarginLine)) |
|
193 | } else if ((previousAngle > 360.0 || (previousAngle > 180.0 && previousGeometryPoint.x() > leftMarginLine)) | |
195 | && previousGeometryPoint.y() < horizontal) { |
|
194 | && previousGeometryPoint.y() < horizontal) { | |
196 | currentSegmentPath = &linePathLeft; |
|
195 | currentSegmentPath = &linePathLeft; | |
197 | } else if (previousAngle > 0.0 && previousAngle < 360.0) { |
|
196 | } else if (previousAngle > 0.0 && previousAngle < 360.0) { | |
198 | currentSegmentPath = &linePath; |
|
197 | currentSegmentPath = &linePath; | |
199 | } else { |
|
198 | } else { | |
200 | currentSegmentPath = 0; |
|
199 | currentSegmentPath = 0; | |
201 | } |
|
200 | } | |
202 |
|
201 | |||
203 | if (currentSegmentPath) { |
|
202 | if (currentSegmentPath) { | |
204 | if (previousSegmentPath != currentSegmentPath) |
|
203 | if (previousSegmentPath != currentSegmentPath) | |
205 | currentSegmentPath->moveTo(previousGeometryPoint); |
|
204 | currentSegmentPath->moveTo(previousGeometryPoint); | |
206 | if (previousPointWasOffGrid) |
|
205 | if (previousPointWasOffGrid) | |
207 | fullPath.moveTo(intersectionPoint); |
|
206 | fullPath.moveTo(intersectionPoint); | |
208 |
|
207 | |||
209 | currentSegmentPath->lineTo(centerPoint); |
|
208 | currentSegmentPath->lineTo(centerPoint); | |
210 | fullPath.lineTo(centerPoint); |
|
209 | fullPath.lineTo(centerPoint); | |
211 | } |
|
210 | } | |
212 |
|
211 | |||
213 | previousSegmentPath = currentSegmentPath; |
|
212 | previousSegmentPath = currentSegmentPath; | |
214 |
|
213 | |||
215 | if ((currentAngle < 0.0 || (currentAngle <= 180.0 && currentGeometryPoint.x() < rightMarginLine)) |
|
214 | if ((currentAngle < 0.0 || (currentAngle <= 180.0 && currentGeometryPoint.x() < rightMarginLine)) | |
216 | && currentGeometryPoint.y() < horizontal) { |
|
215 | && currentGeometryPoint.y() < horizontal) { | |
217 | currentSegmentPath = &linePathRight; |
|
216 | currentSegmentPath = &linePathRight; | |
218 | } else if ((currentAngle > 360.0 || (currentAngle > 180.0 &¤tGeometryPoint.x() > leftMarginLine)) |
|
217 | } else if ((currentAngle > 360.0 || (currentAngle > 180.0 &¤tGeometryPoint.x() > leftMarginLine)) | |
219 | && currentGeometryPoint.y() < horizontal) { |
|
218 | && currentGeometryPoint.y() < horizontal) { | |
220 | currentSegmentPath = &linePathLeft; |
|
219 | currentSegmentPath = &linePathLeft; | |
221 | } else if (currentAngle > 0.0 && currentAngle < 360.0) { |
|
220 | } else if (currentAngle > 0.0 && currentAngle < 360.0) { | |
222 | currentSegmentPath = &linePath; |
|
221 | currentSegmentPath = &linePath; | |
223 | } else { |
|
222 | } else { | |
224 | currentSegmentPath = 0; |
|
223 | currentSegmentPath = 0; | |
225 | } |
|
224 | } | |
226 |
|
225 | |||
227 | if (currentSegmentPath) { |
|
226 | if (currentSegmentPath) { | |
228 | if (previousSegmentPath != currentSegmentPath) |
|
227 | if (previousSegmentPath != currentSegmentPath) | |
229 | currentSegmentPath->moveTo(centerPoint); |
|
228 | currentSegmentPath->moveTo(centerPoint); | |
230 | if (!previousSegmentPath) |
|
229 | if (!previousSegmentPath) | |
231 | fullPath.moveTo(centerPoint); |
|
230 | fullPath.moveTo(centerPoint); | |
232 |
|
231 | |||
233 | currentSegmentPath->lineTo(currentGeometryPoint); |
|
232 | currentSegmentPath->lineTo(currentGeometryPoint); | |
234 | if (pointOffGrid) |
|
233 | if (pointOffGrid) | |
235 | fullPath.lineTo(intersectionPoint); |
|
234 | fullPath.lineTo(intersectionPoint); | |
236 | else |
|
235 | else | |
237 | fullPath.lineTo(currentGeometryPoint); |
|
236 | fullPath.lineTo(currentGeometryPoint); | |
238 | } |
|
237 | } | |
239 | } else { |
|
238 | } else { | |
240 | if (previousAngle < 0.0 || currentAngle < 0.0 |
|
239 | if (previousAngle < 0.0 || currentAngle < 0.0 | |
241 | || ((previousAngle <= 180.0 && currentAngle <= 180.0) |
|
240 | || ((previousAngle <= 180.0 && currentAngle <= 180.0) | |
242 | && ((previousGeometryPoint.x() < rightMarginLine && previousGeometryPoint.y() < horizontal) |
|
241 | && ((previousGeometryPoint.x() < rightMarginLine && previousGeometryPoint.y() < horizontal) | |
243 | || (currentGeometryPoint.x() < rightMarginLine && currentGeometryPoint.y() < horizontal)))) { |
|
242 | || (currentGeometryPoint.x() < rightMarginLine && currentGeometryPoint.y() < horizontal)))) { | |
244 | currentSegmentPath = &linePathRight; |
|
243 | currentSegmentPath = &linePathRight; | |
245 | } else if (previousAngle > 360.0 || currentAngle > 360.0 |
|
244 | } else if (previousAngle > 360.0 || currentAngle > 360.0 | |
246 | || ((previousAngle > 180.0 && currentAngle > 180.0) |
|
245 | || ((previousAngle > 180.0 && currentAngle > 180.0) | |
247 | && ((previousGeometryPoint.x() > leftMarginLine && previousGeometryPoint.y() < horizontal) |
|
246 | && ((previousGeometryPoint.x() > leftMarginLine && previousGeometryPoint.y() < horizontal) | |
248 | || (currentGeometryPoint.x() > leftMarginLine && currentGeometryPoint.y() < horizontal)))) { |
|
247 | || (currentGeometryPoint.x() > leftMarginLine && currentGeometryPoint.y() < horizontal)))) { | |
249 | currentSegmentPath = &linePathLeft; |
|
248 | currentSegmentPath = &linePathLeft; | |
250 | } else { |
|
249 | } else { | |
251 | currentSegmentPath = &linePath; |
|
250 | currentSegmentPath = &linePath; | |
252 | } |
|
251 | } | |
253 |
|
252 | |||
254 | if (currentSegmentPath != previousSegmentPath) |
|
253 | if (currentSegmentPath != previousSegmentPath) | |
255 | currentSegmentPath->moveTo(previousGeometryPoint); |
|
254 | currentSegmentPath->moveTo(previousGeometryPoint); | |
256 | if (previousPointWasOffGrid) |
|
255 | if (previousPointWasOffGrid) | |
257 | fullPath.moveTo(intersectionPoint); |
|
256 | fullPath.moveTo(intersectionPoint); | |
258 |
|
257 | |||
259 | if (pointOffGrid) |
|
258 | if (pointOffGrid) | |
260 | fullPath.lineTo(intersectionPoint); |
|
259 | fullPath.lineTo(intersectionPoint); | |
261 | else |
|
260 | else | |
262 | fullPath.lineTo(currentGeometryPoint); |
|
261 | fullPath.lineTo(currentGeometryPoint); | |
263 | currentSegmentPath->lineTo(currentGeometryPoint); |
|
262 | currentSegmentPath->lineTo(currentGeometryPoint); | |
264 | } |
|
263 | } | |
265 | } else { |
|
264 | } else { | |
266 | currentSegmentPath = 0; |
|
265 | currentSegmentPath = 0; | |
267 | } |
|
266 | } | |
268 |
|
267 | |||
269 | previousPointWasOffGrid = pointOffGrid; |
|
268 | previousPointWasOffGrid = pointOffGrid; | |
270 | if (m_pointsVisible && !pointOffGrid && currentSeriesPoint.y() >= minY) { |
|
269 | if (m_pointsVisible && !pointOffGrid && currentSeriesPoint.y() >= minY) { | |
271 | linePath.addEllipse(points.at(i), size, size); |
|
270 | linePath.addEllipse(points.at(i), size, size); | |
272 | fullPath.addEllipse(points.at(i), size, size); |
|
271 | fullPath.addEllipse(points.at(i), size, size); | |
273 | linePath.moveTo(points.at(i)); |
|
272 | linePath.moveTo(points.at(i)); | |
274 | fullPath.moveTo(points.at(i)); |
|
273 | fullPath.moveTo(points.at(i)); | |
275 | } |
|
274 | } | |
276 | previousSegmentPath = currentSegmentPath; |
|
275 | previousSegmentPath = currentSegmentPath; | |
277 | previousGeometryPoint = currentGeometryPoint; |
|
276 | previousGeometryPoint = currentGeometryPoint; | |
278 | } |
|
277 | } | |
279 | m_linePathPolarRight = linePathRight; |
|
278 | m_linePathPolarRight = linePathRight; | |
280 | m_linePathPolarLeft = linePathLeft; |
|
279 | m_linePathPolarLeft = linePathLeft; | |
281 | // Note: This construction of m_fullpath is not perfect. The partial segments that are |
|
280 | // Note: This construction of m_fullpath is not perfect. The partial segments that are | |
282 | // outside left/right clip regions at axis boundary still generate hover/click events, |
|
281 | // outside left/right clip regions at axis boundary still generate hover/click events, | |
283 | // because shape doesn't get clipped. It doesn't seem possible to do sensibly. |
|
282 | // because shape doesn't get clipped. It doesn't seem possible to do sensibly. | |
284 | } else { // not polar |
|
283 | } else { // not polar | |
285 | linePath.moveTo(points.at(0)); |
|
284 | linePath.moveTo(points.at(0)); | |
286 | if (m_pointsVisible) { |
|
285 | if (m_pointsVisible) { | |
287 | int size = m_linePen.width(); |
|
286 | int size = m_linePen.width(); | |
288 | linePath.addEllipse(points.at(0), size, size); |
|
287 | linePath.addEllipse(points.at(0), size, size); | |
289 | linePath.moveTo(points.at(0)); |
|
288 | linePath.moveTo(points.at(0)); | |
290 | for (int i = 1; i < points.size(); i++) { |
|
289 | for (int i = 1; i < points.size(); i++) { | |
291 | linePath.lineTo(points.at(i)); |
|
290 | linePath.lineTo(points.at(i)); | |
292 | linePath.addEllipse(points.at(i), size, size); |
|
291 | linePath.addEllipse(points.at(i), size, size); | |
293 | linePath.moveTo(points.at(i)); |
|
292 | linePath.moveTo(points.at(i)); | |
294 | } |
|
293 | } | |
295 | } else { |
|
294 | } else { | |
296 | for (int i = 1; i < points.size(); i++) |
|
295 | for (int i = 1; i < points.size(); i++) | |
297 | linePath.lineTo(points.at(i)); |
|
296 | linePath.lineTo(points.at(i)); | |
298 | } |
|
297 | } | |
299 | fullPath = linePath; |
|
298 | fullPath = linePath; | |
300 | } |
|
299 | } | |
301 |
|
300 | |||
302 | QPainterPathStroker stroker; |
|
301 | QPainterPathStroker stroker; | |
303 | // QPainter::drawLine does not respect join styles, for example BevelJoin becomes MiterJoin. |
|
302 | // QPainter::drawLine does not respect join styles, for example BevelJoin becomes MiterJoin. | |
304 | // This is why we are prepared for the "worst case" scenario, i.e. use always MiterJoin and |
|
303 | // This is why we are prepared for the "worst case" scenario, i.e. use always MiterJoin and | |
305 | // multiply line width with square root of two when defining shape and bounding rectangle. |
|
304 | // multiply line width with square root of two when defining shape and bounding rectangle. | |
306 | stroker.setWidth(margin); |
|
305 | stroker.setWidth(margin); | |
307 | stroker.setJoinStyle(Qt::MiterJoin); |
|
306 | stroker.setJoinStyle(Qt::MiterJoin); | |
308 | stroker.setCapStyle(Qt::SquareCap); |
|
307 | stroker.setCapStyle(Qt::SquareCap); | |
309 | stroker.setMiterLimit(m_linePen.miterLimit()); |
|
308 | stroker.setMiterLimit(m_linePen.miterLimit()); | |
310 |
|
309 | |||
311 | QPainterPath checkShapePath = stroker.createStroke(fullPath); |
|
310 | QPainterPath checkShapePath = stroker.createStroke(fullPath); | |
312 |
|
311 | |||
313 | // Only zoom in if the bounding rects of the paths fit inside int limits. QWidget::update() uses |
|
312 | // Only zoom in if the bounding rects of the paths fit inside int limits. QWidget::update() uses | |
314 | // a region that has to be compatible with QRect. |
|
313 | // a region that has to be compatible with QRect. | |
315 | if (checkShapePath.boundingRect().height() <= INT_MAX |
|
314 | if (checkShapePath.boundingRect().height() <= INT_MAX | |
316 | && checkShapePath.boundingRect().width() <= INT_MAX |
|
315 | && checkShapePath.boundingRect().width() <= INT_MAX | |
317 | && linePath.boundingRect().height() <= INT_MAX |
|
316 | && linePath.boundingRect().height() <= INT_MAX | |
318 | && linePath.boundingRect().width() <= INT_MAX |
|
317 | && linePath.boundingRect().width() <= INT_MAX | |
319 | && fullPath.boundingRect().height() <= INT_MAX |
|
318 | && fullPath.boundingRect().height() <= INT_MAX | |
320 | && fullPath.boundingRect().width() <= INT_MAX) { |
|
319 | && fullPath.boundingRect().width() <= INT_MAX) { | |
321 | prepareGeometryChange(); |
|
320 | prepareGeometryChange(); | |
322 |
|
321 | |||
323 | m_linePath = linePath; |
|
322 | m_linePath = linePath; | |
324 | m_fullPath = fullPath; |
|
323 | m_fullPath = fullPath; | |
325 | m_shapePath = checkShapePath; |
|
324 | m_shapePath = checkShapePath; | |
326 |
|
325 | |||
327 | m_rect = m_shapePath.boundingRect(); |
|
326 | m_rect = m_shapePath.boundingRect(); | |
328 | } else { |
|
327 | } else { | |
329 | update(); |
|
328 | update(); | |
330 | } |
|
329 | } | |
331 | } |
|
330 | } | |
332 |
|
331 | |||
333 | void LineChartItem::handleUpdated() |
|
332 | void LineChartItem::handleUpdated() | |
334 | { |
|
333 | { | |
335 | // If points visibility has changed, a geometry update is needed. |
|
334 | // If points visibility has changed, a geometry update is needed. | |
336 | // Also, if pen changes when points are visible, geometry update is needed. |
|
335 | // Also, if pen changes when points are visible, geometry update is needed. | |
337 | bool doGeometryUpdate = |
|
336 | bool doGeometryUpdate = | |
338 | (m_pointsVisible != m_series->pointsVisible()) |
|
337 | (m_pointsVisible != m_series->pointsVisible()) | |
339 | || (m_series->pointsVisible() && (m_linePen != m_series->pen())); |
|
338 | || (m_series->pointsVisible() && (m_linePen != m_series->pen())); | |
340 | setVisible(m_series->isVisible()); |
|
339 | setVisible(m_series->isVisible()); | |
341 | setOpacity(m_series->opacity()); |
|
340 | setOpacity(m_series->opacity()); | |
342 | m_pointsVisible = m_series->pointsVisible(); |
|
341 | m_pointsVisible = m_series->pointsVisible(); | |
343 | m_linePen = m_series->pen(); |
|
342 | m_linePen = m_series->pen(); | |
344 | m_pointLabelsFormat = m_series->pointLabelsFormat(); |
|
343 | m_pointLabelsFormat = m_series->pointLabelsFormat(); | |
345 | m_pointLabelsVisible = m_series->pointLabelsVisible(); |
|
344 | m_pointLabelsVisible = m_series->pointLabelsVisible(); | |
346 | m_pointLabelsFont = m_series->pointLabelsFont(); |
|
345 | m_pointLabelsFont = m_series->pointLabelsFont(); | |
347 | m_pointLabelsColor = m_series->pointLabelsColor(); |
|
346 | m_pointLabelsColor = m_series->pointLabelsColor(); | |
348 | m_pointLabelsClipping = m_series->pointLabelsClipping(); |
|
347 | m_pointLabelsClipping = m_series->pointLabelsClipping(); | |
349 | if (doGeometryUpdate) |
|
348 | if (doGeometryUpdate) | |
350 | updateGeometry(); |
|
349 | updateGeometry(); | |
351 | update(); |
|
350 | update(); | |
352 | } |
|
351 | } | |
353 |
|
352 | |||
354 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
353 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
355 | { |
|
354 | { | |
356 | Q_UNUSED(widget) |
|
355 | Q_UNUSED(widget) | |
357 | Q_UNUSED(option) |
|
356 | Q_UNUSED(option) | |
358 |
|
357 | |||
359 | if (m_series->useOpenGL()) |
|
358 | if (m_series->useOpenGL()) | |
360 | return; |
|
359 | return; | |
361 |
|
360 | |||
362 | QRectF clipRect = QRectF(QPointF(0, 0), domain()->size()); |
|
361 | QRectF clipRect = QRectF(QPointF(0, 0), domain()->size()); | |
363 |
|
362 | |||
364 | painter->save(); |
|
363 | painter->save(); | |
365 | painter->setPen(m_linePen); |
|
364 | painter->setPen(m_linePen); | |
366 | bool alwaysUsePath = false; |
|
365 | bool alwaysUsePath = false; | |
367 |
|
366 | |||
368 | if (m_series->chart()->chartType() == QChart::ChartTypePolar) { |
|
367 | if (m_series->chart()->chartType() == QChart::ChartTypePolar) { | |
369 | qreal halfWidth = domain()->size().width() / 2.0; |
|
368 | qreal halfWidth = domain()->size().width() / 2.0; | |
370 | QRectF clipRectLeft = QRectF(0, 0, halfWidth, domain()->size().height()); |
|
369 | QRectF clipRectLeft = QRectF(0, 0, halfWidth, domain()->size().height()); | |
371 | QRectF clipRectRight = QRectF(halfWidth, 0, halfWidth, domain()->size().height()); |
|
370 | QRectF clipRectRight = QRectF(halfWidth, 0, halfWidth, domain()->size().height()); | |
372 | QRegion fullPolarClipRegion(clipRect.toRect(), QRegion::Ellipse); |
|
371 | QRegion fullPolarClipRegion(clipRect.toRect(), QRegion::Ellipse); | |
373 | QRegion clipRegionLeft(fullPolarClipRegion.intersected(clipRectLeft.toRect())); |
|
372 | QRegion clipRegionLeft(fullPolarClipRegion.intersected(clipRectLeft.toRect())); | |
374 | QRegion clipRegionRight(fullPolarClipRegion.intersected(clipRectRight.toRect())); |
|
373 | QRegion clipRegionRight(fullPolarClipRegion.intersected(clipRectRight.toRect())); | |
375 | painter->setClipRegion(clipRegionLeft); |
|
374 | painter->setClipRegion(clipRegionLeft); | |
376 | painter->drawPath(m_linePathPolarLeft); |
|
375 | painter->drawPath(m_linePathPolarLeft); | |
377 | painter->setClipRegion(clipRegionRight); |
|
376 | painter->setClipRegion(clipRegionRight); | |
378 | painter->drawPath(m_linePathPolarRight); |
|
377 | painter->drawPath(m_linePathPolarRight); | |
379 | painter->setClipRegion(fullPolarClipRegion); |
|
378 | painter->setClipRegion(fullPolarClipRegion); | |
380 | alwaysUsePath = true; // required for proper clipping |
|
379 | alwaysUsePath = true; // required for proper clipping | |
381 | } else { |
|
380 | } else { | |
382 | painter->setClipRect(clipRect); |
|
381 | painter->setClipRect(clipRect); | |
383 | } |
|
382 | } | |
384 |
|
383 | |||
385 | reversePainter(painter, clipRect); |
|
384 | reversePainter(painter, clipRect); | |
386 |
|
385 | |||
387 | if (m_pointsVisible) { |
|
386 | if (m_pointsVisible) { | |
388 | painter->setBrush(m_linePen.color()); |
|
387 | painter->setBrush(m_linePen.color()); | |
389 | painter->drawPath(m_linePath); |
|
388 | painter->drawPath(m_linePath); | |
390 | } else { |
|
389 | } else { | |
391 | painter->setBrush(QBrush(Qt::NoBrush)); |
|
390 | painter->setBrush(QBrush(Qt::NoBrush)); | |
392 | if (m_linePen.style() != Qt::SolidLine || alwaysUsePath) { |
|
391 | if (m_linePen.style() != Qt::SolidLine || alwaysUsePath) { | |
393 | // If pen style is not solid line, always fall back to path painting |
|
392 | // If pen style is not solid line, always fall back to path painting | |
394 | // to ensure proper continuity of the pattern |
|
393 | // to ensure proper continuity of the pattern | |
395 | painter->drawPath(m_linePath); |
|
394 | painter->drawPath(m_linePath); | |
396 | } else { |
|
395 | } else { | |
397 | for (int i(1); i < m_linePoints.size(); i++) |
|
396 | for (int i(1); i < m_linePoints.size(); i++) | |
398 | painter->drawLine(m_linePoints.at(i - 1), m_linePoints.at(i)); |
|
397 | painter->drawLine(m_linePoints.at(i - 1), m_linePoints.at(i)); | |
399 | } |
|
398 | } | |
400 | } |
|
399 | } | |
401 |
|
400 | |||
402 | reversePainter(painter, clipRect); |
|
401 | reversePainter(painter, clipRect); | |
403 |
|
402 | |||
404 | if (m_pointLabelsVisible) { |
|
403 | if (m_pointLabelsVisible) { | |
405 | if (m_pointLabelsClipping) |
|
404 | if (m_pointLabelsClipping) | |
406 | painter->setClipping(true); |
|
405 | painter->setClipping(true); | |
407 | else |
|
406 | else | |
408 | painter->setClipping(false); |
|
407 | painter->setClipping(false); | |
409 | m_series->d_func()->drawSeriesPointLabels(painter, m_linePoints, m_linePen.width() / 2); |
|
408 | m_series->d_func()->drawSeriesPointLabels(painter, m_linePoints, m_linePen.width() / 2); | |
410 | } |
|
409 | } | |
411 |
|
410 | |||
412 | painter->restore(); |
|
411 | painter->restore(); | |
413 |
|
412 | |||
414 | } |
|
413 | } | |
415 |
|
414 | |||
416 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
415 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | |
417 | { |
|
416 | { | |
418 | emit XYChart::pressed(domain()->calculateDomainPoint(event->pos())); |
|
417 | emit XYChart::pressed(domain()->calculateDomainPoint(event->pos())); | |
419 | m_lastMousePos = event->pos(); |
|
418 | m_lastMousePos = event->pos(); | |
420 | m_mousePressed = true; |
|
419 | m_mousePressed = true; | |
421 | QGraphicsItem::mousePressEvent(event); |
|
420 | QGraphicsItem::mousePressEvent(event); | |
422 | } |
|
421 | } | |
423 |
|
422 | |||
424 | void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) |
|
423 | void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | |
425 | { |
|
424 | { | |
426 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true); |
|
425 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true); | |
427 | // event->accept(); |
|
426 | // event->accept(); | |
428 | QGraphicsItem::hoverEnterEvent(event); |
|
427 | QGraphicsItem::hoverEnterEvent(event); | |
429 | } |
|
428 | } | |
430 |
|
429 | |||
431 | void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) |
|
430 | void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | |
432 | { |
|
431 | { | |
433 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false); |
|
432 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false); | |
434 | // event->accept(); |
|
433 | // event->accept(); | |
435 | QGraphicsItem::hoverEnterEvent(event); |
|
434 | QGraphicsItem::hoverEnterEvent(event); | |
436 | } |
|
435 | } | |
437 |
|
436 | |||
438 | void LineChartItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
|
437 | void LineChartItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) | |
439 | { |
|
438 | { | |
440 | emit XYChart::released(domain()->calculateDomainPoint(m_lastMousePos)); |
|
439 | emit XYChart::released(domain()->calculateDomainPoint(m_lastMousePos)); | |
441 | if (m_mousePressed) |
|
440 | if (m_mousePressed) | |
442 | emit XYChart::clicked(domain()->calculateDomainPoint(m_lastMousePos)); |
|
441 | emit XYChart::clicked(domain()->calculateDomainPoint(m_lastMousePos)); | |
443 | m_mousePressed = false; |
|
442 | m_mousePressed = false; | |
444 | QGraphicsItem::mouseReleaseEvent(event); |
|
443 | QGraphicsItem::mouseReleaseEvent(event); | |
445 | } |
|
444 | } | |
446 |
|
445 | |||
447 | void LineChartItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) |
|
446 | void LineChartItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) | |
448 | { |
|
447 | { | |
449 | emit XYChart::doubleClicked(domain()->calculateDomainPoint(m_lastMousePos)); |
|
448 | emit XYChart::doubleClicked(domain()->calculateDomainPoint(m_lastMousePos)); | |
450 | QGraphicsItem::mouseDoubleClickEvent(event); |
|
449 | QGraphicsItem::mouseDoubleClickEvent(event); | |
451 | } |
|
450 | } | |
452 |
|
451 | |||
453 | #include "moc_linechartitem_p.cpp" |
|
452 | #include "moc_linechartitem_p.cpp" | |
454 |
|
453 | |||
455 | QT_CHARTS_END_NAMESPACE |
|
454 | QT_CHARTS_END_NAMESPACE |
@@ -1,283 +1,282 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2015 The Qt Company Ltd |
|
3 | ** Copyright (C) 2015 The Qt Company Ltd | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to The Qt Company, please use contact form at http://qt.io |
|
5 | ** For any questions to The Qt Company, please use contact form at http://qt.io | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Charts module. |
|
7 | ** This file is part of the Qt Charts module. | |
8 | ** |
|
8 | ** | |
9 | ** Licensees holding valid commercial license for Qt may use this file in |
|
9 | ** Licensees holding valid commercial license for Qt may use this file in | |
10 | ** accordance with the Qt License Agreement provided with the Software |
|
10 | ** accordance with the Qt License Agreement provided with the Software | |
11 | ** or, alternatively, in accordance with the terms contained in a written |
|
11 | ** or, alternatively, in accordance with the terms contained in a written | |
12 | ** agreement between you and The Qt Company. |
|
12 | ** agreement between you and The Qt Company. | |
13 | ** |
|
13 | ** | |
14 | ** If you have questions regarding the use of this file, please use |
|
14 | ** If you have questions regarding the use of this file, please use | |
15 | ** contact form at http://qt.io |
|
15 | ** contact form at http://qt.io | |
16 | ** |
|
16 | ** | |
17 | ****************************************************************************/ |
|
17 | ****************************************************************************/ | |
18 |
|
18 | |||
19 | #include <private/scatterchartitem_p.h> |
|
19 | #include <private/scatterchartitem_p.h> | |
20 | #include <QtCharts/QScatterSeries> |
|
20 | #include <QtCharts/QScatterSeries> | |
21 | #include <private/qscatterseries_p.h> |
|
21 | #include <private/qscatterseries_p.h> | |
22 | #include <private/chartpresenter_p.h> |
|
22 | #include <private/chartpresenter_p.h> | |
23 | #include <private/abstractdomain_p.h> |
|
23 | #include <private/abstractdomain_p.h> | |
24 | #include <QtCharts/QChart> |
|
24 | #include <QtCharts/QChart> | |
25 | #include <QtGui/QPainter> |
|
25 | #include <QtGui/QPainter> | |
26 | #include <QtWidgets/QGraphicsScene> |
|
26 | #include <QtWidgets/QGraphicsScene> | |
27 | #include <QtCore/QDebug> |
|
27 | #include <QtCore/QDebug> | |
28 | #include <QtWidgets/QGraphicsSceneMouseEvent> |
|
28 | #include <QtWidgets/QGraphicsSceneMouseEvent> | |
29 |
|
29 | |||
30 | QT_CHARTS_BEGIN_NAMESPACE |
|
30 | QT_CHARTS_BEGIN_NAMESPACE | |
31 |
|
31 | |||
32 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *item) |
|
32 | ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *item) | |
33 | : XYChart(series,item), |
|
33 | : XYChart(series,item), | |
34 | m_series(series), |
|
34 | m_series(series), | |
35 | m_items(this), |
|
35 | m_items(this), | |
36 | m_visible(true), |
|
36 | m_visible(true), | |
37 | m_shape(QScatterSeries::MarkerShapeRectangle), |
|
37 | m_shape(QScatterSeries::MarkerShapeRectangle), | |
38 | m_size(15), |
|
38 | m_size(15), | |
39 | m_pointLabelsVisible(false), |
|
39 | m_pointLabelsVisible(false), | |
40 | m_pointLabelsFormat(series->pointLabelsFormat()), |
|
40 | m_pointLabelsFormat(series->pointLabelsFormat()), | |
41 | m_pointLabelsFont(series->pointLabelsFont()), |
|
41 | m_pointLabelsFont(series->pointLabelsFont()), | |
42 | m_pointLabelsColor(series->pointLabelsColor()), |
|
42 | m_pointLabelsColor(series->pointLabelsColor()), | |
43 | m_pointLabelsClipping(true), |
|
43 | m_pointLabelsClipping(true), | |
44 | m_mousePressed(false) |
|
44 | m_mousePressed(false) | |
45 | { |
|
45 | { | |
46 | QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
46 | QObject::connect(m_series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | |
47 | QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); |
|
47 | QObject::connect(m_series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | |
48 | QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); |
|
48 | QObject::connect(m_series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); | |
49 | QObject::connect(series, SIGNAL(pointLabelsFormatChanged(QString)), |
|
49 | QObject::connect(series, SIGNAL(pointLabelsFormatChanged(QString)), | |
50 | this, SLOT(handleUpdated())); |
|
50 | this, SLOT(handleUpdated())); | |
51 | QObject::connect(series, SIGNAL(pointLabelsVisibilityChanged(bool)), |
|
51 | QObject::connect(series, SIGNAL(pointLabelsVisibilityChanged(bool)), | |
52 | this, SLOT(handleUpdated())); |
|
52 | this, SLOT(handleUpdated())); | |
53 | QObject::connect(series, SIGNAL(pointLabelsFontChanged(QFont)), this, SLOT(handleUpdated())); |
|
53 | QObject::connect(series, SIGNAL(pointLabelsFontChanged(QFont)), this, SLOT(handleUpdated())); | |
54 | QObject::connect(series, SIGNAL(pointLabelsColorChanged(QColor)), this, SLOT(handleUpdated())); |
|
54 | QObject::connect(series, SIGNAL(pointLabelsColorChanged(QColor)), this, SLOT(handleUpdated())); | |
55 | QObject::connect(series, SIGNAL(pointLabelsClippingChanged(bool)), this, SLOT(handleUpdated())); |
|
55 | QObject::connect(series, SIGNAL(pointLabelsClippingChanged(bool)), this, SLOT(handleUpdated())); | |
56 |
|
56 | |||
57 | setZValue(ChartPresenter::ScatterSeriesZValue); |
|
57 | setZValue(ChartPresenter::ScatterSeriesZValue); | |
58 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); |
|
58 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | |
59 |
|
59 | |||
60 | handleUpdated(); |
|
60 | handleUpdated(); | |
61 |
|
61 | |||
62 | m_items.setHandlesChildEvents(false); |
|
62 | m_items.setHandlesChildEvents(false); | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | QRectF ScatterChartItem::boundingRect() const |
|
65 | QRectF ScatterChartItem::boundingRect() const | |
66 | { |
|
66 | { | |
67 | return m_rect; |
|
67 | return m_rect; | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 | void ScatterChartItem::createPoints(int count) |
|
70 | void ScatterChartItem::createPoints(int count) | |
71 | { |
|
71 | { | |
72 | for (int i = 0; i < count; ++i) { |
|
72 | for (int i = 0; i < count; ++i) { | |
73 |
|
73 | |||
74 | QGraphicsItem *item = 0; |
|
74 | QGraphicsItem *item = 0; | |
75 |
|
75 | |||
76 | switch (m_shape) { |
|
76 | switch (m_shape) { | |
77 | case QScatterSeries::MarkerShapeCircle: { |
|
77 | case QScatterSeries::MarkerShapeCircle: { | |
78 | item = new CircleMarker(0, 0, m_size, m_size, this); |
|
78 | item = new CircleMarker(0, 0, m_size, m_size, this); | |
79 | const QRectF &rect = item->boundingRect(); |
|
79 | const QRectF &rect = item->boundingRect(); | |
80 | item->setPos(-rect.width() / 2, -rect.height() / 2); |
|
80 | item->setPos(-rect.width() / 2, -rect.height() / 2); | |
81 | break; |
|
81 | break; | |
82 | } |
|
82 | } | |
83 | case QScatterSeries::MarkerShapeRectangle: |
|
83 | case QScatterSeries::MarkerShapeRectangle: | |
84 | item = new RectangleMarker(0, 0, m_size, m_size, this); |
|
84 | item = new RectangleMarker(0, 0, m_size, m_size, this); | |
85 | item->setPos(-m_size / 2, -m_size / 2); |
|
85 | item->setPos(-m_size / 2, -m_size / 2); | |
86 | break; |
|
86 | break; | |
87 | default: |
|
87 | default: | |
88 | qWarning() << "Unsupported marker type"; |
|
88 | qWarning() << "Unsupported marker type"; | |
89 | break; |
|
89 | break; | |
90 | } |
|
90 | } | |
91 | m_items.addToGroup(item); |
|
91 | m_items.addToGroup(item); | |
92 | } |
|
92 | } | |
93 | } |
|
93 | } | |
94 |
|
94 | |||
95 | void ScatterChartItem::deletePoints(int count) |
|
95 | void ScatterChartItem::deletePoints(int count) | |
96 | { |
|
96 | { | |
97 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
97 | QList<QGraphicsItem *> items = m_items.childItems(); | |
98 |
|
98 | |||
99 | for (int i = 0; i < count; ++i) { |
|
99 | for (int i = 0; i < count; ++i) { | |
100 | QGraphicsItem *item = items.takeLast(); |
|
100 | QGraphicsItem *item = items.takeLast(); | |
101 | m_markerMap.remove(item); |
|
101 | m_markerMap.remove(item); | |
102 | delete(item); |
|
102 | delete(item); | |
103 | } |
|
103 | } | |
104 | } |
|
104 | } | |
105 |
|
105 | |||
106 | void ScatterChartItem::markerSelected(QGraphicsItem *marker) |
|
106 | void ScatterChartItem::markerSelected(QGraphicsItem *marker) | |
107 | { |
|
107 | { | |
108 | emit XYChart::clicked(m_markerMap[marker]); |
|
108 | emit XYChart::clicked(m_markerMap[marker]); | |
109 | } |
|
109 | } | |
110 |
|
110 | |||
111 | void ScatterChartItem::markerHovered(QGraphicsItem *marker, bool state) |
|
111 | void ScatterChartItem::markerHovered(QGraphicsItem *marker, bool state) | |
112 | { |
|
112 | { | |
113 | emit XYChart::hovered(m_markerMap[marker], state); |
|
113 | emit XYChart::hovered(m_markerMap[marker], state); | |
114 | } |
|
114 | } | |
115 |
|
115 | |||
116 | void ScatterChartItem::markerPressed(QGraphicsItem *marker) |
|
116 | void ScatterChartItem::markerPressed(QGraphicsItem *marker) | |
117 | { |
|
117 | { | |
118 | emit XYChart::pressed(m_markerMap[marker]); |
|
118 | emit XYChart::pressed(m_markerMap[marker]); | |
119 | } |
|
119 | } | |
120 |
|
120 | |||
121 | void ScatterChartItem::markerReleased(QGraphicsItem *marker) |
|
121 | void ScatterChartItem::markerReleased(QGraphicsItem *marker) | |
122 | { |
|
122 | { | |
123 | emit XYChart::released(m_markerMap[marker]); |
|
123 | emit XYChart::released(m_markerMap[marker]); | |
124 | } |
|
124 | } | |
125 |
|
125 | |||
126 | void ScatterChartItem::markerDoubleClicked(QGraphicsItem *marker) |
|
126 | void ScatterChartItem::markerDoubleClicked(QGraphicsItem *marker) | |
127 | { |
|
127 | { | |
128 | emit XYChart::doubleClicked(m_markerMap[marker]); |
|
128 | emit XYChart::doubleClicked(m_markerMap[marker]); | |
129 | } |
|
129 | } | |
130 |
|
130 | |||
131 | void ScatterChartItem::updateGeometry() |
|
131 | void ScatterChartItem::updateGeometry() | |
132 | { |
|
132 | { | |
133 | static const QRectF dummyRect = QRectF(0.0, 0.0, 0.001, 0.001); |
|
|||
134 | if (m_series->useOpenGL()) { |
|
133 | if (m_series->useOpenGL()) { | |
135 | if (m_items.childItems().count()) |
|
134 | if (m_items.childItems().count()) | |
136 | deletePoints(m_items.childItems().count()); |
|
135 | deletePoints(m_items.childItems().count()); | |
137 | // Fake a miniscule region, so we trigger changed signal. |
|
136 | if (!m_rect.isEmpty()) { | |
138 | if (m_rect.width() != dummyRect.width()) { |
|
|||
139 | prepareGeometryChange(); |
|
137 | prepareGeometryChange(); | |
140 | m_rect = dummyRect; |
|
138 | // Changed signal seems to trigger even with empty region | |
|
139 | m_rect = QRectF(); | |||
141 | } |
|
140 | } | |
142 | update(); |
|
141 | update(); | |
143 | return; |
|
142 | return; | |
144 | } |
|
143 | } | |
145 |
|
144 | |||
146 | const QVector<QPointF>& points = geometryPoints(); |
|
145 | const QVector<QPointF>& points = geometryPoints(); | |
147 |
|
146 | |||
148 | if (points.size() == 0) { |
|
147 | if (points.size() == 0) { | |
149 | deletePoints(m_items.childItems().count()); |
|
148 | deletePoints(m_items.childItems().count()); | |
150 | return; |
|
149 | return; | |
151 | } |
|
150 | } | |
152 |
|
151 | |||
153 | int diff = m_items.childItems().size() - points.size(); |
|
152 | int diff = m_items.childItems().size() - points.size(); | |
154 |
|
153 | |||
155 | if (diff > 0) |
|
154 | if (diff > 0) | |
156 | deletePoints(diff); |
|
155 | deletePoints(diff); | |
157 | else if (diff < 0) |
|
156 | else if (diff < 0) | |
158 | createPoints(-diff); |
|
157 | createPoints(-diff); | |
159 |
|
158 | |||
160 | if (diff != 0) |
|
159 | if (diff != 0) | |
161 | handleUpdated(); |
|
160 | handleUpdated(); | |
162 |
|
161 | |||
163 | QList<QGraphicsItem *> items = m_items.childItems(); |
|
162 | QList<QGraphicsItem *> items = m_items.childItems(); | |
164 |
|
163 | |||
165 | QRectF clipRect(QPointF(0,0),domain()->size()); |
|
164 | QRectF clipRect(QPointF(0,0),domain()->size()); | |
166 |
|
165 | |||
167 | // Only zoom in if the clipRect fits inside int limits. QWidget::update() uses |
|
166 | // Only zoom in if the clipRect fits inside int limits. QWidget::update() uses | |
168 | // a region that has to be compatible with QRect. |
|
167 | // a region that has to be compatible with QRect. | |
169 | if (clipRect.height() <= INT_MAX |
|
168 | if (clipRect.height() <= INT_MAX | |
170 | && clipRect.width() <= INT_MAX) { |
|
169 | && clipRect.width() <= INT_MAX) { | |
171 | QVector<bool> offGridStatus = offGridStatusVector(); |
|
170 | QVector<bool> offGridStatus = offGridStatusVector(); | |
172 | const int seriesLastIndex = m_series->count() - 1; |
|
171 | const int seriesLastIndex = m_series->count() - 1; | |
173 |
|
172 | |||
174 | for (int i = 0; i < points.size(); i++) { |
|
173 | for (int i = 0; i < points.size(); i++) { | |
175 | QGraphicsItem *item = items.at(i); |
|
174 | QGraphicsItem *item = items.at(i); | |
176 | const QPointF &point = points.at(i); |
|
175 | const QPointF &point = points.at(i); | |
177 | const QRectF &rect = item->boundingRect(); |
|
176 | const QRectF &rect = item->boundingRect(); | |
178 | // During remove animation series may have different number of points, |
|
177 | // During remove animation series may have different number of points, | |
179 | // so ensure we don't go over the index. Animation handling itself ensures that |
|
178 | // so ensure we don't go over the index. Animation handling itself ensures that | |
180 | // if there is actually no points in the series, then it won't generate a fake point, |
|
179 | // if there is actually no points in the series, then it won't generate a fake point, | |
181 | // so we can be assured there is always at least one point in m_series here. |
|
180 | // so we can be assured there is always at least one point in m_series here. | |
182 | // Note that marker map values can be technically incorrect during the animation, |
|
181 | // Note that marker map values can be technically incorrect during the animation, | |
183 | // if it was caused by an insert, but this shouldn't be a problem as the points are |
|
182 | // if it was caused by an insert, but this shouldn't be a problem as the points are | |
184 | // fake anyway. After remove animation stops, geometry is updated to correct one. |
|
183 | // fake anyway. After remove animation stops, geometry is updated to correct one. | |
185 | m_markerMap[item] = m_series->at(qMin(seriesLastIndex, i)); |
|
184 | m_markerMap[item] = m_series->at(qMin(seriesLastIndex, i)); | |
186 | QPointF position; |
|
185 | QPointF position; | |
187 | if (seriesPrivate()->reverseXAxis()) |
|
186 | if (seriesPrivate()->reverseXAxis()) | |
188 | position.setX(domain()->size().width() - point.x() - rect.width() / 2); |
|
187 | position.setX(domain()->size().width() - point.x() - rect.width() / 2); | |
189 | else |
|
188 | else | |
190 | position.setX(point.x() - rect.width() / 2); |
|
189 | position.setX(point.x() - rect.width() / 2); | |
191 | if (seriesPrivate()->reverseYAxis()) |
|
190 | if (seriesPrivate()->reverseYAxis()) | |
192 | position.setY(domain()->size().height() - point.y() - rect.height() / 2); |
|
191 | position.setY(domain()->size().height() - point.y() - rect.height() / 2); | |
193 | else |
|
192 | else | |
194 | position.setY(point.y() - rect.height() / 2); |
|
193 | position.setY(point.y() - rect.height() / 2); | |
195 | item->setPos(position); |
|
194 | item->setPos(position); | |
196 |
|
195 | |||
197 |
|
196 | |||
198 | if (!m_visible || offGridStatus.at(i)) |
|
197 | if (!m_visible || offGridStatus.at(i)) | |
199 | item->setVisible(false); |
|
198 | item->setVisible(false); | |
200 | else |
|
199 | else | |
201 | item->setVisible(true); |
|
200 | item->setVisible(true); | |
202 | } |
|
201 | } | |
203 |
|
202 | |||
204 | prepareGeometryChange(); |
|
203 | prepareGeometryChange(); | |
205 | m_rect = clipRect; |
|
204 | m_rect = clipRect; | |
206 | } |
|
205 | } | |
207 | } |
|
206 | } | |
208 |
|
207 | |||
209 | void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
208 | void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
210 | { |
|
209 | { | |
211 | Q_UNUSED(option) |
|
210 | Q_UNUSED(option) | |
212 | Q_UNUSED(widget) |
|
211 | Q_UNUSED(widget) | |
213 |
|
212 | |||
214 | if (m_series->useOpenGL()) |
|
213 | if (m_series->useOpenGL()) | |
215 | return; |
|
214 | return; | |
216 |
|
215 | |||
217 | QRectF clipRect = QRectF(QPointF(0, 0), domain()->size()); |
|
216 | QRectF clipRect = QRectF(QPointF(0, 0), domain()->size()); | |
218 |
|
217 | |||
219 | painter->save(); |
|
218 | painter->save(); | |
220 | painter->setClipRect(clipRect); |
|
219 | painter->setClipRect(clipRect); | |
221 |
|
220 | |||
222 | if (m_pointLabelsVisible) { |
|
221 | if (m_pointLabelsVisible) { | |
223 | if (m_pointLabelsClipping) |
|
222 | if (m_pointLabelsClipping) | |
224 | painter->setClipping(true); |
|
223 | painter->setClipping(true); | |
225 | else |
|
224 | else | |
226 | painter->setClipping(false); |
|
225 | painter->setClipping(false); | |
227 | m_series->d_func()->drawSeriesPointLabels(painter, m_points, |
|
226 | m_series->d_func()->drawSeriesPointLabels(painter, m_points, | |
228 | m_series->markerSize() / 2 |
|
227 | m_series->markerSize() / 2 | |
229 | + m_series->pen().width()); |
|
228 | + m_series->pen().width()); | |
230 | } |
|
229 | } | |
231 |
|
230 | |||
232 | painter->restore(); |
|
231 | painter->restore(); | |
233 | } |
|
232 | } | |
234 |
|
233 | |||
235 | void ScatterChartItem::setPen(const QPen &pen) |
|
234 | void ScatterChartItem::setPen(const QPen &pen) | |
236 | { |
|
235 | { | |
237 | foreach (QGraphicsItem *item , m_items.childItems()) |
|
236 | foreach (QGraphicsItem *item , m_items.childItems()) | |
238 | static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen); |
|
237 | static_cast<QAbstractGraphicsShapeItem*>(item)->setPen(pen); | |
239 | } |
|
238 | } | |
240 |
|
239 | |||
241 | void ScatterChartItem::setBrush(const QBrush &brush) |
|
240 | void ScatterChartItem::setBrush(const QBrush &brush) | |
242 | { |
|
241 | { | |
243 | foreach (QGraphicsItem *item , m_items.childItems()) |
|
242 | foreach (QGraphicsItem *item , m_items.childItems()) | |
244 | static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush); |
|
243 | static_cast<QAbstractGraphicsShapeItem*>(item)->setBrush(brush); | |
245 | } |
|
244 | } | |
246 |
|
245 | |||
247 | void ScatterChartItem::handleUpdated() |
|
246 | void ScatterChartItem::handleUpdated() | |
248 | { |
|
247 | { | |
249 | int count = m_items.childItems().count(); |
|
248 | int count = m_items.childItems().count(); | |
250 |
|
249 | |||
251 | if (count == 0) |
|
250 | if (count == 0) | |
252 | return; |
|
251 | return; | |
253 |
|
252 | |||
254 | bool recreate = m_visible != m_series->isVisible() |
|
253 | bool recreate = m_visible != m_series->isVisible() | |
255 | || m_size != m_series->markerSize() |
|
254 | || m_size != m_series->markerSize() | |
256 | || m_shape != m_series->markerShape(); |
|
255 | || m_shape != m_series->markerShape(); | |
257 |
|
256 | |||
258 | m_visible = m_series->isVisible(); |
|
257 | m_visible = m_series->isVisible(); | |
259 | m_size = m_series->markerSize(); |
|
258 | m_size = m_series->markerSize(); | |
260 | m_shape = m_series->markerShape(); |
|
259 | m_shape = m_series->markerShape(); | |
261 | setOpacity(m_series->opacity()); |
|
260 | setOpacity(m_series->opacity()); | |
262 | m_pointLabelsFormat = m_series->pointLabelsFormat(); |
|
261 | m_pointLabelsFormat = m_series->pointLabelsFormat(); | |
263 | m_pointLabelsVisible = m_series->pointLabelsVisible(); |
|
262 | m_pointLabelsVisible = m_series->pointLabelsVisible(); | |
264 | m_pointLabelsFont = m_series->pointLabelsFont(); |
|
263 | m_pointLabelsFont = m_series->pointLabelsFont(); | |
265 | m_pointLabelsColor = m_series->pointLabelsColor(); |
|
264 | m_pointLabelsColor = m_series->pointLabelsColor(); | |
266 | m_pointLabelsClipping = m_series->pointLabelsClipping(); |
|
265 | m_pointLabelsClipping = m_series->pointLabelsClipping(); | |
267 |
|
266 | |||
268 | if (recreate) { |
|
267 | if (recreate) { | |
269 | deletePoints(count); |
|
268 | deletePoints(count); | |
270 | createPoints(count); |
|
269 | createPoints(count); | |
271 |
|
270 | |||
272 | // Updating geometry is now safe, because it won't call handleUpdated unless it creates/deletes points |
|
271 | // Updating geometry is now safe, because it won't call handleUpdated unless it creates/deletes points | |
273 | updateGeometry(); |
|
272 | updateGeometry(); | |
274 | } |
|
273 | } | |
275 |
|
274 | |||
276 | setPen(m_series->pen()); |
|
275 | setPen(m_series->pen()); | |
277 | setBrush(m_series->brush()); |
|
276 | setBrush(m_series->brush()); | |
278 | update(); |
|
277 | update(); | |
279 | } |
|
278 | } | |
280 |
|
279 | |||
281 | #include "moc_scatterchartitem_p.cpp" |
|
280 | #include "moc_scatterchartitem_p.cpp" | |
282 |
|
281 | |||
283 | QT_CHARTS_END_NAMESPACE |
|
282 | QT_CHARTS_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now