##// END OF EJS Templates
Fix add/remove animation for pie and apply colors from theme when adding/removing
Jani Honkonen -
r659:7d415c8db783
parent child
Show More
@@ -18,6 +18,26 QPointF linearPos(QPointF start, QPointF end, qreal pos)
18 return QPointF(x, y);
18 return QPointF(x, y);
19 }
19 }
20
20
21 QPen linearPos(QPen start, QPen end, qreal pos)
22 {
23 QColor c;
24 c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos));
25 c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos));
26 c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos));
27 end.setColor(c);
28 return end;
29 }
30
31 QBrush linearPos(QBrush start, QBrush end, qreal pos)
32 {
33 QColor c;
34 c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos));
35 c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos));
36 c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos));
37 end.setColor(c);
38 return end;
39 }
40
21 PieSliceAnimation::PieSliceAnimation(PieChartItem *item, QPieSlice *slice)
41 PieSliceAnimation::PieSliceAnimation(PieChartItem *item, QPieSlice *slice)
22 :QVariantAnimation(item),
42 :QVariantAnimation(item),
23 m_item(item),
43 m_item(item),
@@ -34,6 +54,8 void PieSliceAnimation::setValue(const PieSliceLayout &startValue, const PieSlic
34 if (state() != QAbstractAnimation::Stopped)
54 if (state() != QAbstractAnimation::Stopped)
35 stop();
55 stop();
36
56
57 m_currentValue = startValue;
58
37 setKeyValueAt(0.0, qVariantFromValue(startValue));
59 setKeyValueAt(0.0, qVariantFromValue(startValue));
38 setKeyValueAt(1.0, qVariantFromValue(endValue));
60 setKeyValueAt(1.0, qVariantFromValue(endValue));
39 }
61 }
@@ -43,13 +65,18 void PieSliceAnimation::updateValue(const PieSliceLayout &endValue)
43 if (state() != QAbstractAnimation::Stopped)
65 if (state() != QAbstractAnimation::Stopped)
44 stop();
66 stop();
45
67
46 setKeyValueAt(0.0, qVariantFromValue(currentSliceValue()));
68 setKeyValueAt(0.0, qVariantFromValue(m_currentValue));
47 setKeyValueAt(1.0, qVariantFromValue(endValue));
69 setKeyValueAt(1.0, qVariantFromValue(endValue));
48 }
70 }
49
71
50 PieSliceLayout PieSliceAnimation::currentSliceValue()
72 PieSliceLayout PieSliceAnimation::currentSliceValue()
51 {
73 {
52 return qVariantValue<PieSliceLayout>(currentValue());
74 // NOTE:
75 // We must use an internal current value because QVariantAnimation::currentValue() is updated
76 // before the animation is actually started. So if we get 2 updateValue() calls in a row the currentValue()
77 // will have the end value set from the first call and the second call will interpolate that instead of
78 // the original current value as it was before the first call.
79 return m_currentValue;
53 }
80 }
54
81
55 QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
82 QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
@@ -63,15 +90,18 QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &
63 result.m_radius = linearPos(startValue.m_radius, endValue.m_radius, progress);
90 result.m_radius = linearPos(startValue.m_radius, endValue.m_radius, progress);
64 result.m_startAngle = linearPos(startValue.m_startAngle, endValue.m_startAngle, progress);
91 result.m_startAngle = linearPos(startValue.m_startAngle, endValue.m_startAngle, progress);
65 result.m_angleSpan = linearPos(startValue.m_angleSpan, endValue.m_angleSpan, progress);
92 result.m_angleSpan = linearPos(startValue.m_angleSpan, endValue.m_angleSpan, progress);
93 result.m_pen = linearPos(startValue.m_pen, endValue.m_pen, progress);
94 result.m_brush = linearPos(startValue.m_brush, endValue.m_brush, progress);
66
95
67 return qVariantFromValue(result);
96 return qVariantFromValue(result);
68 }
97 }
69
98
70 void PieSliceAnimation::updateCurrentValue(const QVariant &value)
99 void PieSliceAnimation::updateCurrentValue(const QVariant &value)
71 {
100 {
72 PieSliceLayout layout = qVariantValue<PieSliceLayout>(value);
101 if (state() != QAbstractAnimation::Stopped) { //workaround
73 if (state() != QAbstractAnimation::Stopped) //workaround
102 m_currentValue = qVariantValue<PieSliceLayout>(value);
74 m_item->setLayout(m_slice, layout);
103 m_item->setLayout(m_slice, m_currentValue);
104 }
75 }
105 }
76
106
77 QTCOMMERCIALCHART_END_NAMESPACE
107 QTCOMMERCIALCHART_END_NAMESPACE
@@ -25,6 +25,7 protected:
25 private:
25 private:
26 PieChartItem *m_item;
26 PieChartItem *m_item;
27 QPieSlice *m_slice;
27 QPieSlice *m_slice;
28 PieSliceLayout m_currentValue;
28 };
29 };
29
30
30 QTCOMMERCIALCHART_END_NAMESPACE
31 QTCOMMERCIALCHART_END_NAMESPACE
@@ -212,7 +212,7 void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain)
212
212
213 case QSeries::SeriesTypePie: {
213 case QSeries::SeriesTypePie: {
214 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
214 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
215 PieChartItem* pie = new PieChartItem(m_chart, pieSeries);
215 PieChartItem* pie = new PieChartItem(pieSeries, this, m_chart);
216 if(m_options.testFlag(QChart::SeriesAnimations)) {
216 if(m_options.testFlag(QChart::SeriesAnimations)) {
217 m_animator->addAnimation(pie);
217 m_animator->addAnimation(pie);
218 }
218 }
@@ -39,7 +39,10 public:
39 int margin() const;
39 int margin() const;
40
40
41 QRectF geometry() const;
41 QRectF geometry() const;
42 ChartAnimator* animator() const {return m_animator;};
42
43 ChartAnimator* animator() const {return m_animator;}
44 ChartTheme *theme() { return m_chartTheme; }
45 ChartDataSet *dataSet() { return m_dataset; }
43
46
44 void setChartTheme(QChart::ChartTheme theme,bool force = true);
47 void setChartTheme(QChart::ChartTheme theme,bool force = true);
45 QChart::ChartTheme chartTheme();
48 QChart::ChartTheme chartTheme();
@@ -52,6 +55,7 public:
52 void zoomOut();
55 void zoomOut();
53 void zoomReset();
56 void zoomReset();
54 void scroll(int dx,int dy);
57 void scroll(int dx,int dy);
58
55 private:
59 private:
56 void createConnections();
60 void createConnections();
57 void resetAllElements();
61 void resetAllElements();
@@ -3,6 +3,7
3 #include "qpieslice.h"
3 #include "qpieslice.h"
4 #include "qpieseries.h"
4 #include "qpieseries.h"
5 #include "chartpresenter_p.h"
5 #include "chartpresenter_p.h"
6 #include "chartdataset_p.h"
6 #include "chartanimator_p.h"
7 #include "chartanimator_p.h"
7 #include <QDebug>
8 #include <QDebug>
8 #include <QPainter>
9 #include <QPainter>
@@ -10,9 +11,10
10
11
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
13
13 PieChartItem::PieChartItem(QGraphicsItem *parent, QPieSeries *series)
14 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter *presenter, QGraphicsItem *parent)
14 :ChartItem(parent),
15 :ChartItem(parent),
15 m_series(series)
16 m_series(series),
17 m_presenter(presenter)
16 {
18 {
17 Q_ASSERT(series);
19 Q_ASSERT(series);
18 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
20 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
@@ -49,6 +51,8 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
49 {
51 {
50 bool isEmpty = m_slices.isEmpty();
52 bool isEmpty = m_slices.isEmpty();
51
53
54 m_presenter->theme()->decorate(m_series, m_presenter->dataSet()->seriesIndex(m_series));
55
52 foreach (QPieSlice *s, slices) {
56 foreach (QPieSlice *s, slices) {
53 PieSlice* slice = new PieSlice(this);
57 PieSlice* slice = new PieSlice(this);
54 m_slices.insert(s, slice);
58 m_slices.insert(s, slice);
@@ -68,6 +72,8 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
68
72
69 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
73 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
70 {
74 {
75 m_presenter->theme()->decorate(m_series, m_presenter->dataSet()->seriesIndex(m_series));
76
71 foreach (QPieSlice *s, slices) {
77 foreach (QPieSlice *s, slices) {
72 if (m_animator)
78 if (m_animator)
73 m_animator->removeAnimation(this, s);
79 m_animator->removeAnimation(this, s);
@@ -126,6 +132,8 PieSliceLayout PieChartItem::calculateSliceLayout(QPieSlice *slice)
126 sliceLayout.m_radius = m_pieRadius;
132 sliceLayout.m_radius = m_pieRadius;
127 sliceLayout.m_startAngle = slice->startAngle();
133 sliceLayout.m_startAngle = slice->startAngle();
128 sliceLayout.m_angleSpan = slice->m_angleSpan;
134 sliceLayout.m_angleSpan = slice->m_angleSpan;
135 sliceLayout.m_pen = slice->m_slicePen;
136 sliceLayout.m_brush = slice->m_sliceBrush;
129 return sliceLayout;
137 return sliceLayout;
130 }
138 }
131
139
@@ -8,6 +8,7
8 class QGraphicsItem;
8 class QGraphicsItem;
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 class QPieSlice;
10 class QPieSlice;
11 class ChartPresenter;
11
12
12 typedef QHash<QPieSlice*, PieSliceLayout> PieLayout;
13 typedef QHash<QPieSlice*, PieSliceLayout> PieLayout;
13
14
@@ -17,7 +18,7 class PieChartItem : public QObject, public ChartItem
17
18
18 public:
19 public:
19 // TODO: use a generic data class instead of x and y
20 // TODO: use a generic data class instead of x and y
20 PieChartItem(QGraphicsItem *parent, QPieSeries *series);
21 PieChartItem(QPieSeries *series, ChartPresenter *presenter, QGraphicsItem *parent);
21 ~PieChartItem();
22 ~PieChartItem();
22
23
23 public: // from QGraphicsItem
24 public: // from QGraphicsItem
@@ -50,7 +51,7 private:
50 QRectF m_rect;
51 QRectF m_rect;
51 QPointF m_pieCenter;
52 QPointF m_pieCenter;
52 qreal m_pieRadius;
53 qreal m_pieRadius;
53 QRectF m_debugRect;
54 ChartPresenter *m_presenter;
54 };
55 };
55
56
56 QTCOMMERCIALCHART_END_NAMESPACE
57 QTCOMMERCIALCHART_END_NAMESPACE
@@ -54,8 +54,8 void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option
54 painter->setClipRect(parentItem()->boundingRect());
54 painter->setClipRect(parentItem()->boundingRect());
55
55
56 painter->save();
56 painter->save();
57 painter->setPen(m_slicePen);
57 painter->setPen(m_layout.m_pen);
58 painter->setBrush(m_sliceBrush);
58 painter->setBrush(m_layout.m_brush);
59 painter->drawPath(m_slicePath);
59 painter->drawPath(m_slicePath);
60 painter->restore();
60 painter->restore();
61
61
@@ -122,8 +122,6 void PieSlice::updateData(const QPieSlice* sliceData)
122
122
123 m_isExploded = sliceData->isExploded();
123 m_isExploded = sliceData->isExploded();
124 m_explodeDistanceFactor = sliceData->explodeDistanceFactor();
124 m_explodeDistanceFactor = sliceData->explodeDistanceFactor();
125 m_slicePen = sliceData->slicePen();
126 m_sliceBrush = sliceData->sliceBrush();
127
125
128 m_labelVisible = sliceData->isLabelVisible();
126 m_labelVisible = sliceData->isLabelVisible();
129 m_labelText = sliceData->label();
127 m_labelText = sliceData->label();
@@ -23,6 +23,8 public:
23 qreal m_radius;
23 qreal m_radius;
24 qreal m_startAngle;
24 qreal m_startAngle;
25 qreal m_angleSpan;
25 qreal m_angleSpan;
26 QPen m_pen;
27 QBrush m_brush;
26 };
28 };
27
29
28 class PieSlice : public QGraphicsObject
30 class PieSlice : public QGraphicsObject
@@ -63,8 +65,6 private:
63 bool m_isExploded;
65 bool m_isExploded;
64 qreal m_explodeDistanceFactor;
66 qreal m_explodeDistanceFactor;
65 bool m_labelVisible;
67 bool m_labelVisible;
66 QPen m_slicePen;
67 QBrush m_sliceBrush;
68
68
69 QPainterPath m_labelArmPath;
69 QPainterPath m_labelArmPath;
70 qreal m_labelArmLengthFactor;
70 qreal m_labelArmLengthFactor;
General Comments 0
You need to be logged in to leave comments. Login now