##// 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 26 if(newLayout.count()==0) return;
23 27 oldLayout.resize(newLayout.size());
24 28
@@ -31,7 +35,6 void AxisAnimationItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& ne
31 35 m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
32 36 m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
33 37 QTimer::singleShot(0,m_animation,SLOT(start()));
34 oldLayout = newLayout;
35 38 }
36 39
37 40 void AxisAnimationItem::setLabelsAngle(int angle)
@@ -66,7 +69,7 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end,
66 69 void AxisAnimator::updateCurrentValue (const QVariant & value )
67 70 {
68 71 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
69 m_axis->applyLayout(vector);
72 m_axis->setLayout(vector);
70 73 }
71 74
72 75 #include "moc_axisanimationitem_p.cpp"
@@ -21,7 +21,7 public:
21 21 void setLabelsAngle(int angle);
22 22
23 23 protected:
24 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
24 virtual void updateItem();
25 25 private:
26 26 AxisAnimator *m_animation;
27 27 };
@@ -70,11 +70,27 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
70 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;
76 applyLayout(newLayout);
77 oldLayout=newLayout;
75 QStringList labels = createLabels(m_ticks,m_min,m_max);
76
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 96 QStringList AxisItem::createLabels(int ticks, qreal min, qreal max)
@@ -228,9 +244,8 QVector<qreal> AxisItem::calculateLayout() const
228 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 250 QList<QGraphicsItem *> lines = m_grid.childItems();
236 251 QList<QGraphicsItem *> labels = m_labels.childItems();
@@ -238,6 +253,7 void AxisItem::applyLayout(const QVector<qreal>& points)
238 253 QList<QGraphicsItem *> axis = m_axis.childItems();
239 254
240 255 Q_ASSERT(labels.size() == m_thicksList.size());
256 Q_ASSERT(layout.size() == m_thicksList.size());
241 257
242 258 switch (m_type)
243 259 {
@@ -246,20 +262,20 void AxisItem::applyLayout(const QVector<qreal>& points)
246 262 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
247 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 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 268 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
253 269 labelItem->setText(m_thicksList.at(i));
254 270 QPointF center = labelItem->boundingRect().center();
255 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 273 if(i%2){
258 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 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 281 break;
@@ -269,20 +285,20 void AxisItem::applyLayout(const QVector<qreal>& points)
269 285 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
270 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 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 291 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
276 292 labelItem->setText(m_thicksList.at(i));
277 293 QPointF center = labelItem->boundingRect().center();
278 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 296 if(i%2){
281 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 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 304 break;
@@ -290,13 +306,15 void AxisItem::applyLayout(const QVector<qreal>& points)
290 306 qDebug()<<"Unknown axis type";
291 307 break;
292 308 }
309
310 m_layoutVector=layout;
293 311 }
294 312
295 313 //handlers
296 314
297 315 void AxisItem::handleAxisUpdated()
298 316 {
299 if(m_layoutVector.count()==0) return;
317 if(isEmpty()) return;
300 318
301 319 if(m_chartAxis->isAxisVisible()) {
302 320 setAxisOpacity(100);
@@ -335,40 +353,38 void AxisItem::handleAxisUpdated()
335 353 setGridPen(m_chartAxis->gridPen());
336 354 setShadesPen(m_chartAxis->shadesPen());
337 355 setShadesBrush(m_chartAxis->shadesBrush());
356
338 357 }
339 358
340 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){
349 clear(diff);
350 }else if(diff<0){
351 createItems(-diff);
352 }
353 m_thicksList=labels;
362 m_min = min;
363 m_max = max;
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 378 void AxisItem::handleGeometryChanged(const QRectF& rect)
365 379 {
366 380 m_rect = rect;
381 if(isEmpty()) return;
382 updateItem();
383 }
367 384
368 if(m_thicksList.size()==0) return;
369
370 QVector<qreal> vector = calculateLayout();
371 updateItems(m_layoutVector,vector);
385 bool AxisItem::isEmpty()
386 {
387 return m_rect.isEmpty() || m_min==m_max || m_ticks==0;
372 388 }
373 389
374 390 //TODO "nice numbers algorithm"
@@ -52,14 +52,17 public:
52 52 public slots:
53 53 void handleAxisUpdated();//qchartaxis update calls
54 54 void handleRangeChanged(qreal min , qreal max); //domain update calls
55 void handleTicksCountChanged(int ticks); //ticks changed
55 56 void handleGeometryChanged(const QRectF& size); //geometry update calls
56 57
57 58 public:
58 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
59 virtual void updateItem();
59 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 64 private:
65 inline bool isEmpty();
63 66 void clear(int count);
64 67 void createItems(int count);
65 68 QStringList createLabels(int ticks, qreal min, qreal max);
@@ -74,6 +77,9 private:
74 77 QGraphicsItemGroup m_axis;
75 78 QStringList m_thicksList;
76 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 48 if(!domain) {
49 49 domain = new Domain();
50 50
51 QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal)));
52 QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(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(handleAxisRangeXChanged(qreal,qreal)));
53 53 //initialize
54 54 m_axisDomainMap.insert(axisY,domain);
55 55 emit axisAdded(axisY,domain);
@@ -110,11 +110,13 void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain)
110 110 QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
111 111 //initialize
112 112 item->handleRangeChanged(domain->minX(),domain->maxX());
113 item->handleTicksCountChanged(4);
113 114 }
114 115 else{
115 116 QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal)));
116 117 //initialize
117 118 item->handleRangeChanged(domain->minY(),domain->maxY());
119 item->handleTicksCountChanged(4);
118 120 }
119 121
120 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