@@ -48,20 +48,14 QRectF LineChartItem::boundingRect() const | |||||
48 |
|
48 | |||
49 | QPainterPath LineChartItem::shape() const |
|
49 | QPainterPath LineChartItem::shape() const | |
50 | { |
|
50 | { | |
51 | // Increase the size of the path slightly to make mouse interactions more natural |
|
51 | return m_path; | |
52 | QPainterPathStroker s; |
|
|||
53 | s.setCapStyle(Qt::RoundCap); |
|
|||
54 | s.setJoinStyle(Qt::RoundJoin); |
|
|||
55 | qreal spacing = qMax(mouseEventMinWidth, (qreal) m_linePen.width()); |
|
|||
56 | s.setWidth(spacing); |
|
|||
57 | return s.createStroke(m_path); |
|
|||
58 | } |
|
52 | } | |
59 |
|
53 | |||
60 | void LineChartItem::updateGeometry() |
|
54 | void LineChartItem::updateGeometry() | |
61 | { |
|
55 | { | |
62 |
|
|
56 | m_points = geometryPoints(); | |
63 |
|
57 | |||
64 | if(points.size()==0) |
|
58 | if(m_points.size()==0) | |
65 | { |
|
59 | { | |
66 | prepareGeometryChange(); |
|
60 | prepareGeometryChange(); | |
67 | m_path = QPainterPath(); |
|
61 | m_path = QPainterPath(); | |
@@ -69,24 +63,32 void LineChartItem::updateGeometry() | |||||
69 | return; |
|
63 | return; | |
70 | } |
|
64 | } | |
71 |
|
65 | |||
72 | QPainterPath linePath(points.at(0)); |
|
66 | QPainterPath linePath(m_points.at(0)); | |
73 |
|
67 | |||
74 | for(int i=1; i< points.size();i++) { |
|
68 | if(m_pointsVisible) { | |
75 | linePath.lineTo(points.at(i)); |
|
69 | ||
|
70 | int size = m_linePen.width(); | |||
|
71 | linePath.addEllipse(m_points.at(0),size,size); | |||
|
72 | for(int i=1; i< m_points.size();i++) { | |||
|
73 | linePath.lineTo(m_points.at(i)); | |||
|
74 | linePath.addEllipse(m_points.at(i),size,size); | |||
|
75 | } | |||
|
76 | ||||
|
77 | } else { | |||
|
78 | ||||
|
79 | for(int i=1; i< m_points.size();i++) { | |||
|
80 | linePath.lineTo(m_points.at(i)); | |||
|
81 | } | |||
76 | } |
|
82 | } | |
77 |
|
83 | |||
78 | prepareGeometryChange(); |
|
84 | m_linePath=linePath; | |
|
85 | QPainterPathStroker stroker; | |||
|
86 | stroker.setWidth(m_linePen.width()); | |||
79 |
|
87 | |||
80 | m_path = linePath; |
|
88 | prepareGeometryChange(); | |
81 |
|
89 | |||
82 | // When defining bounding rectangle, |
|
90 | m_path = stroker.createStroke(linePath); | |
83 | // 1. take the line width into account (otherwise you will get drawing artifacts) and |
|
91 | m_rect = m_path.boundingRect(); | |
84 | // 2. take the shape into account (otherwise you will not get mouse events through on border |
|
|||
85 | // areas). |
|
|||
86 | const qreal sqrtOf2 = 1.414214; |
|
|||
87 | const qreal spacing = qMax(mouseEventMinWidth / 2.0, |
|
|||
88 | sqrtOf2 * (qreal) m_linePen.width() / 2.0); |
|
|||
89 | m_rect = m_path.boundingRect().adjusted(-spacing, -spacing, spacing, spacing); |
|
|||
90 |
|
92 | |||
91 | setPos(origin()); |
|
93 | setPos(origin()); | |
92 | } |
|
94 | } | |
@@ -96,8 +98,6 void LineChartItem::handleUpdated() | |||||
96 | setVisible(m_series->isVisible()); |
|
98 | setVisible(m_series->isVisible()); | |
97 | m_pointsVisible = m_series->pointsVisible(); |
|
99 | m_pointsVisible = m_series->pointsVisible(); | |
98 | m_linePen = m_series->pen(); |
|
100 | m_linePen = m_series->pen(); | |
99 | m_pointPen = m_series->pen(); |
|
|||
100 | m_pointPen.setWidthF(2*m_pointPen.width()); |
|
|||
101 | update(); |
|
101 | update(); | |
102 | } |
|
102 | } | |
103 |
|
103 | |||
@@ -106,19 +106,17 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt | |||||
106 | Q_UNUSED(widget) |
|
106 | Q_UNUSED(widget) | |
107 | Q_UNUSED(option) |
|
107 | Q_UNUSED(option) | |
108 |
|
108 | |||
109 | painter->save(); |
|
|||
110 | painter->setPen(m_linePen); |
|
109 | painter->setPen(m_linePen); | |
|
110 | painter->setBrush(m_linePen.color()); | |||
111 | painter->setClipRect(clipRect()); |
|
111 | painter->setClipRect(clipRect()); | |
112 | // Draw lines |
|
112 | ||
113 | const QVector<QPointF> &points = geometryPoints(); |
|
113 | if (m_pointsVisible) { | |
114 | for (int i(1); i < points.size();i++) |
|
114 | painter->drawPath(m_linePath); | |
115 | painter->drawLine(points.at(i-1), points.at(i)); |
|
115 | } | |
116 | // Draw points |
|
116 | else { | |
117 | if (m_pointsVisible){ |
|
117 | for (int i(1); i < m_points.size();i++) | |
118 |
painter-> |
|
118 | painter->drawLine(m_points.at(i-1), m_points.at(i)); | |
119 | painter->drawPoints(geometryPoints()); |
|
|||
120 | } |
|
119 | } | |
121 | painter->restore(); |
|
|||
122 | } |
|
120 | } | |
123 |
|
121 | |||
124 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
122 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
@@ -64,9 +64,10 protected: | |||||
64 | private: |
|
64 | private: | |
65 | QLineSeries* m_series; |
|
65 | QLineSeries* m_series; | |
66 | QPainterPath m_path; |
|
66 | QPainterPath m_path; | |
|
67 | QPainterPath m_linePath; | |||
|
68 | QVector<QPointF> m_points; | |||
67 | QRectF m_rect; |
|
69 | QRectF m_rect; | |
68 | QPen m_linePen; |
|
70 | QPen m_linePen; | |
69 | QPen m_pointPen; |
|
|||
70 | bool m_pointsVisible; |
|
71 | bool m_pointsVisible; | |
71 | }; |
|
72 | }; | |
72 |
|
73 |
General Comments 0
You need to be logged in to leave comments.
Login now