verticalaxis.cpp
390 lines
| 17.4 KiB
| text/x-c
|
CppLexer
|
r2111 | /**************************************************************************** | ||
** | ||||
|
r2776 | ** Copyright (C) 2015 The Qt Company Ltd | ||
|
r2111 | ** All rights reserved. | ||
|
r2776 | ** For any questions to The Qt Company, please use contact form at http://qt.io | ||
|
r2111 | ** | ||
|
r2740 | ** This file is part of the Qt Charts module. | ||
|
r2111 | ** | ||
|
r2740 | ** Licensees holding valid commercial license for Qt may use this file in | ||
** accordance with the Qt License Agreement provided with the Software | ||||
** or, alternatively, in accordance with the terms contained in a written | ||||
|
r2776 | ** agreement between you and The Qt Company. | ||
|
r2111 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
|
r2740 | ** contact form at http://qt.io | ||
|
r2111 | ** | ||
****************************************************************************/ | ||||
|
r2714 | #include <private/verticalaxis_p.h> | ||
#include <QtCharts/QAbstractAxis> | ||||
#include <private/chartpresenter_p.h> | ||||
|
r2780 | #include <QtCharts/QCategoryAxis> | ||
|
r2714 | #include <QtCore/QDebug> | ||
|
r2111 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r2111 | |||
|
r2483 | VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem *item, bool intervalAxis) | ||
: CartesianChartAxis(axis, item, intervalAxis) | ||||
|
r2111 | { | ||
} | ||||
VerticalAxis::~VerticalAxis() | ||||
{ | ||||
} | ||||
void VerticalAxis::updateGeometry() | ||||
{ | ||||
|
r2483 | const QVector<qreal> &layout = ChartAxisElement::layout(); | ||
|
r2111 | |||
|
r2759 | if (layout.isEmpty() && axis()->type() != QAbstractAxis::AxisTypeLogValue) | ||
|
r2131 | return; | ||
|
r2111 | |||
QStringList labelList = labels(); | ||||
QList<QGraphicsItem *> labels = labelItems(); | ||||
|
r2483 | QList<QGraphicsItem *> arrow = arrowItems(); | ||
|
r2539 | QGraphicsTextItem *title = titleItem(); | ||
|
r2111 | |||
Q_ASSERT(labels.size() == labelList.size()); | ||||
Q_ASSERT(layout.size() == labelList.size()); | ||||
|
r2131 | const QRectF &axisRect = axisGeometry(); | ||
const QRectF &gridRect = gridGeometry(); | ||||
|
r2111 | |||
qreal height = axisRect.bottom(); | ||||
|
r2133 | //arrow | ||
|
r2483 | QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(arrow.at(0)); | ||
|
r2133 | |||
//arrow position | ||||
|
r2483 | if (axis()->alignment() == Qt::AlignLeft) | ||
arrowItem->setLine(axisRect.right(), gridRect.top(), axisRect.right(), gridRect.bottom()); | ||||
else if (axis()->alignment() == Qt::AlignRight) | ||||
arrowItem->setLine(axisRect.left(), gridRect.top(), axisRect.left(), gridRect.bottom()); | ||||
|
r2111 | |||
|
r2148 | //title | ||
|
r2415 | QRectF titleBoundingRect; | ||
|
r2483 | QString titleText = axis()->titleText(); | ||
|
r2540 | qreal availableSpace = axisRect.width() - labelPadding(); | ||
|
r2483 | if (!titleText.isEmpty() && titleItem()->isVisible()) { | ||
|
r2540 | availableSpace -= titlePadding() * 2.0; | ||
|
r2712 | qreal minimumLabelWidth = ChartPresenter::textBoundingRect(axis()->labelsFont(), | ||
QStringLiteral("...")).width(); | ||||
|
r2540 | qreal titleSpace = availableSpace - minimumLabelWidth; | ||
|
r2604 | title->setHtml(ChartPresenter::truncatedText(axis()->titleFont(), titleText, qreal(90.0), | ||
titleSpace, gridRect.height(), | ||||
titleBoundingRect)); | ||||
|
r2607 | title->setTextWidth(titleBoundingRect.height()); | ||
|
r2148 | |||
|
r2415 | titleBoundingRect = title->boundingRect(); | ||
QPointF center = gridRect.center() - titleBoundingRect.center(); | ||||
|
r2540 | if (axis()->alignment() == Qt::AlignLeft) | ||
title->setPos(axisRect.left() - titleBoundingRect.width() / 2.0 + titleBoundingRect.height() / 2.0 + titlePadding(), center.y()); | ||||
else if (axis()->alignment() == Qt::AlignRight) | ||||
title->setPos(axisRect.right() - titleBoundingRect.width() / 2.0 - titleBoundingRect.height() / 2.0 - titlePadding(), center.y()); | ||||
|
r2415 | title->setTransformOriginPoint(titleBoundingRect.center()); | ||
|
r2148 | title->setRotation(270); | ||
|
r2540 | |||
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 | ||
|
r2131 | 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.left(), gridRect.top() + gridRect.bottom() - layout[i], | ||||
gridRect.right(), gridRect.top() + gridRect.bottom() - layout[i]); | ||||
} else { | ||||
gridItem->setLine(gridRect.left(), layout[i], gridRect.right(), layout[i]); | ||||
} | ||||
|
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 labelHeight = (axisRect.height() / layout.count()) - (2 * labelPadding()); | ||||
|
r2607 | QString truncatedText = ChartPresenter::truncatedText(axis()->labelsFont(), text, | ||
axis()->labelsAngle(), | ||||
availableSpace, | ||||
labelHeight, boundingRect); | ||||
labelItem->setTextWidth(ChartPresenter::textBoundingRect(axis()->labelsFont(), | ||||
truncatedText).width()); | ||||
labelItem->setHtml(truncatedText); | ||||
|
r2604 | } | ||
|
r2412 | |||
|
r2133 | //label transformation origin point | ||
|
r2131 | const QRectF &rect = labelItem->boundingRect(); | ||
|
r2111 | QPointF center = rect.center(); | ||
labelItem->setTransformOriginPoint(center.x(), center.y()); | ||||
|
r2540 | qreal widthDiff = rect.width() - boundingRect.width(); | ||
|
r2570 | qreal heightDiff = rect.height() - boundingRect.height(); | ||
|
r2111 | |||
//ticks and label position | ||||
|
r2483 | if (axis()->alignment() == Qt::AlignLeft) { | ||
|
r2781 | if (axis()->isReverse()) { | ||
labelItem->setPos(axisRect.right() - rect.width() + (widthDiff / 2.0) | ||||
- labelPadding(), | ||||
gridRect.top() + gridRect.bottom() | ||||
- layout[layout.size() - i - 1] - center.y()); | ||||
tickItem->setLine(axisRect.right() - labelPadding(), | ||||
gridRect.top() + gridRect.bottom() - layout[i], | ||||
axisRect.right(), | ||||
gridRect.top() + gridRect.bottom() - layout[i]); | ||||
} else { | ||||
labelItem->setPos(axisRect.right() - rect.width() + (widthDiff / 2.0) | ||||
- labelPadding(), | ||||
layout[i] - center.y()); | ||||
tickItem->setLine(axisRect.right() - labelPadding(), layout[i], | ||||
axisRect.right(), layout[i]); | ||||
} | ||||
|
r2483 | } else if (axis()->alignment() == Qt::AlignRight) { | ||
|
r2781 | if (axis()->isReverse()) { | ||
tickItem->setLine(axisRect.left(), | ||||
gridRect.top() + gridRect.bottom() - layout[i], | ||||
axisRect.left() + labelPadding(), | ||||
gridRect.top() + gridRect.bottom() - layout[i]); | ||||
labelItem->setPos(axisRect.left() + labelPadding() - (widthDiff / 2.0), | ||||
gridRect.top() + gridRect.bottom() | ||||
- layout[layout.size() - i - 1] - center.y()); | ||||
} else { | ||||
labelItem->setPos(axisRect.left() + labelPadding() - (widthDiff / 2.0), | ||||
layout[i] - center.y()); | ||||
tickItem->setLine(axisRect.left(), layout[i], | ||||
axisRect.left() + labelPadding(), layout[i]); | ||||
} | ||||
|
r2111 | } | ||
|
r2133 | |||
|
r2446 | //label in between | ||
bool forceHide = false; | ||||
|
r2780 | bool labelOnValue = false; | ||
|
r2446 | if (intervalAxis() && (i + 1) != layout.size()) { | ||
|
r2781 | qreal lowerBound; | ||
qreal upperBound; | ||||
if (axis()->isReverse()) { | ||||
lowerBound = qMax(gridRect.top() + gridRect.bottom() - layout[i + 1], | ||||
gridRect.top()); | ||||
upperBound = qMin(gridRect.top() + gridRect.bottom() - layout[i], | ||||
gridRect.bottom()); | ||||
} else { | ||||
lowerBound = qMin(layout[i], gridRect.bottom()); | ||||
upperBound = qMax(layout[i + 1], gridRect.top()); | ||||
} | ||||
|
r2446 | const qreal delta = lowerBound - upperBound; | ||
|
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.height() | ||||
&& (lowerBound == gridRect.bottom() || upperBound == gridRect.top())) { | ||||
forceHide = true; | ||||
} else { | ||||
labelItem->setPos(labelItem->pos().x(), | ||||
lowerBound - (delta / 2.0) - center.y()); | ||||
} | ||||
|
r2446 | } else { | ||
|
r2780 | QCategoryAxis *categoryAxis = static_cast<QCategoryAxis *>(axis()); | ||
if (categoryAxis->labelsPosition() == QCategoryAxis::AxisLabelsPositionCenter) { | ||||
if (delta < boundingRect.height() | ||||
&& (lowerBound == gridRect.bottom() || upperBound == gridRect.top())) { | ||||
forceHide = true; | ||||
} else { | ||||
labelItem->setPos(labelItem->pos().x(), | ||||
lowerBound - (delta / 2.0) - center.y()); | ||||
} | ||||
} else if (categoryAxis->labelsPosition() | ||||
== QCategoryAxis::AxisLabelsPositionOnValue) { | ||||
labelOnValue = true; | ||||
|
r2781 | if (axis()->isReverse()) { | ||
labelItem->setPos(labelItem->pos().x(), gridRect.top() + gridRect.bottom() | ||||
- layout[i + 1] - center.y()); | ||||
} else { | ||||
labelItem->setPos(labelItem->pos().x(), upperBound - center.y()); | ||||
} | ||||
|
r2780 | } | ||
|
r2446 | } | ||
|
r2111 | } | ||
|
r2445 | //label overlap detection - compensate one pixel for rounding errors | ||
|
r2781 | if (axis()->isReverse()) { | ||
if (forceHide) | ||||
labelItem->setVisible(false); | ||||
} else if (labelItem->pos().y() + boundingRect.height() > height || forceHide || | ||||
|
r2780 | ((labelItem->pos().y() + (heightDiff / 2.0) - 1.0) > axisRect.bottom() | ||
&& !labelOnValue) || | ||||
(labelItem->pos().y() + (heightDiff / 2.0) < (axisRect.top() - 1.0) && !labelOnValue)) { | ||||
|
r2111 | labelItem->setVisible(false); | ||
|
r2133 | } | ||
else { | ||||
|
r2111 | labelItem->setVisible(true); | ||
|
r2133 | height=labelItem->pos().y(); | ||
|
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 lowerBound; | ||||
qreal upperBound; | ||||
if (i == 0) { | ||||
|
r2781 | if (axis()->isReverse()) { | ||
|
r2721 | upperBound = gridRect.top(); | ||
|
r2781 | lowerBound = gridRect.top() + gridRect.bottom() - layout[i]; | ||
} else { | ||||
lowerBound = gridRect.bottom(); | ||||
upperBound = layout[0]; | ||||
} | ||||
} else { | ||||
if (axis()->isReverse()) { | ||||
upperBound = gridRect.top() + gridRect.bottom() - layout[i]; | ||||
if (i == layout.size() - 1) { | ||||
lowerBound = gridRect.bottom(); | ||||
} else { | ||||
lowerBound = qMax(gridRect.top() + gridRect.bottom() - layout[i + 1], | ||||
gridRect.top()); | ||||
} | ||||
} else { | ||||
lowerBound = layout[i]; | ||||
if (i == layout.size() - 1) | ||||
upperBound = gridRect.top(); | ||||
else | ||||
upperBound = qMax(layout[i + 1], gridRect.top()); | ||||
} | ||||
|
r2721 | |||
} | ||||
|
r2760 | if (lowerBound > gridRect.bottom()) | ||
lowerBound = gridRect.bottom(); | ||||
if (upperBound < gridRect.top()) | ||||
upperBound = gridRect.top(); | ||||
|
r2721 | shadeItem->setRect(gridRect.left(), upperBound, gridRect.width(), | ||
lowerBound - upperBound); | ||||
if (shadeItem->rect().height() <= 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 y = gridItem->line().p1().y(); | ||||
|
r2133 | if ((y < gridRect.top() || y > gridRect.bottom())) | ||
{ | ||||
|
r2111 | gridItem->setVisible(false); | ||
tickItem->setVisible(false); | ||||
|
r2133 | }else{ | ||
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); | ||||
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)); | ||||
if (i == 0) { | ||||
minorGridItem->setLine(gridRect.left(), | ||||
gridRect.bottom() - minorTickDistance * qreal(j + 1), | ||||
gridRect.right(), | ||||
gridRect.bottom() - minorTickDistance * qreal(j + 1)); | ||||
} else { | ||||
minorGridItem->setLine(gridRect.left(), | ||||
gridItem->line().p1().y() | ||||
- minorTickDistance * qreal(j + 1), | ||||
gridRect.right(), | ||||
gridItem->line().p1().y() | ||||
- minorTickDistance * qreal(j + 1)); | ||||
} | ||||
if (axis()->alignment() == Qt::AlignLeft) { | ||||
minorArrowItem->setLine(gridRect.left() - labelPadding() / 2, | ||||
minorGridItem->line().p1().y(), | ||||
gridRect.left(), | ||||
minorGridItem->line().p1().y()); | ||||
} else if (axis()->alignment() == Qt::AlignRight){ | ||||
minorArrowItem->setLine(gridRect.right(), | ||||
minorGridItem->line().p1().y(), | ||||
gridRect.right() + labelPadding() / 2, | ||||
minorGridItem->line().p1().y()); | ||||
} | ||||
// check if the minor grid line and the axis tick should be shown | ||||
qreal minorYPos = minorGridItem->line().p1().y(); | ||||
if (minorYPos < gridRect.top() || minorYPos > gridRect.bottom()) { | ||||
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.left(), gridRect.top(), gridRect.right(), gridRect.top()); | ||
|
r2133 | gridLine->setVisible(true); | ||
|
r2483 | gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size() + 1)); | ||
|
r2111 | gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom()); | ||
|
r2133 | gridLine->setVisible(true); | ||
|
r2111 | } | ||
} | ||||
|
r2138 | QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
{ | ||||
Q_UNUSED(constraint); | ||||
|
r2483 | 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.height() + (titlePadding() * 2.0), titleRect.width()); | ||
|
r2138 | break; | ||
|
r2530 | } | ||
|
r2138 | case Qt::MaximumSize: | ||
|
r2530 | case Qt::PreferredSize: { | ||
|
r2539 | QRectF titleRect = ChartPresenter::textBoundingRect(axis()->titleFont(), axis()->titleText()); | ||
|
r2540 | sh = QSizeF(titleRect.height() + (titlePadding() * 2.0), titleRect.width()); | ||
|
r2138 | break; | ||
|
r2530 | } | ||
|
r2138 | default: | ||
break; | ||||
} | ||||
return sh; | ||||
} | ||||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||