##// END OF EJS Templates
animations for stacked and percentbarchart
sauimone -
r1426:9cc71c12c6d0
parent child
Show More
@@ -0,0 +1,79
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "percentbaranimation_p.h"
22 #include "percentbarchartitem_p.h"
23 #include <QTimer>
24
25 Q_DECLARE_METATYPE(QVector<QRectF>)
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
29 PercentBarAnimation::PercentBarAnimation(BarChartItem *item)
30 :ChartAnimation(item),
31 m_item(item)
32 {
33 setEasingCurve(QEasingCurve::OutQuart);
34 }
35
36 PercentBarAnimation::~PercentBarAnimation()
37 {
38 }
39
40 QVariant PercentBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
41 {
42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
44 QVector<QRectF> result;
45
46 Q_ASSERT(startVector.count() == endVector.count());
47
48 qreal zeroPos = ((m_item->geometry().height() + m_item->geometry().y()) * (1 - progress));
49
50 for(int i = 0; i < startVector.count(); i++) {
51 qreal w = endVector[i].width();
52 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
53 qreal x = endVector[i].topLeft().x();
54 qreal y = startVector[i].topLeft().y() + ((endVector[i].topLeft().y() - startVector[i].topLeft().y()) * progress)
55 + zeroPos;
56
57 QRectF value(x,y,w,h);
58 result << value;
59 }
60 return qVariantFromValue(result);
61 }
62
63 void PercentBarAnimation::updateCurrentValue(const QVariant &value)
64 {
65 QVector<QRectF> layout = qVariantValue<QVector<QRectF> >(value);
66 m_item->setLayout(layout);
67 }
68
69 void PercentBarAnimation::updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
70 {
71 setDuration(ChartAnimationDuration);
72 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
73 setKeyValueAt(1.0, qVariantFromValue(newLayout));
74 QTimer::singleShot(0, this, SLOT(start()));
75 }
76
77 #include "moc_percentbaranimation_p.cpp"
78
79 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,43
1 // W A R N I N G
2 // -------------
3 //
4 // This file is not part of the QtCommercial Chart API. It exists purely as an
5 // implementation detail. This header file may change from version to
6 // version without notice, or even be removed.
7 //
8 // We mean it.
9
10 #ifndef PERCENTBARANIMATION_P_H
11 #define PERCENTBARANIMATION_P_H
12
13 #include "chartanimation_p.h"
14 #include "barchartitem_p.h"
15
16 QTCOMMERCIALCHART_BEGIN_NAMESPACE
17
18 class PercentBarChartItem;
19 class QBarSet;
20 class BarSetAnimation;
21
22 class PercentBarAnimation : public ChartAnimation
23 {
24 Q_OBJECT
25 public:
26 PercentBarAnimation(BarChartItem *item);
27 ~PercentBarAnimation();
28
29 public:
30 virtual QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const;
31 virtual void updateCurrentValue(const QVariant &value);
32
33 void updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
34
35 private:
36 BarChartItem *m_item;
37 QHash<QBarSet *, BarSetAnimation *> m_animations;
38
39 };
40
41 QTCOMMERCIALCHART_END_NAMESPACE
42
43 #endif // PERCENTBARANIMATION_P_H
@@ -0,0 +1,79
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "stackedbaranimation_p.h"
22 #include "stackedbarchartitem_p.h"
23 #include <QTimer>
24
25 Q_DECLARE_METATYPE(QVector<QRectF>)
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
29 StackedBarAnimation::StackedBarAnimation(BarChartItem *item)
30 :ChartAnimation(item),
31 m_item(item)
32 {
33 setEasingCurve(QEasingCurve::OutQuart);
34 }
35
36 StackedBarAnimation::~StackedBarAnimation()
37 {
38 }
39
40 QVariant StackedBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
41 {
42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
44 QVector<QRectF> result;
45
46 Q_ASSERT(startVector.count() == endVector.count());
47
48 qreal zeroPos = ((m_item->geometry().height() + m_item->geometry().y()) * (1 - progress));
49
50 for(int i = 0; i < startVector.count(); i++) {
51 qreal w = endVector[i].width();
52 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
53 qreal x = endVector[i].topLeft().x();
54 qreal y = startVector[i].topLeft().y() + ((endVector[i].topLeft().y() - startVector[i].topLeft().y()) * progress)
55 + zeroPos;
56
57 QRectF value(x,y,w,h);
58 result << value;
59 }
60 return qVariantFromValue(result);
61 }
62
63 void StackedBarAnimation::updateCurrentValue(const QVariant &value)
64 {
65 QVector<QRectF> layout = qVariantValue<QVector<QRectF> >(value);
66 m_item->setLayout(layout);
67 }
68
69
70 void StackedBarAnimation::updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
71 {
72 setDuration(ChartAnimationDuration);
73 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
74 setKeyValueAt(1.0, qVariantFromValue(newLayout));
75 QTimer::singleShot(0, this, SLOT(start()));
76 }
77 #include "moc_stackedbaranimation_p.cpp"
78
79 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,43
1 // W A R N I N G
2 // -------------
3 //
4 // This file is not part of the QtCommercial Chart API. It exists purely as an
5 // implementation detail. This header file may change from version to
6 // version without notice, or even be removed.
7 //
8 // We mean it.
9
10 #ifndef STACKEDBARANIMATION_P_H
11 #define STACKEDBARANIMATION_P_H
12
13 #include "chartanimation_p.h"
14 #include "barchartitem_p.h"
15
16 QTCOMMERCIALCHART_BEGIN_NAMESPACE
17
18 class StackedBarChartItem;
19 class QBarSet;
20 class BarSetAnimation;
21
22 class StackedBarAnimation : public ChartAnimation
23 {
24 Q_OBJECT
25 public:
26 StackedBarAnimation(BarChartItem *item);
27 ~StackedBarAnimation();
28
29 public:
30 virtual QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const;
31 virtual void updateCurrentValue(const QVariant &value);
32
33 void updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
34
35 private:
36 BarChartItem *m_item;
37 QHash<QBarSet *, BarSetAnimation *> m_animations;
38
39 };
40
41 QTCOMMERCIALCHART_END_NAMESPACE
42
43 #endif // STACKEDBARANIMATION_P_H
@@ -1,24 +1,27
1 1 INCLUDEPATH += $$PWD
2 2 DEPENDPATH += $$PWD
3 3
4 4 SOURCES += \
5 5 $$PWD/axisanimation.cpp \
6 6 $$PWD/chartanimator.cpp \
7 7 $$PWD/xyanimation.cpp \
8 8 $$PWD/pieanimation.cpp \
9 9 $$PWD/piesliceanimation.cpp \
10 10 $$PWD/splineanimation.cpp \
11 $$PWD/baranimation.cpp
11 $$PWD/baranimation.cpp \
12 $$PWD/stackedbaranimation.cpp \
13 $$PWD/percentbaranimation.cpp
12 14
13 15
14 16
15 17 PRIVATE_HEADERS += \
16 18 $$PWD/axisanimation_p.h \
17 19 $$PWD/chartanimator_p.h \
18 20 $$PWD/chartanimation_p.h \
19 21 $$PWD/xyanimation_p.h \
20 22 $$PWD/pieanimation_p.h \
21 23 $$PWD/piesliceanimation_p.h \
22 24 $$PWD/splineanimation_p.h \
23 $$PWD/baranimation_p.h
24
25 $$PWD/baranimation_p.h \
26 $$PWD/stackedbaranimation_p.h \
27 $$PWD/percentbaranimation_p.h
@@ -1,72 +1,76
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "baranimation_p.h"
22 22 #include "barchartitem_p.h"
23 #include <QParallelAnimationGroup>
24 23 #include <QTimer>
25 24
26 25 Q_DECLARE_METATYPE(QVector<QRectF>)
27 //Q_DECLARE_METATYPE(BarLayout) // TODO?
28
29 26
30 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 28
32 29 BarAnimation::BarAnimation(BarChartItem *item)
33 30 :ChartAnimation(item),
34 31 m_item(item)
35 32 {
33 setEasingCurve(QEasingCurve::OutQuart);
36 34 }
37 35
38 36 BarAnimation::~BarAnimation()
39 37 {
40 38 }
41 39
42 40 QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
43 41 {
44 42 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> >(from);
45 43 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> >(to);
46 44 QVector<QRectF> result;
47 45
48 46 Q_ASSERT(startVector.count() == endVector.count());
49 47
50 48 for(int i = 0; i < startVector.count(); i++) {
51 49 qreal w = endVector[i].width();
52 50 qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress);
53 51 qreal x = endVector[i].topLeft().x();
54 52 qreal y = endVector[i].topLeft().y() + endVector[i].height() - h;
55 53
56 QPointF topLeft(x,y);
57 QSizeF size(w,h);
58 QRectF value(topLeft,size);
54 QRectF value(x,y,w,h);
59 55 result << value;
60 56 }
61 57 return qVariantFromValue(result);
62 58 }
63 59
64 60 void BarAnimation::updateCurrentValue(const QVariant &value)
65 61 {
66 62 QVector<QRectF> layout = qVariantValue<QVector<QRectF> >(value);
67 63 m_item->setLayout(layout);
68 64 }
69 65
66 void BarAnimation::updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
67 {
68 setDuration(ChartAnimationDuration);
69 setKeyValueAt(0.0, qVariantFromValue(oldLayout));
70 setKeyValueAt(1.0, qVariantFromValue(newLayout));
71 QTimer::singleShot(0, this, SLOT(start()));
72 }
73
70 74 #include "moc_baranimation_p.cpp"
71 75
72 76 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,63 +1,62
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef BARANIMATION_P_H
31 31 #define BARANIMATION_P_H
32 32
33 33 #include "chartanimation_p.h"
34 34 #include "barchartitem_p.h"
35 35
36 36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37 37
38 38 class BarChartItem;
39 39 class QBarSet;
40 40 class BarSetAnimation;
41 41
42 42 class BarAnimation : public ChartAnimation
43 43 {
44 44 Q_OBJECT
45 45
46 46 public:
47 47 BarAnimation(BarChartItem *item);
48 48 ~BarAnimation();
49 49
50 50 public: // from QVariantAnimation
51 51 virtual QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const;
52 52 virtual void updateCurrentValue(const QVariant &value);
53 53
54 public Q_SLOTS:
54 void updateLayout(const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
55 55
56 56 private:
57 57 BarChartItem *m_item;
58 QHash<QBarSet *, BarSetAnimation *> m_animations;
59 58 };
60 59
61 60 QTCOMMERCIALCHART_END_NAMESPACE
62 61
63 62 #endif
@@ -1,118 +1,168
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "chartanimator_p.h"
22 22 #include "axisanimation_p.h"
23 23 #include "xyanimation_p.h"
24 24 #include "splineanimation_p.h"
25 25 #include "xychart_p.h"
26 26 #include "pieanimation_p.h"
27 27 #include "baranimation_p.h"
28 28 #include "barchartitem_p.h"
29 #include "stackedbaranimation_p.h"
30 #include "stackedbarchartitem_p.h"
31 #include "percentbaranimation_p.h"
32 #include "percentbarchartitem_p.h"
29 33 #include "areachartitem_p.h"
30 34 #include "splinechartitem_p.h"
31 35 #include "scatterchartitem_p.h"
32 36 #include "chartaxis_p.h"
33 37 #include <QTimer>
34 38
35 39 Q_DECLARE_METATYPE(QVector<QPointF>)
36 40 Q_DECLARE_METATYPE(QVector<qreal>)
37 41 Q_DECLARE_METATYPE(QVector<QRectF>)
38 42
39 43 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 44
41 45 ChartAnimator::ChartAnimator(QObject *parent):QObject(parent)
42 46 {
43 47 }
44 48
45 49 ChartAnimator::~ChartAnimator()
46 50 {
47 51 }
48 52 void ChartAnimator::addAnimation(PieChartItem *item)
49 53 {
50 54 ChartAnimation *animation = m_animations.value(item);
51 55
52 56 if (!animation) {
53 57 animation = new PieAnimation(item);
54 58 m_animations.insert(item, animation);
55 59 }
56 60
57 61 item->setAnimator(this);
58 62 }
59 63
60 64 void ChartAnimator::addAnimation(BarChartItem *item)
61 65 {
66 // This can handle also GroupedBarChartItem because bars are side by side
62 67 ChartAnimation *animation = m_animations.value(item);
63 68
64 69 if (!animation) {
65 70 animation = new BarAnimation(item);
66 71 m_animations.insert(item, animation);
67 72 }
68 73
69 74 item->setAnimator(this);
70 75 }
71 76
77 void ChartAnimator::addAnimation(StackedBarChartItem *item)
78 {
79 ChartAnimation *animation = m_animations.value(item);
80
81 if (!animation) {
82 animation = new StackedBarAnimation(item);
83 m_animations.insert(item, animation);
84 }
85
86 item->setAnimator(this);
87 }
88
89 void ChartAnimator::addAnimation(PercentBarChartItem *item)
90 {
91 ChartAnimation *animation = m_animations.value(item);
92
93 if (!animation) {
94 animation = new PercentBarAnimation(item);
95 m_animations.insert(item, animation);
96 }
97
98 item->setAnimator(this);
99 }
72 100
73 101 void ChartAnimator::removeAnimation(Chart *item)
74 102 {
75 103 item->setAnimator(0);
76 104 m_animations.remove(item);
77 105 }
78 106
79 107 void ChartAnimator::addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool startupAnimation)
80 108 {
81 109 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
82 110 Q_ASSERT(animation);
83 111 animation->addSlice(sliceItem, sliceData, startupAnimation);
84 112 }
85 113
86 114 void ChartAnimator::removeAnimation(PieChartItem *item, PieSliceItem *sliceItem)
87 115 {
88 116 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
89 117 Q_ASSERT(animation);
90 118 animation->removeSlice(sliceItem);
91 119 }
92 120
93 121 void ChartAnimator::updateAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData)
94 122 {
95 123 PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item));
96 124 Q_ASSERT(animation);
97 125 animation->updateValue(sliceItem, sliceData);
98 126 }
99 127
100 128 void ChartAnimator::updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
101 129 {
102 130 BarAnimation *animation = static_cast<BarAnimation *>(m_animations.value(item));
103 m_animations.remove(item);
104 131 if (animation) {
132 m_animations.remove(item);
105 133 animation->deleteLater();
106 134 animation = 0;
135 addAnimation(item);
107 136 }
108 addAnimation(item);
109 137 animation = static_cast<BarAnimation *>(m_animations.value(item));
110 animation->setDuration(ChartAnimationDuration);
111 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
112 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
113 QTimer::singleShot(0, animation, SLOT(start()));
138 animation->updateLayout(oldLayout,newLayout);
139 }
140
141 void ChartAnimator::updateLayout(StackedBarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
142 {
143 StackedBarAnimation *animation = static_cast<StackedBarAnimation *>(m_animations.value(item));
144 if (animation) {
145 m_animations.remove(item);
146 animation->deleteLater();
147 animation = 0;
148 addAnimation(item);
149 }
150 animation = static_cast<StackedBarAnimation *>(m_animations.value(item));
151 animation->updateLayout(oldLayout,newLayout);
114 152 }
115 153
154 void ChartAnimator::updateLayout(PercentBarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
155 {
156 PercentBarAnimation *animation = static_cast<PercentBarAnimation *>(m_animations.value(item));
157 if (animation) {
158 m_animations.remove(item);
159 animation->deleteLater();
160 animation = 0;
161 addAnimation(item);
162 }
163 animation = static_cast<PercentBarAnimation *>(m_animations.value(item));
164 animation->updateLayout(oldLayout,newLayout);
165 }
116 166 #include "moc_chartanimator_p.cpp"
117 167
118 168 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,73 +1,77
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef CHARTANIMATOR_P_H
31 31 #define CHARTANIMATOR_P_H
32 32
33 33 #include "qchartglobal.h"
34 34 #include "chartanimation_p.h"
35 35 #include "piechartitem_p.h"
36 36 #include "barchartitem_p.h"
37 37 #include <QPointF>
38 38
39 39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 40
41 41 class ChartItem;
42 42 class ChartAxis;
43 43 class AreaChartItem;
44 44 class SplineChartItem;
45 45 class ScatterChartItem;
46 46 class LineChartItem;
47 47 class XYChartItem;
48 48 class XYAnimation;
49 49
50 50 class ChartAnimator : public QObject
51 51 {
52 52 Q_OBJECT
53 53 public:
54 54 ChartAnimator(QObject *parent = 0);
55 55 virtual ~ChartAnimator();
56 56
57 57 void addAnimation(PieChartItem *item);
58 void addAnimation(BarChartItem *item);
58 void addAnimation(BarChartItem *item); // This can handle also grouped bar chart.
59 void addAnimation(StackedBarChartItem *item);
60 void addAnimation(PercentBarChartItem *item);
59 61 void removeAnimation(Chart *item);
60 62
61 63 void addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool isEmpty);
62 64 void removeAnimation(PieChartItem *item, PieSliceItem *sliceItem);
63 65 void updateAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData);
64 66
65 67 void updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
68 void updateLayout(StackedBarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
69 void updateLayout(PercentBarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
66 70
67 71 private:
68 72 QMap<Chart *, ChartAnimation *> m_animations;
69 73 };
70 74
71 75 QTCOMMERCIALCHART_END_NAMESPACE
72 76
73 77 #endif
@@ -1,56 +1,55
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "bar_p.h"
22 #include <QDebug>
23 22 #include <QPainter>
24 23 #include <QGraphicsSceneEvent>
25 24
26 25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 26
28 27 Bar::Bar(QBarSet *barset, int index, QGraphicsItem *parent) : QGraphicsRectItem(parent),
29 28 m_index(index),
30 29 m_barset(barset)
31 30 {
32 31 setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
33 32 setAcceptHoverEvents(true);
34 33 }
35 34
36 35 void Bar::mousePressEvent(QGraphicsSceneMouseEvent *event)
37 36 {
38 37 Q_UNUSED(event)
39 38 emit clicked(m_barset, m_index);
40 39 }
41 40
42 41 void Bar::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
43 42 {
44 43 Q_UNUSED(event)
45 44 emit hovered(m_barset, true);
46 45 }
47 46
48 47 void Bar::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
49 48 {
50 49 Q_UNUSED(event)
51 50 emit hovered(m_barset, false);
52 51 }
53 52
54 53 #include "moc_bar_p.cpp"
55 54
56 55 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,88 +1,88
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30
31 31 #ifndef BARCHARTITEM_H
32 32 #define BARCHARTITEM_H
33 33
34 34 #include "chartitem_p.h"
35 35 #include "qbarseries.h"
36 36 #include <QPen>
37 37 #include <QBrush>
38 38
39 39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 40
41 41 class Bar;
42 42 class QAxisCategories;
43 43 class QChart;
44 44
45 45 class BarChartItem : public ChartItem
46 46 {
47 47 Q_OBJECT
48 48 public:
49 49 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
50 50 virtual ~BarChartItem();
51 51
52 52 public:
53 53 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
54 54 QRectF boundingRect() const;
55 55
56 56 virtual QVector<QRectF> calculateLayout();
57 void applyLayout(const QVector<QRectF> &layout);
57 virtual void applyLayout(const QVector<QRectF> &layout);
58 58 void setLayout(const QVector<QRectF> &layout);
59 59 void updateLayout(const QVector<QRectF> &layout);
60 60
61 61 QRectF geometry() const { return m_rect;}
62 62
63 63 public Q_SLOTS:
64 64 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
65 65 void handleGeometryChanged(const QRectF &size);
66 66 void handleLayoutChanged();
67 67 void handleLabelsVisibleChanged(bool visible);
68 68 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
69 69
70 70 protected:
71 71
72 72 qreal m_domainMinX;
73 73 qreal m_domainMaxX;
74 74 qreal m_domainMinY;
75 75 qreal m_domainMaxY;
76 76
77 77 QRectF m_rect;
78 78 QVector<QRectF> m_layout;
79 79
80 80 // Not owned.
81 81 QBarSeries *m_series;
82 82 QList<Bar *> m_bars;
83 83 QList<QGraphicsSimpleTextItem *> m_labels;
84 84 };
85 85
86 86 QTCOMMERCIALCHART_END_NAMESPACE
87 87
88 88 #endif // BARCHARTITEM_H
@@ -1,97 +1,106
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "percentbarchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "qbarseries_p.h"
24 24 #include "qbarset.h"
25 #include <QDebug>
25 #include "chartanimator_p.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 30 BarChartItem(series, presenter)
31 31 {
32 32 }
33 33
34 34 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 35 {
36 36 QVector<QRectF> layout;
37 37
38 38 // Use temporary qreals for accuracy
39 39 qreal categoryCount = m_series->d_func()->categoryCount();
40 40 qreal setCount = m_series->barsetCount();
41 41 bool barsVisible = m_series->isVisible();
42 42
43 43 // Domain:
44 44 qreal width = geometry().width();
45 45 qreal height = geometry().height();
46 46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 48 qreal scaleY = (height / rangeY);
49 49 qreal scaleX = (width / rangeX);
50 50 qreal barWidth = scaleX * m_series->d_func()->barWidth();
51 51
52 52 int itemIndex(0);
53 53 for (int category = 0; category < categoryCount; category++) {
54 54 qreal colSum = m_series->d_func()->categorySum(category);
55 55 qreal percentage = (100 / colSum);
56 56 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
57 57 for (int set=0; set < setCount; set++) {
58 58 QBarSet* barSet = m_series->d_func()->barsetAt(set);
59 59
60 60 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
61 61
62 62 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
63 63 Bar* bar = m_bars.at(itemIndex);
64 64 bar->setPen(barSet->pen());
65 65 bar->setBrush(barSet->brush());
66 66 bar->setVisible(barsVisible);
67 67
68 68 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
69 69 layout.append(rect);
70 70
71 71 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
72 72
73 73 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
74 74 int p = m_series->d_func()->percentageAt(set,category) * 100;
75 75 QString vString(QString::number(p));
76 76 vString.truncate(3);
77 77 vString.append("%");
78 78 label->setText(vString);
79 79 } else {
80 80 label->setText(QString(""));
81 81 }
82 82
83 83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
84 84 ,yPos - barHeight/2 - label->boundingRect().height()/2);
85 85 label->setFont(barSet->labelFont());
86 86 label->setBrush(barSet->labelBrush());
87 87
88 88 itemIndex++;
89 89 yPos -= barHeight;
90 90 }
91 91 }
92 92 return layout;
93 93 }
94 94
95 void PercentBarChartItem::applyLayout(const QVector<QRectF> &layout)
96 {
97 if (animator()) {
98 animator()->updateLayout(this, m_layout, layout);
99 } else {
100 setLayout(layout);
101 update();
102 }
103 }
95 104 #include "moc_percentbarchartitem_p.cpp"
96 105
97 106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,53 +1,54
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30
31 31 #ifndef PERCENTBARCHARTITEM_H
32 32 #define PERCENTBARCHARTITEM_H
33 33
34 34 #include "barchartitem_p.h"
35 35 #include <QGraphicsItem>
36 36
37 37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 38
39 39 class QBarSeries;
40 40
41 41 class PercentBarChartItem : public BarChartItem
42 42 {
43 43 Q_OBJECT
44 44 public:
45 45 PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter);
46 46
47 47 private:
48 48 virtual QVector<QRectF> calculateLayout();
49 virtual void applyLayout(const QVector<QRectF> &layout);
49 50 };
50 51
51 52 QTCOMMERCIALCHART_END_NAMESPACE
52 53
53 54 #endif // PERCENTBARCHARTITEM_H
@@ -1,90 +1,101
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "stackedbarchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "qbarset_p.h"
24 24 #include "qbarseries_p.h"
25 25 #include "qbarset.h"
26 #include "chartanimator_p.h"
26 27
27 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 29
29 30 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 31 BarChartItem(series, presenter)
31 32 {
32 33 }
33 34
34 35 QVector<QRectF> StackedBarChartItem::calculateLayout()
35 36 {
36 37 QVector<QRectF> layout;
37 38 // Use temporary qreals for accuracy
38 39 qreal categoryCount = m_series->d_func()->categoryCount();
39 40 qreal setCount = m_series->barsetCount();
40 41 bool barsVisible = m_series->isVisible();
41 42
42 43 // Domain:
43 44 qreal width = geometry().width();
44 45 qreal height = geometry().height();
45 46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 48 qreal scaleY = (height / rangeY);
48 49 qreal scaleX = (width / rangeX);
49 50 qreal barWidth = scaleX * m_series->d_func()->barWidth();
50 51
51 52 int itemIndex(0);
52 53 for (int category = 0; category < categoryCount; category++) {
53 54 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
54 55 for (int set=0; set < setCount; set++) {
55 56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56 57
57 58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
58 59
59 60 qreal barHeight = barSet->at(category).y() * scaleY;
60 61 Bar* bar = m_bars.at(itemIndex);
61 62 bar->setPen(barSet->pen());
62 63 bar->setBrush(barSet->brush());
63 64 bar->setVisible(barsVisible);
64 65
65 66 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
66 67 layout.append(rect);
67 68
68 69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
69 70
70 71 if (!qFuzzyIsNull(barSet->at(category).y())) {
71 72 label->setText(QString::number(barSet->at(category).y()));
72 73 } else {
73 74 label->setText(QString(""));
74 75 }
75 76
76 77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
77 78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
78 79 label->setFont(barSet->labelFont());
79 80 label->setBrush(barSet->labelBrush());
80 81 itemIndex++;
81 82 yPos -= barHeight;
82 83 }
83 84 }
84 85
85 86 return layout;
86 87 }
87 88
89 void StackedBarChartItem::applyLayout(const QVector<QRectF> &layout)
90 {
91 if (animator()) {
92 animator()->updateLayout(this, m_layout, layout);
93 } else {
94 setLayout(layout);
95 update();
96 }
97 }
98
88 99 #include "moc_stackedbarchartitem_p.cpp"
89 100
90 101 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,52 +1,53
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30
31 31 #ifndef STACKEDBARCHARTITEM_H
32 32 #define STACKEDBARCHARTITEM_H
33 33
34 34 #include "barchartitem_p.h"
35 35 #include "qstackedbarseries.h"
36 36 #include <QGraphicsItem>
37 37
38 38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 39
40 40 class StackedBarChartItem : public BarChartItem
41 41 {
42 42 Q_OBJECT
43 43 public:
44 44 StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter);
45 45
46 46 private:
47 47 virtual QVector<QRectF> calculateLayout();
48 virtual void applyLayout(const QVector<QRectF> &layout);
48 49 };
49 50
50 51 QTCOMMERCIALCHART_END_NAMESPACE
51 52
52 53 #endif // STACKEDBARCHARTITEM_H
General Comments 0
You need to be logged in to leave comments. Login now