From 5f790ea7fbddcb5c5f37b3df7f63f221336d3d89 2013-05-24 13:20:29 From: Miikka Heikkinen Date: 2013-05-24 13:20:29 Subject: [PATCH] Fix multi-line axis titles. Since QFontMetrics doesn't support getting bounding rects for multi-line strings, we use a dummy graphicsitem instead. Task-number: QTRD-2057 Change-Id: Id6e6a27a00977d295e6f1bce070b08df21c2a666 Reviewed-by: Mika Salmela --- diff --git a/src/axis/horizontalaxis.cpp b/src/axis/horizontalaxis.cpp index 7068d59..323aefe 100644 --- a/src/axis/horizontalaxis.cpp +++ b/src/axis/horizontalaxis.cpp @@ -72,14 +72,21 @@ void HorizontalAxis::updateGeometry() QRectF titleBoundingRect; QString titleText = axis()->titleText(); if (!titleText.isEmpty() && titleItem()->isVisible()) { - QFontMetrics fn(title->font()); int size(0); size = gridRect.width(); - if (fn.boundingRect(titleText).width() > size) { + QGraphicsSimpleTextItem dummyTitle; + dummyTitle.setFont(axis()->titleFont()); + dummyTitle.setText(axis()->titleText()); + QRectF dummyRect = dummyTitle.boundingRect(); + + if (dummyRect.width() > size) { QString string = titleText + "..."; - while (fn.boundingRect(string).width() > size && string.length() > 3) + while (dummyRect.width() > size && string.length() > 3) { string.remove(string.length() - 4, 1); + dummyTitle.setText(string); + dummyRect = dummyTitle.boundingRect(); + } title->setText(string); } else { title->setText(titleText); @@ -200,20 +207,26 @@ void HorizontalAxis::updateGeometry() QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const { Q_UNUSED(constraint); - QFontMetrics fn(axis()->titleFont()); QSizeF sh(0,0); if (axis()->titleText().isEmpty() || !titleItem()->isVisible()) return sh; switch (which) { - case Qt::MinimumSize: - sh = QSizeF(fn.boundingRect("...").width(), fn.height() + (titlePadding() * 2)); + case Qt::MinimumSize: { + QFontMetrics fn(axis()->titleFont()); + 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() + (titlePadding() * 2)); + case Qt::PreferredSize: { + QGraphicsSimpleTextItem dummyTitle; + dummyTitle.setFont(axis()->titleFont()); + dummyTitle.setText(axis()->titleText()); + QRectF titleRect = dummyTitle.boundingRect(); + sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2)); break; + } default: break; } diff --git a/src/axis/polarchartaxisangular.cpp b/src/axis/polarchartaxisangular.cpp index 22b6c8f..07339e4 100644 --- a/src/axis/polarchartaxisangular.cpp +++ b/src/axis/polarchartaxisangular.cpp @@ -224,11 +224,18 @@ void PolarChartAxisAngular::updateGeometry() int size(0); size = axisGeometry().width(); - QFontMetrics titleMetrics(axis()->titleFont()); - if (titleMetrics.boundingRect(titleText).width() > size) { + QGraphicsSimpleTextItem dummyTitle; + dummyTitle.setFont(axis()->titleFont()); + dummyTitle.setText(titleText); + QRectF dummyRect = dummyTitle.boundingRect(); + + if (dummyRect.width() > size) { QString string = titleText + "..."; - while (titleMetrics.boundingRect(string).width() > size && string.length() > 3) + while (dummyRect.width() > size && string.length() > 3) { string.remove(string.length() - 4, 1); + dummyTitle.setText(string); + dummyRect = dummyTitle.boundingRect(); + } title->setText(string); } else { title->setText(titleText); @@ -385,9 +392,12 @@ qreal PolarChartAxisAngular::preferredAxisRadius(const QSizeF &maxSize) } if (!axis()->titleText().isEmpty() && axis()->isTitleVisible()) { - QFontMetrics titleMetrics(axis()->titleFont()); - int titleHeight = titleMetrics.boundingRect(axis()->titleText()).height(); - radius -= titlePadding() + (titleHeight / 2); + QGraphicsSimpleTextItem dummyTitle; + dummyTitle.setFont(axis()->titleFont()); + dummyTitle.setText(axis()->titleText()); + QRectF dummyRect = dummyTitle.boundingRect(); + + radius -= titlePadding() + (dummyRect.height() / 2.0); if (radius < 1.0) // safeguard return 1.0; } diff --git a/src/axis/polarchartaxisradial.cpp b/src/axis/polarchartaxisradial.cpp index 995eafc..c82c588 100644 --- a/src/axis/polarchartaxisradial.cpp +++ b/src/axis/polarchartaxisradial.cpp @@ -205,11 +205,18 @@ void PolarChartAxisRadial::updateGeometry() // Title, along the 0 axis QString titleText = axis()->titleText(); if (!titleText.isEmpty() && axis()->isTitleVisible()) { - QFontMetrics titleMetrics(axis()->titleFont()); - if (titleMetrics.boundingRect(titleText).width() > radius) { + QGraphicsSimpleTextItem dummyTitle; + dummyTitle.setFont(axis()->titleFont()); + dummyTitle.setText(titleText); + QRectF dummyRect = dummyTitle.boundingRect(); + + if (dummyRect.width() > radius) { QString string = titleText + "..."; - while (titleMetrics.boundingRect(string).width() > radius && string.length() > 3) + while (dummyRect.width() > radius && string.length() > 3) { string.remove(string.length() - 4, 1); + dummyTitle.setText(string); + dummyRect = dummyTitle.boundingRect(); + } title->setText(string); } else { title->setText(titleText); diff --git a/src/axis/verticalaxis.cpp b/src/axis/verticalaxis.cpp index 3726359..532fb93 100644 --- a/src/axis/verticalaxis.cpp +++ b/src/axis/verticalaxis.cpp @@ -74,14 +74,21 @@ void VerticalAxis::updateGeometry() QRectF titleBoundingRect; QString titleText = axis()->titleText(); if (!titleText.isEmpty() && titleItem()->isVisible()) { - QFontMetrics fn(title->font()); int size(0); size = gridRect.height(); - if (fn.boundingRect(titleText).width() > size) { + QGraphicsSimpleTextItem dummyTitle; + dummyTitle.setFont(axis()->titleFont()); + dummyTitle.setText(axis()->titleText()); + QRectF dummyRect = dummyTitle.boundingRect(); + + if (dummyRect.width() > size) { QString string = titleText + "..."; - while (fn.boundingRect(string).width() > size && string.length() > 3) + while (dummyRect.width() > size && string.length() > 3) { string.remove(string.length() - 4, 1); + dummyTitle.setText(string); + dummyRect = dummyTitle.boundingRect(); + } title->setText(string); } else { title->setText(titleText); @@ -206,22 +213,27 @@ void VerticalAxis::updateGeometry() QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const { - Q_UNUSED(constraint); - QFontMetrics fn(axis()->titleFont()); QSizeF sh(0, 0); if (axis()->titleText().isEmpty() || !titleItem()->isVisible()) return sh; switch (which) { - case Qt::MinimumSize: - sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect("...").width()); + case Qt::MinimumSize: { + QFontMetrics fn(axis()->titleFont()); + sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect("...").width()); break; + } case Qt::MaximumSize: - case Qt::PreferredSize: - sh = QSizeF(fn.height() + (titlePadding() * 2), fn.boundingRect(axis()->titleText()).width()); + case Qt::PreferredSize: { + QGraphicsSimpleTextItem dummyTitle; + dummyTitle.setFont(axis()->titleFont()); + dummyTitle.setText(axis()->titleText()); + QRectF titleRect = dummyTitle.boundingRect(); + sh = QSizeF(titleRect.height() + (titlePadding() * 2), titleRect.width()); break; + } default: break; } diff --git a/src/layout/polarchartlayout.cpp b/src/layout/polarchartlayout.cpp index ad48244..905f43c 100644 --- a/src/layout/polarchartlayout.cpp +++ b/src/layout/polarchartlayout.cpp @@ -58,8 +58,11 @@ QRectF PolarChartLayout::calculateAxisGeometry(const QRectF &geometry, const QLi && chartAxis->axis()->isTitleVisible() && !chartAxis->axis()->titleText().isEmpty()) { // If axis has angular title, adjust geometry down by the space title takes - QFontMetrics titleMetrics(chartAxis->axis()->titleFont()); - titleHeight = (titleMetrics.boundingRect(chartAxis->axis()->titleText()).height() / 2) + chartAxis->titlePadding(); + QGraphicsSimpleTextItem dummyTitle; + dummyTitle.setFont(chartAxis->axis()->titleFont()); + dummyTitle.setText(chartAxis->axis()->titleText()); + QRectF dummyRect = dummyTitle.boundingRect(); + titleHeight = (dummyRect.height() / 2.0) + chartAxis->titlePadding(); } }