@@ -1,130 +1,134 | |||||
1 | /**************************************************************************** |
|
1 | /**************************************************************************** | |
2 | ** |
|
2 | ** | |
3 | ** Copyright (C) 2012 Digia Plc |
|
3 | ** Copyright (C) 2012 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 <QPainter> |
|
25 | #include <QPainter> | |
26 | #include <QGraphicsSceneMouseEvent> |
|
26 | #include <QGraphicsSceneMouseEvent> | |
27 |
|
27 | |||
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
29 |
|
29 | |||
30 | const qreal mouseEventMinWidth(12); |
|
30 | const qreal mouseEventMinWidth(12); | |
31 |
|
31 | |||
32 | LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter): |
|
32 | LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter): | |
33 | XYChart(series, presenter), |
|
33 | XYChart(series, presenter), | |
34 | QGraphicsItem(presenter ? presenter->rootItem() : 0), |
|
34 | QGraphicsItem(presenter ? presenter->rootItem() : 0), | |
35 | m_series(series), |
|
35 | m_series(series), | |
36 | m_pointsVisible(false) |
|
36 | m_pointsVisible(false) | |
37 | { |
|
37 | { | |
38 | setZValue(ChartPresenter::LineChartZValue); |
|
38 | setZValue(ChartPresenter::LineChartZValue); | |
39 | QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); |
|
39 | QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); | |
40 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); |
|
40 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); | |
41 | handleUpdated(); |
|
41 | handleUpdated(); | |
42 | } |
|
42 | } | |
43 |
|
43 | |||
44 | QRectF LineChartItem::boundingRect() const |
|
44 | QRectF LineChartItem::boundingRect() const | |
45 | { |
|
45 | { | |
46 | return m_rect; |
|
46 | return m_rect; | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | QPainterPath LineChartItem::shape() const |
|
49 | QPainterPath LineChartItem::shape() const | |
50 | { |
|
50 | { | |
51 | return m_path; |
|
51 | return m_path; | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | void LineChartItem::updateGeometry() |
|
54 | void LineChartItem::updateGeometry() | |
55 | { |
|
55 | { | |
56 | m_points = geometryPoints(); |
|
56 | m_points = geometryPoints(); | |
57 |
|
57 | |||
58 | if(m_points.size()==0) |
|
58 | if(m_points.size()==0) | |
59 | { |
|
59 | { | |
60 | prepareGeometryChange(); |
|
60 | prepareGeometryChange(); | |
61 | m_path = QPainterPath(); |
|
61 | m_path = QPainterPath(); | |
62 | m_rect = QRect(); |
|
62 | m_rect = QRect(); | |
63 | return; |
|
63 | return; | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | QPainterPath linePath(m_points.at(0)); |
|
66 | QPainterPath linePath(m_points.at(0)); | |
67 |
|
67 | |||
68 | if(m_pointsVisible) { |
|
68 | if(m_pointsVisible) { | |
69 |
|
69 | |||
70 | int size = m_linePen.width(); |
|
70 | int size = m_linePen.width(); | |
71 | linePath.addEllipse(m_points.at(0),size,size); |
|
71 | linePath.addEllipse(m_points.at(0),size,size); | |
72 | for(int i=1; i< m_points.size();i++) { |
|
72 | for(int i=1; i< m_points.size();i++) { | |
73 | linePath.lineTo(m_points.at(i)); |
|
73 | linePath.lineTo(m_points.at(i)); | |
74 | linePath.addEllipse(m_points.at(i),size,size); |
|
74 | linePath.addEllipse(m_points.at(i),size,size); | |
75 | } |
|
75 | } | |
76 |
|
76 | |||
77 | } else { |
|
77 | } else { | |
78 |
|
78 | |||
79 | for(int i=1; i< m_points.size();i++) { |
|
79 | for(int i=1; i< m_points.size();i++) { | |
80 | linePath.lineTo(m_points.at(i)); |
|
80 | linePath.lineTo(m_points.at(i)); | |
81 | } |
|
81 | } | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | m_linePath=linePath; |
|
84 | m_linePath=linePath; | |
85 | QPainterPathStroker stroker; |
|
85 | QPainterPathStroker stroker; | |
86 | stroker.setWidth(m_linePen.width()); |
|
86 | stroker.setWidth(m_linePen.width()*1.42); | |
|
87 | stroker.setJoinStyle(m_linePen.joinStyle()); | |||
|
88 | stroker.setCapStyle(m_linePen.capStyle()); | |||
|
89 | stroker.setMiterLimit(m_linePen.miterLimit()); | |||
87 |
|
90 | |||
88 | prepareGeometryChange(); |
|
91 | prepareGeometryChange(); | |
89 |
|
92 | |||
90 | m_path = stroker.createStroke(linePath); |
|
93 | m_path = stroker.createStroke(linePath); | |
91 | m_rect = m_path.boundingRect(); |
|
94 | m_rect = m_path.boundingRect(); | |
92 |
|
95 | |||
93 | setPos(origin()); |
|
96 | setPos(origin()); | |
94 | } |
|
97 | } | |
95 |
|
98 | |||
96 | void LineChartItem::handleUpdated() |
|
99 | void LineChartItem::handleUpdated() | |
97 | { |
|
100 | { | |
98 | setVisible(m_series->isVisible()); |
|
101 | setVisible(m_series->isVisible()); | |
99 | m_pointsVisible = m_series->pointsVisible(); |
|
102 | m_pointsVisible = m_series->pointsVisible(); | |
100 | m_linePen = m_series->pen(); |
|
103 | m_linePen = m_series->pen(); | |
101 | update(); |
|
104 | update(); | |
102 | } |
|
105 | } | |
103 |
|
106 | |||
104 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
107 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
105 | { |
|
108 | { | |
106 | Q_UNUSED(widget) |
|
109 | Q_UNUSED(widget) | |
107 | Q_UNUSED(option) |
|
110 | Q_UNUSED(option) | |
108 |
|
111 | |||
109 | painter->setPen(m_linePen); |
|
112 | painter->setPen(m_linePen); | |
|
113 | ||||
110 | painter->setBrush(m_linePen.color()); |
|
114 | painter->setBrush(m_linePen.color()); | |
111 | painter->setClipRect(clipRect()); |
|
115 | painter->setClipRect(clipRect()); | |
112 |
|
116 | |||
113 | if (m_pointsVisible) { |
|
117 | if (m_pointsVisible) { | |
114 | painter->drawPath(m_linePath); |
|
118 | painter->drawPath(m_linePath); | |
115 | } |
|
119 | } | |
116 | else { |
|
120 | else { | |
117 | for (int i(1); i < m_points.size();i++) |
|
121 | for (int i(1); i < m_points.size();i++) | |
118 | painter->drawLine(m_points.at(i-1), m_points.at(i)); |
|
122 | painter->drawLine(m_points.at(i-1), m_points.at(i)); | |
119 | } |
|
123 | } | |
120 | } |
|
124 | } | |
121 |
|
125 | |||
122 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
126 | void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | |
123 | { |
|
127 | { | |
124 | emit XYChart::clicked(calculateDomainPoint(event->pos())); |
|
128 | emit XYChart::clicked(calculateDomainPoint(event->pos())); | |
125 | QGraphicsItem::mousePressEvent(event); |
|
129 | QGraphicsItem::mousePressEvent(event); | |
126 | } |
|
130 | } | |
127 |
|
131 | |||
128 | #include "moc_linechartitem_p.cpp" |
|
132 | #include "moc_linechartitem_p.cpp" | |
129 |
|
133 | |||
130 | QTCOMMERCIALCHART_END_NAMESPACE |
|
134 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now