horizontalaxis.cpp
224 lines
| 9.0 KiB
| text/x-c
|
CppLexer
|
r2111 | /**************************************************************************** | ||
** | ||||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
r2111 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
|
r2574 | ** This file is part of the Qt Enterprise Charts Add-on. | ||
|
r2111 | ** | ||
** $QT_BEGIN_LICENSE$ | ||||
|
r2574 | ** Licensees holding valid Qt Enterprise licenses may use this file in | ||
** accordance with the Qt Enterprise License Agreement provided with the | ||||
|
r2111 | ** Software or, alternatively, in accordance with the terms contained in | ||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
#include "horizontalaxis_p.h" | ||||
|
r2483 | #include "qabstractaxis_p.h" | ||
|
r2539 | #include "chartpresenter_p.h" | ||
|
r2133 | #include <qmath.h> | ||
|
r2111 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2483 | HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis) | ||
: CartesianChartAxis(axis, item, intervalAxis) | ||||
|
r2111 | { | ||
} | ||||
HorizontalAxis::~HorizontalAxis() | ||||
{ | ||||
} | ||||
void HorizontalAxis::updateGeometry() | ||||
{ | ||||
|
r2483 | const QVector<qreal> &layout = ChartAxisElement::layout(); | ||
|
r2111 | |||
|
r2131 | if (layout.isEmpty()) | ||
return; | ||||
|
r2111 | |||
|
r2133 | QStringList labelList = labels(); | ||
|
r2111 | |||
|
r2483 | QList<QGraphicsItem *> lines = gridItems(); | ||
|
r2111 | QList<QGraphicsItem *> labels = labelItems(); | ||
QList<QGraphicsItem *> shades = shadeItems(); | ||||
|
r2483 | QList<QGraphicsItem *> arrow = arrowItems(); | ||
|
r2539 | QGraphicsTextItem *title = titleItem(); | ||
|
r2111 | |||
|
r2133 | Q_ASSERT(labels.size() == labelList.size()); | ||
Q_ASSERT(layout.size() == labelList.size()); | ||||
|
r2111 | |||
|
r2131 | const QRectF &axisRect = axisGeometry(); | ||
const QRectF &gridRect = gridGeometry(); | ||||
|
r2111 | |||
//arrow | ||||
|
r2483 | QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(arrow.at(0)); | ||
|
r2111 | |||
|
r2483 | if (axis()->alignment() == Qt::AlignTop) | ||
|
r2111 | arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom()); | ||
|
r2483 | else if (axis()->alignment() == Qt::AlignBottom) | ||
|
r2111 | arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top()); | ||
qreal width = 0; | ||||
|
r2148 | //title | ||
|
r2415 | QRectF titleBoundingRect; | ||
|
r2483 | QString titleText = axis()->titleText(); | ||
|
r2540 | qreal availableSpace = axisRect.height() - labelPadding(); | ||
|
r2483 | if (!titleText.isEmpty() && titleItem()->isVisible()) { | ||
|
r2540 | availableSpace -= titlePadding() * 2.0; | ||
qreal minimumLabelHeight = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...").height(); | ||||
qreal titleSpace = availableSpace - minimumLabelHeight; | ||||
|
r2604 | title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(0.0), | ||
gridRect.width(), titleSpace, | ||||
titleBoundingRect)); | ||||
|
r2607 | title->setTextWidth(titleBoundingRect.width()); | ||
|
r2148 | |||
|
r2415 | titleBoundingRect = title->boundingRect(); | ||
QPointF center = gridRect.center() - titleBoundingRect.center(); | ||||
|
r2540 | if (axis()->alignment() == Qt::AlignTop) | ||
title->setPos(center.x(), axisRect.top() + titlePadding()); | ||||
else if (axis()->alignment() == Qt::AlignBottom) | ||||
title->setPos(center.x(), axisRect.bottom() - titleBoundingRect.height() - titlePadding()); | ||||
availableSpace -= titleBoundingRect.height(); | ||||
|
r2148 | } | ||
|
r2111 | for (int i = 0; i < layout.size(); ++i) { | ||
|
r2133 | //items | ||
QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | ||||
|
r2483 | QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(arrow.at(i + 1)); | ||
|
r2539 | QGraphicsTextItem *labelItem = static_cast<QGraphicsTextItem *>(labels.at(i)); | ||
|
r2111 | |||
//grid line | ||||
gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom()); | ||||
|
r2133 | //label text wrapping | ||
|
r2412 | QString text = labelList.at(i); | ||
|
r2534 | QRectF boundingRect; | ||
|
r2604 | // don't truncate empty labels | ||
if (text.isEmpty()) { | ||||
labelItem->setHtml(text); | ||||
} else { | ||||
qreal labelWidth = axisRect.width() / layout.count() - (2 * labelPadding()); | ||||
|
r2607 | QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text, | ||
axis()->labelsAngle(), | ||||
labelWidth, | ||||
availableSpace, boundingRect); | ||||
labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(), | ||||
truncatedText).width()); | ||||
labelItem->setHtml(truncatedText); | ||||
|
r2604 | } | ||
|
r2133 | |||
//label transformation origin point | ||||
const QRectF& rect = labelItem->boundingRect(); | ||||
|
r2111 | QPointF center = rect.center(); | ||
labelItem->setTransformOriginPoint(center.x(), center.y()); | ||||
|
r2540 | qreal heightDiff = rect.height() - boundingRect.height(); | ||
|
r2570 | qreal widthDiff = rect.width() - boundingRect.width(); | ||
|
r2111 | |||
//ticks and label position | ||||
|
r2483 | if (axis()->alignment() == Qt::AlignTop) { | ||
|
r2540 | labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() + (heightDiff / 2.0) - labelPadding()); | ||
|
r2131 | tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding()); | ||
|
r2483 | } else if (axis()->alignment() == Qt::AlignBottom) { | ||
|
r2540 | labelItem->setPos(layout[i] - center.x(), axisRect.top() - (heightDiff / 2.0) + labelPadding()); | ||
|
r2131 | tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding()); | ||
|
r2111 | } | ||
|
r2446 | //label in between | ||
bool forceHide = false; | ||||
if (intervalAxis() && (i + 1) != layout.size()) { | ||||
qreal leftBound = qMax(layout[i], gridRect.left()); | ||||
qreal rightBound = qMin(layout[i + 1], gridRect.right()); | ||||
const qreal delta = rightBound - leftBound; | ||||
// Hide label in case visible part of the category at the grid edge is too narrow | ||||
if (delta < boundingRect.width() | ||||
|
r2604 | && (leftBound == gridRect.left() || rightBound == gridRect.right()) | ||
&& !intervalAxis()) { | ||||
|
r2446 | forceHide = true; | ||
} else { | ||||
labelItem->setPos(leftBound + (delta / 2.0) - center.x(), labelItem->pos().y()); | ||||
} | ||||
|
r2111 | } | ||
|
r2445 | //label overlap detection - compensate one pixel for rounding errors | ||
|
r2604 | if ((labelItem->pos().x() < width || forceHide || | ||
|
r2570 | (labelItem->pos().x() + (widthDiff / 2.0)) < (axisRect.left() - 1.0) || | ||
|
r2604 | (labelItem->pos().x() + (widthDiff / 2.0) - 1.0) > axisRect.right()) | ||
&& !intervalAxis()) { | ||||
|
r2111 | labelItem->setVisible(false); | ||
|
r2145 | } else { | ||
|
r2111 | labelItem->setVisible(true); | ||
|
r2412 | width = boundingRect.width() + labelItem->pos().x(); | ||
|
r2111 | } | ||
//shades | ||||
|
r2131 | if ((i + 1) % 2 && i > 1) { | ||
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1)); | ||||
|
r2448 | qreal leftBound = qMax(layout[i - 1], gridRect.left()); | ||
qreal rightBound = qMin(layout[i], gridRect.right()); | ||||
rectItem->setRect(leftBound, gridRect.top(), rightBound - leftBound, gridRect.height()); | ||||
if (rectItem->rect().width() <= 0.0) | ||||
rectItem->setVisible(false); | ||||
else | ||||
rectItem->setVisible(true); | ||||
|
r2111 | } | ||
// check if the grid line and the axis tick should be shown | ||||
qreal x = gridItem->line().p1().x(); | ||||
if (x < gridRect.left() || x > gridRect.right()) { | ||||
|
r2131 | gridItem->setVisible(false); | ||
tickItem->setVisible(false); | ||||
|
r2483 | } else { | ||
|
r2133 | gridItem->setVisible(true); | ||
tickItem->setVisible(true); | ||||
|
r2111 | } | ||
} | ||||
//begin/end grid line in case labels between | ||||
|
r2131 | if (intervalAxis()) { | ||
|
r2111 | QGraphicsLineItem *gridLine; | ||
|
r2131 | gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size())); | ||
|
r2111 | gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom()); | ||
|
r2133 | gridLine->setVisible(true); | ||
gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1)); | ||||
|
r2111 | gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom()); | ||
|
r2133 | gridLine->setVisible(true); | ||
|
r2111 | } | ||
} | ||||
|
r2138 | QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
{ | ||||
Q_UNUSED(constraint); | ||||
|
r2306 | QSizeF sh(0,0); | ||
|
r2138 | |||
|
r2483 | if (axis()->titleText().isEmpty() || !titleItem()->isVisible()) | ||
|
r2138 | return sh; | ||
switch (which) { | ||||
|
r2530 | case Qt::MinimumSize: { | ||
|
r2539 | QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), "..."); | ||
|
r2540 | sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0)); | ||
|
r2138 | break; | ||
|
r2530 | } | ||
|
r2138 | case Qt::MaximumSize: | ||
|
r2530 | case Qt::PreferredSize: { | ||
|
r2539 | QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText()); | ||
|
r2540 | sh = QSizeF(titleRect.width(), titleRect.height() + (titlePadding() * 2.0)); | ||
|
r2138 | break; | ||
|
r2530 | } | ||
|
r2138 | default: | ||
break; | ||||
} | ||||
return sh; | ||||
} | ||||
|
r2111 | QTCOMMERCIALCHART_END_NAMESPACE | ||