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