##// END OF EJS Templates
LineChartItem::updateGeometry(): Catch and warn about unexpected domains....
Friedemann Kleint -
r2771:8283f962d987
parent child
Show More
@@ -1,422 +1,427
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2014 Digia Plc
3 ** Copyright (C) 2014 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.io
5 ** For any questions to Digia, 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 Digia.
12 ** agreement between you and Digia.
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_mousePressed(false)
42 m_mousePressed(false)
43 {
43 {
44 setAcceptHoverEvents(true);
44 setAcceptHoverEvents(true);
45 setFlag(QGraphicsItem::ItemIsSelectable);
45 setFlag(QGraphicsItem::ItemIsSelectable);
46 setZValue(ChartPresenter::LineChartZValue);
46 setZValue(ChartPresenter::LineChartZValue);
47 QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
47 QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated()));
48 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
48 QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated()));
49 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
49 QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated()));
50 QObject::connect(series, SIGNAL(pointLabelsFormatChanged(QString)),
50 QObject::connect(series, SIGNAL(pointLabelsFormatChanged(QString)),
51 this, SLOT(handleUpdated()));
51 this, SLOT(handleUpdated()));
52 QObject::connect(series, SIGNAL(pointLabelsVisibilityChanged(bool)),
52 QObject::connect(series, SIGNAL(pointLabelsVisibilityChanged(bool)),
53 this, SLOT(handleUpdated()));
53 this, SLOT(handleUpdated()));
54 QObject::connect(series, SIGNAL(pointLabelsFontChanged(QFont)), this, SLOT(handleUpdated()));
54 QObject::connect(series, SIGNAL(pointLabelsFontChanged(QFont)), this, SLOT(handleUpdated()));
55 QObject::connect(series, SIGNAL(pointLabelsColorChanged(QColor)), this, SLOT(handleUpdated()));
55 QObject::connect(series, SIGNAL(pointLabelsColorChanged(QColor)), this, SLOT(handleUpdated()));
56 handleUpdated();
56 handleUpdated();
57 }
57 }
58
58
59 QRectF LineChartItem::boundingRect() const
59 QRectF LineChartItem::boundingRect() const
60 {
60 {
61 return m_rect;
61 return m_rect;
62 }
62 }
63
63
64 QPainterPath LineChartItem::shape() const
64 QPainterPath LineChartItem::shape() const
65 {
65 {
66 return m_shapePath;
66 return m_shapePath;
67 }
67 }
68
68
69 void LineChartItem::updateGeometry()
69 void LineChartItem::updateGeometry()
70 {
70 {
71 m_points = geometryPoints();
71 m_points = geometryPoints();
72 const QVector<QPointF> &points = m_points;
72 const QVector<QPointF> &points = m_points;
73
73
74 if (points.size() == 0) {
74 if (points.size() == 0) {
75 prepareGeometryChange();
75 prepareGeometryChange();
76 m_fullPath = QPainterPath();
76 m_fullPath = QPainterPath();
77 m_linePath = QPainterPath();
77 m_linePath = QPainterPath();
78 m_rect = QRect();
78 m_rect = QRect();
79 return;
79 return;
80 }
80 }
81
81
82 QPainterPath linePath;
82 QPainterPath linePath;
83 QPainterPath fullPath;
83 QPainterPath fullPath;
84 // Use worst case scenario to determine required margin.
84 // Use worst case scenario to determine required margin.
85 qreal margin = m_linePen.width() * 1.42;
85 qreal margin = m_linePen.width() * 1.42;
86
86
87 // Area series use component line series that aren't necessarily added to the chart themselves,
87 // Area series use component line series that aren't necessarily added to the chart themselves,
88 // so check if chart type is forced before trying to obtain it from the chart.
88 // so check if chart type is forced before trying to obtain it from the chart.
89 QChart::ChartType chartType = m_chartType;
89 QChart::ChartType chartType = m_chartType;
90 if (chartType == QChart::ChartTypeUndefined)
90 if (chartType == QChart::ChartTypeUndefined)
91 chartType = m_series->chart()->chartType();
91 chartType = m_series->chart()->chartType();
92
92
93 // For polar charts, we need special handling for angular (horizontal)
93 // For polar charts, we need special handling for angular (horizontal)
94 // points that are off-grid.
94 // points that are off-grid.
95 if (chartType == QChart::ChartTypePolar) {
95 if (chartType == QChart::ChartTypePolar) {
96 QPainterPath linePathLeft;
96 QPainterPath linePathLeft;
97 QPainterPath linePathRight;
97 QPainterPath linePathRight;
98 QPainterPath *currentSegmentPath = 0;
98 QPainterPath *currentSegmentPath = 0;
99 QPainterPath *previousSegmentPath = 0;
99 QPainterPath *previousSegmentPath = 0;
100 qreal minX = domain()->minX();
100 qreal minX = domain()->minX();
101 qreal maxX = domain()->maxX();
101 qreal maxX = domain()->maxX();
102 qreal minY = domain()->minY();
102 qreal minY = domain()->minY();
103 QPointF currentSeriesPoint = m_series->at(0);
103 QPointF currentSeriesPoint = m_series->at(0);
104 QPointF currentGeometryPoint = points.at(0);
104 QPointF currentGeometryPoint = points.at(0);
105 QPointF previousGeometryPoint = points.at(0);
105 QPointF previousGeometryPoint = points.at(0);
106 int size = m_linePen.width();
106 int size = m_linePen.width();
107 bool pointOffGrid = false;
107 bool pointOffGrid = false;
108 bool previousPointWasOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX);
108 bool previousPointWasOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX);
109
109
110 qreal domainRadius = domain()->size().height() / 2.0;
110 qreal domainRadius = domain()->size().height() / 2.0;
111 const QPointF centerPoint(domainRadius, domainRadius);
111 const QPointF centerPoint(domainRadius, domainRadius);
112
112
113 if (!previousPointWasOffGrid) {
113 if (!previousPointWasOffGrid) {
114 fullPath.moveTo(points.at(0));
114 fullPath.moveTo(points.at(0));
115 if (m_pointsVisible && currentSeriesPoint.y() >= minY) {
115 if (m_pointsVisible && currentSeriesPoint.y() >= minY) {
116 // Do not draw ellipses for points below minimum Y.
116 // Do not draw ellipses for points below minimum Y.
117 linePath.addEllipse(points.at(0), size, size);
117 linePath.addEllipse(points.at(0), size, size);
118 fullPath.addEllipse(points.at(0), size, size);
118 fullPath.addEllipse(points.at(0), size, size);
119 linePath.moveTo(points.at(0));
119 linePath.moveTo(points.at(0));
120 fullPath.moveTo(points.at(0));
120 fullPath.moveTo(points.at(0));
121 }
121 }
122 }
122 }
123
123
124 qreal leftMarginLine = centerPoint.x() - margin;
124 qreal leftMarginLine = centerPoint.x() - margin;
125 qreal rightMarginLine = centerPoint.x() + margin;
125 qreal rightMarginLine = centerPoint.x() + margin;
126 qreal horizontal = centerPoint.y();
126 qreal horizontal = centerPoint.y();
127
127
128 // See ScatterChartItem::updateGeometry() for explanation why seriesLastIndex is needed
128 // See ScatterChartItem::updateGeometry() for explanation why seriesLastIndex is needed
129 const int seriesLastIndex = m_series->count() - 1;
129 const int seriesLastIndex = m_series->count() - 1;
130
130
131 for (int i = 1; i < points.size(); i++) {
131 for (int i = 1; i < points.size(); i++) {
132 // Interpolating line fragments would be ugly when thick pen is used,
132 // Interpolating line fragments would be ugly when thick pen is used,
133 // so we work around it by utilizing three separate
133 // so we work around it by utilizing three separate
134 // paths for line segments and clip those with custom regions at paint time.
134 // paths for line segments and clip those with custom regions at paint time.
135 // "Right" path contains segments that cross the axis line with visible point on the
135 // "Right" path contains segments that cross the axis line with visible point on the
136 // right side of the axis line, as well as segments that have one point within the margin
136 // right side of the axis line, as well as segments that have one point within the margin
137 // on the right side of the axis line and another point on the right side of the chart.
137 // on the right side of the axis line and another point on the right side of the chart.
138 // "Left" path contains points with similarly on the left side.
138 // "Left" path contains points with similarly on the left side.
139 // "Full" path contains rest of the points.
139 // "Full" path contains rest of the points.
140 // This doesn't yield perfect results always. E.g. when segment covers more than 90
140 // This doesn't yield perfect results always. E.g. when segment covers more than 90
141 // degrees and both of the points are within the margin, one in the top half and one in the
141 // degrees and both of the points are within the margin, one in the top half and one in the
142 // bottom half of the chart, the bottom one gets clipped incorrectly.
142 // bottom half of the chart, the bottom one gets clipped incorrectly.
143 // However, this should be rare occurrence in any sensible chart.
143 // However, this should be rare occurrence in any sensible chart.
144 currentSeriesPoint = m_series->at(qMin(seriesLastIndex, i));
144 currentSeriesPoint = m_series->at(qMin(seriesLastIndex, i));
145 currentGeometryPoint = points.at(i);
145 currentGeometryPoint = points.at(i);
146 pointOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX);
146 pointOffGrid = (currentSeriesPoint.x() < minX || currentSeriesPoint.x() > maxX);
147
147
148 // Draw something unless both off-grid
148 // Draw something unless both off-grid
149 if (!pointOffGrid || !previousPointWasOffGrid) {
149 if (!pointOffGrid || !previousPointWasOffGrid) {
150 QPointF intersectionPoint;
150 QPointF intersectionPoint;
151 qreal y;
151 qreal y;
152 if (pointOffGrid != previousPointWasOffGrid) {
152 if (pointOffGrid != previousPointWasOffGrid) {
153 if (currentGeometryPoint.x() == previousGeometryPoint.x()) {
153 if (currentGeometryPoint.x() == previousGeometryPoint.x()) {
154 y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) / 2.0;
154 y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) / 2.0;
155 } else {
155 } else {
156 qreal ratio = (centerPoint.x() - currentGeometryPoint.x()) / (currentGeometryPoint.x() - previousGeometryPoint.x());
156 qreal ratio = (centerPoint.x() - currentGeometryPoint.x()) / (currentGeometryPoint.x() - previousGeometryPoint.x());
157 y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) * ratio;
157 y = currentGeometryPoint.y() + (currentGeometryPoint.y() - previousGeometryPoint.y()) * ratio;
158 }
158 }
159 intersectionPoint = QPointF(centerPoint.x(), y);
159 intersectionPoint = QPointF(centerPoint.x(), y);
160 }
160 }
161
161
162 bool dummyOk; // We know points are ok, but this is needed
162 bool dummyOk; // We know points are ok, but this is needed
163 qreal currentAngle = static_cast<PolarDomain *>(domain())->toAngularCoordinate(currentSeriesPoint.x(), dummyOk);
163 qreal currentAngle = 0;
164 qreal previousAngle = static_cast<PolarDomain *>(domain())->toAngularCoordinate(m_series->at(i - 1).x(), dummyOk);
164 qreal previousAngle = 0;
165
165 if (const PolarDomain *pd = qobject_cast<const PolarDomain *>(domain())) {
166 currentAngle = pd->toAngularCoordinate(currentSeriesPoint.x(), dummyOk);
167 previousAngle = pd->toAngularCoordinate(m_series->at(i - 1).x(), dummyOk);
168 } else {
169 qWarning() << Q_FUNC_INFO << "Unexpected domain: " << domain();
170 }
166 if ((qAbs(currentAngle - previousAngle) > 180.0)) {
171 if ((qAbs(currentAngle - previousAngle) > 180.0)) {
167 // If the angle between two points is over 180 degrees (half X range),
172 // If the angle between two points is over 180 degrees (half X range),
168 // any direct segment between them becomes meaningless.
173 // any direct segment between them becomes meaningless.
169 // In this case two line segments are drawn instead, from previous
174 // In this case two line segments are drawn instead, from previous
170 // point to the center and from center to current point.
175 // point to the center and from center to current point.
171 if ((previousAngle < 0.0 || (previousAngle <= 180.0 && previousGeometryPoint.x() < rightMarginLine))
176 if ((previousAngle < 0.0 || (previousAngle <= 180.0 && previousGeometryPoint.x() < rightMarginLine))
172 && previousGeometryPoint.y() < horizontal) {
177 && previousGeometryPoint.y() < horizontal) {
173 currentSegmentPath = &linePathRight;
178 currentSegmentPath = &linePathRight;
174 } else if ((previousAngle > 360.0 || (previousAngle > 180.0 && previousGeometryPoint.x() > leftMarginLine))
179 } else if ((previousAngle > 360.0 || (previousAngle > 180.0 && previousGeometryPoint.x() > leftMarginLine))
175 && previousGeometryPoint.y() < horizontal) {
180 && previousGeometryPoint.y() < horizontal) {
176 currentSegmentPath = &linePathLeft;
181 currentSegmentPath = &linePathLeft;
177 } else if (previousAngle > 0.0 && previousAngle < 360.0) {
182 } else if (previousAngle > 0.0 && previousAngle < 360.0) {
178 currentSegmentPath = &linePath;
183 currentSegmentPath = &linePath;
179 } else {
184 } else {
180 currentSegmentPath = 0;
185 currentSegmentPath = 0;
181 }
186 }
182
187
183 if (currentSegmentPath) {
188 if (currentSegmentPath) {
184 if (previousSegmentPath != currentSegmentPath)
189 if (previousSegmentPath != currentSegmentPath)
185 currentSegmentPath->moveTo(previousGeometryPoint);
190 currentSegmentPath->moveTo(previousGeometryPoint);
186 if (previousPointWasOffGrid)
191 if (previousPointWasOffGrid)
187 fullPath.moveTo(intersectionPoint);
192 fullPath.moveTo(intersectionPoint);
188
193
189 currentSegmentPath->lineTo(centerPoint);
194 currentSegmentPath->lineTo(centerPoint);
190 fullPath.lineTo(centerPoint);
195 fullPath.lineTo(centerPoint);
191 }
196 }
192
197
193 previousSegmentPath = currentSegmentPath;
198 previousSegmentPath = currentSegmentPath;
194
199
195 if ((currentAngle < 0.0 || (currentAngle <= 180.0 && currentGeometryPoint.x() < rightMarginLine))
200 if ((currentAngle < 0.0 || (currentAngle <= 180.0 && currentGeometryPoint.x() < rightMarginLine))
196 && currentGeometryPoint.y() < horizontal) {
201 && currentGeometryPoint.y() < horizontal) {
197 currentSegmentPath = &linePathRight;
202 currentSegmentPath = &linePathRight;
198 } else if ((currentAngle > 360.0 || (currentAngle > 180.0 &&currentGeometryPoint.x() > leftMarginLine))
203 } else if ((currentAngle > 360.0 || (currentAngle > 180.0 &&currentGeometryPoint.x() > leftMarginLine))
199 && currentGeometryPoint.y() < horizontal) {
204 && currentGeometryPoint.y() < horizontal) {
200 currentSegmentPath = &linePathLeft;
205 currentSegmentPath = &linePathLeft;
201 } else if (currentAngle > 0.0 && currentAngle < 360.0) {
206 } else if (currentAngle > 0.0 && currentAngle < 360.0) {
202 currentSegmentPath = &linePath;
207 currentSegmentPath = &linePath;
203 } else {
208 } else {
204 currentSegmentPath = 0;
209 currentSegmentPath = 0;
205 }
210 }
206
211
207 if (currentSegmentPath) {
212 if (currentSegmentPath) {
208 if (previousSegmentPath != currentSegmentPath)
213 if (previousSegmentPath != currentSegmentPath)
209 currentSegmentPath->moveTo(centerPoint);
214 currentSegmentPath->moveTo(centerPoint);
210 if (!previousSegmentPath)
215 if (!previousSegmentPath)
211 fullPath.moveTo(centerPoint);
216 fullPath.moveTo(centerPoint);
212
217
213 currentSegmentPath->lineTo(currentGeometryPoint);
218 currentSegmentPath->lineTo(currentGeometryPoint);
214 if (pointOffGrid)
219 if (pointOffGrid)
215 fullPath.lineTo(intersectionPoint);
220 fullPath.lineTo(intersectionPoint);
216 else
221 else
217 fullPath.lineTo(currentGeometryPoint);
222 fullPath.lineTo(currentGeometryPoint);
218 }
223 }
219 } else {
224 } else {
220 if (previousAngle < 0.0 || currentAngle < 0.0
225 if (previousAngle < 0.0 || currentAngle < 0.0
221 || ((previousAngle <= 180.0 && currentAngle <= 180.0)
226 || ((previousAngle <= 180.0 && currentAngle <= 180.0)
222 && ((previousGeometryPoint.x() < rightMarginLine && previousGeometryPoint.y() < horizontal)
227 && ((previousGeometryPoint.x() < rightMarginLine && previousGeometryPoint.y() < horizontal)
223 || (currentGeometryPoint.x() < rightMarginLine && currentGeometryPoint.y() < horizontal)))) {
228 || (currentGeometryPoint.x() < rightMarginLine && currentGeometryPoint.y() < horizontal)))) {
224 currentSegmentPath = &linePathRight;
229 currentSegmentPath = &linePathRight;
225 } else if (previousAngle > 360.0 || currentAngle > 360.0
230 } else if (previousAngle > 360.0 || currentAngle > 360.0
226 || ((previousAngle > 180.0 && currentAngle > 180.0)
231 || ((previousAngle > 180.0 && currentAngle > 180.0)
227 && ((previousGeometryPoint.x() > leftMarginLine && previousGeometryPoint.y() < horizontal)
232 && ((previousGeometryPoint.x() > leftMarginLine && previousGeometryPoint.y() < horizontal)
228 || (currentGeometryPoint.x() > leftMarginLine && currentGeometryPoint.y() < horizontal)))) {
233 || (currentGeometryPoint.x() > leftMarginLine && currentGeometryPoint.y() < horizontal)))) {
229 currentSegmentPath = &linePathLeft;
234 currentSegmentPath = &linePathLeft;
230 } else {
235 } else {
231 currentSegmentPath = &linePath;
236 currentSegmentPath = &linePath;
232 }
237 }
233
238
234 if (currentSegmentPath != previousSegmentPath)
239 if (currentSegmentPath != previousSegmentPath)
235 currentSegmentPath->moveTo(previousGeometryPoint);
240 currentSegmentPath->moveTo(previousGeometryPoint);
236 if (previousPointWasOffGrid)
241 if (previousPointWasOffGrid)
237 fullPath.moveTo(intersectionPoint);
242 fullPath.moveTo(intersectionPoint);
238
243
239 if (pointOffGrid)
244 if (pointOffGrid)
240 fullPath.lineTo(intersectionPoint);
245 fullPath.lineTo(intersectionPoint);
241 else
246 else
242 fullPath.lineTo(currentGeometryPoint);
247 fullPath.lineTo(currentGeometryPoint);
243 currentSegmentPath->lineTo(currentGeometryPoint);
248 currentSegmentPath->lineTo(currentGeometryPoint);
244 }
249 }
245 } else {
250 } else {
246 currentSegmentPath = 0;
251 currentSegmentPath = 0;
247 }
252 }
248
253
249 previousPointWasOffGrid = pointOffGrid;
254 previousPointWasOffGrid = pointOffGrid;
250 if (m_pointsVisible && !pointOffGrid && currentSeriesPoint.y() >= minY) {
255 if (m_pointsVisible && !pointOffGrid && currentSeriesPoint.y() >= minY) {
251 linePath.addEllipse(points.at(i), size, size);
256 linePath.addEllipse(points.at(i), size, size);
252 fullPath.addEllipse(points.at(i), size, size);
257 fullPath.addEllipse(points.at(i), size, size);
253 linePath.moveTo(points.at(i));
258 linePath.moveTo(points.at(i));
254 fullPath.moveTo(points.at(i));
259 fullPath.moveTo(points.at(i));
255 }
260 }
256 previousSegmentPath = currentSegmentPath;
261 previousSegmentPath = currentSegmentPath;
257 previousGeometryPoint = currentGeometryPoint;
262 previousGeometryPoint = currentGeometryPoint;
258 }
263 }
259 m_linePathPolarRight = linePathRight;
264 m_linePathPolarRight = linePathRight;
260 m_linePathPolarLeft = linePathLeft;
265 m_linePathPolarLeft = linePathLeft;
261 // Note: This construction of m_fullpath is not perfect. The partial segments that are
266 // Note: This construction of m_fullpath is not perfect. The partial segments that are
262 // outside left/right clip regions at axis boundary still generate hover/click events,
267 // outside left/right clip regions at axis boundary still generate hover/click events,
263 // because shape doesn't get clipped. It doesn't seem possible to do sensibly.
268 // because shape doesn't get clipped. It doesn't seem possible to do sensibly.
264 } else { // not polar
269 } else { // not polar
265 linePath.moveTo(points.at(0));
270 linePath.moveTo(points.at(0));
266 if (m_pointsVisible) {
271 if (m_pointsVisible) {
267 int size = m_linePen.width();
272 int size = m_linePen.width();
268 linePath.addEllipse(points.at(0), size, size);
273 linePath.addEllipse(points.at(0), size, size);
269 linePath.moveTo(points.at(0));
274 linePath.moveTo(points.at(0));
270 for (int i = 1; i < points.size(); i++) {
275 for (int i = 1; i < points.size(); i++) {
271 linePath.lineTo(points.at(i));
276 linePath.lineTo(points.at(i));
272 linePath.addEllipse(points.at(i), size, size);
277 linePath.addEllipse(points.at(i), size, size);
273 linePath.moveTo(points.at(i));
278 linePath.moveTo(points.at(i));
274 }
279 }
275 } else {
280 } else {
276 for (int i = 1; i < points.size(); i++)
281 for (int i = 1; i < points.size(); i++)
277 linePath.lineTo(points.at(i));
282 linePath.lineTo(points.at(i));
278 }
283 }
279 fullPath = linePath;
284 fullPath = linePath;
280 }
285 }
281
286
282 QPainterPathStroker stroker;
287 QPainterPathStroker stroker;
283 // QPainter::drawLine does not respect join styles, for example BevelJoin becomes MiterJoin.
288 // QPainter::drawLine does not respect join styles, for example BevelJoin becomes MiterJoin.
284 // This is why we are prepared for the "worst case" scenario, i.e. use always MiterJoin and
289 // This is why we are prepared for the "worst case" scenario, i.e. use always MiterJoin and
285 // multiply line width with square root of two when defining shape and bounding rectangle.
290 // multiply line width with square root of two when defining shape and bounding rectangle.
286 stroker.setWidth(margin);
291 stroker.setWidth(margin);
287 stroker.setJoinStyle(Qt::MiterJoin);
292 stroker.setJoinStyle(Qt::MiterJoin);
288 stroker.setCapStyle(Qt::SquareCap);
293 stroker.setCapStyle(Qt::SquareCap);
289 stroker.setMiterLimit(m_linePen.miterLimit());
294 stroker.setMiterLimit(m_linePen.miterLimit());
290
295
291 QPainterPath checkShapePath = stroker.createStroke(fullPath);
296 QPainterPath checkShapePath = stroker.createStroke(fullPath);
292
297
293 // Only zoom in if the bounding rects of the paths fit inside int limits. QWidget::update() uses
298 // Only zoom in if the bounding rects of the paths fit inside int limits. QWidget::update() uses
294 // a region that has to be compatible with QRect.
299 // a region that has to be compatible with QRect.
295 if (checkShapePath.boundingRect().height() <= INT_MAX
300 if (checkShapePath.boundingRect().height() <= INT_MAX
296 && checkShapePath.boundingRect().width() <= INT_MAX
301 && checkShapePath.boundingRect().width() <= INT_MAX
297 && linePath.boundingRect().height() <= INT_MAX
302 && linePath.boundingRect().height() <= INT_MAX
298 && linePath.boundingRect().width() <= INT_MAX
303 && linePath.boundingRect().width() <= INT_MAX
299 && fullPath.boundingRect().height() <= INT_MAX
304 && fullPath.boundingRect().height() <= INT_MAX
300 && fullPath.boundingRect().width() <= INT_MAX) {
305 && fullPath.boundingRect().width() <= INT_MAX) {
301 prepareGeometryChange();
306 prepareGeometryChange();
302
307
303 m_linePath = linePath;
308 m_linePath = linePath;
304 m_fullPath = fullPath;
309 m_fullPath = fullPath;
305 m_shapePath = checkShapePath;
310 m_shapePath = checkShapePath;
306
311
307 m_rect = m_shapePath.boundingRect();
312 m_rect = m_shapePath.boundingRect();
308 } else {
313 } else {
309 update();
314 update();
310 }
315 }
311 }
316 }
312
317
313 void LineChartItem::handleUpdated()
318 void LineChartItem::handleUpdated()
314 {
319 {
315 // If points visibility has changed, a geometry update is needed.
320 // If points visibility has changed, a geometry update is needed.
316 // Also, if pen changes when points are visible, geometry update is needed.
321 // Also, if pen changes when points are visible, geometry update is needed.
317 bool doGeometryUpdate =
322 bool doGeometryUpdate =
318 (m_pointsVisible != m_series->pointsVisible())
323 (m_pointsVisible != m_series->pointsVisible())
319 || (m_series->pointsVisible() && (m_linePen != m_series->pen()));
324 || (m_series->pointsVisible() && (m_linePen != m_series->pen()));
320 setVisible(m_series->isVisible());
325 setVisible(m_series->isVisible());
321 setOpacity(m_series->opacity());
326 setOpacity(m_series->opacity());
322 m_pointsVisible = m_series->pointsVisible();
327 m_pointsVisible = m_series->pointsVisible();
323 m_linePen = m_series->pen();
328 m_linePen = m_series->pen();
324 m_pointLabelsFormat = m_series->pointLabelsFormat();
329 m_pointLabelsFormat = m_series->pointLabelsFormat();
325 m_pointLabelsVisible = m_series->pointLabelsVisible();
330 m_pointLabelsVisible = m_series->pointLabelsVisible();
326 m_pointLabelsFont = m_series->pointLabelsFont();
331 m_pointLabelsFont = m_series->pointLabelsFont();
327 m_pointLabelsColor = m_series->pointLabelsColor();
332 m_pointLabelsColor = m_series->pointLabelsColor();
328 if (doGeometryUpdate)
333 if (doGeometryUpdate)
329 updateGeometry();
334 updateGeometry();
330 update();
335 update();
331 }
336 }
332
337
333 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
338 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
334 {
339 {
335 Q_UNUSED(widget)
340 Q_UNUSED(widget)
336 Q_UNUSED(option)
341 Q_UNUSED(option)
337
342
338 QRectF clipRect = QRectF(QPointF(0, 0), domain()->size());
343 QRectF clipRect = QRectF(QPointF(0, 0), domain()->size());
339
344
340 painter->save();
345 painter->save();
341 painter->setPen(m_linePen);
346 painter->setPen(m_linePen);
342 bool alwaysUsePath = false;
347 bool alwaysUsePath = false;
343
348
344 if (m_series->chart()->chartType() == QChart::ChartTypePolar) {
349 if (m_series->chart()->chartType() == QChart::ChartTypePolar) {
345 qreal halfWidth = domain()->size().width() / 2.0;
350 qreal halfWidth = domain()->size().width() / 2.0;
346 QRectF clipRectLeft = QRectF(0, 0, halfWidth, domain()->size().height());
351 QRectF clipRectLeft = QRectF(0, 0, halfWidth, domain()->size().height());
347 QRectF clipRectRight = QRectF(halfWidth, 0, halfWidth, domain()->size().height());
352 QRectF clipRectRight = QRectF(halfWidth, 0, halfWidth, domain()->size().height());
348 QRegion fullPolarClipRegion(clipRect.toRect(), QRegion::Ellipse);
353 QRegion fullPolarClipRegion(clipRect.toRect(), QRegion::Ellipse);
349 QRegion clipRegionLeft(fullPolarClipRegion.intersected(clipRectLeft.toRect()));
354 QRegion clipRegionLeft(fullPolarClipRegion.intersected(clipRectLeft.toRect()));
350 QRegion clipRegionRight(fullPolarClipRegion.intersected(clipRectRight.toRect()));
355 QRegion clipRegionRight(fullPolarClipRegion.intersected(clipRectRight.toRect()));
351 painter->setClipRegion(clipRegionLeft);
356 painter->setClipRegion(clipRegionLeft);
352 painter->drawPath(m_linePathPolarLeft);
357 painter->drawPath(m_linePathPolarLeft);
353 painter->setClipRegion(clipRegionRight);
358 painter->setClipRegion(clipRegionRight);
354 painter->drawPath(m_linePathPolarRight);
359 painter->drawPath(m_linePathPolarRight);
355 painter->setClipRegion(fullPolarClipRegion);
360 painter->setClipRegion(fullPolarClipRegion);
356 alwaysUsePath = true; // required for proper clipping
361 alwaysUsePath = true; // required for proper clipping
357 } else {
362 } else {
358 painter->setClipRect(clipRect);
363 painter->setClipRect(clipRect);
359 }
364 }
360
365
361 if (m_pointsVisible) {
366 if (m_pointsVisible) {
362 painter->setBrush(m_linePen.color());
367 painter->setBrush(m_linePen.color());
363 painter->drawPath(m_linePath);
368 painter->drawPath(m_linePath);
364 } else {
369 } else {
365 painter->setBrush(QBrush(Qt::NoBrush));
370 painter->setBrush(QBrush(Qt::NoBrush));
366 if (m_linePen.style() != Qt::SolidLine || alwaysUsePath) {
371 if (m_linePen.style() != Qt::SolidLine || alwaysUsePath) {
367 // If pen style is not solid line, always fall back to path painting
372 // If pen style is not solid line, always fall back to path painting
368 // to ensure proper continuity of the pattern
373 // to ensure proper continuity of the pattern
369 painter->drawPath(m_linePath);
374 painter->drawPath(m_linePath);
370 } else {
375 } else {
371 for (int i(1); i < m_points.size(); i++)
376 for (int i(1); i < m_points.size(); i++)
372 painter->drawLine(m_points.at(i - 1), m_points.at(i));
377 painter->drawLine(m_points.at(i - 1), m_points.at(i));
373 }
378 }
374 }
379 }
375
380
376 if (m_pointLabelsVisible)
381 if (m_pointLabelsVisible)
377 m_series->d_func()->drawSeriesPointLabels(painter, m_points, m_linePen.width() / 2);
382 m_series->d_func()->drawSeriesPointLabels(painter, m_points, m_linePen.width() / 2);
378
383
379 painter->restore();
384 painter->restore();
380
385
381 }
386 }
382
387
383 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
388 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
384 {
389 {
385 emit XYChart::pressed(domain()->calculateDomainPoint(event->pos()));
390 emit XYChart::pressed(domain()->calculateDomainPoint(event->pos()));
386 m_lastMousePos = event->pos();
391 m_lastMousePos = event->pos();
387 m_mousePressed = true;
392 m_mousePressed = true;
388 QGraphicsItem::mousePressEvent(event);
393 QGraphicsItem::mousePressEvent(event);
389 }
394 }
390
395
391 void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
396 void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
392 {
397 {
393 emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true);
398 emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true);
394 // event->accept();
399 // event->accept();
395 QGraphicsItem::hoverEnterEvent(event);
400 QGraphicsItem::hoverEnterEvent(event);
396 }
401 }
397
402
398 void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
403 void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
399 {
404 {
400 emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false);
405 emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false);
401 // event->accept();
406 // event->accept();
402 QGraphicsItem::hoverEnterEvent(event);
407 QGraphicsItem::hoverEnterEvent(event);
403 }
408 }
404
409
405 void LineChartItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
410 void LineChartItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
406 {
411 {
407 emit XYChart::released(domain()->calculateDomainPoint(m_lastMousePos));
412 emit XYChart::released(domain()->calculateDomainPoint(m_lastMousePos));
408 if (m_mousePressed)
413 if (m_mousePressed)
409 emit XYChart::clicked(domain()->calculateDomainPoint(m_lastMousePos));
414 emit XYChart::clicked(domain()->calculateDomainPoint(m_lastMousePos));
410 m_mousePressed = false;
415 m_mousePressed = false;
411 QGraphicsItem::mouseReleaseEvent(event);
416 QGraphicsItem::mouseReleaseEvent(event);
412 }
417 }
413
418
414 void LineChartItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
419 void LineChartItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
415 {
420 {
416 emit XYChart::doubleClicked(domain()->calculateDomainPoint(m_lastMousePos));
421 emit XYChart::doubleClicked(domain()->calculateDomainPoint(m_lastMousePos));
417 QGraphicsItem::mouseDoubleClickEvent(event);
422 QGraphicsItem::mouseDoubleClickEvent(event);
418 }
423 }
419
424
420 #include "moc_linechartitem_p.cpp"
425 #include "moc_linechartitem_p.cpp"
421
426
422 QT_CHARTS_END_NAMESPACE
427 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now