@@ -1,160 +1,168 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2013 Digia Plc |
|
3 | ** Copyright (C) 2013 Digia Plc | |
4 | ** All rights reserved. |
|
4 | ** All rights reserved. | |
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com |
|
5 | ** For any questions to Digia, please use contact form at http://qt.digia.com | |
6 | ** |
|
6 | ** | |
7 | ** This file is part of the Qt Commercial Charts Add-on. |
|
7 | ** This file is part of the Qt Commercial Charts Add-on. | |
8 | ** |
|
8 | ** | |
9 | ** $QT_BEGIN_LICENSE$ |
|
9 | ** $QT_BEGIN_LICENSE$ | |
10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in | |
11 | ** accordance with the Qt Commercial License Agreement provided with the |
|
11 | ** accordance with the Qt Commercial License Agreement provided with the | |
12 | ** Software or, alternatively, in accordance with the terms contained in |
|
12 | ** Software or, alternatively, in accordance with the terms contained in | |
13 | ** a written agreement between you and Digia. |
|
13 | ** a written agreement between you and Digia. | |
14 | ** |
|
14 | ** | |
15 | ** If you have questions regarding the use of this file, please use |
|
15 | ** If you have questions regarding the use of this file, please use | |
16 | ** contact form at http://qt.digia.com |
|
16 | ** contact form at http://qt.digia.com | |
17 | ** $QT_END_LICENSE$ |
|
17 | ** $QT_END_LICENSE$ | |
18 | ** |
|
18 | ** | |
19 | ****************************************************************************/ |
|
19 | ****************************************************************************/ | |
20 |
|
20 | |||
21 | #include "linechartitem_p.h" |
|
21 | #include "linechartitem_p.h" | |
22 | #include "qlineseries.h" |
|
22 | #include "qlineseries.h" | |
23 | #include "qlineseries_p.h" |
|
23 | #include "qlineseries_p.h" | |
24 | #include "chartpresenter_p.h" |
|
24 | #include "chartpresenter_p.h" | |
25 | #include "abstractdomain_p.h" |
|
25 | #include "abstractdomain_p.h" | |
26 | #include <QPainter> |
|
26 | #include <QPainter> | |
27 | #include <QGraphicsSceneMouseEvent> |
|
27 | #include <QGraphicsSceneMouseEvent> | |
28 |
|
28 | |||
29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | QTCOMMERCIALCHART_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 | { |
|
37 | { | |
38 | setAcceptHoverEvents(true); |
|
38 | setAcceptHoverEvents(true); | |
39 | setZValue(ChartPresenter::LineChartZValue); |
|
39 | setZValue(ChartPresenter::LineChartZValue); | |
40 | QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
40 | QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); | |
41 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); |
|
41 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | |
42 | QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); |
|
42 | QObject::connect(series, SIGNAL(opacityChanged()), this, SLOT(handleUpdated())); | |
43 | handleUpdated(); |
|
43 | handleUpdated(); | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 | QRectF LineChartItem::boundingRect() const |
|
46 | QRectF LineChartItem::boundingRect() const | |
47 | { |
|
47 | { | |
48 | return m_rect; |
|
48 | return m_rect; | |
49 | } |
|
49 | } | |
50 |
|
50 | |||
51 | QPainterPath LineChartItem::shape() const |
|
51 | QPainterPath LineChartItem::shape() const | |
52 | { |
|
52 | { | |
53 | return m_path; |
|
53 | return m_path; | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | void LineChartItem::updateGeometry() |
|
56 | void LineChartItem::updateGeometry() | |
57 | { |
|
57 | { | |
58 | m_points = geometryPoints(); |
|
58 | m_points = geometryPoints(); | |
59 |
|
59 | |||
60 | if (m_points.size() == 0) { |
|
60 | if (m_points.size() == 0) { | |
61 | prepareGeometryChange(); |
|
61 | prepareGeometryChange(); | |
62 | m_path = QPainterPath(); |
|
62 | m_path = QPainterPath(); | |
63 | m_linePath = QPainterPath(); |
|
63 | m_linePath = QPainterPath(); | |
64 | m_rect = QRect(); |
|
64 | m_rect = QRect(); | |
65 | return; |
|
65 | return; | |
66 | } |
|
66 | } | |
67 |
|
67 | |||
68 | QPainterPath linePath(m_points.at(0)); |
|
68 | QPainterPath linePath(m_points.at(0)); | |
69 |
|
69 | |||
70 | if (m_pointsVisible) { |
|
70 | if (m_pointsVisible) { | |
71 |
|
71 | |||
72 | int size = m_linePen.width(); |
|
72 | int size = m_linePen.width(); | |
73 | linePath.addEllipse(m_points.at(0), size, size); |
|
73 | linePath.addEllipse(m_points.at(0), size, size); | |
74 | linePath.moveTo(m_points.at(0)); |
|
74 | linePath.moveTo(m_points.at(0)); | |
75 | for (int i = 1; i < m_points.size(); i++) { |
|
75 | for (int i = 1; i < m_points.size(); i++) { | |
76 | linePath.lineTo(m_points.at(i)); |
|
76 | linePath.lineTo(m_points.at(i)); | |
77 | linePath.addEllipse(m_points.at(i), size, size); |
|
77 | linePath.addEllipse(m_points.at(i), size, size); | |
78 | linePath.moveTo(m_points.at(i)); |
|
78 | linePath.moveTo(m_points.at(i)); | |
79 | } |
|
79 | } | |
80 |
|
80 | |||
81 | } else { |
|
81 | } else { | |
82 | for (int i = 1; i < m_points.size(); i++) |
|
82 | for (int i = 1; i < m_points.size(); i++) | |
83 | linePath.lineTo(m_points.at(i)); |
|
83 | linePath.lineTo(m_points.at(i)); | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | m_linePath = linePath; |
|
86 | m_linePath = linePath; | |
87 |
|
87 | |||
88 | QPainterPathStroker stroker; |
|
88 | QPainterPathStroker stroker; | |
89 | // QPainter::drawLine does not respect join styles, for example BevelJoin becomes MiterJoin. |
|
89 | // QPainter::drawLine does not respect join styles, for example BevelJoin becomes MiterJoin. | |
90 | // This is why we are prepared for the "worst case" scenario, i.e. use always MiterJoin and |
|
90 | // This is why we are prepared for the "worst case" scenario, i.e. use always MiterJoin and | |
91 | // multiply line width with square root of two when defining shape and bounding rectangle. |
|
91 | // multiply line width with square root of two when defining shape and bounding rectangle. | |
92 | stroker.setWidth(m_linePen.width() * 1.42); |
|
92 | stroker.setWidth(m_linePen.width() * 1.42); | |
93 | stroker.setJoinStyle(Qt::MiterJoin); |
|
93 | stroker.setJoinStyle(Qt::MiterJoin); | |
94 | stroker.setCapStyle(Qt::SquareCap); |
|
94 | stroker.setCapStyle(Qt::SquareCap); | |
95 | stroker.setMiterLimit(m_linePen.miterLimit()); |
|
95 | stroker.setMiterLimit(m_linePen.miterLimit()); | |
96 |
|
96 | |||
97 | prepareGeometryChange(); |
|
97 | prepareGeometryChange(); | |
98 |
|
98 | |||
99 | m_path = stroker.createStroke(linePath); |
|
99 | m_path = stroker.createStroke(linePath); | |
100 | m_rect = m_path.boundingRect(); |
|
100 | m_rect = m_path.boundingRect(); | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | void LineChartItem::handleUpdated() |
|
103 | void LineChartItem::handleUpdated() | |
104 | { |
|
104 | { | |
105 | // If points visiblity has changed, a geometry update is needed. |
|
105 | // If points visiblity has changed, a geometry update is needed. | |
106 | // Also, if pen changes when points are visible, geometry update is needed. |
|
106 | // Also, if pen changes when points are visible, geometry update is needed. | |
107 | bool doGeometryUpdate = |
|
107 | bool doGeometryUpdate = | |
108 | (m_pointsVisible != m_series->pointsVisible()) |
|
108 | (m_pointsVisible != m_series->pointsVisible()) | |
109 | || (m_series->pointsVisible() && (m_linePen != m_series->pen())); |
|
109 | || (m_series->pointsVisible() && (m_linePen != m_series->pen())); | |
110 | setVisible(m_series->isVisible()); |
|
110 | setVisible(m_series->isVisible()); | |
111 | setOpacity(m_series->opacity()); |
|
111 | setOpacity(m_series->opacity()); | |
112 | m_pointsVisible = m_series->pointsVisible(); |
|
112 | m_pointsVisible = m_series->pointsVisible(); | |
113 | m_linePen = m_series->pen(); |
|
113 | m_linePen = m_series->pen(); | |
114 | if (doGeometryUpdate) |
|
114 | if (doGeometryUpdate) | |
115 | updateGeometry(); |
|
115 | updateGeometry(); | |
116 | update(); |
|
116 | update(); | |
117 | } |
|
117 | } | |
118 |
|
118 | |||
119 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
119 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
120 | { |
|
120 | { | |
121 | Q_UNUSED(widget) |
|
121 | Q_UNUSED(widget) | |
122 | Q_UNUSED(option) |
|
122 | Q_UNUSED(option) | |
123 |
|
123 | |||
124 | painter->save(); |
|
124 | painter->save(); | |
125 | painter->setPen(m_linePen); |
|
125 | painter->setPen(m_linePen); | |
126 | painter->setBrush(m_linePen.color()); |
|
|||
127 | painter->setClipRect(QRectF(QPointF(0,0),domain()->size())); |
|
126 | painter->setClipRect(QRectF(QPointF(0,0),domain()->size())); | |
128 |
|
127 | |||
129 | if (m_pointsVisible) { |
|
128 | if (m_pointsVisible) { | |
|
129 | painter->setBrush(m_linePen.color()); | |||
|
130 | painter->drawPath(m_linePath); | |||
|
131 | } else { | |||
|
132 | painter->setBrush(QBrush(Qt::NoBrush)); | |||
|
133 | if (m_linePen.style() != Qt::SolidLine) { | |||
|
134 | // If pen style is not solid line, always fall back to path painting | |||
|
135 | // to ensure proper continuity of the pattern | |||
130 | painter->drawPath(m_linePath); |
|
136 | painter->drawPath(m_linePath); | |
131 | } else { |
|
137 | } else { | |
132 | for (int i(1); i < m_points.size(); i++) |
|
138 | for (int i(1); i < m_points.size(); i++) | |
133 | painter->drawLine(m_points.at(i - 1), m_points.at(i)); |
|
139 | painter->drawLine(m_points.at(i - 1), m_points.at(i)); | |
134 | } |
|
140 | } | |
|
141 | } | |||
|
142 | ||||
135 | painter->restore(); |
|
143 | painter->restore(); | |
136 | } |
|
144 | } | |
137 |
|
145 | |||
138 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
146 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | |
139 | { |
|
147 | { | |
140 | emit XYChart::clicked(domain()->calculateDomainPoint(event->pos())); |
|
148 | emit XYChart::clicked(domain()->calculateDomainPoint(event->pos())); | |
141 | QGraphicsItem::mousePressEvent(event); |
|
149 | QGraphicsItem::mousePressEvent(event); | |
142 | } |
|
150 | } | |
143 |
|
151 | |||
144 | void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) |
|
152 | void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | |
145 | { |
|
153 | { | |
146 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true); |
|
154 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), true); | |
147 | // event->accept(); |
|
155 | // event->accept(); | |
148 | QGraphicsItem::hoverEnterEvent(event); |
|
156 | QGraphicsItem::hoverEnterEvent(event); | |
149 | } |
|
157 | } | |
150 |
|
158 | |||
151 | void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) |
|
159 | void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | |
152 | { |
|
160 | { | |
153 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false); |
|
161 | emit XYChart::hovered(domain()->calculateDomainPoint(event->pos()), false); | |
154 | // event->accept(); |
|
162 | // event->accept(); | |
155 | QGraphicsItem::hoverEnterEvent(event); |
|
163 | QGraphicsItem::hoverEnterEvent(event); | |
156 | } |
|
164 | } | |
157 |
|
165 | |||
158 | #include "moc_linechartitem_p.cpp" |
|
166 | #include "moc_linechartitem_p.cpp" | |
159 |
|
167 | |||
160 | QTCOMMERCIALCHART_END_NAMESPACE |
|
168 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now