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