From c1d8fdd92b718fafd105a845e5df17e473ce0695 2012-03-08 15:08:10 From: Michal Klocek Date: 2012-03-08 15:08:10 Subject: [PATCH] Adding zoom in out animation support --- diff --git a/src/axis/axisanimationitem.cpp b/src/axis/axisanimationitem.cpp index 0379102..8fbdc06 100644 --- a/src/axis/axisanimationitem.cpp +++ b/src/axis/axisanimationitem.cpp @@ -24,23 +24,40 @@ void AxisAnimationItem::updateLayout(QVector& newLayout) if(newLayout.count()==0) return; - QRectF rect = geometry(); + if(zoomFactor()<0) { - oldLayout.resize(newLayout.size()); + QRectF rect = geometry(); + oldLayout.resize(newLayout.count()); - for(int i=0;istate()!=QAbstractAnimation::Stopped){ - m_animation->stop(); + if(m_animation->state()!=QAbstractAnimation::Stopped) { + m_animation->stop(); } m_animation->setDuration(duration); m_animation->setEasingCurve(QEasingCurve::OutQuart); + QVariantAnimation::KeyValues value; + m_animation->setKeyValues(value); //workaround for wrong interpolation call m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout)); m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout)); + QTimer::singleShot(0,m_animation,SLOT(start())); } @@ -63,6 +80,7 @@ QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, QVector startVector = qVariantValue >(start); QVector endVecotr = qVariantValue >(end); QVector result; + Q_ASSERT(startVector.count() == endVecotr.count()); for(int i =0 ;i< startVector.count();i++){ @@ -75,8 +93,13 @@ QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, void AxisAnimator::updateCurrentValue (const QVariant & value ) { - QVector vector = qVariantValue >(value); - m_axis->setLayout(vector); + //Q_ASSERT(state()!=QAbstractAnimation::Stopped); + if(state()!=QAbstractAnimation::Stopped)//workaround + { + QVector vector = qVariantValue >(value); + m_axis->setLayout(vector); + } + } #include "moc_axisanimationitem_p.cpp" diff --git a/src/axis/axisitem.cpp b/src/axis/axisitem.cpp index d0d3146..019cbf2 100644 --- a/src/axis/axisitem.cpp +++ b/src/axis/axisitem.cpp @@ -19,7 +19,8 @@ m_labels(parent), m_axis(parent), m_min(0), m_max(0), -m_ticksCount(0) +m_ticksCount(0), +m_zoomFactor(0) { //initial initialization m_axis.setZValue(ChartPresenter::AxisZValue); @@ -93,8 +94,6 @@ QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) const labels << label; } } - - return labels; } @@ -239,13 +238,17 @@ void AxisItem::setLayout(QVector& layout) createItems(-diff); } + if(diff!=0) handleAxisUpdated(); + + QStringList ticksList = createLabels(m_ticksCount,m_min,m_max); + QList lines = m_grid.childItems(); QList labels = m_labels.childItems(); QList shades = m_shades.childItems(); QList axis = m_axis.childItems(); - Q_ASSERT(labels.size() == m_ticksList.size()); - Q_ASSERT(layout.size() == m_ticksList.size()); + Q_ASSERT(labels.size() == ticksList.size()); + Q_ASSERT(layout.size() == ticksList.size()); switch (m_type) { @@ -258,7 +261,7 @@ void AxisItem::setLayout(QVector& layout) QGraphicsLineItem *lineItem = static_cast(lines.at(i)); lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - labelItem->setText(m_ticksList.at(i)); + labelItem->setText(ticksList.at(i)); QPointF center = labelItem->boundingRect().center(); labelItem->setTransformOriginPoint(center.x(), center.y()); labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); @@ -281,7 +284,7 @@ void AxisItem::setLayout(QVector& layout) QGraphicsLineItem *lineItem = static_cast(lines.at(i)); lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - labelItem->setText(m_ticksList.at(i)); + labelItem->setText(ticksList.at(i)); QPointF center = labelItem->boundingRect().center(); labelItem->setTransformOriginPoint(center.x(), center.y()); labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y()); @@ -299,7 +302,7 @@ void AxisItem::setLayout(QVector& layout) break; } - if(diff!=0) handleAxisUpdated(); + //if(diff!=0) handleAxisUpdated(); m_layoutVector=layout; } @@ -313,7 +316,6 @@ bool AxisItem::isEmpty() void AxisItem::handleAxisCategoriesUpdated() { if(isEmpty()) return; - m_ticksList = createLabels(m_ticksCount,m_min,m_max); updateLayout(m_layoutVector); } @@ -323,7 +325,7 @@ void AxisItem::handleAxisUpdated() int count = m_chartAxis->ticksCount(); if(m_ticksCount!=count){ - handleTicksCountChanged(count); + //handleTicksCountChanged(count); } if(isEmpty()) return; @@ -373,7 +375,6 @@ void AxisItem::handleTicksCountChanged(int count) m_ticksCount=count; if(isEmpty()) return; - m_ticksList = createLabels(m_ticksCount,m_min,m_max); QVector layout = calculateLayout(); updateLayout(layout); } @@ -381,11 +382,25 @@ void AxisItem::handleTicksCountChanged(int count) void AxisItem::handleRangeChanged(qreal min, qreal max) { + if(m_minmax){ + m_zoomFactor = (min + (max-min)/2 - m_min)/(m_max - m_min); + } + else + m_zoomFactor=-1; + m_min = min; m_max = max; + m_ticksCount = qrand()%10; + + while(m_ticksCount<2){ + m_ticksCount = qrand()%10; + } + + qDebug()<<"Warning : This is testing . Simulating new random ticks "<< m_ticksCount; + //m_chartAxis->setTicksCount(m_ticksCount); + if(isEmpty()) return; - m_ticksList = createLabels(m_ticksCount,m_min,m_max); QVector layout = calculateLayout(); updateLayout(layout); @@ -396,7 +411,6 @@ void AxisItem::handleGeometryChanged(const QRectF& rect) m_rect = rect; if(isEmpty()) return; - m_ticksList = createLabels(m_ticksCount,m_min,m_max); QVector layout = calculateLayout(); updateLayout(layout); } diff --git a/src/axis/axisitem_p.h b/src/axis/axisitem_p.h index 6edbebe..c847bd5 100644 --- a/src/axis/axisitem_p.h +++ b/src/axis/axisitem_p.h @@ -49,7 +49,8 @@ public: void setLabelsBrush(const QBrush& brush); void setLabelsFont(const QFont& font); - QRectF geometry() const { return m_rect; } + inline QRectF geometry() const { return m_rect; } + inline qreal zoomFactor() const { return m_zoomFactor;} public slots: void handleAxisUpdated(); @@ -79,11 +80,11 @@ private: QGraphicsItemGroup m_shades; QGraphicsItemGroup m_labels; QGraphicsItemGroup m_axis; - QStringList m_ticksList; QVector m_layoutVector; qreal m_min; qreal m_max; int m_ticksCount; + qreal m_zoomFactor; }; diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index c1e72b6..06b7379 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -15,7 +15,6 @@ m_items(this) //m_items.setZValue(ChartPresenter::LineChartZValue); setZValue(ChartPresenter::LineChartZValue); QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); - handleUpdated(); } diff --git a/src/xychart/xychartanimationitem_p.h b/src/xychart/xychartanimationitem_p.h index 798016c..4b1b88f 100644 --- a/src/xychart/xychartanimationitem_p.h +++ b/src/xychart/xychartanimationitem_p.h @@ -51,6 +51,8 @@ void XYChartAnimationItem::updatePoints(QVector& newPoints) QVector oldPoints = T::points(); if(newPoints.count()==0) return; + + bool empty = oldPoints.count()==0; oldPoints.resize(newPoints.size()); if(m_animation->state()!=QAbstractAnimation::Stopped){ @@ -58,8 +60,11 @@ void XYChartAnimationItem::updatePoints(QVector& newPoints) } m_animation->setDuration(duration); - m_animation->setAnimationType(XYChartAnimator::LineDrawAnimation); - m_animation->setEasingCurve(QEasingCurve::InOutBack); + if(!empty) + m_animation->setAnimationType(XYChartAnimator::MoveDownAnimation); + else + m_animation->setAnimationType(XYChartAnimator::LineDrawAnimation); + m_animation->setEasingCurve(QEasingCurve::OutQuart); m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints)); m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints)); QTimer::singleShot(0,m_animation,SLOT(start())); @@ -84,7 +89,7 @@ void XYChartAnimationItem::updatePoint(QVector& newPoints) m_animation->setDuration(duration); m_animation->setAnimationType(XYChartAnimator::MoveDownAnimation); - m_animation->setEasingCurve(QEasingCurve::InOutBack); + m_animation->setEasingCurve(QEasingCurve::OutQuart); m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points)); m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints)); diff --git a/src/xychart/xychartanimator_p.h b/src/xychart/xychartanimator_p.h index 31bd208..313037c 100644 --- a/src/xychart/xychartanimator_p.h +++ b/src/xychart/xychartanimator_p.h @@ -84,9 +84,9 @@ QVariant XYChartAnimator::interpolated(const QVariant &start, const QVarian template void XYChartAnimator::updateCurrentValue (const QVariant & value ) { - QVector vector = qVariantValue >(value); if(state()!=QAbstractAnimation::Stopped){ //workaround - m_item->setGeometry(vector); + QVector vector = qVariantValue >(value); + m_item->setGeometry(vector); } }