##// END OF EJS Templates
Refactor xychartitem -> xychart
Michal Klocek -
r1218:b8de085c4198
parent child
Show More
@@ -1,211 +1,211
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 "chartanimator_p.h"
21 #include "chartanimator_p.h"
22 #include "axisanimation_p.h"
22 #include "axisanimation_p.h"
23 #include "xyanimation_p.h"
23 #include "xyanimation_p.h"
24 #include "splineanimation_p.h"
24 #include "splineanimation_p.h"
25 #include "xychartitem_p.h"
25 #include "xychart_p.h"
26 #include "pieanimation_p.h"
26 #include "pieanimation_p.h"
27 #include "baranimation_p.h"
27 #include "baranimation_p.h"
28 #include "barchartitem_p.h"
28 #include "barchartitem_p.h"
29 #include "areachartitem_p.h"
29 #include "areachartitem_p.h"
30 #include "splinechartitem_p.h"
30 #include "splinechartitem_p.h"
31 #include "scatterchartitem_p.h"
31 #include "scatterchartitem_p.h"
32 #include <QTimer>
32 #include <QTimer>
33
33
34 Q_DECLARE_METATYPE(QVector<QPointF>)
34 Q_DECLARE_METATYPE(QVector<QPointF>)
35 Q_DECLARE_METATYPE(QVector<qreal>)
35 Q_DECLARE_METATYPE(QVector<qreal>)
36 Q_DECLARE_METATYPE(QVector<QRectF>)
36 Q_DECLARE_METATYPE(QVector<QRectF>)
37
37
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39
39
40 ChartAnimator::ChartAnimator(QObject *parent):QObject(parent),
40 ChartAnimator::ChartAnimator(QObject *parent):QObject(parent),
41 m_state(ShowState)
41 m_state(ShowState)
42 {
42 {
43 }
43 }
44
44
45 ChartAnimator::~ChartAnimator()
45 ChartAnimator::~ChartAnimator()
46 {
46 {
47 }
47 }
48
48
49 void ChartAnimator::addAnimation(ChartAxis *item)
49 void ChartAnimator::addAnimation(ChartAxis *item)
50 {
50 {
51 ChartAnimation *animation = m_animations.value(item);
51 ChartAnimation *animation = m_animations.value(item);
52
52
53 if (!animation) {
53 if (!animation) {
54 animation = new AxisAnimation(item);
54 animation = new AxisAnimation(item);
55 m_animations.insert(item, animation);
55 m_animations.insert(item, animation);
56 }
56 }
57
57
58 item->setAnimator(this);
58 item->setAnimator(this);
59 }
59 }
60
60
61 void ChartAnimator::addAnimation(PieChartItem *item)
61 void ChartAnimator::addAnimation(PieChartItem *item)
62 {
62 {
63 ChartAnimation *animation = m_animations.value(item);
63 ChartAnimation *animation = m_animations.value(item);
64
64
65 if (!animation) {
65 if (!animation) {
66 animation = new PieAnimation(item);
66 animation = new PieAnimation(item);
67 m_animations.insert(item, animation);
67 m_animations.insert(item, animation);
68 }
68 }
69
69
70 item->setAnimator(this);
70 item->setAnimator(this);
71 }
71 }
72
72
73 void ChartAnimator::addAnimation(BarChartItem *item)
73 void ChartAnimator::addAnimation(BarChartItem *item)
74 {
74 {
75 ChartAnimation *animation = m_animations.value(item);
75 ChartAnimation *animation = m_animations.value(item);
76
76
77 if (!animation) {
77 if (!animation) {
78 animation = new BarAnimation(item);
78 animation = new BarAnimation(item);
79 m_animations.insert(item, animation);
79 m_animations.insert(item, animation);
80 }
80 }
81
81
82 item->setAnimator(this);
82 item->setAnimator(this);
83 }
83 }
84
84
85
85
86 void ChartAnimator::removeAnimation(Chart *item)
86 void ChartAnimator::removeAnimation(Chart *item)
87 {
87 {
88 item->setAnimator(0);
88 item->setAnimator(0);
89 m_animations.remove(item);
89 m_animations.remove(item);
90 }
90 }
91
91
92 void ChartAnimator::updateLayout(ChartAxis *item , QVector<qreal> &newLayout)
92 void ChartAnimator::updateLayout(ChartAxis *item , QVector<qreal> &newLayout)
93 {
93 {
94 AxisAnimation *animation = static_cast<AxisAnimation*>(m_animations.value(item));
94 AxisAnimation *animation = static_cast<AxisAnimation*>(m_animations.value(item));
95
95
96 Q_ASSERT(animation);
96 Q_ASSERT(animation);
97
97
98 QVector<qreal> oldLayout = item->layout();
98 QVector<qreal> oldLayout = item->layout();
99
99
100 if (newLayout.count() == 0)
100 if (newLayout.count() == 0)
101 return;
101 return;
102
102
103 switch (m_state) {
103 switch (m_state) {
104 case ZoomOutState: {
104 case ZoomOutState: {
105 QRectF rect = item->geometry();
105 QRectF rect = item->geometry();
106 oldLayout.resize(newLayout.count());
106 oldLayout.resize(newLayout.count());
107
107
108 for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
108 for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) {
109 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom();
109 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom();
110 oldLayout[j] = item->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top();
110 oldLayout[j] = item->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top();
111 }
111 }
112 }
112 }
113 break;
113 break;
114 case ZoomInState: {
114 case ZoomInState: {
115 int index = qMin(oldLayout.count() * (item->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - 1.0);
115 int index = qMin(oldLayout.count() * (item->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - 1.0);
116 oldLayout.resize(newLayout.count());
116 oldLayout.resize(newLayout.count());
117
117
118 for(int i = 0; i < oldLayout.count(); i++)
118 for(int i = 0; i < oldLayout.count(); i++)
119 oldLayout[i]= oldLayout[index];
119 oldLayout[i]= oldLayout[index];
120 }
120 }
121 break;
121 break;
122 case ScrollDownState:
122 case ScrollDownState:
123 case ScrollRightState: {
123 case ScrollRightState: {
124 oldLayout.resize(newLayout.count());
124 oldLayout.resize(newLayout.count());
125
125
126 for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
126 for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j)
127 oldLayout[i]= oldLayout[j];
127 oldLayout[i]= oldLayout[j];
128 }
128 }
129 break;
129 break;
130 case ScrollUpState:
130 case ScrollUpState:
131 case ScrollLeftState: {
131 case ScrollLeftState: {
132 oldLayout.resize(newLayout.count());
132 oldLayout.resize(newLayout.count());
133
133
134 for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
134 for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j)
135 oldLayout[i]= oldLayout[j];
135 oldLayout[i]= oldLayout[j];
136 }
136 }
137 break;
137 break;
138 default: {
138 default: {
139 oldLayout.resize(newLayout.count());
139 oldLayout.resize(newLayout.count());
140 QRectF rect = item->geometry();
140 QRectF rect = item->geometry();
141 for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
141 for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j)
142 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top();
142 oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top();
143 }
143 }
144 break;
144 break;
145 }
145 }
146
146
147
147
148 if (animation->state() != QAbstractAnimation::Stopped)
148 if (animation->state() != QAbstractAnimation::Stopped)
149 animation->stop();
149 animation->stop();
150
150
151 animation->setDuration(ChartAnimationDuration);
151 animation->setDuration(ChartAnimationDuration);
152 animation->setEasingCurve(QEasingCurve::OutQuart);
152 animation->setEasingCurve(QEasingCurve::OutQuart);
153 QVariantAnimation::KeyValues value;
153 QVariantAnimation::KeyValues value;
154 animation->setKeyValues(value); //workaround for wrong interpolation call
154 animation->setKeyValues(value); //workaround for wrong interpolation call
155 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
155 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
156 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
156 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
157
157
158 QTimer::singleShot(0, animation, SLOT(start()));
158 QTimer::singleShot(0, animation, SLOT(start()));
159 }
159 }
160
160
161 void ChartAnimator::addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool startupAnimation)
161 void ChartAnimator::addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool startupAnimation)
162 {
162 {
163 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
163 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
164 Q_ASSERT(animation);
164 Q_ASSERT(animation);
165 animation->addSlice(sliceItem, sliceData, startupAnimation);
165 animation->addSlice(sliceItem, sliceData, startupAnimation);
166 }
166 }
167
167
168 void ChartAnimator::removeAnimation(PieChartItem *item, PieSliceItem *sliceItem)
168 void ChartAnimator::removeAnimation(PieChartItem *item, PieSliceItem *sliceItem)
169 {
169 {
170 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
170 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
171 Q_ASSERT(animation);
171 Q_ASSERT(animation);
172 animation->removeSlice(sliceItem);
172 animation->removeSlice(sliceItem);
173 }
173 }
174
174
175 void ChartAnimator::updateAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData)
175 void ChartAnimator::updateAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData)
176 {
176 {
177 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
177 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
178 Q_ASSERT(animation);
178 Q_ASSERT(animation);
179 animation->updateValue(sliceItem, sliceData);
179 animation->updateValue(sliceItem, sliceData);
180 }
180 }
181
181
182 void ChartAnimator::updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
182 void ChartAnimator::updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
183 {
183 {
184 BarAnimation *animation = static_cast<BarAnimation *>(m_animations.value(item));
184 BarAnimation *animation = static_cast<BarAnimation *>(m_animations.value(item));
185 Q_ASSERT(animation);
185 Q_ASSERT(animation);
186 animation->setDuration(ChartAnimationDuration);
186 animation->setDuration(ChartAnimationDuration);
187 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
187 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
188 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
188 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
189 QTimer::singleShot(0, animation, SLOT(start()));
189 QTimer::singleShot(0, animation, SLOT(start()));
190 }
190 }
191
191
192
192
193 void ChartAnimator::setState(State state, const QPointF &point)
193 void ChartAnimator::setState(State state, const QPointF &point)
194 {
194 {
195 m_state = state;
195 m_state = state;
196 m_point = point;
196 m_point = point;
197 }
197 }
198
198
199 void ChartAnimator::startAnimation(XYAnimation* animation)
199 void ChartAnimator::startAnimation(XYAnimation* animation)
200 {
200 {
201 Q_ASSERT(animation);
201 Q_ASSERT(animation);
202 if (animation->state() != QAbstractAnimation::Stopped)
202 if (animation->state() != QAbstractAnimation::Stopped)
203 animation->stop();
203 animation->stop();
204 animation->setDuration(ChartAnimationDuration);
204 animation->setDuration(ChartAnimationDuration);
205 animation->setEasingCurve(QEasingCurve::OutQuart);
205 animation->setEasingCurve(QEasingCurve::OutQuart);
206 QTimer::singleShot(0, animation, SLOT(start()));
206 QTimer::singleShot(0, animation, SLOT(start()));
207 }
207 }
208
208
209 #include "moc_chartanimator_p.cpp"
209 #include "moc_chartanimator_p.cpp"
210
210
211 QTCOMMERCIALCHART_END_NAMESPACE
211 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,117 +1,117
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 "xyanimation_p.h"
21 #include "xyanimation_p.h"
22 #include "xychartitem_p.h"
22 #include "xychart_p.h"
23 #include <QDebug>
23 #include <QDebug>
24
24
25 Q_DECLARE_METATYPE(QVector<QPointF>)
25 Q_DECLARE_METATYPE(QVector<QPointF>)
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 XYAnimation::XYAnimation(XYChartItem *item):ChartAnimation(item),
29 XYAnimation::XYAnimation(XYChart *item):ChartAnimation(item),
30 m_item(item),
30 m_item(item),
31 m_dirty(false),
31 m_dirty(false),
32 m_type(MoveDownAnimation)
32 m_type(MoveDownAnimation)
33 {
33 {
34 }
34 }
35
35
36 XYAnimation::~XYAnimation()
36 XYAnimation::~XYAnimation()
37 {
37 {
38 }
38 }
39
39
40 void XYAnimation::setAnimationType(Animation type)
40 void XYAnimation::setAnimationType(Animation type)
41 {
41 {
42 m_type=type;
42 m_type=type;
43 }
43 }
44
44
45 void XYAnimation::setValues(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
45 void XYAnimation::setValues(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index)
46 {
46 {
47 int x = oldPoints.count();
47 int x = oldPoints.count();
48 int y = newPoints.count();
48 int y = newPoints.count();
49
49
50 if (x != y && abs(x - y) != 1) {
50 if (x != y && abs(x - y) != 1) {
51 m_oldPoints = newPoints;
51 m_oldPoints = newPoints;
52 oldPoints.resize(newPoints.size());
52 oldPoints.resize(newPoints.size());
53 setKeyValueAt(0.0, qVariantFromValue(oldPoints));
53 setKeyValueAt(0.0, qVariantFromValue(oldPoints));
54 setKeyValueAt(1.0, qVariantFromValue(newPoints));
54 setKeyValueAt(1.0, qVariantFromValue(newPoints));
55 m_dirty = false;
55 m_dirty = false;
56 }
56 }
57 else {
57 else {
58 if (m_dirty) {
58 if (m_dirty) {
59 m_oldPoints = oldPoints;
59 m_oldPoints = oldPoints;
60 m_dirty = false;
60 m_dirty = false;
61 }
61 }
62 oldPoints = newPoints;
62 oldPoints = newPoints;
63 if (y < x)
63 if (y < x)
64 m_oldPoints.remove(index); //remove
64 m_oldPoints.remove(index); //remove
65 if (y > x)
65 if (y > x)
66 m_oldPoints.insert(index, x > 0 ? m_oldPoints[index-1] : newPoints[index]); //add
66 m_oldPoints.insert(index, x > 0 ? m_oldPoints[index-1] : newPoints[index]); //add
67 setKeyValueAt(0.0, qVariantFromValue(m_oldPoints));
67 setKeyValueAt(0.0, qVariantFromValue(m_oldPoints));
68 setKeyValueAt(1.0, qVariantFromValue(newPoints));
68 setKeyValueAt(1.0, qVariantFromValue(newPoints));
69 Q_ASSERT(m_oldPoints.count() == newPoints.count());
69 Q_ASSERT(m_oldPoints.count() == newPoints.count());
70 }
70 }
71 }
71 }
72
72
73 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const
73 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const
74 {
74 {
75 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
75 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
76 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
76 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
77 QVector<QPointF> result;
77 QVector<QPointF> result;
78
78
79 switch (m_type) {
79 switch (m_type) {
80
80
81 case MoveDownAnimation: {
81 case MoveDownAnimation: {
82
82
83 if (startVector.count() != endVector.count())
83 if (startVector.count() != endVector.count())
84 break;
84 break;
85
85
86 for(int i = 0; i < startVector.count(); i++) {
86 for(int i = 0; i < startVector.count(); i++) {
87 qreal x = startVector[i].x() + ((endVector[i].x() - startVector[i].x()) * progress);
87 qreal x = startVector[i].x() + ((endVector[i].x() - startVector[i].x()) * progress);
88 qreal y = startVector[i].y() + ((endVector[i].y() - startVector[i].y()) * progress);
88 qreal y = startVector[i].y() + ((endVector[i].y() - startVector[i].y()) * progress);
89 result << QPointF(x, y);
89 result << QPointF(x, y);
90 }
90 }
91
91
92 }
92 }
93 break;
93 break;
94 case LineDrawAnimation: {
94 case LineDrawAnimation: {
95 for(int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++)
95 for(int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++)
96 result << endVector[i];
96 result << endVector[i];
97 }
97 }
98 break;
98 break;
99 default:
99 default:
100 qWarning() << "Unknown type of animation";
100 qWarning() << "Unknown type of animation";
101 break;
101 break;
102 }
102 }
103
103
104 return qVariantFromValue(result);
104 return qVariantFromValue(result);
105 }
105 }
106
106
107 void XYAnimation::updateCurrentValue (const QVariant &value)
107 void XYAnimation::updateCurrentValue (const QVariant &value)
108 {
108 {
109 if(state()!=QAbstractAnimation::Stopped){ //workaround
109 if(state()!=QAbstractAnimation::Stopped){ //workaround
110 m_dirty = true;
110 m_dirty = true;
111 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
111 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
112 m_item->setGeometryPoints(vector);
112 m_item->setGeometryPoints(vector);
113 m_item->updateGeometry();
113 m_item->updateGeometry();
114 }
114 }
115 }
115 }
116
116
117 QTCOMMERCIALCHART_END_NAMESPACE
117 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,54 +1,54
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 #ifndef XYANIMATION_P_H
21 #ifndef XYANIMATION_P_H
22 #define XYANIMATION_P_H
22 #define XYANIMATION_P_H
23
23
24 #include "chartanimation_p.h"
24 #include "chartanimation_p.h"
25 #include <QPointF>
25 #include <QPointF>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 class XYChartItem;
29 class XYChart;
30
30
31 class XYAnimation : public ChartAnimation
31 class XYAnimation : public ChartAnimation
32 {
32 {
33 public:
33 public:
34 enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation };
34 enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation };
35 XYAnimation(XYChartItem *item);
35 XYAnimation(XYChart *item);
36 ~XYAnimation();
36 ~XYAnimation();
37 void setValues(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index);
37 void setValues(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index);
38 void setAnimationType(Animation type);
38 void setAnimationType(Animation type);
39 Animation animationType() const { return m_type; };
39 Animation animationType() const { return m_type; };
40
40
41 protected:
41 protected:
42 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress ) const;
42 QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress ) const;
43 void updateCurrentValue (const QVariant &value );
43 void updateCurrentValue (const QVariant &value );
44
44
45 private:
45 private:
46 XYChartItem *m_item;
46 XYChart *m_item;
47 QVector<QPointF> m_oldPoints;
47 QVector<QPointF> m_oldPoints;
48 bool m_dirty;
48 bool m_dirty;
49 Animation m_type;
49 Animation m_type;
50 };
50 };
51
51
52 QTCOMMERCIALCHART_END_NAMESPACE
52 QTCOMMERCIALCHART_END_NAMESPACE
53
53
54 #endif
54 #endif
@@ -1,101 +1,109
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
27
27
28
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
30
30 //TODO: optimize : remove points which are not visible
31 //TODO: optimize : remove points which are not visible
31
32
32 LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):XYChartItem(series,presenter),
33 LineChartItem::LineChartItem(QLineSeries* series,ChartPresenter *presenter):XYChart(series,presenter),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
33 m_series(series),
35 m_series(series),
34 m_pointsVisible(false)
36 m_pointsVisible(false)
35 {
37 {
36 setZValue(ChartPresenter::LineChartZValue);
38 setZValue(ChartPresenter::LineChartZValue);
37 QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
39 QObject::connect(series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
38 handleUpdated();
40 handleUpdated();
39 }
41 }
40
42
41 QRectF LineChartItem::boundingRect() const
43 QRectF LineChartItem::boundingRect() const
42 {
44 {
43 return m_rect;
45 return m_rect;
44 }
46 }
45
47
46 QPainterPath LineChartItem::shape() const
48 QPainterPath LineChartItem::shape() const
47 {
49 {
48 return m_path;
50 return m_path;
49 }
51 }
50
52
51 void LineChartItem::updateGeometry()
53 void LineChartItem::updateGeometry()
52 {
54 {
53 const QVector<QPointF>& points = geometryPoints();
55 const QVector<QPointF>& points = geometryPoints();
54
56
55 if(points.size()==0)
57 if(points.size()==0)
56 {
58 {
57 m_path = QPainterPath();
59 m_path = QPainterPath();
58 return;
60 return;
59 }
61 }
60
62
61 QPainterPath linePath(points.at(0));
63 QPainterPath linePath(points.at(0));
62
64
63 for(int i=1; i< points.size();i++) {
65 for(int i=1; i< points.size();i++) {
64 linePath.lineTo(points.at(i));
66 linePath.lineTo(points.at(i));
65 }
67 }
66
68
67 prepareGeometryChange();
69 prepareGeometryChange();
68 m_path = linePath;
70 m_path = linePath;
69 m_rect = linePath.boundingRect();
71 m_rect = linePath.boundingRect();
72 setPos(origin());
70 }
73 }
71
74
72 void LineChartItem::handleUpdated()
75 void LineChartItem::handleUpdated()
73 {
76 {
74 m_pointsVisible = m_series->pointsVisible();
77 m_pointsVisible = m_series->pointsVisible();
75 m_linePen = m_series->pen();
78 m_linePen = m_series->pen();
76 m_pointPen = m_series->pen();
79 m_pointPen = m_series->pen();
77 m_pointPen.setWidthF(2*m_pointPen.width());
80 m_pointPen.setWidthF(2*m_pointPen.width());
78 update();
81 update();
79 }
82 }
80
83
81 //painter
84 //painter
82
85
83 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
86 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
84 {
87 {
85 Q_UNUSED(widget)
88 Q_UNUSED(widget)
86 Q_UNUSED(option)
89 Q_UNUSED(option)
87
90
88 painter->save();
91 painter->save();
89 painter->setPen(m_linePen);
92 painter->setPen(m_linePen);
90 painter->setClipRect(clipRect());
93 painter->setClipRect(clipRect());
91 painter->drawPath(m_path);
94 painter->drawPath(m_path);
92 if(m_pointsVisible){
95 if(m_pointsVisible){
93 painter->setPen(m_pointPen);
96 painter->setPen(m_pointPen);
94 painter->drawPoints(geometryPoints());
97 painter->drawPoints(geometryPoints());
95 }
98 }
96 painter->restore();
99 painter->restore();
97 }
100 }
98
101
102 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
103 {
104 emit XYChart::clicked(calculateDomainPoint(event->pos()));
105 }
106
99 #include "moc_linechartitem_p.cpp"
107 #include "moc_linechartitem_p.cpp"
100
108
101 QTCOMMERCIALCHART_END_NAMESPACE
109 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,62 +1,64
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 #ifndef LINECHARTITEM_H
21 #ifndef LINECHARTITEM_H
22 #define LINECHARTITEM_H
22 #define LINECHARTITEM_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "xychartitem_p.h"
25 #include "xychart_p.h"
26 #include <QPen>
26 #include <QPen>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 class QLineSeries;
30 class QLineSeries;
31 class ChartPresenter;
31 class ChartPresenter;
32
32
33 class LineChartItem : public XYChartItem
33 class LineChartItem : public XYChart , public QGraphicsItem
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_INTERFACES(QGraphicsItem)
36 public:
37 public:
37 explicit LineChartItem(QLineSeries *series,ChartPresenter *presenter);
38 explicit LineChartItem(QLineSeries *series,ChartPresenter *presenter);
38 ~LineChartItem() {};
39 ~LineChartItem() {};
39
40
40 //from QGraphicsItem
41 //from QGraphicsItem
41 QRectF boundingRect() const;
42 QRectF boundingRect() const;
42 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
43 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
43 QPainterPath shape() const;
44 QPainterPath shape() const;
44
45
45 public Q_SLOTS:
46 public Q_SLOTS:
46 void handleUpdated();
47 void handleUpdated();
47 protected:
48 protected:
48 void updateGeometry();
49 void updateGeometry();
50 void mousePressEvent(QGraphicsSceneMouseEvent *event);
49
51
50 private:
52 private:
51 QLineSeries* m_series;
53 QLineSeries* m_series;
52 QPainterPath m_path;
54 QPainterPath m_path;
53 QRectF m_rect;
55 QRectF m_rect;
54 QPen m_linePen;
56 QPen m_linePen;
55 QPen m_pointPen;
57 QPen m_pointPen;
56 bool m_pointsVisible;
58 bool m_pointsVisible;
57
59
58 };
60 };
59
61
60 QTCOMMERCIALCHART_END_NAMESPACE
62 QTCOMMERCIALCHART_END_NAMESPACE
61
63
62 #endif
64 #endif
@@ -1,193 +1,198
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 "scatterchartitem_p.h"
21 #include "scatterchartitem_p.h"
22 #include "qscatterseries.h"
22 #include "qscatterseries.h"
23 #include "qscatterseries_p.h"
23 #include "qscatterseries_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include <QPainter>
25 #include <QPainter>
26 #include <QGraphicsScene>
26 #include <QGraphicsScene>
27 #include <QDebug>
27 #include <QDebug>
28 #include <QGraphicsSceneMouseEvent>
28
29
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
31
31 ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) :
32 ScatterChartItem::ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter) :
32 XYChartItem(series,presenter),
33 XYChart(series,presenter),
34 QGraphicsItem(presenter ? presenter->rootItem() : 0),
33 m_series(series),
35 m_series(series),
34 m_items(this),
36 m_items(this),
35 m_shape(QScatterSeries::MarkerShapeRectangle),
37 m_shape(QScatterSeries::MarkerShapeRectangle),
36 m_size(15)
38 m_size(15)
37
38 {
39 {
39 QObject::connect(m_series->d_func(),SIGNAL(updated()), this, SLOT(handleUpdated()));
40 QObject::connect(m_series->d_func(),SIGNAL(updated()), this, SLOT(handleUpdated()));
40
41
41 setZValue(ChartPresenter::ScatterSeriesZValue);
42 setZValue(ChartPresenter::ScatterSeriesZValue);
42 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
43 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
43
44
44 handleUpdated();
45 handleUpdated();
45
46
46 m_items.setHandlesChildEvents(false);
47 m_items.setHandlesChildEvents(false);
47
48
48 // TODO: how to draw a drop shadow?
49 // TODO: how to draw a drop shadow?
49 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
50 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
50 // dropShadow->setOffset(2.0);
51 // dropShadow->setOffset(2.0);
51 // dropShadow->setBlurRadius(2.0);
52 // dropShadow->setBlurRadius(2.0);
52 // setGraphicsEffect(dropShadow);
53 // setGraphicsEffect(dropShadow);
53 }
54 }
54
55
55
56 QRectF ScatterChartItem::boundingRect() const
56 QRectF ScatterChartItem::boundingRect() const
57 {
57 {
58 return m_rect;
58 return m_rect;
59 }
59 }
60
60
61 void ScatterChartItem::createPoints(int count)
61 void ScatterChartItem::createPoints(int count)
62 {
62 {
63 for (int i = 0; i < count; ++i) {
63 for (int i = 0; i < count; ++i) {
64
64
65 QGraphicsItem *item = 0;
65 QGraphicsItem *item = 0;
66
66
67 switch (m_shape) {
67 switch (m_shape) {
68 case QScatterSeries::MarkerShapeCircle:{
68 case QScatterSeries::MarkerShapeCircle: {
69 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
69 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
70 const QRectF& rect = i->boundingRect();
70 const QRectF& rect = i->boundingRect();
71 i->setPos(-rect.width()/2,-rect.height()/2);
71 i->setPos(-rect.width()/2,-rect.height()/2);
72 item = new Marker(i,this);
72 item = new Marker(i,this);
73 break;
73 break;
74 }
74 }
75 case QScatterSeries::MarkerShapeRectangle:{
75 case QScatterSeries::MarkerShapeRectangle: {
76 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
76 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
77 i->setPos(-m_size/2,-m_size/2);
77 i->setPos(-m_size/2,-m_size/2);
78 item = new Marker(i,this);
78 item = new Marker(i,this);
79 break;
79 break;
80 }
80 }
81 default:
81 default:
82 qWarning()<<"Unsupported marker type";
82 qWarning()<<"Unsupported marker type";
83 break;
83 break;
84
84
85 }
85 }
86 m_items.addToGroup(item);
86 m_items.addToGroup(item);
87 }
87 }
88 }
88 }
89
89
90 void ScatterChartItem::deletePoints(int count)
90 void ScatterChartItem::deletePoints(int count)
91 {
91 {
92 QList<QGraphicsItem *> items = m_items.childItems();
92 QList<QGraphicsItem *> items = m_items.childItems();
93
93
94 for (int i = 0; i < count; ++i) {
94 for (int i = 0; i < count; ++i) {
95 delete(items.takeLast());
95 delete(items.takeLast());
96 }
96 }
97 }
97 }
98
98
99 void ScatterChartItem::markerSelected(Marker *marker)
99 void ScatterChartItem::markerSelected(Marker *marker)
100 {
100 {
101 emit XYChartItem::clicked(marker->point());
101 emit XYChart::clicked(marker->point());
102 }
102 }
103
103
104 void ScatterChartItem::updateGeometry()
104 void ScatterChartItem::updateGeometry()
105 {
105 {
106
106
107 const QVector<QPointF>& points = geometryPoints();
107 const QVector<QPointF>& points = geometryPoints();
108
108
109 if(points.size()==0)
109 if(points.size()==0)
110 {
110 {
111 deletePoints(m_items.childItems().count());
111 deletePoints(m_items.childItems().count());
112 return;
112 return;
113 }
113 }
114
114
115 int diff = m_items.childItems().size() - points.size();
115 int diff = m_items.childItems().size() - points.size();
116
116
117 if(diff>0) {
117 if(diff>0) {
118 deletePoints(diff);
118 deletePoints(diff);
119 }
119 }
120 else if(diff<0) {
120 else if(diff<0) {
121 createPoints(-diff);
121 createPoints(-diff);
122 }
122 }
123
123
124 if(diff!=0) handleUpdated();
124 if(diff!=0) handleUpdated();
125
125
126 QList<QGraphicsItem*> items = m_items.childItems();
126 QList<QGraphicsItem*> items = m_items.childItems();
127
127
128 for (int i = 0; i < points.size(); i++) {
128 for (int i = 0; i < points.size(); i++) {
129 Marker* item = static_cast<Marker*>(items.at(i));
129 Marker* item = static_cast<Marker*>(items.at(i));
130 const QPointF& point = points.at(i);
130 const QPointF& point = points.at(i);
131 const QRectF& rect = item->boundingRect();
131 const QRectF& rect = item->boundingRect();
132 item->setPoint(point);
132 item->setPoint(point);
133 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
133 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
134 if(!clipRect().contains(point)) {
134 if(!clipRect().contains(point)) {
135 item->setVisible(false);
135 item->setVisible(false);
136 }
136 }
137 else {
137 else {
138 item->setVisible(true);
138 item->setVisible(true);
139 }
139 }
140 }
140 }
141
141
142 prepareGeometryChange();
142 prepareGeometryChange();
143 m_rect = clipRect();
143 m_rect = clipRect();
144 setPos(origin());
144 }
145 }
145
146
146
147 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
147 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
148 {
148 {
149 Q_UNUSED(painter)
149 Q_UNUSED(painter)
150 Q_UNUSED(option)
150 Q_UNUSED(option)
151 Q_UNUSED(widget)
151 Q_UNUSED(widget)
152 }
152 }
153
153
154 void ScatterChartItem::setPen(const QPen& pen)
154 void ScatterChartItem::setPen(const QPen& pen)
155 {
155 {
156 foreach(QGraphicsItem* item , m_items.childItems()) {
156 foreach(QGraphicsItem* item , m_items.childItems()) {
157 static_cast<Marker*>(item)->setPen(pen);
157 static_cast<Marker*>(item)->setPen(pen);
158 }
158 }
159 }
159 }
160
160
161 void ScatterChartItem::setBrush(const QBrush& brush)
161 void ScatterChartItem::setBrush(const QBrush& brush)
162 {
162 {
163 foreach(QGraphicsItem* item , m_items.childItems()) {
163 foreach(QGraphicsItem* item , m_items.childItems()) {
164 static_cast<Marker*>(item)->setBrush(brush);
164 static_cast<Marker*>(item)->setBrush(brush);
165 }
165 }
166 }
166 }
167
167
168 void ScatterChartItem::handleUpdated()
168 void ScatterChartItem::handleUpdated()
169 {
169 {
170
170
171 int count = m_items.childItems().count();
171 int count = m_items.childItems().count();
172
172
173 if(count==0) return;
173 if(count==0) return;
174
174
175 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
175 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
176
176
177 //TODO: only rewrite on size change
177 //TODO: only rewrite on size change
178
178
179 m_size = m_series->size();
179 m_size = m_series->size();
180 m_shape = m_series->shape();
180 m_shape = m_series->shape();
181
181
182 if(recreate){
182 if(recreate) {
183 deletePoints(count);
183 deletePoints(count);
184 createPoints(count);
184 createPoints(count);
185 }
185 }
186
186
187 setPen(m_series->pen());
187 setPen(m_series->pen());
188 setBrush(m_series->brush());
188 setBrush(m_series->brush());
189 }
189 }
190
190
191 void ScatterChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
192 {
193 emit XYChart::clicked(calculateDomainPoint(event->pos()));
194 }
195
191 #include "moc_scatterchartitem_p.cpp"
196 #include "moc_scatterchartitem_p.cpp"
192
197
193 QTCOMMERCIALCHART_END_NAMESPACE
198 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,140 +1,141
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 #ifndef SCATTERCHARTITEM_H
21 #ifndef SCATTERCHARTITEM_H
22 #define SCATTERCHARTITEM_H
22 #define SCATTERCHARTITEM_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "xychartitem_p.h"
25 #include "xychart_p.h"
26 #include <QGraphicsEllipseItem>
26 #include <QGraphicsEllipseItem>
27 #include <QPen>
27 #include <QPen>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class QScatterSeries;
31 class QScatterSeries;
32 class Marker;
32 class Marker;
33
33
34 class ScatterChartItem : public XYChartItem
34 class ScatterChartItem : public XYChart, public QGraphicsItem
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37 public:
37 public:
38 explicit ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter);
38 explicit ScatterChartItem(QScatterSeries *series, ChartPresenter *presenter);
39
39
40 public:
40 public:
41 //from QGraphicsItem
41 //from QGraphicsItem
42 QRectF boundingRect() const;
42 QRectF boundingRect() const;
43 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
43 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
44
44
45 void setPen(const QPen &pen);
45 void setPen(const QPen &pen);
46 void setBrush(const QBrush &brush);
46 void setBrush(const QBrush &brush);
47
47
48 void markerSelected(Marker *item);
48 void markerSelected(Marker *item);
49
49
50 public Q_SLOTS:
50 public Q_SLOTS:
51 void handleUpdated();
51 void handleUpdated();
52
52
53 private:
53 private:
54 void createPoints(int count);
54 void createPoints(int count);
55 void deletePoints(int count);
55 void deletePoints(int count);
56
56
57 protected:
57 protected:
58 void updateGeometry();
58 void updateGeometry();
59 void mousePressEvent(QGraphicsSceneMouseEvent *event);
59
60
60 private:
61 private:
61 QScatterSeries *m_series;
62 QScatterSeries *m_series;
62 QGraphicsItemGroup m_items;
63 QGraphicsItemGroup m_items;
63 int m_shape;
64 int m_shape;
64 int m_size;
65 int m_size;
65 QRectF m_rect;
66 QRectF m_rect;
66
67
67 };
68 };
68
69
69
70
70 class Marker: public QAbstractGraphicsShapeItem
71 class Marker: public QAbstractGraphicsShapeItem
71 {
72 {
72
73
73 public:
74 public:
74
75
75 Marker(QAbstractGraphicsShapeItem *item , ScatterChartItem *parent) : QAbstractGraphicsShapeItem(0) ,m_item(item), m_parent(parent)
76 Marker(QAbstractGraphicsShapeItem *item , ScatterChartItem *parent) : QAbstractGraphicsShapeItem(0) ,m_item(item), m_parent(parent)
76 {
77 {
77 }
78 }
78
79
79 ~Marker()
80 ~Marker()
80 {
81 {
81 delete m_item;
82 delete m_item;
82 }
83 }
83
84
84 void setPoint(const QPointF& point)
85 void setPoint(const QPointF& point)
85 {
86 {
86 m_point=point;
87 m_point=point;
87 }
88 }
88
89
89 QPointF point() const
90 QPointF point() const
90 {
91 {
91 return m_point;
92 return m_point;
92 }
93 }
93
94
94 QPainterPath shape() const
95 QPainterPath shape() const
95 {
96 {
96 return m_item->shape();
97 return m_item->shape();
97 }
98 }
98
99
99 QRectF boundingRect() const
100 QRectF boundingRect() const
100 {
101 {
101 return m_item->boundingRect();
102 return m_item->boundingRect();
102 }
103 }
103
104
104 bool contains(const QPointF &point) const
105 bool contains(const QPointF &point) const
105 {
106 {
106 return m_item->contains(point);
107 return m_item->contains(point);
107 }
108 }
108
109
109 void setPen(const QPen &pen)
110 void setPen(const QPen &pen)
110 {
111 {
111 m_item->setPen(pen);
112 m_item->setPen(pen);
112 }
113 }
113
114
114 void setBrush(const QBrush &brush)
115 void setBrush(const QBrush &brush)
115 {
116 {
116 m_item->setBrush(brush);
117 m_item->setBrush(brush);
117 }
118 }
118
119
119 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
120 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
120 {
121 {
121 m_item->paint(painter,option,widget);
122 m_item->paint(painter,option,widget);
122 }
123 }
123
124
124 protected:
125 protected:
125
126
126 void mousePressEvent(QGraphicsSceneMouseEvent *event)
127 void mousePressEvent(QGraphicsSceneMouseEvent *event)
127 {
128 {
128 Q_UNUSED(event)
129 Q_UNUSED(event)
129 m_parent->markerSelected(this);
130 m_parent->markerSelected(this);
130 }
131 }
131
132
132 private:
133 private:
133 QAbstractGraphicsShapeItem* m_item;
134 QAbstractGraphicsShapeItem* m_item;
134 ScatterChartItem* m_parent;
135 ScatterChartItem* m_parent;
135 QPointF m_point;
136 QPointF m_point;
136 };
137 };
137
138
138 QTCOMMERCIALCHART_END_NAMESPACE
139 QTCOMMERCIALCHART_END_NAMESPACE
139
140
140 #endif // SCATTERPRESENTER_H
141 #endif // SCATTERPRESENTER_H
@@ -1,159 +1,164
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 "splinechartitem_p.h"
21 #include "splinechartitem_p.h"
22 #include "qsplineseries_p.h"
22 #include "qsplineseries_p.h"
23 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
24 #include "chartanimator_p.h"
24 #include "chartanimator_p.h"
25 #include <QPainter>
25 #include <QPainter>
26 #include <QDebug>
26 #include <QGraphicsSceneMouseEvent>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) :
30 SplineChartItem::SplineChartItem(QSplineSeries *series, ChartPresenter *presenter) :
31 XYChartItem(series, presenter),
31 XYChart(series, presenter),
32 QGraphicsItem(presenter ? presenter->rootItem() : 0),
32 m_series(series),
33 m_series(series),
33 m_pointsVisible(false),
34 m_pointsVisible(false),
34 m_animation(0)
35 m_animation(0)
35 {
36 {
36 setZValue(ChartPresenter::LineChartZValue);
37 setZValue(ChartPresenter::LineChartZValue);
37 QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
38 QObject::connect(m_series->d_func(),SIGNAL(updated()),this,SLOT(handleUpdated()));
38 handleUpdated();
39 handleUpdated();
39 }
40 }
40
41
41 QRectF SplineChartItem::boundingRect() const
42 QRectF SplineChartItem::boundingRect() const
42 {
43 {
43 return m_rect;
44 return m_rect;
44 }
45 }
45
46
46 QPainterPath SplineChartItem::shape() const
47 QPainterPath SplineChartItem::shape() const
47 {
48 {
48 return m_path;
49 return m_path;
49 }
50 }
50
51
51 void SplineChartItem::setAnimation(SplineAnimation* animation)
52 void SplineChartItem::setAnimation(SplineAnimation* animation)
52 {
53 {
53 m_animation=animation;
54 m_animation=animation;
54 XYChartItem::setAnimation(animation);
55 XYChart::setAnimation(animation);
55 }
56 }
56
57
57 void SplineChartItem::setControlGeometryPoints(QVector<QPointF>& points)
58 void SplineChartItem::setControlGeometryPoints(QVector<QPointF>& points)
58 {
59 {
59 m_controlPoints=points;
60 m_controlPoints=points;
60 }
61 }
61
62
62 QVector<QPointF> SplineChartItem::controlGeometryPoints() const
63 QVector<QPointF> SplineChartItem::controlGeometryPoints() const
63 {
64 {
64 return m_controlPoints;
65 return m_controlPoints;
65 }
66 }
66
67
67 void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
68 void SplineChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
68 {
69 {
69 QVector<QPointF> controlPoints;
70 QVector<QPointF> controlPoints;
70
71
71 if(newPoints.count()>=2) {
72 if(newPoints.count()>=2) {
72 controlPoints.resize(newPoints.count()*2-2);
73 controlPoints.resize(newPoints.count()*2-2);
73 }
74 }
74
75
75 for (int i = 0; i < newPoints.size() - 1; i++) {
76 for (int i = 0; i < newPoints.size() - 1; i++) {
76 controlPoints[2*i] = calculateGeometryControlPoint(2 * i);
77 controlPoints[2*i] = calculateGeometryControlPoint(2 * i);
77 controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1);
78 controlPoints[2 * i + 1] = calculateGeometryControlPoint(2 * i + 1);
78 }
79 }
79
80
80 if (controlPoints.count()<2) {
81 if (controlPoints.count()<2) {
81 setGeometryPoints(newPoints);
82 setGeometryPoints(newPoints);
82 setControlGeometryPoints(controlPoints);
83 setControlGeometryPoints(controlPoints);
83 updateGeometry();
84 updateGeometry();
84 return;
85 return;
85 }
86 }
86
87
87 if (m_animation) {
88 if (m_animation) {
88 m_animation->setValues(oldPoints,newPoints,m_controlPoints,controlPoints,index);
89 m_animation->setValues(oldPoints,newPoints,m_controlPoints,controlPoints,index);
89 animator()->startAnimation(m_animation);
90 animator()->startAnimation(m_animation);
90 }
91 }
91 else {
92 else {
92 setGeometryPoints(newPoints);
93 setGeometryPoints(newPoints);
93 setControlGeometryPoints(controlPoints);
94 setControlGeometryPoints(controlPoints);
94 updateGeometry();
95 updateGeometry();
95 }
96 }
96 }
97 }
97
98
98 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
99 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
99 {
100 {
100 return XYChartItem::calculateGeometryPoint(m_series->d_func()->controlPoint(index));
101 return XYChart::calculateGeometryPoint(m_series->d_func()->controlPoint(index));
101 }
102 }
102
103
103 void SplineChartItem::updateGeometry()
104 void SplineChartItem::updateGeometry()
104 {
105 {
105 const QVector<QPointF> &points = geometryPoints();
106 const QVector<QPointF> &points = geometryPoints();
106 const QVector<QPointF> &controlPoints = controlGeometryPoints();
107 const QVector<QPointF> &controlPoints = controlGeometryPoints();
107
108
108 if ((points.size()<2) || (controlPoints.size()<2)) {
109 if ((points.size()<2) || (controlPoints.size()<2)) {
109 m_path = QPainterPath();
110 m_path = QPainterPath();
110 return;
111 return;
111 }
112 }
112
113
113 Q_ASSERT(points.count()*2-2 == controlPoints.count());
114 Q_ASSERT(points.count()*2-2 == controlPoints.count());
114
115
115 QPainterPath splinePath(points.at(0));
116 QPainterPath splinePath(points.at(0));
116
117
117 for (int i = 0; i < points.size() - 1; i++) {
118 for (int i = 0; i < points.size() - 1; i++) {
118 const QPointF& point = points.at(i + 1);
119 const QPointF& point = points.at(i + 1);
119 splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point);
120 splinePath.cubicTo(controlPoints[2*i],controlPoints[2 * i + 1],point);
120 }
121 }
121
122
122 prepareGeometryChange();
123 prepareGeometryChange();
123 m_path = splinePath;
124 m_path = splinePath;
124 m_rect = splinePath.boundingRect();
125 m_rect = splinePath.boundingRect();
126 setPos(origin());
125 }
127 }
126
128
127 //handlers
129 //handlers
128
130
129 void SplineChartItem::handleUpdated()
131 void SplineChartItem::handleUpdated()
130 {
132 {
131 m_pointsVisible = m_series->pointsVisible();
133 m_pointsVisible = m_series->pointsVisible();
132 m_linePen = m_series->pen();
134 m_linePen = m_series->pen();
133 m_pointPen = m_series->pen();
135 m_pointPen = m_series->pen();
134 m_pointPen.setWidthF(2*m_pointPen.width());
136 m_pointPen.setWidthF(2*m_pointPen.width());
135 update();
137 update();
136 }
138 }
137
139
138 //painter
140 //painter
139
141
140 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
142 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
141 {
143 {
142 Q_UNUSED(widget)
144 Q_UNUSED(widget)
143 Q_UNUSED(option)
145 Q_UNUSED(option)
144 painter->save();
146 painter->save();
145 painter->setClipRect(clipRect());
147 painter->setClipRect(clipRect());
146 painter->setPen(m_linePen);
148 painter->setPen(m_linePen);
147 painter->drawPath(m_path);
149 painter->drawPath(m_path);
148 if (m_pointsVisible) {
150 if (m_pointsVisible) {
149 painter->setPen(m_pointPen);
151 painter->setPen(m_pointPen);
150 painter->drawPoints(geometryPoints());
152 painter->drawPoints(geometryPoints());
151 }
153 }
152 painter->restore();
154 painter->restore();
153 }
155 }
154
156
155
157 void SplineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
158 {
159 emit XYChart::clicked(calculateDomainPoint(event->pos()));
160 }
156
161
157 #include "moc_splinechartitem_p.cpp"
162 #include "moc_splinechartitem_p.cpp"
158
163
159 QTCOMMERCIALCHART_END_NAMESPACE
164 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,72 +1,74
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 #ifndef SPLINECHARTITEM_P_H
21 #ifndef SPLINECHARTITEM_P_H
22 #define SPLINECHARTITEM_P_H
22 #define SPLINECHARTITEM_P_H
23
23
24 #include "qsplineseries.h"
24 #include "qsplineseries.h"
25 #include "xychartitem_p.h"
25 #include "xychart_p.h"
26 #include "splineanimation_p.h"
26 #include "splineanimation_p.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 class SplineChartItem : public XYChartItem
30 class SplineChartItem : public XYChart, public QGraphicsItem
31 {
31 {
32 Q_OBJECT
32 Q_OBJECT
33 Q_INTERFACES(QGraphicsItem)
33 public:
34 public:
34 SplineChartItem(QSplineSeries *series, ChartPresenter *presenter);
35 SplineChartItem(QSplineSeries *series, ChartPresenter *presenter);
35
36
36 //from QGraphicsItem
37 //from QGraphicsItem
37 QRectF boundingRect() const;
38 QRectF boundingRect() const;
38 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
39 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
39 QPainterPath shape() const;
40 QPainterPath shape() const;
40
41
41 void setControlGeometryPoints(QVector<QPointF>& points);
42 void setControlGeometryPoints(QVector<QPointF>& points);
42 QVector<QPointF> controlGeometryPoints() const;
43 QVector<QPointF> controlGeometryPoints() const;
43
44
44 void setAnimation(SplineAnimation* animation);
45 void setAnimation(SplineAnimation* animation);
45 ChartAnimation* animation() const { return m_animation; }
46 ChartAnimation* animation() const { return m_animation; }
46
47
47 public Q_SLOTS:
48 public Q_SLOTS:
48 void handleUpdated();
49 void handleUpdated();
49
50
50 protected:
51 protected:
51 void updateGeometry();
52 void updateGeometry();
52 void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index);
53 void updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index);
54 void mousePressEvent(QGraphicsSceneMouseEvent *event);
53
55
54 private:
56 private:
55 QPointF calculateGeometryControlPoint(int index) const;
57 QPointF calculateGeometryControlPoint(int index) const;
56
58
57 private:
59 private:
58 QSplineSeries *m_series;
60 QSplineSeries *m_series;
59 QPainterPath m_path;
61 QPainterPath m_path;
60 QRectF m_rect;
62 QRectF m_rect;
61 QPen m_linePen;
63 QPen m_linePen;
62 QPen m_pointPen;
64 QPen m_pointPen;
63 bool m_pointsVisible;
65 bool m_pointsVisible;
64 QVector<QPointF> m_controlPoints;
66 QVector<QPointF> m_controlPoints;
65 SplineAnimation* m_animation;
67 SplineAnimation* m_animation;
66
68
67 friend class SplineAnimation;
69 friend class SplineAnimation;
68 };
70 };
69
71
70 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
71
73
72 #endif // SPLINECHARTITEM_P_H
74 #endif // SPLINECHARTITEM_P_H
@@ -1,86 +1,86
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 #ifndef QXYSERIES_H
21 #ifndef QXYSERIES_H
22 #define QXYSERIES_H
22 #define QXYSERIES_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <qabstractseries.h>
25 #include <qabstractseries.h>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 class QModelIndex;
29 class QModelIndex;
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 class QXYSeriesPrivate;
33 class QXYSeriesPrivate;
34 class QXYModelMapper;
34 class QXYModelMapper;
35
35
36 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
36 class QTCOMMERCIALCHART_EXPORT QXYSeries : public QAbstractSeries
37 {
37 {
38 Q_OBJECT
38 Q_OBJECT
39 Q_PROPERTY(QXYModelMapper *modelMapper READ modelMapper)
39 Q_PROPERTY(QXYModelMapper *modelMapper READ modelMapper)
40
40
41 protected:
41 protected:
42 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
42 explicit QXYSeries(QXYSeriesPrivate &d,QObject *parent = 0);
43 ~QXYSeries();
43 ~QXYSeries();
44
44
45 public:
45 public:
46 void append(qreal x, qreal y);
46 void append(qreal x, qreal y);
47 void append(const QPointF &point);
47 void append(const QPointF &point);
48 void append(const QList<QPointF> &points);
48 void append(const QList<QPointF> &points);
49 void replace(qreal oldX,qreal oldY,qreal newX,qreal newY);
49 void replace(qreal oldX,qreal oldY,qreal newX,qreal newY);
50 void replace(const QPointF &oldPoint,const QPointF &newPoint);
50 void replace(const QPointF &oldPoint,const QPointF &newPoint);
51 void remove(qreal x, qreal y);
51 void remove(qreal x, qreal y);
52 void remove(const QPointF &point);
52 void remove(const QPointF &point);
53 void removeAll();
53 void removeAll();
54
54
55 int count() const;
55 int count() const;
56 QList<QPointF> points() const;
56 QList<QPointF> points() const;
57
57
58 QXYSeries& operator << (const QPointF &point);
58 QXYSeries& operator << (const QPointF &point);
59 QXYSeries& operator << (const QList<QPointF> &points);
59 QXYSeries& operator << (const QList<QPointF> &points);
60
60
61 void setPen(const QPen &pen);
61 void setPen(const QPen &pen);
62 QPen pen() const;
62 QPen pen() const;
63
63
64 void setBrush(const QBrush &brush);
64 void setBrush(const QBrush &brush);
65 QBrush brush() const;
65 QBrush brush() const;
66
66
67 void setPointsVisible(bool visible = true);
67 void setPointsVisible(bool visible = true);
68 bool pointsVisible() const;
68 bool pointsVisible() const;
69
69
70 void setModel(QAbstractItemModel *model);
70 void setModel(QAbstractItemModel *model);
71 virtual void setModelMapper(QXYModelMapper *mapper);
71 virtual void setModelMapper(QXYModelMapper *mapper);
72 QXYModelMapper* modelMapper() const;
72 QXYModelMapper* modelMapper() const;
73
73
74 Q_SIGNALS:
74 Q_SIGNALS:
75 void clicked(const QPointF &point);
75 void clicked(const QPointF &point);
76
76
77 private:
77 private:
78 Q_DECLARE_PRIVATE(QXYSeries);
78 Q_DECLARE_PRIVATE(QXYSeries);
79 Q_DISABLE_COPY(QXYSeries);
79 Q_DISABLE_COPY(QXYSeries);
80 friend class XYLegendMarker;
80 friend class XYLegendMarker;
81 friend class XYChartItem;
81 friend class XYChart;
82 };
82 };
83
83
84 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
85
85
86 #endif
86 #endif
@@ -1,315 +1,304
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 "xychartitem_p.h"
21 #include "xychart_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "chartanimator_p.h"
25 #include "chartanimator_p.h"
26 #include <QPainter>
26 #include <QPainter>
27 #include <QGraphicsSceneMouseEvent>
28 #include <QAbstractItemModel>
27 #include <QAbstractItemModel>
29 #include "qxymodelmapper.h"
28 #include "qxymodelmapper.h"
30 #include <QDebug>
31
29
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
31
34 //TODO: optimize : remove points which are not visible
32 //TODO: optimize : remove points which are not visible
35
33
36 XYChartItem::XYChartItem(QXYSeries *series, ChartPresenter *presenter):ChartItem(presenter),
34 XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter):Chart(presenter),
37 m_minX(0),
35 m_minX(0),
38 m_maxX(0),
36 m_maxX(0),
39 m_minY(0),
37 m_minY(0),
40 m_maxY(0),
38 m_maxY(0),
41 m_series(series),
39 m_series(series),
42 m_animation(0)
40 m_animation(0)
43 {
41 {
44 QObject::connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
42 QObject::connect(series->d_func(),SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
45 QObject::connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
43 QObject::connect(series->d_func(),SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
46 QObject::connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
44 QObject::connect(series->d_func(),SIGNAL(pointsAdded(int, int)),this,SLOT(handlePointsAdded(int, int)));
47 QObject::connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
45 QObject::connect(series->d_func(),SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
48 QObject::connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
46 QObject::connect(series->d_func(),SIGNAL(pointsRemoved(int, int)),this,SLOT(handlePointsRemoved(int, int)));
49 QObject::connect(series->d_func(),SIGNAL(reinitialized()),this,SLOT(handleReinitialized()));
47 QObject::connect(series->d_func(),SIGNAL(reinitialized()),this,SLOT(handleReinitialized()));
50 QObject::connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
48 QObject::connect(this,SIGNAL(clicked(QPointF)),series,SIGNAL(clicked(QPointF)));
51 }
49 }
52
50
53 void XYChartItem::setGeometryPoints(QVector<QPointF>& points)
51 void XYChart::setGeometryPoints(QVector<QPointF>& points)
54 {
52 {
55 m_points = points;
53 m_points = points;
56 }
54 }
57
55
58 void XYChartItem::setClipRect(const QRectF &rect)
56 void XYChart::setClipRect(const QRectF &rect)
59 {
57 {
60 m_clipRect = rect;
58 m_clipRect = rect;
61 }
59 }
62
60
63 void XYChartItem::setAnimation(XYAnimation* animation)
61 void XYChart::setAnimation(XYAnimation* animation)
64 {
62 {
65 m_animation=animation;
63 m_animation=animation;
66 }
64 }
67
65
68 QPointF XYChartItem::calculateGeometryPoint(const QPointF &point) const
66 QPointF XYChart::calculateGeometryPoint(const QPointF &point) const
69 {
67 {
70 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
68 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
71 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
69 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
72 qreal x = (point.x() - m_minX)* deltaX;
70 qreal x = (point.x() - m_minX)* deltaX;
73 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
71 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
74 return QPointF(x,y);
72 return QPointF(x,y);
75 }
73 }
76
74
77 QPointF XYChartItem::calculateGeometryPoint(int index) const
75 QPointF XYChart::calculateGeometryPoint(int index) const
78 {
76 {
79 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
77 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
80 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
78 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
81 const QList<QPointF>& vector = m_series->points();
79 const QList<QPointF>& vector = m_series->points();
82 qreal x = (vector[index].x() - m_minX)* deltaX;
80 qreal x = (vector[index].x() - m_minX)* deltaX;
83 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
81 qreal y = (vector[index].y() - m_minY)*-deltaY + m_size.height();
84 return QPointF(x,y);
82 return QPointF(x,y);
85 }
83 }
86
84
87 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
85 QVector<QPointF> XYChart::calculateGeometryPoints() const
88 {
86 {
89 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
87 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
90 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
88 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
91
89
92 QVector<QPointF> result;
90 QVector<QPointF> result;
93 result.resize(m_series->count());
91 result.resize(m_series->count());
94 const QList<QPointF>& vector = m_series->points();
92 const QList<QPointF>& vector = m_series->points();
95 for (int i = 0; i < m_series->count(); ++i) {
93 for (int i = 0; i < m_series->count(); ++i) {
96 qreal x = (vector[i].x() - m_minX)* deltaX;
94 qreal x = (vector[i].x() - m_minX)* deltaX;
97 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
95 qreal y = (vector[i].y() - m_minY)*-deltaY + m_size.height();
98 result[i].setX(x);
96 result[i].setX(x);
99 result[i].setY(y);
97 result[i].setY(y);
100 }
98 }
101 return result;
99 return result;
102 }
100 }
103
101
104 QPointF XYChartItem::calculateDomainPoint(const QPointF &point) const
102 QPointF XYChart::calculateDomainPoint(const QPointF &point) const
105 {
103 {
106 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
104 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
107 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
105 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
108 qreal x = point.x()/deltaX +m_minX;
106 qreal x = point.x()/deltaX +m_minX;
109 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
107 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
110 return QPointF(x,y);
108 return QPointF(x,y);
111 }
109 }
112
110
113 void XYChartItem::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
111 void XYChart::updateChart(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index)
114 {
112 {
115 if (m_animation) {
113 if (m_animation) {
116 m_animation->setValues(oldPoints, newPoints, index);
114 m_animation->setValues(oldPoints, newPoints, index);
117 animator()->startAnimation(m_animation);
115 animator()->startAnimation(m_animation);
118 }
116 }
119 else {
117 else {
120 setGeometryPoints(newPoints);
118 setGeometryPoints(newPoints);
121 updateGeometry();
119 updateGeometry();
122 }
120 }
123 }
121 }
124
122
125 void XYChartItem::updateGeometry()
126 {
127 update();
128 }
129 //handlers
123 //handlers
130
124
131 void XYChartItem::handlePointAdded(int index)
125 void XYChart::handlePointAdded(int index)
132 {
126 {
133 if (m_series->model() == 0) {
127 if (m_series->model() == 0) {
134 Q_ASSERT(index<m_series->count());
128 Q_ASSERT(index<m_series->count());
135 Q_ASSERT(index>=0);
129 Q_ASSERT(index>=0);
136 }
130 }
137
131
138 QVector<QPointF> points = m_points;
132 QVector<QPointF> points = m_points;
139 QPointF point;
133 QPointF point;
140 point = calculateGeometryPoint(index);
134 point = calculateGeometryPoint(index);
141 points.insert(index, point);
135 points.insert(index, point);
142
136
143 if(m_animation) {
137 if(m_animation) {
144 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
138 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
145 }
139 }
146
140
147 updateChart(m_points,points,index);
141 updateChart(m_points,points,index);
148 }
142 }
149
143
150 void XYChartItem::handlePointsAdded(int start, int end)
144 void XYChart::handlePointsAdded(int start, int end)
151 {
145 {
152 if (m_series->model() == 0) {
146 if (m_series->model() == 0) {
153 for (int i = start; i <= end; i++)
147 for (int i = start; i <= end; i++)
154 handlePointAdded(i);
148 handlePointAdded(i);
155 }
149 }
156 else {
150 else {
157 int mapFirst = m_series->modelMapper()->first();
151 int mapFirst = m_series->modelMapper()->first();
158 int mapCount = m_series->modelMapper()->count();
152 int mapCount = m_series->modelMapper()->count();
159 if (mapCount != -1 && start >= mapFirst + mapCount) {
153 if (mapCount != -1 && start >= mapFirst + mapCount) {
160 return;
154 return;
161 }
155 }
162 else {
156 else {
163 int addedCount = end - start + 1;
157 int addedCount = end - start + 1;
164 if (mapCount != -1 && addedCount > mapCount)
158 if (mapCount != -1 && addedCount > mapCount)
165 addedCount = mapCount;
159 addedCount = mapCount;
166 int first = qMax(start, mapFirst); // get the index of the first item that will be added
160 int first = qMax(start, mapFirst); // get the index of the first item that will be added
167 int last = qMin(first + addedCount - 1, mapCount + mapFirst - 1);// get the index of the last item that will be added
161 int last = qMin(first + addedCount - 1, mapCount + mapFirst - 1);// get the index of the last item that will be added
168 for (int i = first; i <= last; i++) {
162 for (int i = first; i <= last; i++) {
169 handlePointAdded(i - mapFirst);
163 handlePointAdded(i - mapFirst);
170 }
164 }
171 // the map is limited therefore the items that are now outside the map
165 // the map is limited therefore the items that are now outside the map
172 // need to be removed from the drawn points
166 // need to be removed from the drawn points
173 if (mapCount != -1 && m_points.size() > mapCount)
167 if (mapCount != -1 && m_points.size() > mapCount)
174 for (int i = m_points.size() - 1; i >= mapCount; i--)
168 for (int i = m_points.size() - 1; i >= mapCount; i--)
175 handlePointRemoved(i);
169 handlePointRemoved(i);
176 }
170 }
177 }
171 }
178 }
172 }
179
173
180 void XYChartItem::handlePointRemoved(int index)
174 void XYChart::handlePointRemoved(int index)
181 {
175 {
182 if (m_series->model() == 0) {
176 if (m_series->model() == 0) {
183 Q_ASSERT(index<m_series->count() + 1);
177 Q_ASSERT(index<m_series->count() + 1);
184 Q_ASSERT(index>=0);
178 Q_ASSERT(index>=0);
185 }
179 }
186 QVector<QPointF> points = m_points;
180 QVector<QPointF> points = m_points;
187 points.remove(index);
181 points.remove(index);
188
182
189 if(m_animation) {
183 if(m_animation) {
190 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
184 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
191 }
185 }
192
186
193 updateChart(m_points,points,index);
187 updateChart(m_points,points,index);
194 }
188 }
195
189
196 void XYChartItem::handlePointsRemoved(int start, int end)
190 void XYChart::handlePointsRemoved(int start, int end)
197 {
191 {
198 Q_UNUSED(start)
192 Q_UNUSED(start)
199 Q_UNUSED(end)
193 Q_UNUSED(end)
200 if (m_series->model() == 0) {
194 if (m_series->model() == 0) {
201 for (int i = end; i >= start; i--)
195 for (int i = end; i >= start; i--)
202 handlePointRemoved(i);
196 handlePointRemoved(i);
203 }
197 }
204 else {
198 else {
205 // series uses model as a data source
199 // series uses model as a data source
206 int mapFirst = m_series->modelMapper()->first();
200 int mapFirst = m_series->modelMapper()->first();
207 int mapCount = m_series->modelMapper()->count();
201 int mapCount = m_series->modelMapper()->count();
208 int removedCount = end - start + 1;
202 int removedCount = end - start + 1;
209 if (mapCount != -1 && start >= mapFirst + mapCount) {
203 if (mapCount != -1 && start >= mapFirst + mapCount) {
210 return;
204 return;
211 }
205 }
212 else {
206 else {
213 int toRemove = qMin(m_points.size(), removedCount); // first find how many items can actually be removed
207 int toRemove = qMin(m_points.size(), removedCount); // first find how many items can actually be removed
214 int first = qMax(start, mapFirst);// get the index of the first item that will be removed.
208 int first = qMax(start, mapFirst);// get the index of the first item that will be removed.
215 int last = qMin(first + toRemove - 1, m_points.size() + mapFirst - 1);// get the index of the last item that will be removed.
209 int last = qMin(first + toRemove - 1, m_points.size() + mapFirst - 1);// get the index of the last item that will be removed.
216 if (last - first == 0) {
210 if (last - first == 0) {
217 for (int i = last; i >= first; i--) {
211 for (int i = last; i >= first; i--) {
218 handlePointRemoved(i - mapFirst);
212 handlePointRemoved(i - mapFirst);
219
213
220 }
214 }
221 }
215 }
222 else {
216 else {
223 QVector<QPointF> points = m_points;
217 QVector<QPointF> points = m_points;
224 for (int i = last; i >= first; i--)
218 for (int i = last; i >= first; i--)
225 points.remove(i - mapFirst);
219 points.remove(i - mapFirst);
226 setGeometryPoints(points);
220 setGeometryPoints(points);
227 updateGeometry();
221 updateGeometry();
228 }
222 }
229 if (mapCount != -1) {
223 if (mapCount != -1) {
230 int itemsAvailable; // check how many are available to be added
224 int itemsAvailable; // check how many are available to be added
231 if (m_series->modelMapper()->orientation() == Qt::Vertical)
225 if (m_series->modelMapper()->orientation() == Qt::Vertical)
232 itemsAvailable = m_series->model()->rowCount() - mapFirst - m_points.size();
226 itemsAvailable = m_series->model()->rowCount() - mapFirst - m_points.size();
233 else
227 else
234 itemsAvailable = m_series->model()->columnCount() - mapFirst - m_points.size();
228 itemsAvailable = m_series->model()->columnCount() - mapFirst - m_points.size();
235 int toBeAdded = qMin(itemsAvailable, mapCount - m_points.size());// add not more items than there is space left to be filled.
229 int toBeAdded = qMin(itemsAvailable, mapCount - m_points.size());// add not more items than there is space left to be filled.
236 int currentSize = m_points.size();
230 int currentSize = m_points.size();
237 if (toBeAdded > 0)
231 if (toBeAdded > 0)
238 for (int i = m_points.size(); i < currentSize + toBeAdded; i++) {
232 for (int i = m_points.size(); i < currentSize + toBeAdded; i++) {
239 handlePointAdded(i);
233 handlePointAdded(i);
240 }
234 }
241 }
235 }
242 }
236 }
243 }
237 }
244
238
245 }
239 }
246
240
247 void XYChartItem::handlePointReplaced(int index)
241 void XYChart::handlePointReplaced(int index)
248 {
242 {
249 Q_ASSERT(index<m_series->count());
243 Q_ASSERT(index<m_series->count());
250 Q_ASSERT(index>=0);
244 Q_ASSERT(index>=0);
251 QPointF point = calculateGeometryPoint(index);
245 QPointF point = calculateGeometryPoint(index);
252 QVector<QPointF> points = m_points;
246 QVector<QPointF> points = m_points;
253 points.replace(index,point);
247 points.replace(index,point);
254
248
255 if(m_animation) {
249 if(m_animation) {
256 m_animation->setAnimationType(XYAnimation::MoveDownAnimation);
250 m_animation->setAnimationType(XYAnimation::MoveDownAnimation);
257 }
251 }
258
252
259 updateChart(m_points,points,index);
253 updateChart(m_points,points,index);
260 }
254 }
261
255
262 void XYChartItem::handleReinitialized()
256 void XYChart::handleReinitialized()
263 {
257 {
264 QVector<QPointF> points = calculateGeometryPoints();
258 QVector<QPointF> points = calculateGeometryPoints();
265
259
266 if(m_animation) {
260 if(m_animation) {
267 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
261 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
268 }
262 }
269
263
270 updateChart(m_points,points);
264 updateChart(m_points,points);
271 }
265 }
272
266
273 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
267 void XYChart::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
274 {
268 {
275 m_minX=minX;
269 m_minX=minX;
276 m_maxX=maxX;
270 m_maxX=maxX;
277 m_minY=minY;
271 m_minY=minY;
278 m_maxY=maxY;
272 m_maxY=maxY;
279 if (isEmpty()) return;
273 if (isEmpty()) return;
280 QVector<QPointF> points = calculateGeometryPoints();
274 QVector<QPointF> points = calculateGeometryPoints();
281
275
282 if(m_animation) {
276 if(m_animation) {
283 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
277 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
284 }
278 }
285 updateChart(m_points,points);
279 updateChart(m_points,points);
286 }
280 }
287
281
288 void XYChartItem::handleGeometryChanged(const QRectF &rect)
282 void XYChart::handleGeometryChanged(const QRectF &rect)
289 {
283 {
290 Q_ASSERT(rect.isValid());
284 Q_ASSERT(rect.isValid());
291 m_size=rect.size();
285 m_size=rect.size();
292 m_clipRect=rect.translated(-rect.topLeft());
286 m_clipRect=rect.translated(-rect.topLeft());
293 setPos(rect.topLeft());
287 m_origin=rect.topLeft();
294
288
295 if (isEmpty()) return;
289 if (isEmpty()) return;
296 QVector<QPointF> points = calculateGeometryPoints();
290 QVector<QPointF> points = calculateGeometryPoints();
297 if(m_animation) {
291 if(m_animation) {
298 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
292 m_animation->setAnimationType(XYAnimation::LineDrawAnimation);
299 }
293 }
300 updateChart(m_points,points);
294 updateChart(m_points,points);
301 }
295 }
302
296
303 bool XYChartItem::isEmpty()
297 bool XYChart::isEmpty()
304 {
298 {
305 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY) || m_series->points().isEmpty();
299 return !m_clipRect.isValid() || qFuzzyIsNull(m_maxX - m_minX) || qFuzzyIsNull(m_maxY - m_minY) || m_series->points().isEmpty();
306 }
300 }
307
301
308 void XYChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
302 #include "moc_xychart_p.cpp"
309 {
310 emit clicked(calculateDomainPoint(event->pos()));
311 }
312
313 #include "moc_xychartitem_p.cpp"
314
303
315 QTCOMMERCIALCHART_END_NAMESPACE
304 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,16 +1,16
1 INCLUDEPATH += $$PWD
1 INCLUDEPATH += $$PWD
2 DEPENDPATH += $$PWD
2 DEPENDPATH += $$PWD
3
3
4 SOURCES += \
4 SOURCES += \
5 $$PWD/xychartitem.cpp \
5 $$PWD/xychart.cpp \
6 $$PWD/qxyseries.cpp \
6 $$PWD/qxyseries.cpp \
7 $$PWD/qxymodelmapper.cpp
7 $$PWD/qxymodelmapper.cpp
8
8
9 PRIVATE_HEADERS += \
9 PRIVATE_HEADERS += \
10 $$PWD/xychartitem_p.h \
10 $$PWD/xychart_p.h \
11 $$PWD/qxyseries_p.h
11 $$PWD/qxyseries_p.h
12
12
13
13
14 PUBLIC_HEADERS += \
14 PUBLIC_HEADERS += \
15 $$PWD/qxyseries.h \
15 $$PWD/qxyseries.h \
16 $$PWD/qxymodelmapper.h
16 $$PWD/qxymodelmapper.h
@@ -1,92 +1,95
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 #ifndef XYCHARTITEM_H
21 #ifndef XYCHARTITEM_H
22 #define XYCHARTITEM_H
22 #define XYCHARTITEM_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "chartitem_p.h"
25 #include "chartitem_p.h"
26 #include "xyanimation_p.h"
26 #include "xyanimation_p.h"
27 #include <QPen>
27 #include <QPen>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class ChartPresenter;
31 class ChartPresenter;
32 class QXYSeries;
32 class QXYSeries;
33
33
34 class XYChartItem : public ChartItem
34 class XYChart : public Chart
35 {
35 {
36 Q_OBJECT
36 Q_OBJECT
37 public:
37 public:
38 explicit XYChartItem(QXYSeries *series, ChartPresenter *presenter);
38 explicit XYChart(QXYSeries *series, ChartPresenter *presenter);
39 ~XYChartItem(){};
39 ~XYChart(){};
40
40
41 void setGeometryPoints(QVector<QPointF>& points);
41 void setGeometryPoints(QVector<QPointF>& points);
42 QVector<QPointF> geometryPoints() const { return m_points; }
42 QVector<QPointF> geometryPoints() const { return m_points; }
43
43
44 void setClipRect(const QRectF &rect);
44 void setClipRect(const QRectF &rect);
45 QRectF clipRect() const { return m_clipRect; }
45 QRectF clipRect() const { return m_clipRect; }
46
46
47 QSizeF size() const { return m_size; }
48 QPointF origin() const { return m_origin; }
49
47 void setAnimation(XYAnimation* animation);
50 void setAnimation(XYAnimation* animation);
48 ChartAnimation* animation() const { return m_animation; }
51 ChartAnimation* animation() const { return m_animation; }
49 virtual void updateGeometry();
52 virtual void updateGeometry() = 0;
50
53
51 public Q_SLOTS:
54 public Q_SLOTS:
52 void handlePointAdded(int index);
55 void handlePointAdded(int index);
53 void handlePointsAdded(int start, int end);
56 void handlePointsAdded(int start, int end);
54 void handlePointRemoved(int index);
57 void handlePointRemoved(int index);
55 void handlePointsRemoved(int start, int end);
58 void handlePointsRemoved(int start, int end);
56 void handlePointReplaced(int index);
59 void handlePointReplaced(int index);
57 void handleReinitialized();
60 void handleReinitialized();
58 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
61 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
59 void handleGeometryChanged(const QRectF &size);
62 void handleGeometryChanged(const QRectF &size);
60
63
61 Q_SIGNALS:
64 Q_SIGNALS:
62 void clicked(const QPointF& point);
65 void clicked(const QPointF& point);
63
66
64 protected:
67 protected:
65 virtual void updateChart(QVector<QPointF> &oldPoints,QVector<QPointF> &newPoints,int index = 0);
68 virtual void updateChart(QVector<QPointF> &oldPoints,QVector<QPointF> &newPoints,int index = 0);
66 QPointF calculateGeometryPoint(const QPointF &point) const;
69 QPointF calculateGeometryPoint(const QPointF &point) const;
67 QPointF calculateGeometryPoint(int index) const;
70 QPointF calculateGeometryPoint(int index) const;
68 QPointF calculateDomainPoint(const QPointF &point) const;
71 QPointF calculateDomainPoint(const QPointF &point) const;
69 QVector<QPointF> calculateGeometryPoints() const;
72 QVector<QPointF> calculateGeometryPoints() const;
70 void mousePressEvent(QGraphicsSceneMouseEvent *event);
71
73
72 private:
74 private:
73 inline bool isEmpty();
75 inline bool isEmpty();
74
76
75 private:
77 private:
76 qreal m_minX;
78 qreal m_minX;
77 qreal m_maxX;
79 qreal m_maxX;
78 qreal m_minY;
80 qreal m_minY;
79 qreal m_maxY;
81 qreal m_maxY;
80 QXYSeries* m_series;
82 QXYSeries* m_series;
81 QSizeF m_size;
83 QSizeF m_size;
84 QPointF m_origin;
82 QRectF m_clipRect;
85 QRectF m_clipRect;
83 QVector<QPointF> m_points;
86 QVector<QPointF> m_points;
84 XYAnimation* m_animation;
87 XYAnimation* m_animation;
85
88
86 friend class AreaChartItem;
89 friend class AreaChartItem;
87
90
88 };
91 };
89
92
90 QTCOMMERCIALCHART_END_NAMESPACE
93 QTCOMMERCIALCHART_END_NAMESPACE
91
94
92 #endif
95 #endif
General Comments 0
You need to be logged in to leave comments. Login now