##// END OF EJS Templates
Fixed animation to work when adding a new series....
Mika Salmela -
r2478:5245fa8e4b16
parent child
Show More
@@ -1,63 +1,61
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 "boxplotanimation_p.h"
22 22 #include "boxplotchartitem_p.h"
23 23 #include "boxwhiskersdata_p.h"
24 24 #include "boxwhiskersanimation_p.h"
25 #include <QDebug>
25 26
26 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 28
28 29 BoxPlotAnimation::BoxPlotAnimation(BoxPlotChartItem *item)
29 // : ChartAnimation(item),
30 30 : m_item(item)
31 31 {
32 32 }
33 33
34 //BoxPlotAnimation::BoxPlotAnimation(BoxWhiskers *box)
35 // : ChartAnimation(box),
36 // m_box(box)
37 //{
38 //}
39
40 34 BoxPlotAnimation::~BoxPlotAnimation()
41 35 {
42 36 }
43 37
44 38 void BoxPlotAnimation::addBox(BoxWhiskers *box)
45 39 {
46 BoxWhiskersAnimation *animation = new BoxWhiskersAnimation(box);
47 m_animations.insert(box, animation);
48
49 BoxWhiskersData start;
50 start.m_median = box->m_data.m_median;
51 animation->setup(start, box->m_data);
40 BoxWhiskersAnimation *animation = m_animations.value(box);
41 if (!animation) {
42 animation = new BoxWhiskersAnimation(box);
43 m_animations.insert(box, animation);
44 BoxWhiskersData start;
45 start.m_median = box->m_data.m_median;
46 animation->setup(start, box->m_data);
47 } else {
48 animation->stop();
49 animation->setEndData(box->m_data);
50 }
52 51 }
53 52
54 53 ChartAnimation *BoxPlotAnimation::boxAnimation(BoxWhiskers *box)
55 54 {
56 55 // TODO: Check for missing animation
57 56 return m_animations.value(box);
58 57 }
59 58
60
61 59 //#include "moc_boxplotanimation_p.cpp"
62 60
63 61 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,106 +1,113
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 "boxwhiskersanimation_p.h"
22 22 #include "boxplotanimation_p.h"
23 23 #include "boxplotchartitem_p.h"
24 24 #include "boxwhiskersdata_p.h"
25 #include <QDebug>
25 26
26 27 Q_DECLARE_METATYPE(QVector<QRectF>)
27 28 Q_DECLARE_METATYPE(QTCOMMERCIALCHART_NAMESPACE::BoxWhiskersData)
28 29 Q_DECLARE_METATYPE(qreal)
29 30
30 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 32
32 33 BoxWhiskersAnimation::BoxWhiskersAnimation(BoxPlotChartItem *item)
33 34 : ChartAnimation(item),
34 35 m_item(item)
35 36 {
36 37 setDuration(ChartAnimationDuration);
37 38 setEasingCurve(QEasingCurve::OutQuart);
38 39 }
39 40
40 41 BoxWhiskersAnimation::BoxWhiskersAnimation(BoxWhiskers *box)
41 42 : ChartAnimation(box),
42 43 m_box(box)
43 44 {
44 45 setDuration(ChartAnimationDuration);
45 46 setEasingCurve(QEasingCurve::OutQuart);
46 47 }
47 48
48 49 BoxWhiskersAnimation::~BoxWhiskersAnimation()
49 50 {
50 51 }
51 52
52 53 QVariant BoxWhiskersAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
53 54 {
54 55 BoxWhiskersData startData = qvariant_cast<BoxWhiskersData>(from);
55 56 BoxWhiskersData endData = qvariant_cast<BoxWhiskersData>(to);
56 57 BoxWhiskersData result;
57 58
58 59 if (endData.m_index == 1) {
59 60 //qDebug() << "endData.m_lowerExtreme = " << endData.m_lowerExtreme;
60 61 //qDebug() << "endData.m_median = " << endData.m_median;
61 62 }
62 63
63 64 result.m_lowerExtreme = endData.m_median + progress * (endData.m_lowerExtreme - endData.m_median);
64 65 result.m_lowerQuartile = endData.m_median + progress * (endData.m_lowerQuartile - endData.m_median);
65 66 result.m_median = endData.m_median;
66 67 result.m_upperQuartile = endData.m_median + progress * (endData.m_upperQuartile - endData.m_median);
67 68 result.m_upperExtreme = endData.m_median + progress * (endData.m_upperExtreme - endData.m_median);
68 69 result.m_index = endData.m_index;
69 70 result.m_boxItems = endData.m_boxItems;
70 71
71 72 // result.m_lowerExtreme = endData.m_lowerExtreme;
72 73 // result.m_lowerQuartile = endData.m_lowerQuartile;
73 74 // result.m_median = endData.m_median;
74 75 // result.m_upperQuartile = endData.m_upperQuartile;
75 76 // result.m_upperExtreme = endData.m_upperExtreme;
76 77 // result.m_index = endData.m_index;
77 78 // result.m_boxItems = endData.m_boxItems;
78 79
79 80 result.m_maxX = endData.m_maxX;
80 81 result.m_minX = endData.m_minX;
81 82 result.m_maxY = endData.m_maxY;
82 83 result.m_minY = endData.m_minY;
83 //result.m_domainSize = endData.m_domainSize;
84 result.m_seriesIndex = endData.m_seriesIndex;
85 result.m_seriesCount = endData.m_seriesCount;
84 86
85 87 return qVariantFromValue(result);
86 88 }
87 89
88 90 void BoxWhiskersAnimation::updateCurrentValue(const QVariant &value)
89 91 {
90 92 BoxWhiskersData data = qvariant_cast<BoxWhiskersData>(value);
91 93 m_box->setLayout(data);
92 94 }
93 95
94 96 void BoxWhiskersAnimation::setup(const BoxWhiskersData &startData, const BoxWhiskersData &endData)
95 97 {
96 if (endData.m_index == 0) {
97 qDebug() << "BoxPlotAnimation::setup m_upperExtreme" << endData.m_upperExtreme;
98 }
99 98 setKeyValueAt(0.0, qVariantFromValue(startData));
100 99 setKeyValueAt(1.0, qVariantFromValue(endData));
101 100 }
102 101
102 void BoxWhiskersAnimation::setEndData(const BoxWhiskersData &endData)
103 {
104 if (state() != QAbstractAnimation::Stopped)
105 stop();
106
107 setEndValue(qVariantFromValue(endData));
108 }
109
103 110 #include "moc_boxwhiskersanimation_p.cpp"
104 111
105 112 QTCOMMERCIALCHART_END_NAMESPACE
106 113
@@ -1,64 +1,65
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2013 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 BOXWHISKERSANIMATION_P_H
31 31 #define BOXWHISKERSANIMATION_P_H
32 32
33 33 #include "chartanimation_p.h"
34 34 #include "boxwhiskers_p.h"
35 35 #include "boxwhiskersdata_p.h"
36 36
37 37 QTCOMMERCIALCHART_BEGIN_NAMESPACE
38 38
39 39 class BoxPlotChartItem;
40 40
41 41 class BoxWhiskersAnimation : public ChartAnimation
42 42 {
43 43 Q_OBJECT
44 44
45 45 public:
46 46 BoxWhiskersAnimation(BoxPlotChartItem *item);
47 47 BoxWhiskersAnimation(BoxWhiskers *box);
48 48 ~BoxWhiskersAnimation();
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 54 void setup(const BoxWhiskersData &startData, const BoxWhiskersData &endData);
55 void setEndData(const BoxWhiskersData &endData);
55 56
56 57 protected:
57 58 BoxPlotChartItem *m_item;
58 59 BoxWhiskers *m_box;
59 60 BoxWhiskersData *m_boxData;
60 61 };
61 62
62 63 QTCOMMERCIALCHART_END_NAMESPACE
63 64
64 65 #endif // BOXWHISKERSANIMATION_P_H
@@ -1,193 +1,189
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 "boxwhiskers_p.h"
22 22 #include <QPainter>
23 23 #include <QDebug>
24 24 #include <QWidget>
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 BoxWhiskers::BoxWhiskers(AbstractDomain *domain, QGraphicsObject *parent) :
29 29 QGraphicsObject(parent),
30 30 m_domain(domain)
31 31 {
32 32 }
33 33
34 34 BoxWhiskers::~BoxWhiskers()
35 35 {
36 36 //qDebug() << "BoxWhiskers::~BoxWhiskers()";
37 37 }
38 38
39 39 void BoxWhiskers::mousePressEvent(QGraphicsSceneMouseEvent *event)
40 40 {
41 41 Q_UNUSED(event)
42 42
43 43 qDebug() << "BoxWhiskers::mousePressEvent";
44 44 }
45 45
46 46 void BoxWhiskers::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
47 47 {
48 48 Q_UNUSED(event)
49 49
50 50 qDebug() << "BoxWhiskers::hoverEnterEvent";
51 51 }
52 52
53 53 void BoxWhiskers::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
54 54 {
55 55 Q_UNUSED(event)
56 56
57 57 qDebug() << "BoxWhiskers::hoverLeaveEvent";
58 58 }
59 59
60 60 void BoxWhiskers::setBrush(const QBrush &brush)
61 61 {
62 62 m_brush = brush;
63 63 }
64 64
65 65 void BoxWhiskers::setPen(const QPen &pen)
66 66 {
67 67 m_pen = pen;
68 68 }
69 69
70 70 void BoxWhiskers::setLayout(const BoxWhiskersData &data)
71 71 {
72 72 m_data = data;
73 73 // if (m_data.m_index == 1)
74 74 // qDebug() << "BoxWhiskers::setLayout";
75 75 updateGeometry();
76 //update(0.0, 0.0, m_data.m_domainSize.width(), m_data.m_domainSize.height());
77 76 update();
78 77 }
79 78
80 79
81 80 QSizeF BoxWhiskers::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
82 81 {
83 82 //Q_UNUSED(which)
84 83 Q_UNUSED(constraint)
85 84
86 85 qDebug() << "BoxWhiskers::sizeHint, which = " << which;
87 86
88 87 return QSizeF();
89 88 }
90 89
91 90 void BoxWhiskers::setGeometry(const QRectF &rect) // TODO: Unused?
92 91 {
93 92 Q_UNUSED(rect)
94 93
95 94 qDebug() << "BoxWhiskers::setGeometry";
96 95 }
97 96
98 97 void BoxWhiskers::setDomainSize(const QSizeF &size)
99 98 {
100 99 m_domainSize = size;
101 100
102 101 updateBoundingRect();
103 102 }
104 103
105 104 QRectF BoxWhiskers::boundingRect() const
106 105 {
107 106 return m_boundingRect;
108 107 }
109 108
110 109 void BoxWhiskers::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
111 110 {
112 111 Q_UNUSED(option)
113 112 Q_UNUSED(widget)
114 113 //Q_UNUSED(painter)
115 114
116 115 //painter->save();
117 116 //painter->setClipRect(parentItem()->boundingRect());
118 117 painter->setPen(m_pen);
119 118 painter->setBrush(m_brush);
120 119 qreal spanY = m_data.m_maxY - m_data.m_minY;
121 120 //painter->setClipRect(parentItem()->boundingRect());
122 121 painter->scale(m_domainSize.width() / m_data.m_boxItems, m_domainSize.height() / spanY);
123 122 painter->drawPath(m_boxPath);
124 123 //painter->restore();
125 124 }
126 125
127 126 void BoxWhiskers::updateGeometry()
128 127 {
129 128 prepareGeometryChange();
130 129
131 130 QPainterPath path;
132 131
133 // TODO: Dirty hack
134 if (m_data.m_seriesCount == 0) m_data.m_seriesCount = 1;
135
136 132 qreal columnWidth = 1.0 / m_data.m_seriesCount;
137 133 qreal left = 0.25 * columnWidth + columnWidth * m_data.m_seriesIndex;
138 134 qreal right = 0.75 * columnWidth + columnWidth * m_data.m_seriesIndex;
139 135 qreal middle = 0.5 * columnWidth + columnWidth * m_data.m_seriesIndex;
140 136
141 137 //whisker = 0.35 0.75
142 138
143 139 // Upper whisker
144 140 path.moveTo(left + m_data.m_index, m_data.m_maxY - m_data.m_upperExtreme);
145 141 path.lineTo(right + m_data.m_index, m_data.m_maxY - m_data.m_upperExtreme);
146 142 path.moveTo(middle + m_data.m_index, m_data.m_maxY - m_data.m_upperExtreme);
147 143 path.lineTo(middle + m_data.m_index, m_data.m_maxY - m_data.m_upperQuartile);
148 144 path.closeSubpath();
149 145
150 146 // Middle Box
151 147 path.addRect(left + m_data.m_index, m_data.m_maxY - m_data.m_upperQuartile,
152 148 0.5 * columnWidth, m_data.m_upperQuartile - m_data.m_lowerQuartile);
153 149
154 150 // Median/mean line
155 151 path.moveTo(left + m_data.m_index, m_data.m_maxY - m_data.m_median);
156 152 path.lineTo(right + m_data.m_index, m_data.m_maxY - m_data.m_median);
157 153
158 154 // Lower whisker
159 155 path.moveTo(left + m_data.m_index, m_data.m_maxY - m_data.m_lowerExtreme);
160 156 path.lineTo(right + m_data.m_index, m_data.m_maxY - m_data.m_lowerExtreme);
161 157 path.moveTo(middle + m_data.m_index, m_data.m_maxY - m_data.m_lowerExtreme);
162 158 path.lineTo(middle + m_data.m_index, m_data.m_maxY - m_data.m_lowerQuartile);
163 159 path.closeSubpath();
164 160
165 161 m_boxPath = path;
166 162
167 163 updateBoundingRect();
168 164
169 165 // qreal scaleY = m_domainSize.height() / (m_data.m_maxY - m_data.m_minY);
170 166 // qreal scaleX = m_domainSize.width() / m_data.m_boxItems;
171 167 // QRectF br = path.boundingRect();
172 168 // m_boundingRect = QRectF( br.x() * scaleX, br.y() * scaleY, br.width() * scaleX, br.height() * scaleY);
173 169
174 170 if (m_data.m_index == 5) {
175 171 //qDebug() << "myValue = " << myValue;
176 172 //qDebug() << "m_data.m_upperExtreme" << m_data.m_upperExtreme;
177 173 //qDebug() << "m_boundingRect = " << m_boundingRect;
178 174 // qDebug() << "x = " << m_boundingRect.x() << ", y = " << m_boundingRect.y() << ", width = "
179 175 // << m_boundingRect.width() << ", height = " << m_boundingRect.height();
180 176 }
181 177 }
182 178
183 179 void BoxWhiskers::updateBoundingRect()
184 180 {
185 181 qreal scaleY = m_domainSize.height() / (m_data.m_maxY - m_data.m_minY);
186 182 qreal scaleX = m_domainSize.width() / m_data.m_boxItems;
187 183 QRectF br = m_boxPath.boundingRect();
188 184 m_boundingRect = QRectF( br.x() * scaleX, br.y() * scaleY, br.width() * scaleX, br.height() * scaleY);
189 185 }
190 186
191 187 #include "moc_boxwhiskers_p.cpp"
192 188
193 189 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now