##// END OF EJS Templates
Refacotr axisitem to handle ticks changes
Michal Klocek -
r452:47a9fc437f2c
parent child
Show More
@@ -17,8 +17,12 AxisAnimationItem::~AxisAnimationItem()
17 {
17 {
18 }
18 }
19
19
20 void AxisAnimationItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
20 void AxisAnimationItem::updateItem()
21 {
21 {
22 QVector<qreal> oldLayout = layout();
23 AxisItem::updateItem();
24 QVector<qreal> newLayout = layout();
25
22 if(newLayout.count()==0) return;
26 if(newLayout.count()==0) return;
23 oldLayout.resize(newLayout.size());
27 oldLayout.resize(newLayout.size());
24
28
@@ -31,7 +35,6 void AxisAnimationItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& ne
31 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
35 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
32 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
36 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
33 QTimer::singleShot(0,m_animation,SLOT(start()));
37 QTimer::singleShot(0,m_animation,SLOT(start()));
34 oldLayout = newLayout;
35 }
38 }
36
39
37 void AxisAnimationItem::setLabelsAngle(int angle)
40 void AxisAnimationItem::setLabelsAngle(int angle)
@@ -66,7 +69,7 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end,
66 void AxisAnimator::updateCurrentValue (const QVariant & value )
69 void AxisAnimator::updateCurrentValue (const QVariant & value )
67 {
70 {
68 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
71 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
69 m_axis->applyLayout(vector);
72 m_axis->setLayout(vector);
70 }
73 }
71
74
72 #include "moc_axisanimationitem_p.cpp"
75 #include "moc_axisanimationitem_p.cpp"
@@ -21,7 +21,7 public:
21 void setLabelsAngle(int angle);
21 void setLabelsAngle(int angle);
22
22
23 protected:
23 protected:
24 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
24 virtual void updateItem();
25 private:
25 private:
26 AxisAnimator *m_animation;
26 AxisAnimator *m_animation;
27 };
27 };
@@ -70,11 +70,27 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
70 Q_UNUSED(widget);
70 Q_UNUSED(widget);
71 }
71 }
72
72
73 void AxisItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
73 void AxisItem::updateItem()
74 {
74 {
75 if(newLayout.count()==0) return;
75 QStringList labels = createLabels(m_ticks,m_min,m_max);
76 applyLayout(newLayout);
76
77 oldLayout=newLayout;
77 int diff = m_thicksList.size() - labels.size();
78
79 if(diff>0) {
80 clear(diff);
81 }
82 else if(diff<0) {
83 createItems(-diff);
84 }
85
86 if(diff!=0) handleAxisUpdated();
87
88 m_thicksList=labels;
89
90 QVector<qreal> layout = calculateLayout();
91 if(layout.count()==0) return;
92 setLayout(layout);
93
78 }
94 }
79
95
80 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max)
96 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max)
@@ -228,9 +244,8 QVector<qreal> AxisItem::calculateLayout() const
228 return points;
244 return points;
229 }
245 }
230
246
231 void AxisItem::applyLayout(const QVector<qreal>& points)
247 void AxisItem::setLayout(const QVector<qreal>& layout)
232 {
248 {
233 Q_ASSERT(points.size() == m_thicksList.size());
234
249
235 QList<QGraphicsItem *> lines = m_grid.childItems();
250 QList<QGraphicsItem *> lines = m_grid.childItems();
236 QList<QGraphicsItem *> labels = m_labels.childItems();
251 QList<QGraphicsItem *> labels = m_labels.childItems();
@@ -238,6 +253,7 void AxisItem::applyLayout(const QVector<qreal>& points)
238 QList<QGraphicsItem *> axis = m_axis.childItems();
253 QList<QGraphicsItem *> axis = m_axis.childItems();
239
254
240 Q_ASSERT(labels.size() == m_thicksList.size());
255 Q_ASSERT(labels.size() == m_thicksList.size());
256 Q_ASSERT(layout.size() == m_thicksList.size());
241
257
242 switch (m_type)
258 switch (m_type)
243 {
259 {
@@ -246,20 +262,20 void AxisItem::applyLayout(const QVector<qreal>& points)
246 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
262 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
247 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
263 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
248
264
249 for (int i = 0; i < points.size(); ++i) {
265 for (int i = 0; i < layout.size(); ++i) {
250 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
266 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
251 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
267 lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom());
252 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
268 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
253 labelItem->setText(m_thicksList.at(i));
269 labelItem->setText(m_thicksList.at(i));
254 QPointF center = labelItem->boundingRect().center();
270 QPointF center = labelItem->boundingRect().center();
255 labelItem->setTransformOriginPoint(center.x(), center.y());
271 labelItem->setTransformOriginPoint(center.x(), center.y());
256 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
272 labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding);
257 if(i%2){
273 if(i%2){
258 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
274 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
259 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
275 rectItem->setRect(layout[i],m_rect.top(),layout[i+1]-layout[i],m_rect.height());
260 }
276 }
261 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
277 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
262 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
278 lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5);
263 }
279 }
264 }
280 }
265 break;
281 break;
@@ -269,20 +285,20 void AxisItem::applyLayout(const QVector<qreal>& points)
269 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
285 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
270 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
286 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
271
287
272 for (int i = 0; i < points.size(); ++i) {
288 for (int i = 0; i < layout.size(); ++i) {
273 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
289 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
274 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
290 lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]);
275 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
291 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
276 labelItem->setText(m_thicksList.at(i));
292 labelItem->setText(m_thicksList.at(i));
277 QPointF center = labelItem->boundingRect().center();
293 QPointF center = labelItem->boundingRect().center();
278 labelItem->setTransformOriginPoint(center.x(), center.y());
294 labelItem->setTransformOriginPoint(center.x(), center.y());
279 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
295 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y());
280 if(i%2){
296 if(i%2){
281 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
297 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
282 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
298 rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i]-layout[i+1]);
283 }
299 }
284 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
300 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
285 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
301 lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]);
286 }
302 }
287 }
303 }
288 break;
304 break;
@@ -290,13 +306,15 void AxisItem::applyLayout(const QVector<qreal>& points)
290 qDebug()<<"Unknown axis type";
306 qDebug()<<"Unknown axis type";
291 break;
307 break;
292 }
308 }
309
310 m_layoutVector=layout;
293 }
311 }
294
312
295 //handlers
313 //handlers
296
314
297 void AxisItem::handleAxisUpdated()
315 void AxisItem::handleAxisUpdated()
298 {
316 {
299 if(m_layoutVector.count()==0) return;
317 if(isEmpty()) return;
300
318
301 if(m_chartAxis->isAxisVisible()) {
319 if(m_chartAxis->isAxisVisible()) {
302 setAxisOpacity(100);
320 setAxisOpacity(100);
@@ -335,40 +353,38 void AxisItem::handleAxisUpdated()
335 setGridPen(m_chartAxis->gridPen());
353 setGridPen(m_chartAxis->gridPen());
336 setShadesPen(m_chartAxis->shadesPen());
354 setShadesPen(m_chartAxis->shadesPen());
337 setShadesBrush(m_chartAxis->shadesBrush());
355 setShadesBrush(m_chartAxis->shadesBrush());
356
338 }
357 }
339
358
340 void AxisItem::handleRangeChanged(qreal min, qreal max)
359 void AxisItem::handleRangeChanged(qreal min, qreal max)
341 {
360 {
342 if(min == max) return;
343
344 QStringList labels = createLabels(4,min,max);
345
346 int diff = m_thicksList.size() - labels.size();
347
361
348 if(diff>0){
362 m_min = min;
349 clear(diff);
363 m_max = max;
350 }else if(diff<0){
351 createItems(-diff);
352 }
353 m_thicksList=labels;
354
364
355 if(m_rect.isEmpty()) return;
365 if(isEmpty()) return;
366 updateItem();
356
367
357 QVector<qreal> vector = calculateLayout();
368 }
358
369
359 updateItems(m_layoutVector,vector);
370 void AxisItem::handleTicksCountChanged(int ticks)
371 {
372 m_ticks=ticks;
360
373
361 if(diff!=0) handleAxisUpdated();
374 if(isEmpty()) return;
375 updateItem();
362 }
376 }
363
377
364 void AxisItem::handleGeometryChanged(const QRectF& rect)
378 void AxisItem::handleGeometryChanged(const QRectF& rect)
365 {
379 {
366 m_rect = rect;
380 m_rect = rect;
381 if(isEmpty()) return;
382 updateItem();
383 }
367
384
368 if(m_thicksList.size()==0) return;
385 bool AxisItem::isEmpty()
369
386 {
370 QVector<qreal> vector = calculateLayout();
387 return m_rect.isEmpty() || m_min==m_max || m_ticks==0;
371 updateItems(m_layoutVector,vector);
372 }
388 }
373
389
374 //TODO "nice numbers algorithm"
390 //TODO "nice numbers algorithm"
@@ -52,14 +52,17 public:
52 public slots:
52 public slots:
53 void handleAxisUpdated();//qchartaxis update calls
53 void handleAxisUpdated();//qchartaxis update calls
54 void handleRangeChanged(qreal min , qreal max); //domain update calls
54 void handleRangeChanged(qreal min , qreal max); //domain update calls
55 void handleTicksCountChanged(int ticks); //ticks changed
55 void handleGeometryChanged(const QRectF& size); //geometry update calls
56 void handleGeometryChanged(const QRectF& size); //geometry update calls
56
57
57 public:
58 public:
58 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
59 virtual void updateItem();
59 QVector<qreal> calculateLayout() const;
60 QVector<qreal> calculateLayout() const;
60 void applyLayout(const QVector<qreal>& points);
61 void setLayout(const QVector<qreal>& points);
62 QVector<qreal> layout() { return m_layoutVector;};
61
63
62 private:
64 private:
65 inline bool isEmpty();
63 void clear(int count);
66 void clear(int count);
64 void createItems(int count);
67 void createItems(int count);
65 QStringList createLabels(int ticks, qreal min, qreal max);
68 QStringList createLabels(int ticks, qreal min, qreal max);
@@ -74,6 +77,9 private:
74 QGraphicsItemGroup m_axis;
77 QGraphicsItemGroup m_axis;
75 QStringList m_thicksList;
78 QStringList m_thicksList;
76 QVector<qreal> m_layoutVector;
79 QVector<qreal> m_layoutVector;
80 qreal m_min;
81 qreal m_max;
82 int m_ticks;
77
83
78 };
84 };
79
85
@@ -48,8 +48,8 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
48 if(!domain) {
48 if(!domain) {
49 domain = new Domain();
49 domain = new Domain();
50
50
51 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
51 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal)));
52 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal)));
52 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
53 //initialize
53 //initialize
54 m_axisDomainMap.insert(axisY,domain);
54 m_axisDomainMap.insert(axisY,domain);
55 emit axisAdded(axisY,domain);
55 emit axisAdded(axisY,domain);
@@ -110,11 +110,13 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
110 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
110 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
111 //initialize
111 //initialize
112 item->handleRangeChanged(domain->minX(),domain->maxX());
112 item->handleRangeChanged(domain->minX(),domain->maxX());
113 item->handleTicksCountChanged(4);
113 }
114 }
114 else{
115 else{
115 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
116 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
116 //initialize
117 //initialize
117 item->handleRangeChanged(domain->minY(),domain->maxY());
118 item->handleRangeChanged(domain->minY(),domain->maxY());
119 item->handleTicksCountChanged(4);
118 }
120 }
119
121
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
122 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
General Comments 0
You need to be logged in to leave comments. Login now