##// END OF EJS Templates
Adding zoom in out animation support
Michal Klocek -
r513:c1d8fdd92b71
parent child
Show More
@@ -1,84 +1,107
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
23 QVector<qreal> oldLayout = layout();
23 QVector<qreal> oldLayout = layout();
24
24
25 if(newLayout.count()==0) return;
25 if(newLayout.count()==0) return;
26
26
27 QRectF rect = geometry();
27 if(zoomFactor()<0) {
28
28
29 oldLayout.resize(newLayout.size());
29 QRectF rect = geometry();
30 oldLayout.resize(newLayout.count());
30
31
31 for(int i=0;i<oldLayout.count();i++)
32 for(int i=0,j=oldLayout.count()-1;i<(oldLayout.count()+1)/2;i++,j--)
32 {
33 {
33 oldLayout[i]= axisType()==X_AXIS?rect.left():rect.top();
34 oldLayout[i]= axisType()==X_AXIS?rect.left():rect.bottom();
35 oldLayout[j]= axisType()==X_AXIS?rect.right():rect.top();
36 }
37
38 }
39 else {
40
41 int index = qMin(oldLayout.count()*zoomFactor(),newLayout.count()-1.0);
42 oldLayout.resize(newLayout.count());
43
44 for(int i=0;i<oldLayout.count();i++)
45 {
46 oldLayout[i]= oldLayout[index]; //axisType()==X_AXIS?rect.center.x():rect.center().y();
47 }
34 }
48 }
35
49
36 if(m_animation->state()!=QAbstractAnimation::Stopped){
50 if(m_animation->state()!=QAbstractAnimation::Stopped) {
37 m_animation->stop();
51 m_animation->stop();
38 }
52 }
39
53
40 m_animation->setDuration(duration);
54 m_animation->setDuration(duration);
41 m_animation->setEasingCurve(QEasingCurve::OutQuart);
55 m_animation->setEasingCurve(QEasingCurve::OutQuart);
56 QVariantAnimation::KeyValues value;
57 m_animation->setKeyValues(value); //workaround for wrong interpolation call
42 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
58 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
43 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
59 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
60
44 QTimer::singleShot(0,m_animation,SLOT(start()));
61 QTimer::singleShot(0,m_animation,SLOT(start()));
45 }
62 }
46
63
47 void AxisAnimationItem::setLabelsAngle(int angle)
64 void AxisAnimationItem::setLabelsAngle(int angle)
48 {
65 {
49 AxisItem::setLabelsAngle(angle);
66 AxisItem::setLabelsAngle(angle);
50 }
67 }
51
68
52 AxisAnimator::AxisAnimator(AxisItem *axis,QObject *parent): QVariantAnimation(parent),
69 AxisAnimator::AxisAnimator(AxisItem *axis,QObject *parent): QVariantAnimation(parent),
53 m_axis(axis)
70 m_axis(axis)
54 {
71 {
55 }
72 }
56
73
57 AxisAnimator::~AxisAnimator()
74 AxisAnimator::~AxisAnimator()
58 {
75 {
59 }
76 }
60
77
61 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
78 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
62 {
79 {
63 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
80 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
64 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
81 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
65 QVector<qreal> result;
82 QVector<qreal> result;
83
66 Q_ASSERT(startVector.count() == endVecotr.count());
84 Q_ASSERT(startVector.count() == endVecotr.count());
67
85
68 for(int i =0 ;i< startVector.count();i++){
86 for(int i =0 ;i< startVector.count();i++){
69 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
87 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
70 result << value;
88 result << value;
71 }
89 }
72 return qVariantFromValue(result);
90 return qVariantFromValue(result);
73 }
91 }
74
92
75
93
76 void AxisAnimator::updateCurrentValue (const QVariant & value )
94 void AxisAnimator::updateCurrentValue (const QVariant & value )
77 {
95 {
78 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
96 //Q_ASSERT(state()!=QAbstractAnimation::Stopped);
79 m_axis->setLayout(vector);
97 if(state()!=QAbstractAnimation::Stopped)//workaround
98 {
99 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
100 m_axis->setLayout(vector);
101 }
102
80 }
103 }
81
104
82 #include "moc_axisanimationitem_p.cpp"
105 #include "moc_axisanimationitem_p.cpp"
83
106
84 QTCOMMERCIALCHART_END_NAMESPACE
107 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,416 +1,430
1 #include "axisitem_p.h"
1 #include "axisitem_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QDebug>
5 #include <QDebug>
6
6
7 static int label_padding = 5;
7 static int label_padding = 5;
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 AxisItem::AxisItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
11 AxisItem::AxisItem(QChartAxis* axis,AxisType type,QGraphicsItem* parent) :
12 ChartItem(parent),
12 ChartItem(parent),
13 m_chartAxis(axis),
13 m_chartAxis(axis),
14 m_type(type),
14 m_type(type),
15 m_labelsAngle(0),
15 m_labelsAngle(0),
16 m_grid(parent),
16 m_grid(parent),
17 m_shades(parent),
17 m_shades(parent),
18 m_labels(parent),
18 m_labels(parent),
19 m_axis(parent),
19 m_axis(parent),
20 m_min(0),
20 m_min(0),
21 m_max(0),
21 m_max(0),
22 m_ticksCount(0)
22 m_ticksCount(0),
23 m_zoomFactor(0)
23 {
24 {
24 //initial initialization
25 //initial initialization
25 m_axis.setZValue(ChartPresenter::AxisZValue);
26 m_axis.setZValue(ChartPresenter::AxisZValue);
26 m_shades.setZValue(ChartPresenter::ShadesZValue);
27 m_shades.setZValue(ChartPresenter::ShadesZValue);
27 m_grid.setZValue(ChartPresenter::GridZValue);
28 m_grid.setZValue(ChartPresenter::GridZValue);
28 setFlags(QGraphicsItem::ItemHasNoContents);
29 setFlags(QGraphicsItem::ItemHasNoContents);
29
30
30 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
31 QObject::connect(m_chartAxis,SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
31 QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
32 QObject::connect(m_chartAxis->categories(),SIGNAL(updated()),this,SLOT(handleAxisCategoriesUpdated()));
32
33
33 handleAxisUpdated();
34 handleAxisUpdated();
34 }
35 }
35
36
36 AxisItem::~AxisItem()
37 AxisItem::~AxisItem()
37 {
38 {
38 }
39 }
39
40
40 QRectF AxisItem::boundingRect() const
41 QRectF AxisItem::boundingRect() const
41 {
42 {
42 return QRectF();
43 return QRectF();
43 }
44 }
44
45
45 void AxisItem::createItems(int count)
46 void AxisItem::createItems(int count)
46 {
47 {
47 if(m_axis.children().size()==0)
48 if(m_axis.children().size()==0)
48 m_axis.addToGroup(new QGraphicsLineItem());
49 m_axis.addToGroup(new QGraphicsLineItem());
49 for (int i = 0; i < count; ++i) {
50 for (int i = 0; i < count; ++i) {
50 m_grid.addToGroup(new QGraphicsLineItem());
51 m_grid.addToGroup(new QGraphicsLineItem());
51 m_labels.addToGroup(new QGraphicsSimpleTextItem());
52 m_labels.addToGroup(new QGraphicsSimpleTextItem());
52 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
53 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
53 m_axis.addToGroup(new QGraphicsLineItem());
54 m_axis.addToGroup(new QGraphicsLineItem());
54 }
55 }
55 }
56 }
56
57
57 void AxisItem::deleteItems(int count)
58 void AxisItem::deleteItems(int count)
58 {
59 {
59 QList<QGraphicsItem *> lines = m_grid.childItems();
60 QList<QGraphicsItem *> lines = m_grid.childItems();
60 QList<QGraphicsItem *> labels = m_labels.childItems();
61 QList<QGraphicsItem *> labels = m_labels.childItems();
61 QList<QGraphicsItem *> shades = m_shades.childItems();
62 QList<QGraphicsItem *> shades = m_shades.childItems();
62 QList<QGraphicsItem *> axis = m_axis.childItems();
63 QList<QGraphicsItem *> axis = m_axis.childItems();
63
64
64 for (int i = 0; i < count; ++i) {
65 for (int i = 0; i < count; ++i) {
65 delete(lines.takeLast());
66 delete(lines.takeLast());
66 delete(labels.takeLast());
67 delete(labels.takeLast());
67 if(lines.size()%2) delete(shades.takeLast());
68 if(lines.size()%2) delete(shades.takeLast());
68 delete(axis.takeLast());
69 delete(axis.takeLast());
69 }
70 }
70 }
71 }
71
72
72 void AxisItem::updateLayout(QVector<qreal>& layout)
73 void AxisItem::updateLayout(QVector<qreal>& layout)
73 {
74 {
74 setLayout(layout);
75 setLayout(layout);
75 }
76 }
76
77
77 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) const
78 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) const
78 {
79 {
79 Q_ASSERT(max>=min);
80 Q_ASSERT(max>=min);
80 Q_ASSERT(ticks>0);
81 Q_ASSERT(ticks>0);
81
82
82 QStringList labels;
83 QStringList labels;
83
84
84 QChartAxisCategories* categories = m_chartAxis->categories();
85 QChartAxisCategories* categories = m_chartAxis->categories();
85
86
86 for(int i=0; i< ticks; i++) {
87 for(int i=0; i< ticks; i++) {
87 qreal value = min + (i * (max - min)/ (ticks-1));
88 qreal value = min + (i * (max - min)/ (ticks-1));
88 if(categories->count()==0) {
89 if(categories->count()==0) {
89 labels << QString::number(value);
90 labels << QString::number(value);
90 }
91 }
91 else {
92 else {
92 QString label = categories->label(value);
93 QString label = categories->label(value);
93 labels << label;
94 labels << label;
94 }
95 }
95 }
96 }
96
97
98 return labels;
97 return labels;
99 }
98 }
100
99
101 void AxisItem::setAxisOpacity(qreal opacity)
100 void AxisItem::setAxisOpacity(qreal opacity)
102 {
101 {
103 m_axis.setOpacity(opacity);
102 m_axis.setOpacity(opacity);
104 }
103 }
105
104
106 qreal AxisItem::axisOpacity() const
105 qreal AxisItem::axisOpacity() const
107 {
106 {
108 return m_axis.opacity();
107 return m_axis.opacity();
109 }
108 }
110
109
111 void AxisItem::setGridOpacity(qreal opacity)
110 void AxisItem::setGridOpacity(qreal opacity)
112 {
111 {
113 m_grid.setOpacity(opacity);
112 m_grid.setOpacity(opacity);
114 }
113 }
115
114
116 qreal AxisItem::gridOpacity() const
115 qreal AxisItem::gridOpacity() const
117 {
116 {
118 return m_grid.opacity();
117 return m_grid.opacity();
119 }
118 }
120
119
121 void AxisItem::setLabelsOpacity(qreal opacity)
120 void AxisItem::setLabelsOpacity(qreal opacity)
122 {
121 {
123 m_labels.setOpacity(opacity);
122 m_labels.setOpacity(opacity);
124 }
123 }
125
124
126 qreal AxisItem::labelsOpacity() const
125 qreal AxisItem::labelsOpacity() const
127 {
126 {
128 return m_labels.opacity();
127 return m_labels.opacity();
129 }
128 }
130
129
131 void AxisItem::setShadesOpacity(qreal opacity)
130 void AxisItem::setShadesOpacity(qreal opacity)
132 {
131 {
133 m_shades.setOpacity(opacity);
132 m_shades.setOpacity(opacity);
134 }
133 }
135
134
136 qreal AxisItem::shadesOpacity() const
135 qreal AxisItem::shadesOpacity() const
137 {
136 {
138 return m_shades.opacity();
137 return m_shades.opacity();
139 }
138 }
140
139
141 void AxisItem::setLabelsAngle(int angle)
140 void AxisItem::setLabelsAngle(int angle)
142 {
141 {
143 foreach(QGraphicsItem* item , m_labels.childItems()) {
142 foreach(QGraphicsItem* item , m_labels.childItems()) {
144 QPointF center = item->boundingRect().center();
143 QPointF center = item->boundingRect().center();
145 item->setRotation(angle);
144 item->setRotation(angle);
146 }
145 }
147
146
148 m_labelsAngle=angle;
147 m_labelsAngle=angle;
149 }
148 }
150
149
151 void AxisItem::setLabelsPen(const QPen& pen)
150 void AxisItem::setLabelsPen(const QPen& pen)
152 {
151 {
153 foreach(QGraphicsItem* item , m_labels.childItems()) {
152 foreach(QGraphicsItem* item , m_labels.childItems()) {
154 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
153 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
155 }
154 }
156 }
155 }
157
156
158 void AxisItem::setLabelsBrush(const QBrush& brush)
157 void AxisItem::setLabelsBrush(const QBrush& brush)
159 {
158 {
160 foreach(QGraphicsItem* item , m_labels.childItems()) {
159 foreach(QGraphicsItem* item , m_labels.childItems()) {
161 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
160 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
162 }
161 }
163 }
162 }
164
163
165 void AxisItem::setLabelsFont(const QFont& font)
164 void AxisItem::setLabelsFont(const QFont& font)
166 {
165 {
167 foreach(QGraphicsItem* item , m_labels.childItems()) {
166 foreach(QGraphicsItem* item , m_labels.childItems()) {
168 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
167 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
169 }
168 }
170 }
169 }
171
170
172 void AxisItem::setShadesBrush(const QBrush& brush)
171 void AxisItem::setShadesBrush(const QBrush& brush)
173 {
172 {
174 foreach(QGraphicsItem* item , m_shades.childItems()) {
173 foreach(QGraphicsItem* item , m_shades.childItems()) {
175 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
174 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
176 }
175 }
177 }
176 }
178
177
179 void AxisItem::setShadesPen(const QPen& pen)
178 void AxisItem::setShadesPen(const QPen& pen)
180 {
179 {
181 foreach(QGraphicsItem* item , m_shades.childItems()) {
180 foreach(QGraphicsItem* item , m_shades.childItems()) {
182 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
181 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
183 }
182 }
184 }
183 }
185
184
186 void AxisItem::setAxisPen(const QPen& pen)
185 void AxisItem::setAxisPen(const QPen& pen)
187 {
186 {
188 foreach(QGraphicsItem* item , m_axis.childItems()) {
187 foreach(QGraphicsItem* item , m_axis.childItems()) {
189 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
188 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
190 }
189 }
191 }
190 }
192
191
193 void AxisItem::setGridPen(const QPen& pen)
192 void AxisItem::setGridPen(const QPen& pen)
194 {
193 {
195 foreach(QGraphicsItem* item , m_grid.childItems()) {
194 foreach(QGraphicsItem* item , m_grid.childItems()) {
196 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
195 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
197 }
196 }
198 }
197 }
199
198
200 QVector<qreal> AxisItem::calculateLayout() const
199 QVector<qreal> AxisItem::calculateLayout() const
201 {
200 {
202 Q_ASSERT(m_ticksCount>=2);
201 Q_ASSERT(m_ticksCount>=2);
203
202
204 QVector<qreal> points;
203 QVector<qreal> points;
205 points.resize(m_ticksCount);
204 points.resize(m_ticksCount);
206
205
207 switch (m_type)
206 switch (m_type)
208 {
207 {
209 case X_AXIS:
208 case X_AXIS:
210 {
209 {
211 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
210 const qreal deltaX = m_rect.width()/(m_ticksCount-1);
212 for (int i = 0; i < m_ticksCount; ++i) {
211 for (int i = 0; i < m_ticksCount; ++i) {
213 int x = i * deltaX + m_rect.left();
212 int x = i * deltaX + m_rect.left();
214 points[i] = x;
213 points[i] = x;
215 }
214 }
216 }
215 }
217 break;
216 break;
218 case Y_AXIS:
217 case Y_AXIS:
219 {
218 {
220 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
219 const qreal deltaY = m_rect.height()/(m_ticksCount-1);
221 for (int i = 0; i < m_ticksCount; ++i) {
220 for (int i = 0; i < m_ticksCount; ++i) {
222 int y = i * -deltaY + m_rect.bottom();
221 int y = i * -deltaY + m_rect.bottom();
223 points[i] = y;
222 points[i] = y;
224 }
223 }
225 }
224 }
226 break;
225 break;
227 }
226 }
228 return points;
227 return points;
229 }
228 }
230
229
231 void AxisItem::setLayout(QVector<qreal>& layout)
230 void AxisItem::setLayout(QVector<qreal>& layout)
232 {
231 {
233 int diff = m_layoutVector.size() - layout.size();
232 int diff = m_layoutVector.size() - layout.size();
234
233
235 if(diff>0) {
234 if(diff>0) {
236 deleteItems(diff);
235 deleteItems(diff);
237 }
236 }
238 else if(diff<0) {
237 else if(diff<0) {
239 createItems(-diff);
238 createItems(-diff);
240 }
239 }
241
240
241 if(diff!=0) handleAxisUpdated();
242
243 QStringList ticksList = createLabels(m_ticksCount,m_min,m_max);
244
242 QList<QGraphicsItem *> lines = m_grid.childItems();
245 QList<QGraphicsItem *> lines = m_grid.childItems();
243 QList<QGraphicsItem *> labels = m_labels.childItems();
246 QList<QGraphicsItem *> labels = m_labels.childItems();
244 QList<QGraphicsItem *> shades = m_shades.childItems();
247 QList<QGraphicsItem *> shades = m_shades.childItems();
245 QList<QGraphicsItem *> axis = m_axis.childItems();
248 QList<QGraphicsItem *> axis = m_axis.childItems();
246
249
247 Q_ASSERT(labels.size() == m_ticksList.size());
250 Q_ASSERT(labels.size() == ticksList.size());
248 Q_ASSERT(layout.size() == m_ticksList.size());
251 Q_ASSERT(layout.size() == ticksList.size());
249
252
250 switch (m_type)
253 switch (m_type)
251 {
254 {
252 case X_AXIS:
255 case X_AXIS:
253 {
256 {
254 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
257 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
255 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
258 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
256
259
257 for (int i = 0; i < layout.size(); ++i) {
260 for (int i = 0; i < layout.size(); ++i) {
258 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
261 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
259 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
262 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
260 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
263 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
261 labelItem->setText(m_ticksList.at(i));
264 labelItem->setText(ticksList.at(i));
262 QPointF center = labelItem->boundingRect().center();
265 QPointF center = labelItem->boundingRect().center();
263 labelItem->setTransformOriginPoint(center.x(), center.y());
266 labelItem->setTransformOriginPoint(center.x(), center.y());
264 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
267 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
265 if(i%2 && i+1 < layout.size()) {
268 if(i%2 && i+1 < layout.size()) {
266 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
269 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
267 rectItem->setRect(layout[i],m_rect.top(),layout[i+1]-layout[i],m_rect.height());
270 rectItem->setRect(layout[i],m_rect.top(),layout[i+1]-layout[i],m_rect.height());
268 }
271 }
269 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
272 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
270 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
273 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
271 }
274 }
272 }
275 }
273 break;
276 break;
274
277
275 case Y_AXIS:
278 case Y_AXIS:
276 {
279 {
277 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
280 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
278 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
281 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
279
282
280 for (int i = 0; i < layout.size(); ++i) {
283 for (int i = 0; i < layout.size(); ++i) {
281 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
284 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
282 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
285 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
283 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
286 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
284 labelItem->setText(m_ticksList.at(i));
287 labelItem->setText(ticksList.at(i));
285 QPointF center = labelItem->boundingRect().center();
288 QPointF center = labelItem->boundingRect().center();
286 labelItem->setTransformOriginPoint(center.x(), center.y());
289 labelItem->setTransformOriginPoint(center.x(), center.y());
287 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
290 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
288 if(i%2 && i+1 < layout.size()) {
291 if(i%2 && i+1 < layout.size()) {
289 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
292 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
290 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i]-layout[i+1]);
293 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i]-layout[i+1]);
291 }
294 }
292 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
295 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
293 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
296 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
294 }
297 }
295 }
298 }
296 break;
299 break;
297 default:
300 default:
298 qDebug()<<"Unknown axis type";
301 qDebug()<<"Unknown axis type";
299 break;
302 break;
300 }
303 }
301
304
302 if(diff!=0) handleAxisUpdated();
305 //if(diff!=0) handleAxisUpdated();
303 m_layoutVector=layout;
306 m_layoutVector=layout;
304 }
307 }
305
308
306 bool AxisItem::isEmpty()
309 bool AxisItem::isEmpty()
307 {
310 {
308 return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0;
311 return m_rect.isEmpty() || m_min==m_max || m_ticksCount==0;
309 }
312 }
310
313
311 //handlers
314 //handlers
312
315
313 void AxisItem::handleAxisCategoriesUpdated()
316 void AxisItem::handleAxisCategoriesUpdated()
314 {
317 {
315 if(isEmpty()) return;
318 if(isEmpty()) return;
316 m_ticksList = createLabels(m_ticksCount,m_min,m_max);
317 updateLayout(m_layoutVector);
319 updateLayout(m_layoutVector);
318 }
320 }
319
321
320 void AxisItem::handleAxisUpdated()
322 void AxisItem::handleAxisUpdated()
321 {
323 {
322
324
323 int count = m_chartAxis->ticksCount();
325 int count = m_chartAxis->ticksCount();
324
326
325 if(m_ticksCount!=count){
327 if(m_ticksCount!=count){
326 handleTicksCountChanged(count);
328 //handleTicksCountChanged(count);
327 }
329 }
328
330
329 if(isEmpty()) return;
331 if(isEmpty()) return;
330
332
331 if(m_chartAxis->isAxisVisible()) {
333 if(m_chartAxis->isAxisVisible()) {
332 setAxisOpacity(100);
334 setAxisOpacity(100);
333 }
335 }
334 else {
336 else {
335 setAxisOpacity(0);
337 setAxisOpacity(0);
336 }
338 }
337
339
338 if(m_chartAxis->isGridVisible()) {
340 if(m_chartAxis->isGridVisible()) {
339 setGridOpacity(100);
341 setGridOpacity(100);
340 }
342 }
341 else {
343 else {
342 setGridOpacity(0);
344 setGridOpacity(0);
343 }
345 }
344
346
345 if(m_chartAxis->labelsVisible())
347 if(m_chartAxis->labelsVisible())
346 {
348 {
347 setLabelsOpacity(100);
349 setLabelsOpacity(100);
348 }
350 }
349 else {
351 else {
350 setLabelsOpacity(0);
352 setLabelsOpacity(0);
351 }
353 }
352
354
353 if(m_chartAxis->shadesVisible()) {
355 if(m_chartAxis->shadesVisible()) {
354 setShadesOpacity(m_chartAxis->shadesOpacity());
356 setShadesOpacity(m_chartAxis->shadesOpacity());
355 }
357 }
356 else {
358 else {
357 setShadesOpacity(0);
359 setShadesOpacity(0);
358 }
360 }
359
361
360 setLabelsAngle(m_chartAxis->labelsAngle());
362 setLabelsAngle(m_chartAxis->labelsAngle());
361 setAxisPen(m_chartAxis->axisPen());
363 setAxisPen(m_chartAxis->axisPen());
362 setLabelsPen(m_chartAxis->labelsPen());
364 setLabelsPen(m_chartAxis->labelsPen());
363 setLabelsBrush(m_chartAxis->labelsBrush());
365 setLabelsBrush(m_chartAxis->labelsBrush());
364 setLabelsFont(m_chartAxis->labelsFont());
366 setLabelsFont(m_chartAxis->labelsFont());
365 setGridPen(m_chartAxis->gridPen());
367 setGridPen(m_chartAxis->gridPen());
366 setShadesPen(m_chartAxis->shadesPen());
368 setShadesPen(m_chartAxis->shadesPen());
367 setShadesBrush(m_chartAxis->shadesBrush());
369 setShadesBrush(m_chartAxis->shadesBrush());
368
370
369 }
371 }
370
372
371 void AxisItem::handleTicksCountChanged(int count)
373 void AxisItem::handleTicksCountChanged(int count)
372 {
374 {
373 m_ticksCount=count;
375 m_ticksCount=count;
374
376
375 if(isEmpty()) return;
377 if(isEmpty()) return;
376 m_ticksList = createLabels(m_ticksCount,m_min,m_max);
377 QVector<qreal> layout = calculateLayout();
378 QVector<qreal> layout = calculateLayout();
378 updateLayout(layout);
379 updateLayout(layout);
379 }
380 }
380
381
381 void AxisItem::handleRangeChanged(qreal min, qreal max)
382 void AxisItem::handleRangeChanged(qreal min, qreal max)
382 {
383 {
383
384
385 if(m_min<min || m_max>max){
386 m_zoomFactor = (min + (max-min)/2 - m_min)/(m_max - m_min);
387 }
388 else
389 m_zoomFactor=-1;
390
384 m_min = min;
391 m_min = min;
385 m_max = max;
392 m_max = max;
386
393
394 m_ticksCount = qrand()%10;
395
396 while(m_ticksCount<2){
397 m_ticksCount = qrand()%10;
398 }
399
400 qDebug()<<"Warning : This is testing . Simulating new random ticks "<< m_ticksCount;
401 //m_chartAxis->setTicksCount(m_ticksCount);
402
387 if(isEmpty()) return;
403 if(isEmpty()) return;
388 m_ticksList = createLabels(m_ticksCount,m_min,m_max);
389 QVector<qreal> layout = calculateLayout();
404 QVector<qreal> layout = calculateLayout();
390 updateLayout(layout);
405 updateLayout(layout);
391
406
392 }
407 }
393
408
394 void AxisItem::handleGeometryChanged(const QRectF& rect)
409 void AxisItem::handleGeometryChanged(const QRectF& rect)
395 {
410 {
396
411
397 m_rect = rect;
412 m_rect = rect;
398 if(isEmpty()) return;
413 if(isEmpty()) return;
399 m_ticksList = createLabels(m_ticksCount,m_min,m_max);
400 QVector<qreal> layout = calculateLayout();
414 QVector<qreal> layout = calculateLayout();
401 updateLayout(layout);
415 updateLayout(layout);
402 }
416 }
403
417
404 //painter
418 //painter
405
419
406 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
420 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
407 {
421 {
408 Q_UNUSED(painter);
422 Q_UNUSED(painter);
409 Q_UNUSED(option);
423 Q_UNUSED(option);
410 Q_UNUSED(widget);
424 Q_UNUSED(widget);
411 }
425 }
412
426
413 //TODO "nice numbers algorithm"
427 //TODO "nice numbers algorithm"
414 #include "moc_axisitem_p.cpp"
428 #include "moc_axisitem_p.cpp"
415
429
416 QTCOMMERCIALCHART_END_NAMESPACE
430 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,92 +1,93
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; }
52 inline QRectF geometry() const { return m_rect; }
53 inline qreal zoomFactor() const { return m_zoomFactor;}
53
54
54 public slots:
55 public slots:
55 void handleAxisUpdated();
56 void handleAxisUpdated();
56 void handleAxisCategoriesUpdated();
57 void handleAxisCategoriesUpdated();
57 void handleRangeChanged(qreal min , qreal max);
58 void handleRangeChanged(qreal min , qreal max);
58 void handleTicksCountChanged(int count);
59 void handleTicksCountChanged(int count);
59 void handleGeometryChanged(const QRectF& size);
60 void handleGeometryChanged(const QRectF& size);
60
61
61 public:
62 public:
62 virtual void updateLayout(QVector<qreal>& layout);
63 virtual void updateLayout(QVector<qreal>& layout);
63 void setLayout(QVector<qreal>& layout);
64 void setLayout(QVector<qreal>& layout);
64 QVector<qreal> layout() { return m_layoutVector;};
65 QVector<qreal> layout() { return m_layoutVector;};
65
66
66 private:
67 private:
67 inline bool isEmpty();
68 inline bool isEmpty();
68 void createItems(int count);
69 void createItems(int count);
69 void deleteItems(int count);
70 void deleteItems(int count);
70 QVector<qreal> calculateLayout() const;
71 QVector<qreal> calculateLayout() const;
71 QStringList createLabels(int ticks, qreal min, qreal max) const;
72 QStringList createLabels(int ticks, qreal min, qreal max) const;
72
73
73 private:
74 private:
74 QChartAxis* m_chartAxis;
75 QChartAxis* m_chartAxis;
75 AxisType m_type;
76 AxisType m_type;
76 QRectF m_rect;
77 QRectF m_rect;
77 int m_labelsAngle;
78 int m_labelsAngle;
78 QGraphicsItemGroup m_grid;
79 QGraphicsItemGroup m_grid;
79 QGraphicsItemGroup m_shades;
80 QGraphicsItemGroup m_shades;
80 QGraphicsItemGroup m_labels;
81 QGraphicsItemGroup m_labels;
81 QGraphicsItemGroup m_axis;
82 QGraphicsItemGroup m_axis;
82 QStringList m_ticksList;
83 QVector<qreal> m_layoutVector;
83 QVector<qreal> m_layoutVector;
84 qreal m_min;
84 qreal m_min;
85 qreal m_max;
85 qreal m_max;
86 int m_ticksCount;
86 int m_ticksCount;
87 qreal m_zoomFactor;
87
88
88 };
89 };
89
90
90 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
91
92
92 #endif /* AXISITEM_H_ */
93 #endif /* AXISITEM_H_ */
@@ -1,123 +1,122
1 #include "linechartitem_p.h"
1 #include "linechartitem_p.h"
2 #include "qlineseries.h"
2 #include "qlineseries.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5
5
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 //TODO: optimize : remove points which are not visible
9 //TODO: optimize : remove points which are not visible
10
10
11 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
11 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
12 m_series(series),
12 m_series(series),
13 m_items(this)
13 m_items(this)
14 {
14 {
15 //m_items.setZValue(ChartPresenter::LineChartZValue);
15 //m_items.setZValue(ChartPresenter::LineChartZValue);
16 setZValue(ChartPresenter::LineChartZValue);
16 setZValue(ChartPresenter::LineChartZValue);
17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
18
19 handleUpdated();
18 handleUpdated();
20 }
19 }
21
20
22 QRectF LineChartItem::boundingRect() const
21 QRectF LineChartItem::boundingRect() const
23 {
22 {
24 return m_rect;
23 return m_rect;
25 }
24 }
26
25
27 QPainterPath LineChartItem::shape() const
26 QPainterPath LineChartItem::shape() const
28 {
27 {
29 return m_path;
28 return m_path;
30 }
29 }
31
30
32 void LineChartItem::createPoints(int count)
31 void LineChartItem::createPoints(int count)
33 {
32 {
34 for (int i = 0; i < count; ++i) {
33 for (int i = 0; i < count; ++i) {
35 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3);
34 QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3);
36 m_items.addToGroup(item);
35 m_items.addToGroup(item);
37 }
36 }
38 }
37 }
39
38
40 void LineChartItem::deletePoints(int count)
39 void LineChartItem::deletePoints(int count)
41 {
40 {
42 QList<QGraphicsItem *> items = m_items.childItems();
41 QList<QGraphicsItem *> items = m_items.childItems();
43
42
44 for (int i = 0; i < count; ++i) {
43 for (int i = 0; i < count; ++i) {
45 delete(items.takeLast());
44 delete(items.takeLast());
46 }
45 }
47 }
46 }
48
47
49 void LineChartItem::setGeometry(QVector<QPointF>& points)
48 void LineChartItem::setGeometry(QVector<QPointF>& points)
50 {
49 {
51 if(points.size()==0) return;
50 if(points.size()==0) return;
52
51
53 int diff = XYChartItem::points().size() - points.size();
52 int diff = XYChartItem::points().size() - points.size();
54
53
55 if(diff>0) {
54 if(diff>0) {
56 deletePoints(diff);
55 deletePoints(diff);
57 }
56 }
58 else if(diff<0) {
57 else if(diff<0) {
59 createPoints(-diff);
58 createPoints(-diff);
60 }
59 }
61
60
62 QList<QGraphicsItem*> items = m_items.childItems();
61 QList<QGraphicsItem*> items = m_items.childItems();
63
62
64 QPainterPath path;
63 QPainterPath path;
65 const QPointF& point = points.at(0);
64 const QPointF& point = points.at(0);
66 path.moveTo(point);
65 path.moveTo(point);
67 QGraphicsItem* item = items.at(0);
66 QGraphicsItem* item = items.at(0);
68 item->setPos(point.x()-1,point.y()-1);
67 item->setPos(point.x()-1,point.y()-1);
69 if(!clipRect().contains(point)) {
68 if(!clipRect().contains(point)) {
70 item->setVisible(false);
69 item->setVisible(false);
71 }
70 }
72 else {
71 else {
73 item->setVisible(true);
72 item->setVisible(true);
74 }
73 }
75
74
76 for(int i=1; i< points.size();i++) {
75 for(int i=1; i< points.size();i++) {
77 QGraphicsItem* item = items.at(i);
76 QGraphicsItem* item = items.at(i);
78 const QPointF& point = points.at(i);
77 const QPointF& point = points.at(i);
79 item->setPos(point.x()-1,point.y()-1);
78 item->setPos(point.x()-1,point.y()-1);
80 if(!clipRect().contains(point)) {
79 if(!clipRect().contains(point)) {
81 item->setVisible(false);
80 item->setVisible(false);
82 }
81 }
83 else {
82 else {
84 item->setVisible(true);
83 item->setVisible(true);
85 }
84 }
86 path.lineTo(point);
85 path.lineTo(point);
87 }
86 }
88
87
89 prepareGeometryChange();
88 prepareGeometryChange();
90 m_path = path;
89 m_path = path;
91 m_rect = path.boundingRect();
90 m_rect = path.boundingRect();
92
91
93 XYChartItem::setGeometry(points);
92 XYChartItem::setGeometry(points);
94 }
93 }
95
94
96 void LineChartItem::setLinePen(const QPen& pen)
95 void LineChartItem::setLinePen(const QPen& pen)
97 {
96 {
98 m_pen = pen;
97 m_pen = pen;
99 }
98 }
100
99
101 void LineChartItem::handleUpdated()
100 void LineChartItem::handleUpdated()
102 {
101 {
103 m_items.setVisible(m_series->pointsVisible());
102 m_items.setVisible(m_series->pointsVisible());
104 setLinePen(m_series->pen());
103 setLinePen(m_series->pen());
105 update();
104 update();
106 }
105 }
107
106
108 //painter
107 //painter
109
108
110 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
109 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
111 {
110 {
112 Q_UNUSED(widget);
111 Q_UNUSED(widget);
113 Q_UNUSED(option);
112 Q_UNUSED(option);
114 painter->save();
113 painter->save();
115 painter->setPen(m_pen);
114 painter->setPen(m_pen);
116 painter->setClipRect(clipRect());
115 painter->setClipRect(clipRect());
117 painter->drawPath(m_path);
116 painter->drawPath(m_path);
118 painter->restore();
117 painter->restore();
119 }
118 }
120
119
121 #include "moc_linechartitem_p.cpp"
120 #include "moc_linechartitem_p.cpp"
122
121
123 QTCOMMERCIALCHART_END_NAMESPACE
122 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,102 +1,107
1 #ifndef XYCHARTANIMATIONITEM_P_H_
1 #ifndef XYCHARTANIMATIONITEM_P_H_
2 #define XYCHARTANIMATIONITEM_P_H_
2 #define XYCHARTANIMATIONITEM_P_H_
3 #include "qchartglobal.h"
3 #include "qchartglobal.h"
4 #include "xychartanimator_p.h"
4 #include "xychartanimator_p.h"
5 #include <QPointF>
5 #include <QPointF>
6 #include <QTimer>
6 #include <QTimer>
7 #include <QGraphicsItem>
7 #include <QGraphicsItem>
8
8
9 Q_DECLARE_METATYPE(QVector<QPointF>)
9 Q_DECLARE_METATYPE(QVector<QPointF>)
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13
13
14 const static int duration = 1000;
14 const static int duration = 1000;
15
15
16 template <class T , class U>
16 template <class T , class U>
17 class XYChartAnimationItem : public T {
17 class XYChartAnimationItem : public T {
18
18
19 public:
19 public:
20 XYChartAnimationItem(U *series, QGraphicsItem *parent = 0);
20 XYChartAnimationItem(U *series, QGraphicsItem *parent = 0);
21 virtual ~XYChartAnimationItem();
21 virtual ~XYChartAnimationItem();
22
22
23 void animationStarted();
23 void animationStarted();
24
24
25 protected:
25 protected:
26 virtual void updatePoints(QVector<QPointF>& newPoints);
26 virtual void updatePoints(QVector<QPointF>& newPoints);
27 virtual void updatePoint(QVector<QPointF>& newPoints);
27 virtual void updatePoint(QVector<QPointF>& newPoints);
28
28
29 private:
29 private:
30 XYChartAnimator<T,U> *m_animation;
30 XYChartAnimator<T,U> *m_animation;
31 QVector<QPointF> m_points;
31 QVector<QPointF> m_points;
32 bool m_dirty;
32 bool m_dirty;
33 };
33 };
34
34
35 template <class T, class U>
35 template <class T, class U>
36 XYChartAnimationItem<T,U>::XYChartAnimationItem(U *series,QGraphicsItem *parent):
36 XYChartAnimationItem<T,U>::XYChartAnimationItem(U *series,QGraphicsItem *parent):
37 T(series,parent),
37 T(series,parent),
38 m_animation(new XYChartAnimator<T,U>(this,this)),
38 m_animation(new XYChartAnimator<T,U>(this,this)),
39 m_dirty(false)
39 m_dirty(false)
40 {
40 {
41 }
41 }
42
42
43 template <class T, class U>
43 template <class T, class U>
44 XYChartAnimationItem<T,U>::~XYChartAnimationItem()
44 XYChartAnimationItem<T,U>::~XYChartAnimationItem()
45 {
45 {
46 }
46 }
47
47
48 template <class T, class U>
48 template <class T, class U>
49 void XYChartAnimationItem<T,U>::updatePoints(QVector<QPointF>& newPoints)
49 void XYChartAnimationItem<T,U>::updatePoints(QVector<QPointF>& newPoints)
50 {
50 {
51 QVector<QPointF> oldPoints = T::points();
51 QVector<QPointF> oldPoints = T::points();
52
52
53 if(newPoints.count()==0) return;
53 if(newPoints.count()==0) return;
54
55 bool empty = oldPoints.count()==0;
54 oldPoints.resize(newPoints.size());
56 oldPoints.resize(newPoints.size());
55
57
56 if(m_animation->state()!=QAbstractAnimation::Stopped){
58 if(m_animation->state()!=QAbstractAnimation::Stopped){
57 m_animation->stop();
59 m_animation->stop();
58 }
60 }
59
61
60 m_animation->setDuration(duration);
62 m_animation->setDuration(duration);
61 m_animation->setAnimationType(XYChartAnimator<T,U>::LineDrawAnimation);
63 if(!empty)
62 m_animation->setEasingCurve(QEasingCurve::InOutBack);
64 m_animation->setAnimationType(XYChartAnimator<T,U>::MoveDownAnimation);
65 else
66 m_animation->setAnimationType(XYChartAnimator<T,U>::LineDrawAnimation);
67 m_animation->setEasingCurve(QEasingCurve::OutQuart);
63 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints));
68 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints));
64 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
69 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
65 QTimer::singleShot(0,m_animation,SLOT(start()));
70 QTimer::singleShot(0,m_animation,SLOT(start()));
66
71
67 m_points = newPoints;
72 m_points = newPoints;
68 m_dirty=false;
73 m_dirty=false;
69 }
74 }
70
75
71 template <class T, class U>
76 template <class T, class U>
72 void XYChartAnimationItem<T,U>::updatePoint(QVector<QPointF>& newPoints)
77 void XYChartAnimationItem<T,U>::updatePoint(QVector<QPointF>& newPoints)
73 {
78 {
74
79
75 if(m_animation->state()!=QAbstractAnimation::Stopped) {
80 if(m_animation->state()!=QAbstractAnimation::Stopped) {
76 m_animation->stop();
81 m_animation->stop();
77 m_dirty=true;
82 m_dirty=true;
78 }
83 }
79
84
80 if(m_dirty) {
85 if(m_dirty) {
81 m_points=newPoints;
86 m_points=newPoints;
82 m_dirty=false;
87 m_dirty=false;
83 }
88 }
84
89
85 m_animation->setDuration(duration);
90 m_animation->setDuration(duration);
86 m_animation->setAnimationType(XYChartAnimator<T,U>::MoveDownAnimation);
91 m_animation->setAnimationType(XYChartAnimator<T,U>::MoveDownAnimation);
87 m_animation->setEasingCurve(QEasingCurve::InOutBack);
92 m_animation->setEasingCurve(QEasingCurve::OutQuart);
88 m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points));
93 m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points));
89 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
94 m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints));
90
95
91 QTimer::singleShot(0,m_animation,SLOT(start()));
96 QTimer::singleShot(0,m_animation,SLOT(start()));
92 }
97 }
93
98
94 template <class T, class U>
99 template <class T, class U>
95 void XYChartAnimationItem<T,U>::animationStarted()
100 void XYChartAnimationItem<T,U>::animationStarted()
96 {
101 {
97 m_dirty=true;
102 m_dirty=true;
98 }
103 }
99
104
100 QTCOMMERCIALCHART_END_NAMESPACE
105 QTCOMMERCIALCHART_END_NAMESPACE
101
106
102 #endif
107 #endif
@@ -1,103 +1,103
1 #ifndef XYCHARTANIMATOR_P_H_
1 #ifndef XYCHARTANIMATOR_P_H_
2 #define XYCHARTANIMATOR_P_H_
2 #define XYCHARTANIMATOR_P_H_
3 #include "qchartglobal.h"
3 #include "qchartglobal.h"
4 #include <QVariantAnimation>
4 #include <QVariantAnimation>
5 #include <QPointF>
5 #include <QPointF>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 template <class T, class U>
9 template <class T, class U>
10 class XYChartAnimationItem;
10 class XYChartAnimationItem;
11
11
12
12
13 template <class T, class U>
13 template <class T, class U>
14 class XYChartAnimator : public QVariantAnimation
14 class XYChartAnimator : public QVariantAnimation
15 {
15 {
16 public:
16 public:
17 enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation };
17 enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation };
18 XYChartAnimator(XYChartAnimationItem<T,U> *item, QObject *parent = 0 );
18 XYChartAnimator(XYChartAnimationItem<T,U> *item, QObject *parent = 0 );
19 ~XYChartAnimator();
19 ~XYChartAnimator();
20 void setAnimationType(Animation type);
20 void setAnimationType(Animation type);
21
21
22 protected:
22 protected:
23 QVariant interpolated(const QVariant &start, const QVariant & end, qreal progress ) const;
23 QVariant interpolated(const QVariant &start, const QVariant & end, qreal progress ) const;
24 void updateCurrentValue (const QVariant & value );
24 void updateCurrentValue (const QVariant & value );
25 void updateState ( QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
25 void updateState ( QAbstractAnimation::State newState, QAbstractAnimation::State oldState);
26
26
27 private:
27 private:
28 XYChartAnimationItem<T,U> *m_item;
28 XYChartAnimationItem<T,U> *m_item;
29 Animation m_type;
29 Animation m_type;
30 };
30 };
31
31
32 template <class T, class U>
32 template <class T, class U>
33 XYChartAnimator<T,U>::XYChartAnimator(XYChartAnimationItem<T,U> *item , QObject *parent):QVariantAnimation(parent),
33 XYChartAnimator<T,U>::XYChartAnimator(XYChartAnimationItem<T,U> *item , QObject *parent):QVariantAnimation(parent),
34 m_item(item),
34 m_item(item),
35 m_type(MoveDownAnimation)
35 m_type(MoveDownAnimation)
36 {
36 {
37 }
37 }
38
38
39 template <class T,class U>
39 template <class T,class U>
40 XYChartAnimator<T,U>::~XYChartAnimator()
40 XYChartAnimator<T,U>::~XYChartAnimator()
41 {
41 {
42 }
42 }
43
43
44 template <class T,class U>
44 template <class T,class U>
45 void XYChartAnimator<T,U>::setAnimationType(Animation type)
45 void XYChartAnimator<T,U>::setAnimationType(Animation type)
46 {
46 {
47 m_type=type;
47 m_type=type;
48 }
48 }
49
49
50 template <class T, class U>
50 template <class T, class U>
51 QVariant XYChartAnimator<T,U>::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
51 QVariant XYChartAnimator<T,U>::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
52 {
52 {
53 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
53 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
54 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
54 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
55 QVector<QPointF> result;
55 QVector<QPointF> result;
56
56
57 switch(m_type) {
57 switch(m_type) {
58
58
59 case MoveDownAnimation: {
59 case MoveDownAnimation: {
60
60
61 Q_ASSERT(startVector.count() == endVector.count());
61 Q_ASSERT(startVector.count() == endVector.count());
62 for(int i =0;i< startVector.count();i++) {
62 for(int i =0;i< startVector.count();i++) {
63 qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress);
63 qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress);
64 qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress);
64 qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress);
65 result << QPointF(x,y);
65 result << QPointF(x,y);
66 }
66 }
67
67
68 }
68 }
69 break;
69 break;
70 case LineDrawAnimation:{
70 case LineDrawAnimation:{
71 for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) {
71 for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) {
72 result << endVector[i];
72 result << endVector[i];
73 }
73 }
74 }
74 }
75 break;
75 break;
76 default:
76 default:
77 qWarning()<<"Unknow type of animation";
77 qWarning()<<"Unknow type of animation";
78 break;
78 break;
79 }
79 }
80
80
81 return qVariantFromValue(result);
81 return qVariantFromValue(result);
82 }
82 }
83
83
84 template <class T, class U>
84 template <class T, class U>
85 void XYChartAnimator<T,U>::updateCurrentValue (const QVariant & value )
85 void XYChartAnimator<T,U>::updateCurrentValue (const QVariant & value )
86 {
86 {
87 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
88 if(state()!=QAbstractAnimation::Stopped){ //workaround
87 if(state()!=QAbstractAnimation::Stopped){ //workaround
89 m_item->setGeometry(vector);
88 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
89 m_item->setGeometry(vector);
90 }
90 }
91 }
91 }
92
92
93 template <class T, class U>
93 template <class T, class U>
94 void XYChartAnimator<T,U>::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
94 void XYChartAnimator<T,U>::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
95 {
95 {
96 Q_UNUSED(oldState)
96 Q_UNUSED(oldState)
97 if (newState==QAbstractAnimation::Running) m_item->animationStarted();
97 if (newState==QAbstractAnimation::Running) m_item->animationStarted();
98 QVariantAnimation::updateState(newState,oldState);
98 QVariantAnimation::updateState(newState,oldState);
99 }
99 }
100
100
101 QTCOMMERCIALCHART_END_NAMESPACE
101 QTCOMMERCIALCHART_END_NAMESPACE
102
102
103 #endif /* XYCHARTANIMATOR_P_H_ */
103 #endif /* XYCHARTANIMATOR_P_H_ */
General Comments 0
You need to be logged in to leave comments. Login now