From f80448ee21369f9e766967627c1dbd369d422734 2013-02-06 09:55:40 From: Miikka Heikkinen Date: 2013-02-06 09:55:40 Subject: [PATCH] Add padding for axis title Added three pixel padding on either side of the axis title to avoid overlaps with axis labels and neighboring axis lines. Task-number: QTRD-1903 Reviewed-by: Mika Salmela --- diff --git a/src/axis/chartaxis.cpp b/src/axis/chartaxis.cpp index 1cdb0fc..7ef4c79 100644 --- a/src/axis/chartaxis.cpp +++ b/src/axis/chartaxis.cpp @@ -44,7 +44,8 @@ ChartAxis::ChartAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxi m_title(new QGraphicsSimpleTextItem(item)), m_animation(0), m_labelPadding(5), - m_intervalAxis(intervalAxis) + m_intervalAxis(intervalAxis), + m_titlePadding(3) { Q_ASSERT(item); //initial initialization @@ -197,6 +198,11 @@ void ChartAxis::setLabelPadding(int padding) m_labelPadding = padding; } +void ChartAxis::setTitlePadding(int padding) +{ + m_titlePadding = padding; +} + bool ChartAxis::isEmpty() { return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(min(),max()); diff --git a/src/axis/chartaxis_p.h b/src/axis/chartaxis_p.h index 351bee4..15c4594 100644 --- a/src/axis/chartaxis_p.h +++ b/src/axis/chartaxis_p.h @@ -56,6 +56,9 @@ public: void setLabelPadding(int padding); int labelPadding() const { return m_labelPadding;}; + void setTitlePadding(int padding); + int titlePadding() const { return m_titlePadding;}; + QFont titleFont() const; QString titleText() const; @@ -159,6 +162,7 @@ private: int m_labelPadding; QStringList m_labelsList; bool m_intervalAxis; + int m_titlePadding; friend class AxisAnimation; friend class ArrowItem; diff --git a/src/axis/horizontalaxis.cpp b/src/axis/horizontalaxis.cpp index 341975a..fd468b2 100644 --- a/src/axis/horizontalaxis.cpp +++ b/src/axis/horizontalaxis.cpp @@ -88,9 +88,9 @@ void HorizontalAxis::updateGeometry() QPointF center = gridRect.center() - title->boundingRect().center(); if (alignment() == Qt::AlignTop) { - title->setPos(center.x(), axisRect.top()); + title->setPos(center.x(), axisRect.top() + titlePadding()); } else if (alignment() == Qt::AlignBottom) { - title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height()); + title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height() - titlePadding()); } } @@ -107,7 +107,7 @@ void HorizontalAxis::updateGeometry() //label text wrapping QString text = labelList.at(i); QRectF boundingRect = labelBoundingRect(fn, text); - qreal size = axisRect.bottom() - axisRect.top() - labelPadding() - title->boundingRect().height(); + qreal size = axisRect.bottom() - axisRect.top() - labelPadding() - title->boundingRect().height() - (titlePadding() * 2); if (boundingRect.height() > size) { QString label = text + "..."; while (boundingRect.height() >= size && label.length() > 3) { @@ -191,11 +191,11 @@ QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) co switch (which) { case Qt::MinimumSize: - sh = QSizeF(fn.boundingRect("...").width(), fn.height()); + sh = QSizeF(fn.boundingRect("...").width(), fn.height() + (titlePadding() * 2)); break; case Qt::MaximumSize: case Qt::PreferredSize: - sh = QSizeF(fn.boundingRect(axis()->titleText()).width(), fn.height()); + sh = QSizeF(fn.boundingRect(axis()->titleText()).width(), fn.height() + (titlePadding() * 2)); break; default: break; diff --git a/src/axis/verticalaxis.cpp b/src/axis/verticalaxis.cpp index d9334c2..10501bf 100644 --- a/src/axis/verticalaxis.cpp +++ b/src/axis/verticalaxis.cpp @@ -92,10 +92,10 @@ void VerticalAxis::updateGeometry() QPointF center = gridRect.center() - title->boundingRect().center(); if (alignment() == Qt::AlignLeft) { - title->setPos(axisRect.left() - title->boundingRect().width()/2 + title->boundingRect().height()/2 , center.y()); + title->setPos(axisRect.left() - title->boundingRect().width() / 2 + title->boundingRect().height() / 2 + titlePadding(), center.y()); } else if (alignment() == Qt::AlignRight) { - title->setPos(axisRect.right()- title->boundingRect().width()/2 - title->boundingRect().height()/2, center.y()); + title->setPos(axisRect.right() - title->boundingRect().width() / 2 - title->boundingRect().height() / 2 - titlePadding(), center.y()); } title->setTransformOriginPoint(title->boundingRect().center()); title->setRotation(270); @@ -115,7 +115,7 @@ void VerticalAxis::updateGeometry() QString text = labelList.at(i); QRectF boundingRect = labelBoundingRect(fn, text); - qreal size = axisRect.right() - axisRect.left() - labelPadding() - title->boundingRect().height(); + qreal size = axisRect.right() - axisRect.left() - labelPadding() - title->boundingRect().height() - (titlePadding() * 2); if (boundingRect.width() > size) { QString label = text + "..."; while (boundingRect.width() > size && label.length() > 3) { @@ -202,11 +202,11 @@ QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) cons switch (which) { case Qt::MinimumSize: - sh = QSizeF(fn.height(), fn.boundingRect("...").width()); + sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect("...").width()); break; case Qt::MaximumSize: case Qt::PreferredSize: - sh = QSizeF(fn.height(), fn.boundingRect(axis()->titleText()).width()); + sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect(axis()->titleText()).width()); break; default: break;