From 65ea14fdb7471b78d73d603823337c8d58c2b0b4 2013-02-07 10:07:12 From: Miikka Heikkinen Date: 2013-02-07 10:07:12 Subject: [PATCH] Fix axis geometry when there is no axis title SizeHint won't include title padding when there is no title, yet updateGeometry() always assumed it was there when determining space for labels, causing truncated labels when title was missing. Also made missing/empty/hidden axis title work consistently. Now axis geometries should be updated correctly always when title text or visibility status changes. Task-number: QTRD-1903 Reviewed-by: Mika Salmela --- diff --git a/src/axis/chartaxis.cpp b/src/axis/chartaxis.cpp index 7ef4c79..2dc0a00 100644 --- a/src/axis/chartaxis.cpp +++ b/src/axis/chartaxis.cpp @@ -296,8 +296,9 @@ void ChartAxis::handleShadesVisibleChanged(bool visible) void ChartAxis::handleTitleVisibleChanged(bool visible) { - m_title->setVisible(visible); + QGraphicsLayoutItem::updateGeometry(); presenter()->layout()->invalidate(); + m_title->setVisible(visible); } void ChartAxis::handleLabelsAngleChanged(int angle) @@ -354,9 +355,9 @@ void ChartAxis::handleGridPenChanged(const QPen &pen) void ChartAxis::handleTitleTextChanged(const QString &title) { - Q_UNUSED(title) QGraphicsLayoutItem::updateGeometry(); presenter()->layout()->invalidate(); + m_title->setText(title); } diff --git a/src/axis/horizontalaxis.cpp b/src/axis/horizontalaxis.cpp index fd468b2..165a8a6 100644 --- a/src/axis/horizontalaxis.cpp +++ b/src/axis/horizontalaxis.cpp @@ -68,12 +68,11 @@ void HorizontalAxis::updateGeometry() QFontMetrics fn(font()); //title - - if (!titleText().isNull()) { + int titlePad = 0; + QRectF titleBoundingRect; + if (!titleText().isEmpty() && titleItem()->isVisible()) { QFontMetrics fn(title->font()); - int size(0); - size = gridRect.width(); QString titleText = this->titleText(); @@ -86,11 +85,14 @@ void HorizontalAxis::updateGeometry() title->setText(titleText); } - QPointF center = gridRect.center() - title->boundingRect().center(); + titlePad = titlePadding(); + titleBoundingRect = title->boundingRect(); + + QPointF center = gridRect.center() - titleBoundingRect.center(); if (alignment() == Qt::AlignTop) { - title->setPos(center.x(), axisRect.top() + titlePadding()); + title->setPos(center.x(), axisRect.top() + titlePad); } else if (alignment() == Qt::AlignBottom) { - title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height() - titlePadding()); + title->setPos(center.x(), axisRect.bottom() - titleBoundingRect.height() - titlePad); } } @@ -107,7 +109,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() - (titlePadding() * 2); + qreal size = axisRect.bottom() - axisRect.top() - labelPadding() - titleBoundingRect.height() - (titlePad * 2); if (boundingRect.height() > size) { QString label = text + "..."; while (boundingRect.height() >= size && label.length() > 3) { @@ -186,7 +188,7 @@ QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) co QFontMetrics fn(titleFont()); QSizeF sh(0,0); - if (titleText().isNull() || !titleItem()->isVisible()) + if (titleText().isEmpty() || !titleItem()->isVisible()) return sh; switch (which) { diff --git a/src/axis/verticalaxis.cpp b/src/axis/verticalaxis.cpp index 10501bf..776713b 100644 --- a/src/axis/verticalaxis.cpp +++ b/src/axis/verticalaxis.cpp @@ -72,10 +72,10 @@ void VerticalAxis::updateGeometry() QFontMetrics fn(font()); //title - - if (!titleText().isNull()) { + int titlePad = 0; + QRectF titleBoundingRect; + if (!titleText().isEmpty() && titleItem()->isVisible()) { QFontMetrics fn(title->font()); - int size(0); size = gridRect.height(); QString titleText = this->titleText(); @@ -90,14 +90,17 @@ void VerticalAxis::updateGeometry() title->setText(titleText); } - QPointF center = gridRect.center() - title->boundingRect().center(); + titlePad = titlePadding(); + titleBoundingRect = title->boundingRect(); + + QPointF center = gridRect.center() - titleBoundingRect.center(); if (alignment() == Qt::AlignLeft) { - title->setPos(axisRect.left() - title->boundingRect().width() / 2 + title->boundingRect().height() / 2 + titlePadding(), center.y()); + title->setPos(axisRect.left() - titleBoundingRect.width() / 2 + titleBoundingRect.height() / 2 + titlePad, center.y()); } else if (alignment() == Qt::AlignRight) { - title->setPos(axisRect.right() - title->boundingRect().width() / 2 - title->boundingRect().height() / 2 - titlePadding(), center.y()); + title->setPos(axisRect.right() - titleBoundingRect.width() / 2 - titleBoundingRect.height() / 2 - titlePad, center.y()); } - title->setTransformOriginPoint(title->boundingRect().center()); + title->setTransformOriginPoint(titleBoundingRect.center()); title->setRotation(270); } @@ -115,7 +118,7 @@ void VerticalAxis::updateGeometry() QString text = labelList.at(i); QRectF boundingRect = labelBoundingRect(fn, text); - qreal size = axisRect.right() - axisRect.left() - labelPadding() - title->boundingRect().height() - (titlePadding() * 2); + qreal size = axisRect.right() - axisRect.left() - labelPadding() - titleBoundingRect.height() - (titlePad * 2); if (boundingRect.width() > size) { QString label = text + "..."; while (boundingRect.width() > size && label.length() > 3) { @@ -197,7 +200,7 @@ QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) cons QFontMetrics fn(titleFont()); QSizeF sh(0,0); - if (titleText().isNull() || !titleItem()->isVisible()) + if (titleText().isEmpty() || !titleItem()->isVisible()) return sh; switch (which) {