diff --git a/src/animations/chartanimator.cpp b/src/animations/chartanimator.cpp index 63f90eb..30a6eac 100644 --- a/src/animations/chartanimator.cpp +++ b/src/animations/chartanimator.cpp @@ -224,11 +224,11 @@ void ChartAnimator::updateLayout(XYChartItem* item, QVector& oldPoints QTimer::singleShot(0,animation,SLOT(start())); } -void ChartAnimator::addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout, bool isEmpty) +void ChartAnimator::addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty) { PieAnimation* animation = static_cast(m_animations.value(item)); Q_ASSERT(animation); - animation->addSlice(slice, layout, isEmpty); + animation->addSlice(slice, sliceData, isEmpty); } void ChartAnimator::removeAnimation(PieChartItem* item, QPieSlice *slice) @@ -245,11 +245,11 @@ void ChartAnimator::updateLayout(PieChartItem* item, const PieLayout &layout) animation->updateValues(layout); } -void ChartAnimator::updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout) +void ChartAnimator::updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData) { PieAnimation* animation = static_cast(m_animations.value(item)); Q_ASSERT(animation); - animation->updateValue(slice, layout); + animation->updateValue(slice, sliceData); } void ChartAnimator::setState(State state,const QPointF& point) diff --git a/src/animations/chartanimator_p.h b/src/animations/chartanimator_p.h index b4cdf69..4571707 100644 --- a/src/animations/chartanimator_p.h +++ b/src/animations/chartanimator_p.h @@ -35,10 +35,10 @@ public: void updateLayout(SplineChartItem* item, QVector& oldPoints , QVector& newPoints, QVector& oldControlPoints, QVector& newContorlPoints,int index); void updateLayout(AxisItem* item, QVector& layout); - void addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout, bool isEmpty); + void addAnimation(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty); void removeAnimation(PieChartItem* item, QPieSlice *slice); void updateLayout(PieChartItem* item, const PieLayout &layout); - void updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceLayout &layout); + void updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData); void setState(State state,const QPointF& point = QPointF()); diff --git a/src/animations/pieanimation.cpp b/src/animations/pieanimation.cpp index a777598..7e09615 100644 --- a/src/animations/pieanimation.cpp +++ b/src/animations/pieanimation.cpp @@ -22,32 +22,32 @@ void PieAnimation::updateValues(const PieLayout &newValues) updateValue(s, newValues.value(s)); } -void PieAnimation::updateValue(QPieSlice *slice, const PieSliceLayout &endLayout) +void PieAnimation::updateValue(QPieSlice *slice, const PieSliceData &sliceData) { PieSliceAnimation *animation = m_animations.value(slice); Q_ASSERT(animation); animation->stop(); - animation->updateValue(endLayout); + animation->updateValue(sliceData); animation->setDuration(1000); animation->setEasingCurve(QEasingCurve::OutQuart); QTimer::singleShot(0, animation, SLOT(start())); } -void PieAnimation::addSlice(QPieSlice *slice, const PieSliceLayout &endLayout, bool isEmpty) +void PieAnimation::addSlice(QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty) { PieSliceAnimation *animation = new PieSliceAnimation(m_item, slice); m_animations.insert(slice, animation); - PieSliceLayout startLayout = endLayout; - startLayout.m_radius = 0; + PieSliceData startValue = sliceData; + startValue.m_radius = 0; if (isEmpty) - startLayout.m_startAngle = 0; + startValue.m_startAngle = 0; else - startLayout.m_startAngle = endLayout.m_startAngle + (endLayout.m_angleSpan/2); - startLayout.m_angleSpan = 0; - animation->setValue(startLayout, endLayout); + startValue.m_startAngle = sliceData.m_startAngle + (sliceData.m_angleSpan/2); + startValue.m_angleSpan = 0; + animation->setValue(startValue, sliceData); animation->setDuration(1000); animation->setEasingCurve(QEasingCurve::OutQuart); @@ -60,13 +60,13 @@ void PieAnimation::removeSlice(QPieSlice *slice) Q_ASSERT(animation); animation->stop(); - PieSliceLayout endLayout = animation->currentSliceValue(); - endLayout.m_radius = 0; + PieSliceData endValue = animation->currentSliceValue(); + endValue.m_radius = 0; // TODO: find the actual angle where this slice disappears - endLayout.m_startAngle = endLayout.m_startAngle + endLayout.m_angleSpan; - endLayout.m_angleSpan = 0; + endValue.m_startAngle = endValue.m_startAngle + endValue.m_angleSpan; + endValue.m_angleSpan = 0; - animation->updateValue(endLayout); + animation->updateValue(endValue); animation->setDuration(1000); animation->setEasingCurve(QEasingCurve::OutQuart); diff --git a/src/animations/pieanimation_p.h b/src/animations/pieanimation_p.h index c88dc38..600b27a 100644 --- a/src/animations/pieanimation_p.h +++ b/src/animations/pieanimation_p.h @@ -17,8 +17,8 @@ public: PieAnimation(PieChartItem *item); ~PieAnimation(); void updateValues(const PieLayout &newValues); - void updateValue(QPieSlice *slice, const PieSliceLayout &newValue); - void addSlice(QPieSlice *slice, const PieSliceLayout &endLayout, bool isEmpty); + void updateValue(QPieSlice *slice, const PieSliceData &newValue); + void addSlice(QPieSlice *slice, const PieSliceData &endValue, bool isEmpty); void removeSlice(QPieSlice *slice); public: // from QVariantAnimation diff --git a/src/animations/piesliceanimation.cpp b/src/animations/piesliceanimation.cpp index e144e7d..7dc6958 100644 --- a/src/animations/piesliceanimation.cpp +++ b/src/animations/piesliceanimation.cpp @@ -2,7 +2,7 @@ #include "piechartitem_p.h" #include "qpieslice.h" -Q_DECLARE_METATYPE(QtCommercialChart::PieSliceLayout) +Q_DECLARE_METATYPE(QtCommercialChart::PieSliceData) QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -49,7 +49,7 @@ PieSliceAnimation::~PieSliceAnimation() { } -void PieSliceAnimation::setValue(const PieSliceLayout &startValue, const PieSliceLayout &endValue) +void PieSliceAnimation::setValue(const PieSliceData &startValue, const PieSliceData &endValue) { if (state() != QAbstractAnimation::Stopped) stop(); @@ -60,7 +60,7 @@ void PieSliceAnimation::setValue(const PieSliceLayout &startValue, const PieSlic setKeyValueAt(1.0, qVariantFromValue(endValue)); } -void PieSliceAnimation::updateValue(const PieSliceLayout &endValue) +void PieSliceAnimation::updateValue(const PieSliceData &endValue) { if (state() != QAbstractAnimation::Stopped) stop(); @@ -69,7 +69,7 @@ void PieSliceAnimation::updateValue(const PieSliceLayout &endValue) setKeyValueAt(1.0, qVariantFromValue(endValue)); } -PieSliceLayout PieSliceAnimation::currentSliceValue() +PieSliceData PieSliceAnimation::currentSliceValue() { // NOTE: // We must use an internal current value because QVariantAnimation::currentValue() is updated @@ -81,10 +81,10 @@ PieSliceLayout PieSliceAnimation::currentSliceValue() QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const { - PieSliceLayout startValue = qVariantValue(start); - PieSliceLayout endValue = qVariantValue(end); + PieSliceData startValue = qVariantValue(start); + PieSliceData endValue = qVariantValue(end); - PieSliceLayout result; + PieSliceData result; result = endValue; result.m_center = linearPos(startValue.m_center, endValue.m_center, progress); result.m_radius = linearPos(startValue.m_radius, endValue.m_radius, progress); @@ -99,7 +99,7 @@ QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant & void PieSliceAnimation::updateCurrentValue(const QVariant &value) { if (state() != QAbstractAnimation::Stopped) { //workaround - m_currentValue = qVariantValue(value); + m_currentValue = qVariantValue(value); m_item->setLayout(m_slice, m_currentValue); } } diff --git a/src/animations/piesliceanimation_p.h b/src/animations/piesliceanimation_p.h index c79d1bd..de771bb 100644 --- a/src/animations/piesliceanimation_p.h +++ b/src/animations/piesliceanimation_p.h @@ -14,9 +14,9 @@ class PieSliceAnimation : public QVariantAnimation public: PieSliceAnimation(PieChartItem *item, QPieSlice *slice); ~PieSliceAnimation(); - void setValue(const PieSliceLayout &startValue, const PieSliceLayout &endValue); - void updateValue(const PieSliceLayout &endValue); - PieSliceLayout currentSliceValue(); + void setValue(const PieSliceData &startValue, const PieSliceData &endValue); + void updateValue(const PieSliceData &endValue); + PieSliceData currentSliceValue(); protected: QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress) const; @@ -25,7 +25,7 @@ protected: private: PieChartItem *m_item; QPieSlice *m_slice; - PieSliceLayout m_currentValue; + PieSliceData m_currentValue; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/piechart/piechartitem.cpp b/src/piechart/piechartitem.cpp index 6df5137..32b87f2 100644 --- a/src/piechart/piechartitem.cpp +++ b/src/piechart/piechartitem.cpp @@ -61,12 +61,12 @@ void PieChartItem::handleSlicesAdded(QList slices) connect(slice, SIGNAL(hoverEnter()), s, SIGNAL(hoverEnter())); connect(slice, SIGNAL(hoverLeave()), s, SIGNAL(hoverLeave())); - PieSliceLayout layout = calculateSliceLayout(s); + PieSliceData data = sliceData(s); if (m_animator) - m_animator->addAnimation(this, s, layout, isEmpty); + m_animator->addAnimation(this, s, data, isEmpty); else - setLayout(s, layout); + setLayout(s, data); } } @@ -93,8 +93,8 @@ void PieChartItem::handleSliceChanged() { QPieSlice* slice = qobject_cast(sender()); Q_ASSERT(m_slices.contains(slice)); - PieSliceLayout layout = calculateSliceLayout(slice); - updateLayout(slice, layout); + PieSliceData data = sliceData(slice); + updateLayout(slice, data); update(); } @@ -125,25 +125,40 @@ void PieChartItem::calculatePieLayout() m_pieRadius *= m_series->pieSize(); } -PieSliceLayout PieChartItem::calculateSliceLayout(QPieSlice *slice) +PieSliceData PieChartItem::sliceData(QPieSlice *slice) { - PieSliceLayout sliceLayout; - sliceLayout.m_center = PieSlice::sliceCenter(m_pieCenter, m_pieRadius, slice); - sliceLayout.m_radius = m_pieRadius; - sliceLayout.m_startAngle = slice->startAngle(); - sliceLayout.m_angleSpan = slice->m_angleSpan; - sliceLayout.m_pen = slice->m_slicePen; - sliceLayout.m_brush = slice->m_sliceBrush; - return sliceLayout; + PieSliceData sliceData; + + // TODO: + // sliceData = slice->m_data; + + sliceData.m_center = PieSlice::sliceCenter(m_pieCenter, m_pieRadius, slice); + sliceData.m_radius = m_pieRadius; + sliceData.m_startAngle = slice->startAngle(); + sliceData.m_angleSpan = slice->m_angleSpan; + + sliceData.m_pen = slice->m_slicePen; + sliceData.m_brush = slice->m_sliceBrush; + + sliceData.m_isExploded = slice->isExploded(); + sliceData.m_explodeDistanceFactor = slice->explodeDistanceFactor(); + + sliceData.m_labelVisible = slice->isLabelVisible(); + sliceData.m_labelText = slice->label(); + sliceData.m_labelFont = slice->labelFont(); + sliceData.m_labelArmLengthFactor = slice->labelArmLengthFactor(); + sliceData.m_labelArmPen = slice->labelArmPen(); + + return sliceData; } PieLayout PieChartItem::calculateLayout() { calculatePieLayout(); - PieLayout layout; + PieLayout layout; foreach (QPieSlice* s, m_series->slices()) { if (m_slices.contains(s)) // calculate layout only for those slices that are already visible - layout.insert(s, calculateSliceLayout(s)); + layout.insert(s, sliceData(s)); } return layout; } @@ -156,12 +171,12 @@ void PieChartItem::applyLayout(const PieLayout &layout) setLayout(layout); } -void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceLayout &layout) +void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData) { if (m_animator) - m_animator->updateLayout(this, slice, layout); + m_animator->updateLayout(this, slice, sliceData); else - setLayout(slice, layout); + setLayout(slice, sliceData); } void PieChartItem::setLayout(const PieLayout &layout) @@ -169,21 +184,18 @@ void PieChartItem::setLayout(const PieLayout &layout) foreach (QPieSlice *slice, layout.keys()) { PieSlice *s = m_slices.value(slice); Q_ASSERT(s); - s->setLayout(layout.value(slice)); - s->updateData(slice); + s->setSliceData(layout.value(slice)); s->updateGeometry(); s->update(); } } -void PieChartItem::setLayout(QPieSlice *slice, const PieSliceLayout &layout) +void PieChartItem::setLayout(QPieSlice *slice, const PieSliceData &sliceData) { // find slice PieSlice *s = m_slices.value(slice); Q_ASSERT(s); - s->setLayout(layout); - if (m_series->m_slices.contains(slice)) // Slice has been deleted if not found. Animations ongoing... - s->updateData(slice); + s->setSliceData(sliceData); s->updateGeometry(); s->update(); } diff --git a/src/piechart/piechartitem_p.h b/src/piechart/piechartitem_p.h index 9dda481..43c866a 100644 --- a/src/piechart/piechartitem_p.h +++ b/src/piechart/piechartitem_p.h @@ -10,7 +10,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QPieSlice; class ChartPresenter; -typedef QHash PieLayout; +typedef QHash PieLayout; class PieChartItem : public QObject, public ChartItem { @@ -36,12 +36,12 @@ public Q_SLOTS: public: void calculatePieLayout(); - PieSliceLayout calculateSliceLayout(QPieSlice *slice); + PieSliceData sliceData(QPieSlice *slice); PieLayout calculateLayout(); void applyLayout(const PieLayout &layout); - void updateLayout(QPieSlice *slice, const PieSliceLayout &layout); + void updateLayout(QPieSlice *slice, const PieSliceData &sliceData); void setLayout(const PieLayout &layout); - void setLayout(QPieSlice *slice, const PieSliceLayout &layout); + void setLayout(QPieSlice *slice, const PieSliceData &sliceData); void destroySlice(QPieSlice *slice); private: diff --git a/src/piechart/pieslice.cpp b/src/piechart/pieslice.cpp index 5c52656..9d39164 100644 --- a/src/piechart/pieslice.cpp +++ b/src/piechart/pieslice.cpp @@ -21,11 +21,7 @@ QPointF offset(qreal angle, qreal length) } PieSlice::PieSlice(QGraphicsItem* parent) - :QGraphicsObject(parent), - m_isExploded(false), - m_explodeDistanceFactor(0), - m_labelVisible(false), - m_labelArmLengthFactor(0) + :QGraphicsObject(parent) { setAcceptHoverEvents(true); setAcceptedMouseButtons(Qt::LeftButton); @@ -54,19 +50,19 @@ void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option painter->setClipRect(parentItem()->boundingRect()); painter->save(); - painter->setPen(m_layout.m_pen); - painter->setBrush(m_layout.m_brush); + painter->setPen(m_data.m_pen); + painter->setBrush(m_data.m_brush); painter->drawPath(m_slicePath); painter->restore(); - if (m_labelVisible) { + if (m_data.m_labelVisible) { painter->save(); - painter->setPen(m_labelArmPen); + painter->setPen(m_data.m_labelArmPen); painter->drawPath(m_labelArmPath); painter->restore(); - painter->setFont(m_labelFont); - painter->drawText(m_labelTextRect.bottomLeft(), m_labelText); + painter->setFont(m_data.m_labelFont); + painter->drawText(m_labelTextRect.bottomLeft(), m_data.m_labelText); } } @@ -85,14 +81,14 @@ void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) emit clicked(); } -void PieSlice::setLayout(PieSliceLayout layout) +void PieSlice::setSliceData(PieSliceData sliceData) { - m_layout = layout; + m_data = sliceData; } void PieSlice::updateGeometry() { - if (m_layout.m_radius <= 0) + if (m_data.m_radius <= 0) return; prepareGeometryChange(); @@ -100,14 +96,14 @@ void PieSlice::updateGeometry() // update slice path qreal centerAngle; QPointF armStart; - m_slicePath = slicePath(m_layout.m_center, m_layout.m_radius, m_layout.m_startAngle, m_layout.m_angleSpan, ¢erAngle, &armStart); + m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, ¢erAngle, &armStart); // update text rect - m_labelTextRect = labelTextRect(m_labelFont, m_labelText); + m_labelTextRect = labelTextRect(m_data.m_labelFont, m_data.m_labelText); // update label arm path QPointF labelTextStart; - m_labelArmPath = labelArmPath(armStart, centerAngle, m_layout.m_radius * m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); + m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart); // update text position m_labelTextRect.moveBottomLeft(labelTextStart); @@ -116,20 +112,6 @@ void PieSlice::updateGeometry() m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect); } -void PieSlice::updateData(const QPieSlice* sliceData) -{ - // TODO: compare what has changes to avoid unneccesary geometry updates - - m_isExploded = sliceData->isExploded(); - m_explodeDistanceFactor = sliceData->explodeDistanceFactor(); - - m_labelVisible = sliceData->isLabelVisible(); - m_labelText = sliceData->label(); - m_labelFont = sliceData->labelFont(); - m_labelArmLengthFactor = sliceData->labelArmLengthFactor(); - m_labelArmPen = sliceData->labelArmPen(); -} - QPointF PieSlice::sliceCenter(QPointF point, qreal radius, QPieSlice *slice) { if (slice->isExploded()) { diff --git a/src/piechart/pieslice_p.h b/src/piechart/pieslice_p.h index 115e091..6ee684d 100644 --- a/src/piechart/pieslice_p.h +++ b/src/piechart/pieslice_p.h @@ -16,15 +16,25 @@ class PieChartItem; class PieSliceLabel; class QPieSlice; -class PieSliceLayout +class PieSliceData { public: QPointF m_center; qreal m_radius; qreal m_startAngle; qreal m_angleSpan; + QPen m_pen; QBrush m_brush; + + bool m_isExploded; + qreal m_explodeDistanceFactor; + + bool m_labelVisible; + QString m_labelText; + QFont m_labelFont; + qreal m_labelArmLengthFactor; + QPen m_labelArmPen; }; class PieSlice : public QGraphicsObject @@ -49,30 +59,19 @@ Q_SIGNALS: void hoverLeave(); public: - void setLayout(PieSliceLayout layout); + void setSliceData(PieSliceData sliceData); void updateGeometry(); - void updateData(const QPieSlice *sliceData); static QPointF sliceCenter(QPointF point, qreal radius, QPieSlice *slice); static QPainterPath slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal* centerAngle, QPointF* armStart); static QPainterPath labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF* textStart); static QRectF labelTextRect(QFont font, QString text); private: - PieSliceLayout m_layout; + PieSliceData m_data; QRectF m_boundingRect; - QPainterPath m_slicePath; - bool m_isExploded; - qreal m_explodeDistanceFactor; - bool m_labelVisible; - QPainterPath m_labelArmPath; - qreal m_labelArmLengthFactor; - QPen m_labelArmPen; - QRectF m_labelTextRect; - QFont m_labelFont; - QString m_labelText; }; QTCOMMERCIALCHART_END_NAMESPACE