##// END OF EJS Templates
Fix grid animation to do not go out of bound
Michal Klocek -
r504:142210a64aff
parent child
Show More
@@ -1,75 +1,84
1 #include "axisanimationitem_p.h"
1 #include "axisanimationitem_p.h"
2 #include <QTimer>
2 #include <QTimer>
3
3
4 Q_DECLARE_METATYPE(QVector<qreal>)
4 Q_DECLARE_METATYPE(QVector<qreal>)
5
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
7
8 const static int duration = 500;
8 const static int duration = 500;
9
9
10 AxisAnimationItem::AxisAnimationItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
10 AxisAnimationItem::AxisAnimationItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
11 AxisItem(axis,type,parent),
11 AxisItem(axis,type,parent),
12 m_animation(new AxisAnimator(this,this))
12 m_animation(new AxisAnimator(this,this))
13 {
13 {
14 }
14 }
15
15
16 AxisAnimationItem::~AxisAnimationItem()
16 AxisAnimationItem::~AxisAnimationItem()
17 {
17 {
18 }
18 }
19
19
20 void AxisAnimationItem::updateLayout(QVector<qreal>& newLayout)
20 void AxisAnimationItem::updateLayout(QVector<qreal>& newLayout)
21 {
21 {
22
22 QVector<qreal> oldLayout = layout();
23 QVector<qreal> oldLayout = layout();
23
24
24 if(newLayout.count()==0) return;
25 if(newLayout.count()==0) return;
26
27 QRectF rect = geometry();
28
25 oldLayout.resize(newLayout.size());
29 oldLayout.resize(newLayout.size());
26
30
31 for(int i=0;i<oldLayout.count();i++)
32 {
33 oldLayout[i]= axisType()==X_AXIS?rect.left():rect.top();
34 }
35
27 if(m_animation->state()!=QAbstractAnimation::Stopped){
36 if(m_animation->state()!=QAbstractAnimation::Stopped){
28 m_animation->stop();
37 m_animation->stop();
29 }
38 }
30
39
31 m_animation->setDuration(duration);
40 m_animation->setDuration(duration);
32 m_animation->setEasingCurve(QEasingCurve::InOutBack);
41 m_animation->setEasingCurve(QEasingCurve::OutQuart);
33 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
42 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
34 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
43 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
35 QTimer::singleShot(0,m_animation,SLOT(start()));
44 QTimer::singleShot(0,m_animation,SLOT(start()));
36 }
45 }
37
46
38 void AxisAnimationItem::setLabelsAngle(int angle)
47 void AxisAnimationItem::setLabelsAngle(int angle)
39 {
48 {
40 AxisItem::setLabelsAngle(angle);
49 AxisItem::setLabelsAngle(angle);
41 }
50 }
42
51
43 AxisAnimator::AxisAnimator(AxisItem *axis,QObject *parent): QVariantAnimation(parent),
52 AxisAnimator::AxisAnimator(AxisItem *axis,QObject *parent): QVariantAnimation(parent),
44 m_axis(axis)
53 m_axis(axis)
45 {
54 {
46 }
55 }
47
56
48 AxisAnimator::~AxisAnimator()
57 AxisAnimator::~AxisAnimator()
49 {
58 {
50 }
59 }
51
60
52 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
61 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
53 {
62 {
54 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
63 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
55 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
64 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
56 QVector<qreal> result;
65 QVector<qreal> result;
57 Q_ASSERT(startVector.count() == endVecotr.count());
66 Q_ASSERT(startVector.count() == endVecotr.count());
58
67
59 for(int i =0 ;i< startVector.count();i++){
68 for(int i =0 ;i< startVector.count();i++){
60 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
69 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
61 result << value;
70 result << value;
62 }
71 }
63 return qVariantFromValue(result);
72 return qVariantFromValue(result);
64 }
73 }
65
74
66
75
67 void AxisAnimator::updateCurrentValue (const QVariant & value )
76 void AxisAnimator::updateCurrentValue (const QVariant & value )
68 {
77 {
69 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
78 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
70 m_axis->setLayout(vector);
79 m_axis->setLayout(vector);
71 }
80 }
72
81
73 #include "moc_axisanimationitem_p.cpp"
82 #include "moc_axisanimationitem_p.cpp"
74
83
75 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,90 +1,92
1 #ifndef AXISITEM_H_
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
2 #define AXISITEM_H_
3
3
4 #include "domain_p.h"
4 #include "domain_p.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChartAxis;
10 class QChartAxis;
11
11
12 class AxisItem : public QObject, public ChartItem
12 class AxisItem : public QObject, public ChartItem
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 enum AxisType{X_AXIS,Y_AXIS};
16 enum AxisType{X_AXIS,Y_AXIS};
17
17
18 AxisItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisItem(QChartAxis* axis,AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 ~AxisItem();
19 ~AxisItem();
20
20
21 //from QGraphicsItem
21 //from QGraphicsItem
22 QRectF boundingRect() const;
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24
24
25 AxisType axisType() const {return m_type;};
25 AxisType axisType() const {return m_type;};
26
26
27 void setAxisOpacity(qreal opacity);
27 void setAxisOpacity(qreal opacity);
28 qreal axisOpacity() const;
28 qreal axisOpacity() const;
29
29
30 void setGridOpacity(qreal opacity);
30 void setGridOpacity(qreal opacity);
31 qreal gridOpacity() const;
31 qreal gridOpacity() const;
32
32
33 void setLabelsOpacity(qreal opacity);
33 void setLabelsOpacity(qreal opacity);
34 qreal labelsOpacity() const;
34 qreal labelsOpacity() const;
35
35
36 void setShadesOpacity(qreal opacity);
36 void setShadesOpacity(qreal opacity);
37 qreal shadesOpacity() const;
37 qreal shadesOpacity() const;
38
38
39 void setLabelsAngle(int angle);
39 void setLabelsAngle(int angle);
40 int labelsAngle()const { return m_labelsAngle; }
40 int labelsAngle()const { return m_labelsAngle; }
41
41
42 void setShadesBrush(const QBrush& brush);
42 void setShadesBrush(const QBrush& brush);
43 void setShadesPen(const QPen& pen);
43 void setShadesPen(const QPen& pen);
44
44
45 void setAxisPen(const QPen& pen);
45 void setAxisPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
47
47
48 void setLabelsPen(const QPen& pen);
48 void setLabelsPen(const QPen& pen);
49 void setLabelsBrush(const QBrush& brush);
49 void setLabelsBrush(const QBrush& brush);
50 void setLabelsFont(const QFont& font);
50 void setLabelsFont(const QFont& font);
51
51
52 QRectF geometry() const { return m_rect; }
53
52 public slots:
54 public slots:
53 void handleAxisUpdated();
55 void handleAxisUpdated();
54 void handleAxisCategoriesUpdated();
56 void handleAxisCategoriesUpdated();
55 void handleRangeChanged(qreal min , qreal max);
57 void handleRangeChanged(qreal min , qreal max);
56 void handleTicksCountChanged(int count);
58 void handleTicksCountChanged(int count);
57 void handleGeometryChanged(const QRectF& size);
59 void handleGeometryChanged(const QRectF& size);
58
60
59 public:
61 public:
60 virtual void updateLayout(QVector<qreal>& layout);
62 virtual void updateLayout(QVector<qreal>& layout);
61 void setLayout(QVector<qreal>& layout);
63 void setLayout(QVector<qreal>& layout);
62 QVector<qreal> layout() { return m_layoutVector;};
64 QVector<qreal> layout() { return m_layoutVector;};
63
65
64 private:
66 private:
65 inline bool isEmpty();
67 inline bool isEmpty();
66 void createItems(int count);
68 void createItems(int count);
67 void deleteItems(int count);
69 void deleteItems(int count);
68 QVector<qreal> calculateLayout() const;
70 QVector<qreal> calculateLayout() const;
69 QStringList createLabels(int ticks, qreal min, qreal max) const;
71 QStringList createLabels(int ticks, qreal min, qreal max) const;
70
72
71 private:
73 private:
72 QChartAxis* m_chartAxis;
74 QChartAxis* m_chartAxis;
73 AxisType m_type;
75 AxisType m_type;
74 QRectF m_rect;
76 QRectF m_rect;
75 int m_labelsAngle;
77 int m_labelsAngle;
76 QGraphicsItemGroup m_grid;
78 QGraphicsItemGroup m_grid;
77 QGraphicsItemGroup m_shades;
79 QGraphicsItemGroup m_shades;
78 QGraphicsItemGroup m_labels;
80 QGraphicsItemGroup m_labels;
79 QGraphicsItemGroup m_axis;
81 QGraphicsItemGroup m_axis;
80 QStringList m_ticksList;
82 QStringList m_ticksList;
81 QVector<qreal> m_layoutVector;
83 QVector<qreal> m_layoutVector;
82 qreal m_min;
84 qreal m_min;
83 qreal m_max;
85 qreal m_max;
84 int m_ticksCount;
86 int m_ticksCount;
85
87
86 };
88 };
87
89
88 QTCOMMERCIALCHART_END_NAMESPACE
90 QTCOMMERCIALCHART_END_NAMESPACE
89
91
90 #endif /* AXISITEM_H_ */
92 #endif /* AXISITEM_H_ */
General Comments 0
You need to be logged in to leave comments. Login now