horizontalaxis.cpp
383 lines
| 17.0 KiB
| text/x-c
|
CppLexer
|
r2854 | /**************************************************************************** | ||
|
r2111 | ** | ||
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
|
r2111 | ** | ||
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
|
r2111 | ** | ||
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
|
r2854 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||
** information use the contact form at https://www.qt.io/contact-us. | ||||
** | ||||
** GNU General Public License Usage | ||||
** Alternatively, this file may be used under the terms of the GNU | ||||
** General Public License version 3 or (at your option) any later version | ||||
** approved by the KDE Free Qt Foundation. The licenses are as published by | ||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3 | ||||
** included in the packaging of this file. Please review the following | ||||
** information to ensure the GNU General Public License requirements will | ||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html. | ||||
|
r2111 | ** | ||
|
r2845 | ** $QT_END_LICENSE$ | ||
** | ||||
|
r2854 | ****************************************************************************/ | ||
|
r2111 | |||
|
r2714 | #include <private/horizontalaxis_p.h> | ||
#include <private/qabstractaxis_p.h> | ||||
#include <private/chartpresenter_p.h> | ||||
|
r2780 | #include <QtCharts/QCategoryAxis> | ||
|
r2714 | #include <QtCore/QtMath> | ||
#include <QtCore/QDebug> | ||||
|
r2111 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r2111 | |||
|
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 | |||
|
r2759 | if (layout.isEmpty() && axis()->type() != QAbstractAxis::AxisTypeLogValue) | ||
|
r2131 | return; | ||
|
r2111 | |||
|
r2133 | QStringList labelList = labels(); | ||
|
r2111 | |||
QList<QGraphicsItem *> labels = labelItems(); | ||||
|
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; | ||||
|
r2706 | const QLatin1String ellipsis("..."); | ||
|
r2111 | |||
|
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; | ||
|
r2712 | qreal minimumLabelHeight = ChartPresenter::textBoundingRect(axis()->labelsFont(), | ||
QStringLiteral("...")).height(); | ||||
|
r2540 | 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 | } | ||
|
r2759 | if (layout.isEmpty() && axis()->type() == QAbstractAxis::AxisTypeLogValue) | ||
return; | ||||
QList<QGraphicsItem *> lines = gridItems(); | ||||
QList<QGraphicsItem *> shades = shadeItems(); | ||||
|
r2795 | QList<QGraphicsItem *> minorLines = minorGridItems(); | ||
QList<QGraphicsItem *> minorArrows = minorArrowItems(); | ||||
|
r2759 | |||
|
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 | ||||
|
r2781 | if (axis()->isReverse()) { | ||
gridItem->setLine(gridRect.right() - layout[i] + gridRect.left(), gridRect.top(), | ||||
gridRect.right() - layout[i] + gridRect.left(), gridRect.bottom()); | ||||
} else { | ||||
gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom()); | ||||
} | ||||
|
r2111 | |||
|
r2133 | //label text wrapping | ||
|
r2781 | QString text; | ||
if (axis()->isReverse() && axis()->type() != QAbstractAxis::AxisTypeCategory) | ||||
text = labelList.at(labelList.count() - i - 1); | ||||
else | ||||
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 | ||||
|
r2828 | QPointF labelPos; | ||
|
r2483 | if (axis()->alignment() == Qt::AlignTop) { | ||
|
r2781 | if (axis()->isReverse()) { | ||
|
r2828 | labelPos = QPointF(gridRect.right() - layout[layout.size() - i - 1] | ||
|
r2781 | + gridRect.left() - center.x(), | ||
axisRect.bottom() - rect.height() | ||||
+ (heightDiff / 2.0) - labelPadding()); | ||||
tickItem->setLine(gridRect.right() + gridRect.left() - layout[i], | ||||
axisRect.bottom(), | ||||
gridRect.right() + gridRect.left() - layout[i], | ||||
axisRect.bottom() - labelPadding()); | ||||
} else { | ||||
|
r2828 | labelPos = QPointF(layout[i] - center.x(), axisRect.bottom() - rect.height() | ||
|
r2781 | + (heightDiff / 2.0) - labelPadding()); | ||
tickItem->setLine(layout[i], axisRect.bottom(), | ||||
layout[i], axisRect.bottom() - labelPadding()); | ||||
} | ||||
|
r2483 | } else if (axis()->alignment() == Qt::AlignBottom) { | ||
|
r2781 | if (axis()->isReverse()) { | ||
|
r2828 | labelPos = QPointF(gridRect.right() - layout[layout.size() - i - 1] | ||
|
r2781 | + gridRect.left() - center.x(), | ||
axisRect.top() - (heightDiff / 2.0) + labelPadding()); | ||||
tickItem->setLine(gridRect.right() + gridRect.left() - layout[i], axisRect.top(), | ||||
gridRect.right() + gridRect.left() - layout[i], | ||||
axisRect.top() + labelPadding()); | ||||
} else { | ||||
|
r2828 | labelPos = QPointF(layout[i] - center.x(), axisRect.top() - (heightDiff / 2.0) | ||
|
r2781 | + labelPadding()); | ||
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()) { | ||||
|
r2781 | qreal leftBound; | ||
qreal rightBound; | ||||
if (axis()->isReverse()) { | ||||
leftBound = qMax(gridRect.right() + gridRect.left() - layout[i + 1], | ||||
gridRect.left()); | ||||
rightBound = qMin(gridRect.right() + gridRect.left() - layout[i], gridRect.right()); | ||||
} else { | ||||
leftBound = qMax(layout[i], gridRect.left()); | ||||
rightBound = qMin(layout[i + 1], gridRect.right()); | ||||
} | ||||
|
r2446 | const qreal delta = rightBound - leftBound; | ||
|
r2780 | if (axis()->type() != QAbstractAxis::AxisTypeCategory) { | ||
// Hide label in case visible part of the category at the grid edge is too narrow | ||||
if (delta < boundingRect.width() | ||||
&& (leftBound == gridRect.left() || rightBound == gridRect.right())) { | ||||
forceHide = true; | ||||
} else { | ||||
|
r2828 | labelPos.setX(leftBound + (delta / 2.0) - center.x()); | ||
|
r2780 | } | ||
|
r2446 | } else { | ||
|
r2780 | QCategoryAxis *categoryAxis = static_cast<QCategoryAxis *>(axis()); | ||
if (categoryAxis->labelsPosition() == QCategoryAxis::AxisLabelsPositionCenter) { | ||||
if (delta < boundingRect.width() | ||||
&& (leftBound == gridRect.left() || rightBound == gridRect.right())) { | ||||
forceHide = true; | ||||
} else { | ||||
|
r2828 | labelPos.setX(leftBound + (delta / 2.0) - center.x()); | ||
|
r2780 | } | ||
} else if (categoryAxis->labelsPosition() | ||||
== QCategoryAxis::AxisLabelsPositionOnValue) { | ||||
|
r2781 | if (axis()->isReverse()) | ||
|
r2828 | labelPos.setX(leftBound - center.x()); | ||
|
r2781 | else | ||
|
r2828 | labelPos.setX(rightBound - center.x()); | ||
|
r2780 | } | ||
|
r2446 | } | ||
|
r2111 | } | ||
|
r2706 | |||
|
r2828 | // Round to full pixel via QPoint to avoid one pixel clipping on the edge in some cases | ||
labelItem->setPos(labelPos.toPoint()); | ||||
|
r2445 | //label overlap detection - compensate one pixel for rounding errors | ||
|
r2706 | if ((labelItem->pos().x() < width && labelItem->toPlainText() == ellipsis) || forceHide || | ||
|
r2570 | (labelItem->pos().x() + (widthDiff / 2.0)) < (axisRect.left() - 1.0) || | ||
|
r2651 | (labelItem->pos().x() + (widthDiff / 2.0) - 1.0) > axisRect.right()) { | ||
|
r2111 | labelItem->setVisible(false); | ||
|
r2145 | } else { | ||
|
r2111 | labelItem->setVisible(true); | ||
|
r2412 | width = boundingRect.width() + labelItem->pos().x(); | ||
|
r2111 | } | ||
//shades | ||||
|
r2721 | QGraphicsRectItem *shadeItem = 0; | ||
if (i == 0) | ||||
shadeItem = static_cast<QGraphicsRectItem *>(shades.at(0)); | ||||
else if (i % 2) | ||||
shadeItem = static_cast<QGraphicsRectItem *>(shades.at((i / 2) + 1)); | ||||
if (shadeItem) { | ||||
qreal leftBound; | ||||
qreal rightBound; | ||||
if (i == 0) { | ||||
|
r2781 | if (axis()->isReverse()) { | ||
leftBound = gridRect.right() + gridRect.left() - layout[i]; | ||||
|
r2721 | rightBound = gridRect.right(); | ||
|
r2781 | } else { | ||
leftBound = gridRect.left(); | ||||
rightBound = layout[0]; | ||||
} | ||||
} else { | ||||
if (axis()->isReverse()) { | ||||
rightBound = gridRect.right() + gridRect.left() - layout[i]; | ||||
if (i == layout.size() - 1) { | ||||
leftBound = gridRect.left(); | ||||
} else { | ||||
leftBound = qMax(gridRect.right() + gridRect.left() - layout[i + 1], | ||||
gridRect.left()); | ||||
} | ||||
} else { | ||||
leftBound = layout[i]; | ||||
if (i == layout.size() - 1) | ||||
rightBound = gridRect.right(); | ||||
else | ||||
rightBound = qMin(layout[i + 1], gridRect.right()); | ||||
} | ||||
|
r2721 | } | ||
|
r2760 | if (leftBound < gridRect.left()) | ||
leftBound = gridRect.left(); | ||||
if (rightBound > gridRect.right()) | ||||
rightBound = gridRect.right(); | ||||
|
r2721 | shadeItem->setRect(leftBound, gridRect.top(), rightBound - leftBound, | ||
gridRect.height()); | ||||
if (shadeItem->rect().width() <= 0.0) | ||||
shadeItem->setVisible(false); | ||||
|
r2448 | else | ||
|
r2721 | shadeItem->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 | } | ||
|
r2795 | // add minor ticks | ||
QValueAxis *valueAxis = qobject_cast<QValueAxis *>(axis()); | ||||
if ((i + 1) != layout.size() && valueAxis) { | ||||
int minorTickCount = valueAxis->minorTickCount(); | ||||
if (minorTickCount != 0) { | ||||
qreal minorTickDistance = (layout[i] - layout[i + 1]) / qreal(minorTickCount + 1); | ||||
|
r2851 | if (axis()->isReverse()) | ||
minorTickDistance = -minorTickDistance; | ||||
|
r2795 | for (int j = 0; j < minorTickCount; j++) { | ||
QGraphicsLineItem *minorGridItem = | ||||
static_cast<QGraphicsLineItem *>(minorLines.at(i * minorTickCount + j)); | ||||
QGraphicsLineItem *minorArrowItem = | ||||
static_cast<QGraphicsLineItem *>(minorArrows.at(i * minorTickCount + j)); | ||||
|
r2851 | minorGridItem->setLine(gridItem->line().p1().x() | ||
- minorTickDistance * qreal(j + 1), | ||||
gridRect.top(), | ||||
gridItem->line().p1().x() | ||||
- minorTickDistance * qreal(j + 1), | ||||
gridRect.bottom()); | ||||
|
r2795 | if (axis()->alignment() == Qt::AlignTop) { | ||
minorArrowItem->setLine(minorGridItem->line().p1().x(), | ||||
axisRect.bottom(), | ||||
minorGridItem->line().p1().x(), | ||||
axisRect.bottom() - labelPadding() / 2); | ||||
} else if (axis()->alignment() == Qt::AlignBottom){ | ||||
minorArrowItem->setLine(minorGridItem->line().p1().x(), | ||||
axisRect.top(), | ||||
minorGridItem->line().p1().x(), | ||||
axisRect.top() + labelPadding() / 2); | ||||
} | ||||
// check if the minor grid line and the axis tick should be shown | ||||
qreal minorXPos = minorGridItem->line().p1().x(); | ||||
if (minorXPos < gridRect.left() || minorXPos > gridRect.right()) { | ||||
minorGridItem->setVisible(false); | ||||
minorArrowItem->setVisible(false); | ||||
} else { | ||||
minorGridItem->setVisible(true); | ||||
minorArrowItem->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: { | ||
|
r2712 | QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), | ||
QStringLiteral("...")); | ||||
|
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; | ||||
} | ||||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||