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