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