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