From 1946c776c83b65b92e8c7098043404586075042c 2012-02-22 10:09:01 From: Michal Klocek Date: 2012-02-22 10:09:01 Subject: [PATCH] Fix zorder of axis, and ticks --- diff --git a/src/axisitem.cpp b/src/axisitem.cpp index ceaa778..aa56fba 100644 --- a/src/axisitem.cpp +++ b/src/axisitem.cpp @@ -16,11 +16,13 @@ m_shadesEnabled(true), m_grid(parent), m_shades(parent), m_labels(parent), -m_origin(0,0) +m_axis(parent) { //initial initialization + m_axis.setZValue(ChartPresenter::AxisZValue); m_shades.setZValue(ChartPresenter::ShadesZValue); m_grid.setZValue(ChartPresenter::GridZValue); + setFlags(QGraphicsItem::ItemHasNoContents); } AxisItem::~AxisItem() @@ -34,10 +36,12 @@ QRectF AxisItem::boundingRect() const void AxisItem::createItems(int count) { + m_axis.addToGroup(new QGraphicsLineItem(this)); for (int i = 0; i < count; ++i) { m_grid.addToGroup(new QGraphicsLineItem(this)); m_labels.addToGroup(new QGraphicsSimpleTextItem(this)); if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this)); + m_axis.addToGroup(new QGraphicsLineItem(this)); } } @@ -55,6 +59,10 @@ void AxisItem::clear() delete item; } + foreach(QGraphicsItem* item , m_axis.childItems()) { + delete item; + } + m_thicksList.clear(); } @@ -66,10 +74,12 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, void AxisItem::updateItem(int count) { + if(count ==0) return; QList lines = m_grid.childItems(); QList labels = m_labels.childItems(); QList shades = m_shades.childItems(); + QList axis = m_axis.childItems(); switch (m_type) { @@ -77,7 +87,8 @@ void AxisItem::updateItem(int count) { const qreal deltaX = m_rect.width() / (count-1); - m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); + 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 < count; ++i) { int x = i * deltaX + m_rect.left(); @@ -88,11 +99,12 @@ void AxisItem::updateItem(int count) QPointF center = labelItem->boundingRect().center(); labelItem->setTransformOriginPoint(center.x(), center.y()); labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding); - if(i%2){ QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height()); } + lineItem = static_cast(axis.at(i+1)); + lineItem->setLine(x,m_rect.bottom(),x,m_rect.bottom()+5); } } break; @@ -101,7 +113,8 @@ void AxisItem::updateItem(int count) { const qreal deltaY = m_rect.height()/ (count-1); - m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); + 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 < count; ++i) { int y = i * -deltaY + m_rect.bottom(); @@ -116,6 +129,8 @@ void AxisItem::updateItem(int count) QGraphicsRectItem *rectItem = static_cast(shades.at(i/2)); rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY); } + lineItem = static_cast(axis.at(i+1)); + lineItem->setLine(m_rect.left()-5,y,m_rect.left(),y); } } break; @@ -170,14 +185,14 @@ void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels) { m_thicksList=labels; QList items = m_labels.childItems(); - if(items.size()!=m_thicksList.size()){ + //if(items.size()!=m_thicksList.size()){ clear(); m_thicksList=labels; createItems(m_thicksList.size()); updateItem(m_thicksList.size()); items = m_labels.childItems(); handleAxisUpdate(axis); - } + // } Q_ASSERT(items.size()==m_thicksList.size()); @@ -283,7 +298,9 @@ void AxisItem::setShadesPen(const QPen& pen) void AxisItem::setAxisPen(const QPen& pen) { - m_axis.setPen(pen); + foreach(QGraphicsItem* item , m_axis.childItems()) { + static_cast(item)->setPen(pen); + } } void AxisItem::setGridPen(const QPen& pen) diff --git a/src/axisitem_p.h b/src/axisitem_p.h index cd28482..db42b61 100644 --- a/src/axisitem_p.h +++ b/src/axisitem_p.h @@ -66,9 +66,8 @@ private: QGraphicsItemGroup m_grid; QGraphicsItemGroup m_shades; QGraphicsItemGroup m_labels; - QGraphicsLineItem m_axis; + QGraphicsItemGroup m_axis; QStringList m_thicksList; - QPointF m_origin; }; diff --git a/src/qchart.cpp b/src/qchart.cpp index f6c8b9a..f67271b 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -86,6 +86,7 @@ void QChart::createChartBackgroundItem() { if(!m_backgroundItem) { m_backgroundItem = new QGraphicsRectItem(this); + m_backgroundItem->setPen(Qt::NoPen); m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); } }