From 19aaaff661112a24d80b387879daadaa98135fa3 2012-10-09 10:52:38 From: Michal Klocek Date: 2012-10-09 10:52:38 Subject: [PATCH] Adds title support for mulitaxis * moves setGeometry form axischart to sublcasses * adds aligment handling for title --- diff --git a/src/axis/chartaxis.cpp b/src/axis/chartaxis.cpp index b4715bb..f0d591e 100644 --- a/src/axis/chartaxis.cpp +++ b/src/axis/chartaxis.cpp @@ -363,35 +363,6 @@ void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid) if (isEmpty()) return; - if (!m_titleText.isNull()) { - QFontMetrics fn(m_title->font()); - - int size(0); - - if (orientation() == Qt::Horizontal) - size = grid.width(); - else if (orientation() == Qt::Vertical) - size = grid.height(); - - if (fn.boundingRect(m_titleText).width() > size) { - QString string = m_titleText + "..."; - while (fn.boundingRect(string).width() > size && string.length() > 3) - string.remove(string.length() - 4, 1); - m_title->setText(string); - } else { - m_title->setText(m_titleText); - } - - QPointF center = grid.center() - m_title->boundingRect().center(); - if (orientation() == Qt::Horizontal) { - m_title->setPos(center.x(), m_axisRect.bottom() - m_title->boundingRect().height()); - } else if (orientation() == Qt::Vertical) { - m_title->setTransformOriginPoint(m_title->boundingRect().center()); - m_title->setRotation(270); - m_title->setPos(m_axisRect.left() - m_title->boundingRect().width() / 2 + m_title->boundingRect().height() / 2, center.y()); - } - } - QVector layout = calculateLayout(); updateLayout(layout); diff --git a/src/axis/chartaxis_p.h b/src/axis/chartaxis_p.h index c2473f9..a58ef58 100644 --- a/src/axis/chartaxis_p.h +++ b/src/axis/chartaxis_p.h @@ -87,6 +87,7 @@ public: void setTitlePen(const QPen &pen); void setTitleBrush(const QBrush &brush); void setTitleFont(const QFont &font); + QFont titleFont() const { return m_titleFont; }; void setTitleText(const QString &title); QString titleText() const {return m_titleText; }; @@ -157,6 +158,7 @@ private: qreal m_max; AxisAnimation *m_animation; QFont m_font; + QFont m_titleFont; QString m_titleText; int m_labelPadding; QStringList m_labelsList; diff --git a/src/axis/horizontalaxis.cpp b/src/axis/horizontalaxis.cpp index 2a3c676..66bfa21 100644 --- a/src/axis/horizontalaxis.cpp +++ b/src/axis/horizontalaxis.cpp @@ -48,6 +48,7 @@ void HorizontalAxis::updateGeometry() QList labels = labelItems(); QList shades = shadeItems(); QList axis = arrowItems(); + QGraphicsSimpleTextItem* title = titleItem(); Q_ASSERT(labels.size() == labelList.size()); Q_ASSERT(layout.size() == labelList.size()); @@ -154,12 +155,40 @@ 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 { Q_UNUSED(constraint); - QFontMetrics fn(titleItem()->font()); + QFontMetrics fn(titleFont()); QSizeF sh; if (titleText().isNull()) diff --git a/src/axis/verticalaxis.cpp b/src/axis/verticalaxis.cpp index 6919374..4922950 100644 --- a/src/axis/verticalaxis.cpp +++ b/src/axis/verticalaxis.cpp @@ -21,6 +21,7 @@ #include "verticalaxis_p.h" #include "qabstractaxis.h" #include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -48,6 +49,7 @@ void VerticalAxis::updateGeometry() QList labels = labelItems(); QList shades = shadeItems(); QList axis = arrowItems(); + QGraphicsSimpleTextItem* title = titleItem(); Q_ASSERT(labels.size() == labelList.size()); Q_ASSERT(layout.size() == labelList.size()); @@ -149,13 +151,41 @@ 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 { Q_UNUSED(constraint); - QFontMetrics fn(titleItem()->font()); + QFontMetrics fn(titleFont()); QSizeF sh; if (titleText().isNull()) diff --git a/src/chartlayout.cpp b/src/chartlayout.cpp index 12003b6..cd58a7c 100644 --- a/src/chartlayout.cpp +++ b/src/chartlayout.cpp @@ -29,7 +29,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -static const qreal golden_ratio = 0.4; +static const qreal golden_ratio = 0.8;//0.4; ChartLayout::ChartLayout(ChartPresenter *presenter) : m_presenter(presenter),