diff --git a/src/axisanimationitem.cpp b/src/axisanimationitem.cpp index c05563d..2db29e0 100644 --- a/src/axisanimationitem.cpp +++ b/src/axisanimationitem.cpp @@ -17,8 +17,12 @@ AxisAnimationItem::~AxisAnimationItem() { } -void AxisAnimationItem::updateItems(QVector& oldLayout,QVector& newLayout) +void AxisAnimationItem::updateItem() { + QVector oldLayout = layout(); + AxisItem::updateItem(); + QVector newLayout = layout(); + if(newLayout.count()==0) return; oldLayout.resize(newLayout.size()); @@ -31,7 +35,6 @@ void AxisAnimationItem::updateItems(QVector& oldLayout,QVector& ne m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout)); m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout)); QTimer::singleShot(0,m_animation,SLOT(start())); - oldLayout = newLayout; } void AxisAnimationItem::setLabelsAngle(int angle) @@ -66,7 +69,7 @@ QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, void AxisAnimator::updateCurrentValue (const QVariant & value ) { QVector vector = qVariantValue >(value); - m_axis->applyLayout(vector); + m_axis->setLayout(vector); } #include "moc_axisanimationitem_p.cpp" diff --git a/src/axisanimationitem_p.h b/src/axisanimationitem_p.h index 3cfe36e..c192eb8 100644 --- a/src/axisanimationitem_p.h +++ b/src/axisanimationitem_p.h @@ -21,7 +21,7 @@ public: void setLabelsAngle(int angle); protected: - virtual void updateItems(QVector& oldLayout,QVector& newLayout); + virtual void updateItem(); private: AxisAnimator *m_animation; }; diff --git a/src/axisitem.cpp b/src/axisitem.cpp index 1aceb6b..8743fb2 100644 --- a/src/axisitem.cpp +++ b/src/axisitem.cpp @@ -70,11 +70,27 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q_UNUSED(widget); } -void AxisItem::updateItems(QVector& oldLayout,QVector& newLayout) +void AxisItem::updateItem() { - if(newLayout.count()==0) return; - applyLayout(newLayout); - oldLayout=newLayout; + QStringList labels = createLabels(m_ticks,m_min,m_max); + + int diff = m_thicksList.size() - labels.size(); + + if(diff>0) { + clear(diff); + } + else if(diff<0) { + createItems(-diff); + } + + if(diff!=0) handleAxisUpdated(); + + m_thicksList=labels; + + QVector layout = calculateLayout(); + if(layout.count()==0) return; + setLayout(layout); + } QStringList AxisItem::createLabels(int ticks, qreal min, qreal max) @@ -228,9 +244,8 @@ QVector AxisItem::calculateLayout() const return points; } -void AxisItem::applyLayout(const QVector& points) +void AxisItem::setLayout(const QVector& layout) { - Q_ASSERT(points.size() == m_thicksList.size()); QList lines = m_grid.childItems(); QList labels = m_labels.childItems(); @@ -238,6 +253,7 @@ void AxisItem::applyLayout(const QVector& points) QList axis = m_axis.childItems(); Q_ASSERT(labels.size() == m_thicksList.size()); + Q_ASSERT(layout.size() == m_thicksList.size()); switch (m_type) { @@ -246,20 +262,20 @@ void AxisItem::applyLayout(const QVector& points) QGraphicsLineItem *lineItem = static_cast(axis.at(0)); lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); - for (int i = 0; i < points.size(); ++i) { + for (int i = 0; i < layout.size(); ++i) { QGraphicsLineItem *lineItem = static_cast(lines.at(i)); - lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom()); + lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); labelItem->setText(m_thicksList.at(i)); QPointF center = labelItem->boundingRect().center(); labelItem->setTransformOriginPoint(center.x(), center.y()); - labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding); + labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); if(i%2){ QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); - rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height()); + rectItem->setRect(layout[i],m_rect.top(),layout[i+1]-layout[i],m_rect.height()); } lineItem = static_cast(axis.at(i+1)); - lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5); + lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); } } break; @@ -269,20 +285,20 @@ void AxisItem::applyLayout(const QVector& points) QGraphicsLineItem *lineItem = static_cast(axis.at(0)); lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); - for (int i = 0; i < points.size(); ++i) { + for (int i = 0; i < layout.size(); ++i) { QGraphicsLineItem *lineItem = static_cast(lines.at(i)); - lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]); + lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); labelItem->setText(m_thicksList.at(i)); QPointF center = labelItem->boundingRect().center(); labelItem->setTransformOriginPoint(center.x(), center.y()); - labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y()); + labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , layout[i]-center.y()); if(i%2){ QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); - rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]); + rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i]-layout[i+1]); } lineItem = static_cast(axis.at(i+1)); - lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]); + lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); } } break; @@ -290,13 +306,15 @@ void AxisItem::applyLayout(const QVector& points) qDebug()<<"Unknown axis type"; break; } + + m_layoutVector=layout; } //handlers void AxisItem::handleAxisUpdated() { - if(m_layoutVector.count()==0) return; + if(isEmpty()) return; if(m_chartAxis->isAxisVisible()) { setAxisOpacity(100); @@ -335,40 +353,38 @@ void AxisItem::handleAxisUpdated() setGridPen(m_chartAxis->gridPen()); setShadesPen(m_chartAxis->shadesPen()); setShadesBrush(m_chartAxis->shadesBrush()); + } void AxisItem::handleRangeChanged(qreal min, qreal max) { - if(min == max) return; - - QStringList labels = createLabels(4,min,max); - - int diff = m_thicksList.size() - labels.size(); - if(diff>0){ - clear(diff); - }else if(diff<0){ - createItems(-diff); - } - m_thicksList=labels; + m_min = min; + m_max = max; - if(m_rect.isEmpty()) return; + if(isEmpty()) return; + updateItem(); - QVector vector = calculateLayout(); +} - updateItems(m_layoutVector,vector); +void AxisItem::handleTicksCountChanged(int ticks) +{ + m_ticks=ticks; - if(diff!=0) handleAxisUpdated(); + if(isEmpty()) return; + updateItem(); } void AxisItem::handleGeometryChanged(const QRectF& rect) { m_rect = rect; + if(isEmpty()) return; + updateItem(); +} - if(m_thicksList.size()==0) return; - - QVector vector = calculateLayout(); - updateItems(m_layoutVector,vector); +bool AxisItem::isEmpty() +{ + return m_rect.isEmpty() || m_min==m_max || m_ticks==0; } //TODO "nice numbers algorithm" diff --git a/src/axisitem_p.h b/src/axisitem_p.h index e709965..37348ff 100644 --- a/src/axisitem_p.h +++ b/src/axisitem_p.h @@ -52,14 +52,17 @@ public: public slots: void handleAxisUpdated();//qchartaxis update calls void handleRangeChanged(qreal min , qreal max); //domain update calls + void handleTicksCountChanged(int ticks); //ticks changed void handleGeometryChanged(const QRectF& size); //geometry update calls public: - virtual void updateItems(QVector& oldLayout,QVector& newLayout); + virtual void updateItem(); QVector calculateLayout() const; - void applyLayout(const QVector& points); + void setLayout(const QVector& points); + QVector layout() { return m_layoutVector;}; private: + inline bool isEmpty(); void clear(int count); void createItems(int count); QStringList createLabels(int ticks, qreal min, qreal max); @@ -74,6 +77,9 @@ private: QGraphicsItemGroup m_axis; QStringList m_thicksList; QVector m_layoutVector; + qreal m_min; + qreal m_max; + int m_ticks; }; diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index e5d33c6..6b2d132 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -48,8 +48,8 @@ void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY) if(!domain) { domain = new Domain(); - QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal))); - QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal))); + QObject::connect(axisY,SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeYChanged(qreal,qreal))); + QObject::connect(axisX(),SIGNAL(rangeChanged(qreal,qreal)),domain,SLOT(handleAxisRangeXChanged(qreal,qreal))); //initialize m_axisDomainMap.insert(axisY,domain); emit axisAdded(axisY,domain); diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index ed4b803..7fccce5 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -110,11 +110,13 @@ void ChartPresenter::handleAxisAdded(QChartAxis* axis,Domain* domain) QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal))); //initialize item->handleRangeChanged(domain->minX(),domain->maxX()); + item->handleTicksCountChanged(4); } else{ QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal)),item,SLOT(handleRangeChanged(qreal,qreal))); //initialize item->handleRangeChanged(domain->minY(),domain->maxY()); + item->handleTicksCountChanged(4); } QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));