##// END OF EJS Templates
Getting rid of slice pointer in PieSliceLayout.
Jani Honkonen -
r629:54034d98d5c4
parent child
Show More
@@ -224,7 +224,7 void ChartAnimator::updateLayout(XYChartItem* item, QVector<QPointF>& oldPoints
224 QTimer::singleShot(0,animation,SLOT(start()));
224 QTimer::singleShot(0,animation,SLOT(start()));
225 }
225 }
226
226
227 void ChartAnimator::addAnimation(PieChartItem* item, QPieSlice *slice, PieSliceLayout &layout)
227 void ChartAnimator::addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout)
228 {
228 {
229 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
229 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
230 Q_ASSERT(animation);
230 Q_ASSERT(animation);
@@ -238,18 +238,18 void ChartAnimator::removeAnimation(PieChartItem* item, QPieSlice *slice)
238 animation->removeSlice(slice);
238 animation->removeSlice(slice);
239 }
239 }
240
240
241 void ChartAnimator::updateLayout(PieChartItem* item, QVector<PieSliceLayout> &layout)
241 void ChartAnimator::updateLayout(PieChartItem* item, const PieLayout &layout)
242 {
242 {
243 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
243 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
244 Q_ASSERT(animation);
244 Q_ASSERT(animation);
245 animation->updateValues(layout);
245 animation->updateValues(layout);
246 }
246 }
247
247
248 void ChartAnimator::updateLayout(PieChartItem* item, PieSliceLayout &layout)
248 void ChartAnimator::updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout)
249 {
249 {
250 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
250 PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item));
251 Q_ASSERT(animation);
251 Q_ASSERT(animation);
252 animation->updateValue(layout);
252 animation->updateValue(slice, layout);
253 }
253 }
254
254
255 void ChartAnimator::setState(State state,const QPointF& point)
255 void ChartAnimator::setState(State state,const QPointF& point)
@@ -35,10 +35,10 public:
35 void updateLayout(SplineChartItem* item, QVector<QPointF>& oldPoints , QVector<QPointF>& newPoints, QVector<QPointF>& oldControlPoints, QVector<QPointF>& newContorlPoints,int index);
35 void updateLayout(SplineChartItem* item, QVector<QPointF>& oldPoints , QVector<QPointF>& newPoints, QVector<QPointF>& oldControlPoints, QVector<QPointF>& newContorlPoints,int index);
36 void applyLayout(AxisItem* item, QVector<qreal>& layout);
36 void applyLayout(AxisItem* item, QVector<qreal>& layout);
37
37
38 void addAnimation(PieChartItem* item, QPieSlice *slice, PieSliceLayout &layout);
38 void addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout);
39 void removeAnimation(PieChartItem* item, QPieSlice *slice);
39 void removeAnimation(PieChartItem* item, QPieSlice *slice);
40 void updateLayout(PieChartItem* item, QVector<PieSliceLayout> &layout);
40 void updateLayout(PieChartItem* item, const PieLayout &layout);
41 void updateLayout(PieChartItem* item, PieSliceLayout &layout);
41 void updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout);
42
42
43 void setState(State state,const QPointF& point = QPointF());
43 void setState(State state,const QPointF& point = QPointF());
44
44
@@ -16,15 +16,15 PieAnimation::~PieAnimation()
16 {
16 {
17 }
17 }
18
18
19 void PieAnimation::updateValues(QVector<PieSliceLayout>& newValues)
19 void PieAnimation::updateValues(const PieLayout &newValues)
20 {
20 {
21 foreach (PieSliceLayout endLayout, newValues)
21 foreach (QPieSlice* s, newValues.keys())
22 updateValue(endLayout);
22 updateValue(s, newValues.value(s));
23 }
23 }
24
24
25 void PieAnimation::updateValue(PieSliceLayout& endLayout)
25 void PieAnimation::updateValue(QPieSlice *slice, const PieSliceLayout &endLayout)
26 {
26 {
27 PieSliceAnimation *animation = m_animations.value(endLayout.m_data);
27 PieSliceAnimation *animation = m_animations.value(slice);
28 Q_ASSERT(animation);
28 Q_ASSERT(animation);
29 animation->stop();
29 animation->stop();
30
30
@@ -35,9 +35,9 void PieAnimation::updateValue(PieSliceLayout& endLayout)
35 QTimer::singleShot(0, animation, SLOT(start()));
35 QTimer::singleShot(0, animation, SLOT(start()));
36 }
36 }
37
37
38 void PieAnimation::addSlice(QPieSlice *slice, PieSliceLayout endLayout)
38 void PieAnimation::addSlice(QPieSlice *slice, const PieSliceLayout &endLayout)
39 {
39 {
40 PieSliceAnimation *animation = new PieSliceAnimation(m_item);
40 PieSliceAnimation *animation = new PieSliceAnimation(m_item, slice);
41 m_animations.insert(slice, animation);
41 m_animations.insert(slice, animation);
42
42
43 PieSliceLayout startLayout = endLayout;
43 PieSliceLayout startLayout = endLayout;
@@ -16,9 +16,9 class PieAnimation : public ChartAnimation
16 public:
16 public:
17 PieAnimation(PieChartItem *item);
17 PieAnimation(PieChartItem *item);
18 ~PieAnimation();
18 ~PieAnimation();
19 void updateValues(QVector<PieSliceLayout>& newValues);
19 void updateValues(const PieLayout &newValues);
20 void updateValue(PieSliceLayout& newValue);
20 void updateValue(QPieSlice *slice, const PieSliceLayout &newValue);
21 void addSlice(QPieSlice *slice, PieSliceLayout endLayout);
21 void addSlice(QPieSlice *slice, const PieSliceLayout &endLayout);
22 void removeSlice(QPieSlice *slice);
22 void removeSlice(QPieSlice *slice);
23
23
24 public: // from QVariantAnimation
24 public: // from QVariantAnimation
@@ -18,9 +18,10 QPointF linearPos(QPointF start, QPointF end, qreal pos)
18 return QPointF(x, y);
18 return QPointF(x, y);
19 }
19 }
20
20
21 PieSliceAnimation::PieSliceAnimation(PieChartItem *item)
21 PieSliceAnimation::PieSliceAnimation(PieChartItem *item, QPieSlice *slice)
22 :QVariantAnimation(item),
22 :QVariantAnimation(item),
23 m_item(item)
23 m_item(item),
24 m_slice(slice)
24 {
25 {
25 }
26 }
26
27
@@ -28,7 +29,7 PieSliceAnimation::~PieSliceAnimation()
28 {
29 {
29 }
30 }
30
31
31 void PieSliceAnimation::setValue(PieSliceLayout& startValue, PieSliceLayout& endValue)
32 void PieSliceAnimation::setValue(const PieSliceLayout &startValue, const PieSliceLayout &endValue)
32 {
33 {
33 if (state() != QAbstractAnimation::Stopped)
34 if (state() != QAbstractAnimation::Stopped)
34 stop();
35 stop();
@@ -37,13 +38,11 void PieSliceAnimation::setValue(PieSliceLayout& startValue, PieSliceLayout& end
37 setKeyValueAt(1.0, qVariantFromValue(endValue));
38 setKeyValueAt(1.0, qVariantFromValue(endValue));
38 }
39 }
39
40
40 void PieSliceAnimation::updateValue(PieSliceLayout& endValue)
41 void PieSliceAnimation::updateValue(const PieSliceLayout &endValue)
41 {
42 {
42 if (state() != QAbstractAnimation::Stopped)
43 if (state() != QAbstractAnimation::Stopped)
43 stop();
44 stop();
44
45
45 //qDebug() << "PieSliceAnimation::updateValue()" << endValue.m_data->label() << currentSliceValue().m_startAngle << endValue.m_startAngle;
46
47 setKeyValueAt(0.0, qVariantFromValue(currentSliceValue()));
46 setKeyValueAt(0.0, qVariantFromValue(currentSliceValue()));
48 setKeyValueAt(1.0, qVariantFromValue(endValue));
47 setKeyValueAt(1.0, qVariantFromValue(endValue));
49 }
48 }
@@ -72,7 +71,7 void PieSliceAnimation::updateCurrentValue(const QVariant &value)
72 {
71 {
73 PieSliceLayout layout = qVariantValue<PieSliceLayout>(value);
72 PieSliceLayout layout = qVariantValue<PieSliceLayout>(value);
74 if (state() != QAbstractAnimation::Stopped) //workaround
73 if (state() != QAbstractAnimation::Stopped) //workaround
75 m_item->setLayout(layout);
74 m_item->setLayout(m_slice, layout);
76 }
75 }
77
76
78 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
@@ -7,14 +7,15
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class PieChartItem;
9 class PieChartItem;
10 class QPieSlice;
10
11
11 class PieSliceAnimation : public QVariantAnimation
12 class PieSliceAnimation : public QVariantAnimation
12 {
13 {
13 public:
14 public:
14 PieSliceAnimation(PieChartItem *item);
15 PieSliceAnimation(PieChartItem *item, QPieSlice *slice);
15 ~PieSliceAnimation();
16 ~PieSliceAnimation();
16 void setValue(PieSliceLayout& startValue, PieSliceLayout& endValue);
17 void setValue(const PieSliceLayout &startValue, const PieSliceLayout &endValue);
17 void updateValue(PieSliceLayout& endValue);
18 void updateValue(const PieSliceLayout &endValue);
18 PieSliceLayout currentSliceValue();
19 PieSliceLayout currentSliceValue();
19
20
20 protected:
21 protected:
@@ -23,6 +24,7 protected:
23
24
24 private:
25 private:
25 PieChartItem *m_item;
26 PieChartItem *m_item;
27 QPieSlice *m_slice;
26 };
28 };
27
29
28 QTCOMMERCIALCHART_END_NAMESPACE
30 QTCOMMERCIALCHART_END_NAMESPACE
@@ -60,7 +60,7 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
60 if (m_animator)
60 if (m_animator)
61 m_animator->addAnimation(this, s, layout);
61 m_animator->addAnimation(this, s, layout);
62 else
62 else
63 setLayout(layout);
63 setLayout(s, layout);
64 }
64 }
65 }
65 }
66
66
@@ -76,7 +76,7 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
76
76
77 void PieChartItem::handlePieLayoutChanged()
77 void PieChartItem::handlePieLayoutChanged()
78 {
78 {
79 QVector<PieSliceLayout> layout = calculateLayout();
79 PieLayout layout = calculateLayout();
80 applyLayout(layout);
80 applyLayout(layout);
81 update();
81 update();
82 }
82 }
@@ -86,7 +86,7 void PieChartItem::handleSliceChanged()
86 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
86 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
87 Q_ASSERT(m_slices.contains(slice));
87 Q_ASSERT(m_slices.contains(slice));
88 PieSliceLayout layout = calculateSliceLayout(slice);
88 PieSliceLayout layout = calculateSliceLayout(slice);
89 updateLayout(layout);
89 updateLayout(slice, layout);
90 update();
90 update();
91 }
91 }
92
92
@@ -120,7 +120,6 void PieChartItem::calculatePieLayout()
120 PieSliceLayout PieChartItem::calculateSliceLayout(QPieSlice *slice)
120 PieSliceLayout PieChartItem::calculateSliceLayout(QPieSlice *slice)
121 {
121 {
122 PieSliceLayout sliceLayout;
122 PieSliceLayout sliceLayout;
123 sliceLayout.m_data = slice;
124 sliceLayout.m_center = PieSlice::sliceCenter(m_pieCenter, m_pieRadius, slice);
123 sliceLayout.m_center = PieSlice::sliceCenter(m_pieCenter, m_pieRadius, slice);
125 sliceLayout.m_radius = m_pieRadius;
124 sliceLayout.m_radius = m_pieRadius;
126 sliceLayout.m_startAngle = slice->startAngle();
125 sliceLayout.m_startAngle = slice->startAngle();
@@ -128,18 +127,18 PieSliceLayout PieChartItem::calculateSliceLayout(QPieSlice *slice)
128 return sliceLayout;
127 return sliceLayout;
129 }
128 }
130
129
131 QVector<PieSliceLayout> PieChartItem::calculateLayout()
130 PieLayout PieChartItem::calculateLayout()
132 {
131 {
133 calculatePieLayout();
132 calculatePieLayout();
134 QVector<PieSliceLayout> layout;
133 PieLayout layout;
135 foreach (QPieSlice* s, m_series->slices()) {
134 foreach (QPieSlice* s, m_series->slices()) {
136 if (m_slices.contains(s)) // calculate layout only for those slices that are already visible
135 if (m_slices.contains(s)) // calculate layout only for those slices that are already visible
137 layout << calculateSliceLayout(s);
136 layout.insert(s, calculateSliceLayout(s));
138 }
137 }
139 return layout;
138 return layout;
140 }
139 }
141
140
142 void PieChartItem::applyLayout(QVector<PieSliceLayout> &layout)
141 void PieChartItem::applyLayout(const PieLayout &layout)
143 {
142 {
144 if (m_animator)
143 if (m_animator)
145 m_animator->updateLayout(this, layout);
144 m_animator->updateLayout(this, layout);
@@ -147,36 +146,36 void PieChartItem::applyLayout(QVector<PieSliceLayout> &layout)
147 setLayout(layout);
146 setLayout(layout);
148 }
147 }
149
148
150 void PieChartItem::updateLayout(PieSliceLayout &layout)
149 void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceLayout &layout)
151 {
150 {
152 if (m_animator)
151 if (m_animator)
153 m_animator->updateLayout(this, layout);
152 m_animator->updateLayout(this, slice, layout);
154 else
153 else
155 setLayout(layout);
154 setLayout(slice, layout);
156 }
155 }
157
156
158 void PieChartItem::setLayout(QVector<PieSliceLayout> &layout)
157 void PieChartItem::setLayout(const PieLayout &layout)
159 {
158 {
160 foreach (PieSliceLayout l, layout) {
159 foreach (QPieSlice *slice, layout.keys()) {
161 PieSlice *slice = m_slices.value(l.m_data);
160 PieSlice *s = m_slices.value(slice);
162 Q_ASSERT(slice);
161 Q_ASSERT(s);
163 slice->setLayout(l);
162 s->setLayout(layout.value(slice));
164 slice->updateData(l.m_data);
163 s->updateData(slice);
165 slice->updateGeometry();
164 s->updateGeometry();
166 slice->update();
165 s->update();
167 }
166 }
168 }
167 }
169
168
170 void PieChartItem::setLayout(PieSliceLayout &layout)
169 void PieChartItem::setLayout(QPieSlice *slice, const PieSliceLayout &layout)
171 {
170 {
172 // find slice
171 // find slice
173 PieSlice *slice = m_slices.value(layout.m_data);
172 PieSlice *s = m_slices.value(slice);
174 Q_ASSERT(slice);
173 Q_ASSERT(s);
175 slice->setLayout(layout);
174 s->setLayout(layout);
176 if (m_series->m_slices.contains(layout.m_data)) // Slice has been deleted if not found. Animations ongoing...
175 if (m_series->m_slices.contains(slice)) // Slice has been deleted if not found. Animations ongoing...
177 slice->updateData(layout.m_data);
176 s->updateData(slice);
178 slice->updateGeometry();
177 s->updateGeometry();
179 slice->update();
178 s->update();
180 }
179 }
181
180
182 void PieChartItem::destroySlice(QPieSlice *slice)
181 void PieChartItem::destroySlice(QPieSlice *slice)
@@ -9,6 +9,8 class QGraphicsItem;
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 class QPieSlice;
10 class QPieSlice;
11
11
12 typedef QHash<QPieSlice*, PieSliceLayout> PieLayout;
13
12 class PieChartItem : public QObject, public ChartItem
14 class PieChartItem : public QObject, public ChartItem
13 {
15 {
14 Q_OBJECT
16 Q_OBJECT
@@ -34,11 +36,11 public Q_SLOTS:
34 public:
36 public:
35 void calculatePieLayout();
37 void calculatePieLayout();
36 PieSliceLayout calculateSliceLayout(QPieSlice *slice);
38 PieSliceLayout calculateSliceLayout(QPieSlice *slice);
37 QVector<PieSliceLayout> calculateLayout();
39 PieLayout calculateLayout();
38 void applyLayout(QVector<PieSliceLayout> &layout);
40 void applyLayout(const PieLayout &layout);
39 void updateLayout(PieSliceLayout &layout);
41 void updateLayout(QPieSlice *slice, const PieSliceLayout &layout);
40 void setLayout(QVector<PieSliceLayout> &layout);
42 void setLayout(const PieLayout &layout);
41 void setLayout(PieSliceLayout &layout);
43 void setLayout(QPieSlice *slice, const PieSliceLayout &layout);
42 void destroySlice(QPieSlice *slice);
44 void destroySlice(QPieSlice *slice);
43
45
44 private:
46 private:
@@ -19,7 +19,6 class QPieSlice;
19 class PieSliceLayout
19 class PieSliceLayout
20 {
20 {
21 public:
21 public:
22 QPieSlice* m_data; // TODO: get rid of this
23 QPointF m_center;
22 QPointF m_center;
24 qreal m_radius;
23 qreal m_radius;
25 qreal m_startAngle;
24 qreal m_startAngle;
General Comments 0
You need to be logged in to leave comments. Login now