@@ -1,134 +1,140 | |||
|
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 "splinechartitem_p.h" |
|
22 | 22 | #include "qsplineseries_p.h" |
|
23 | 23 | #include "chartpresenter_p.h" |
|
24 | 24 | #include "chartanimator_p.h" |
|
25 | 25 | #include <QPainter> |
|
26 | 26 | |
|
27 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | 28 | |
|
29 | 29 | SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) : |
|
30 | 30 | XYChartItem(series, presenter), |
|
31 | 31 | m_series(series), |
|
32 | 32 | m_pointsVisible(false) |
|
33 | 33 | { |
|
34 | 34 | setZValue(ChartPresenter::LineChartZValue); |
|
35 | 35 | QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated())); |
|
36 | 36 | handleUpdated(); |
|
37 | 37 | } |
|
38 | 38 | |
|
39 | 39 | QRectF SplineChartItem::boundingRect() const |
|
40 | 40 | { |
|
41 | 41 | return m_rect; |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | QPainterPath SplineChartItem::shape() const |
|
45 | 45 | { |
|
46 | 46 | return m_path; |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | void SplineChartItem::updateLayout(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index) |
|
50 | 50 | { |
|
51 | 51 | QVector<QPointF> controlPoints; |
|
52 | 52 | |
|
53 | 53 | controlPoints.resize(newPoints.count()*2-2); |
|
54 | 54 | |
|
55 | 55 | for (int i = 0; i < newPoints.size() - 1; i++) { |
|
56 | 56 | controlPoints[2*i] = calculateGeometryControlPoint(2 * i); |
|
57 | 57 | controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1); |
|
58 | 58 | } |
|
59 | 59 | |
|
60 | 60 | if (controlPoints.count()<2) { |
|
61 | 61 | setLayout(newPoints,controlPoints); |
|
62 | 62 | return; |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | if (animator()) { |
|
66 | 66 | animator()->updateLayout(this,oldPoints,newPoints,m_controlPoints,controlPoints,index); |
|
67 | 67 | } else { |
|
68 | 68 | setLayout(newPoints,controlPoints); |
|
69 | 69 | } |
|
70 | 70 | } |
|
71 | 71 | |
|
72 | 72 | QPointF SplineChartItem::calculateGeometryControlPoint(int index) const |
|
73 | 73 | { |
|
74 | 74 | return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index)); |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | void SplineChartItem::setLayout(QVector<QPointF> &points) | |
|
78 | { | |
|
79 | // Dummy implementation because of a bug in Clang compiler | |
|
80 | XYChartItem::setLayout(points); | |
|
81 | } | |
|
82 | ||
|
77 | 83 | void SplineChartItem::setLayout(QVector<QPointF> &points, QVector<QPointF> &controlPoints) |
|
78 | 84 | { |
|
79 | 85 | if ((points.size()<2) || (controlPoints.size()<2)) { |
|
80 | 86 | XYChartItem::setLayout(points); |
|
81 | 87 | m_controlPoints=controlPoints; |
|
82 | 88 | return; |
|
83 | 89 | } |
|
84 | 90 | |
|
85 | 91 | Q_ASSERT(points.count()*2-2 == controlPoints.count()); |
|
86 | 92 | |
|
87 | 93 | QPainterPath splinePath(points.at(0)); |
|
88 | 94 | |
|
89 | 95 | for (int i = 0; i < points.size() - 1; i++) { |
|
90 | 96 | const QPointF& point = points.at(i + 1); |
|
91 | 97 | splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point); |
|
92 | 98 | } |
|
93 | 99 | |
|
94 | 100 | prepareGeometryChange(); |
|
95 | 101 | m_path = splinePath; |
|
96 | 102 | m_rect = splinePath.boundingRect(); |
|
97 | 103 | XYChartItem::setLayout(points); |
|
98 | 104 | m_controlPoints=controlPoints; |
|
99 | 105 | } |
|
100 | 106 | |
|
101 | 107 | //handlers |
|
102 | 108 | |
|
103 | 109 | void SplineChartItem::handleUpdated() |
|
104 | 110 | { |
|
105 | 111 | m_pointsVisible = m_series->pointsVisible(); |
|
106 | 112 | m_linePen = m_series->pen(); |
|
107 | 113 | m_pointPen = m_series->pen(); |
|
108 | 114 | m_pointPen.setWidthF(2*m_pointPen.width()); |
|
109 | 115 | update(); |
|
110 | 116 | } |
|
111 | 117 | |
|
112 | 118 | //painter |
|
113 | 119 | |
|
114 | 120 | void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
115 | 121 | { |
|
116 | 122 | Q_UNUSED(widget) |
|
117 | 123 | Q_UNUSED(option) |
|
118 | 124 | |
|
119 | 125 | painter->save(); |
|
120 | 126 | painter->setClipRect(clipRect()); |
|
121 | 127 | painter->setPen(m_linePen); |
|
122 | 128 | painter->drawPath(m_path); |
|
123 | 129 | if (m_pointsVisible) { |
|
124 | 130 | painter->setPen(m_pointPen); |
|
125 | 131 | painter->drawPoints(points()); |
|
126 | 132 | } |
|
127 | 133 | painter->restore(); |
|
128 | 134 | } |
|
129 | 135 | |
|
130 | 136 | |
|
131 | 137 | |
|
132 | 138 | #include "moc_splinechartitem_p.cpp" |
|
133 | 139 | |
|
134 | 140 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,64 +1,65 | |||
|
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 | #ifndef SPLINECHARTITEM_P_H |
|
22 | 22 | #define SPLINECHARTITEM_P_H |
|
23 | 23 | |
|
24 | 24 | #include "qsplineseries.h" |
|
25 | 25 | #include "xychartitem_p.h" |
|
26 | 26 | |
|
27 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | 28 | |
|
29 | 29 | class SplineChartItem : public XYChartItem |
|
30 | 30 | { |
|
31 | 31 | Q_OBJECT |
|
32 | 32 | public: |
|
33 | 33 | SplineChartItem(QSplineSeries *series, ChartPresenter *presenter); |
|
34 | 34 | |
|
35 | 35 | //from QGraphicsItem |
|
36 | 36 | QRectF boundingRect() const; |
|
37 | 37 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
38 | 38 | QPainterPath shape() const; |
|
39 | 39 | |
|
40 | 40 | public Q_SLOTS: |
|
41 | 41 | void handleUpdated(); |
|
42 | 42 | |
|
43 | 43 | protected: |
|
44 | void setLayout(QVector<QPointF> &points); | |
|
44 | 45 | void setLayout(QVector<QPointF> &points,QVector<QPointF> &controlPoints); |
|
45 | 46 | void updateLayout(QVector<QPointF> &oldPoints,QVector<QPointF> &newPoints,int index); |
|
46 | 47 | |
|
47 | 48 | private: |
|
48 | 49 | QPointF calculateGeometryControlPoint(int index) const; |
|
49 | 50 | |
|
50 | 51 | private: |
|
51 | 52 | QSplineSeries* m_series; |
|
52 | 53 | QPainterPath m_path; |
|
53 | 54 | QRectF m_rect; |
|
54 | 55 | QPen m_linePen; |
|
55 | 56 | QPen m_pointPen; |
|
56 | 57 | bool m_pointsVisible; |
|
57 | 58 | QVector<QPointF> m_controlPoints; |
|
58 | 59 | |
|
59 | 60 | friend class SplineAnimation; |
|
60 | 61 | }; |
|
61 | 62 | |
|
62 | 63 | QTCOMMERCIALCHART_END_NAMESPACE |
|
63 | 64 | |
|
64 | 65 | #endif // SPLINECHARTITEM_P_H |
General Comments 0
You need to be logged in to leave comments.
Login now