From b2d455450b59da78cbb57b3342e7c1371f2562c5 2012-10-09 14:12:38 From: Michal Klocek Date: 2012-10-09 14:12:38 Subject: [PATCH] Adds consider title boundary for axis label wrapping --- diff --git a/src/axis/horizontalaxis.cpp b/src/axis/horizontalaxis.cpp index 2be7569..811f895 100644 --- a/src/axis/horizontalaxis.cpp +++ b/src/axis/horizontalaxis.cpp @@ -67,6 +67,33 @@ void HorizontalAxis::updateGeometry() qreal width = 0; QFontMetrics fn(font()); + //title + + if (!titleText().isNull()) { + QFontMetrics fn(title->font()); + + int size(0); + + size = gridRect.width(); + QString titleText = this->titleText(); + + if (fn.boundingRect(titleText).width() > size) { + QString string = titleText + "..."; + while (fn.boundingRect(string).width() > size && string.length() > 3) + string.remove(string.length() - 4, 1); + title->setText(string); + } else { + title->setText(titleText); + } + + QPointF center = gridRect.center() - title->boundingRect().center(); + if (alignment() == Qt::AlignTop) { + title->setPos(center.x(), axisRect.top()); + } else if (alignment() == Qt::AlignBottom) { + title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height()); + } + } + for (int i = 0; i < layout.size(); ++i) { //items @@ -154,34 +181,6 @@ void HorizontalAxis::updateGeometry() gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom()); gridLine->setVisible(true); } - - //title - - if (!titleText().isNull()) { - QFontMetrics fn(title->font()); - - int size(0); - - size = gridRect.width(); - QString titleText = this->titleText(); - - if (fn.boundingRect(titleText).width() > size) { - QString string = titleText + "..."; - while (fn.boundingRect(string).width() > size && string.length() > 3) - string.remove(string.length() - 4, 1); - title->setText(string); - } else { - title->setText(titleText); - } - - QPointF center = gridRect.center() - title->boundingRect().center(); - if (alignment() == Qt::AlignTop) { - title->setPos(center.x(), axisRect.top()); - } else if (alignment() == Qt::AlignBottom) { - title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height()); - } - } - } QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const diff --git a/src/axis/verticalaxis.cpp b/src/axis/verticalaxis.cpp index 4922950..415d6f9 100644 --- a/src/axis/verticalaxis.cpp +++ b/src/axis/verticalaxis.cpp @@ -71,6 +71,36 @@ void VerticalAxis::updateGeometry() QFontMetrics fn(font()); + //title + + if (!titleText().isNull()) { + QFontMetrics fn(title->font()); + + int size(0); + size = gridRect.height(); + QString titleText = this->titleText(); + + if (fn.boundingRect(titleText).width() > size) { + QString string = titleText + "..."; + while (fn.boundingRect(string).width() > size && string.length() > 3) + string.remove(string.length() - 4, 1); + title->setText(string); + } + else { + title->setText(titleText); + } + + 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()); + } + else if (alignment() == Qt::AlignRight) { + title->setPos(axisRect.right()- title->boundingRect().width()/2 - title->boundingRect().height()/2, center.y()); + } + title->setTransformOriginPoint(title->boundingRect().center()); + title->setRotation(270); + } + for (int i = 0; i < layout.size(); ++i) { //items @@ -83,9 +113,10 @@ void VerticalAxis::updateGeometry() //label text wrapping QString text = labelList.at(i); - if (fn.boundingRect(text).width() > axisRect.right() - axisRect.left() - labelPadding()) { + qreal size = axisRect.right() - axisRect.left() - labelPadding() - title->boundingRect().height(); + if (fn.boundingRect(text).width() > size) { QString label = text + "..."; - while (fn.boundingRect(label).width() > axisRect.right() - axisRect.left() - labelPadding() && label.length() > 3) + while (fn.boundingRect(label).width() > size && label.length() > 3) label.remove(label.length() - 4, 1); labelItem->setText(label); } else { @@ -151,34 +182,6 @@ void VerticalAxis::updateGeometry() gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom()); gridLine->setVisible(true); } - - //title - - if (!titleText().isNull()) { - QFontMetrics fn(title->font()); - - int size(0); - size = gridRect.height(); - QString titleText = this->titleText(); - - if (fn.boundingRect(titleText).width() > size) { - QString string = titleText + "..."; - while (fn.boundingRect(string).width() > size && string.length() > 3) - string.remove(string.length() - 4, 1); - title->setText(string); - } else { - title->setText(titleText); - } - - 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()); - }else if (alignment() == Qt::AlignRight) { - title->setPos(axisRect.right()- title->boundingRect().width()/2 - title->boundingRect().height()/2, center.y()); - } - title->setTransformOriginPoint(title->boundingRect().center()); - title->setRotation(270); - } } QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const diff --git a/src/chartlayout.cpp b/src/chartlayout.cpp index cd58a7c..12003b6 100644 --- a/src/chartlayout.cpp +++ b/src/chartlayout.cpp @@ -29,7 +29,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -static const qreal golden_ratio = 0.8;//0.4; +static const qreal golden_ratio = 0.4; ChartLayout::ChartLayout(ChartPresenter *presenter) : m_presenter(presenter),