chartbarcategoryaxisy.cpp
140 lines
| 4.5 KiB
| text/x-c
|
CppLexer
|
r1555 | /**************************************************************************** | ||
|
r1716 | ** | ||
|
r2776 | ** Copyright (C) 2015 The Qt Company Ltd | ||
|
r1716 | ** All rights reserved. | ||
|
r2776 | ** For any questions to The Qt Company, please use contact form at http://qt.io | ||
|
r1716 | ** | ||
|
r2740 | ** This file is part of the Qt Charts module. | ||
|
r1716 | ** | ||
|
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. | ||
|
r1716 | ** | ||
** If you have questions regarding the use of this file, please use | ||||
|
r2740 | ** contact form at http://qt.io | ||
|
r1716 | ** | ||
****************************************************************************/ | ||||
|
r1555 | |||
|
r2714 | #include <private/chartbarcategoryaxisy_p.h> | ||
#include <private/chartpresenter_p.h> | ||||
#include <private/qbarcategoryaxis_p.h> | ||||
#include <private/abstractchartlayout_p.h> | ||||
#include <QtCore/QtMath> | ||||
#include <QtCore/QDebug> | ||||
|
r1555 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r1555 | |||
|
r2273 | ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, QGraphicsItem* item) | ||
: VerticalAxis(axis, item, true), | ||||
|
r2131 | m_categoriesAxis(axis) | ||
|
r1555 | { | ||
|
r2306 | QObject::connect( m_categoriesAxis,SIGNAL(categoriesChanged()),this, SLOT(handleCategoriesChanged())); | ||
handleCategoriesChanged(); | ||||
|
r1555 | } | ||
|
r1918 | ChartBarCategoryAxisY::~ChartBarCategoryAxisY() | ||
|
r1555 | { | ||
} | ||||
|
r1918 | QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const | ||
|
r1555 | { | ||
QVector<qreal> points; | ||||
|
r2145 | const QRectF& gridRect = gridGeometry(); | ||
qreal range = max() - min(); | ||||
|
r2436 | const qreal delta = gridRect.height() / range; | ||
|
r1555 | |||
|
r2436 | if (delta < 2) | ||
return points; | ||||
|
r2111 | |||
|
r2436 | qreal adjustedMin = min() + 0.5; | ||
|
r2775 | qreal offset = (qCeil(adjustedMin) - adjustedMin) * delta; | ||
|
r1716 | |||
|
r2145 | int count = qFloor(range); | ||
|
r2436 | if (count < 1) | ||
return points; | ||||
|
r1837 | |||
|
r2436 | points.resize(count + 2); | ||
|
r1716 | |||
|
r2436 | for (int i = 0; i < count + 2; ++i) | ||
|
r2447 | points[i] = gridRect.bottom() - (qreal(i) * delta) - offset; | ||
|
r2145 | |||
|
r1555 | return points; | ||
} | ||||
|
r1918 | QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const | ||
|
r1720 | { | ||
QStringList result; | ||||
|
r2131 | const QRectF &gridRect = gridGeometry(); | ||
qreal d = (max() - min()) / gridRect.height(); | ||||
|
r2145 | |||
|
r2131 | for (int i = 0; i < layout.count() - 1; ++i) { | ||
qreal x = qFloor(((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5)); | ||||
|
r2105 | if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { | ||
|
r1720 | result << m_categoriesAxis->categories().at(x); | ||
|
r2131 | } else { | ||
|
r2105 | // No label for x coordinate | ||
|
r2712 | result << QString(); | ||
|
r2105 | } | ||
|
r1720 | } | ||
|
r2712 | result << QString(); | ||
|
r1720 | return result; | ||
} | ||||
|
r1918 | void ChartBarCategoryAxisY::updateGeometry() | ||
|
r1555 | { | ||
|
r2483 | const QVector<qreal>& layout = ChartAxisElement::layout(); | ||
|
r2131 | if (layout.isEmpty()) | ||
return; | ||||
|
r2111 | setLabels(createCategoryLabels(layout)); | ||
VerticalAxis::updateGeometry(); | ||||
|
r1555 | } | ||
|
r2306 | void ChartBarCategoryAxisY::handleCategoriesChanged() | ||
|
r1643 | { | ||
|
r2306 | QGraphicsLayoutItem::updateGeometry(); | ||
if(presenter()) presenter()->layout()->invalidate(); | ||||
|
r1643 | } | ||
|
r2131 | QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
|
r1965 | { | ||
Q_UNUSED(constraint) | ||||
QSizeF sh; | ||||
|
r2138 | QSizeF base = VerticalAxis::sizeHint(which, constraint); | ||
|
r2306 | QStringList ticksList = m_categoriesAxis->categories(); | ||
|
r2443 | qreal width = 0; | ||
qreal height = 0; // Height is irrelevant for Y axes with interval labels | ||||
|
r2133 | |||
|
r2412 | switch (which) { | ||
case Qt::MinimumSize: { | ||||
|
r2712 | QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), | ||
QStringLiteral("..."), | ||||
axis()->labelsAngle()); | ||||
|
r2534 | width = boundingRect.width() + labelPadding() + base.width() + 1.0; | ||
if (base.width() > 0.0) | ||||
|
r2412 | width += labelPadding(); | ||
sh = QSizeF(width, height); | ||||
|
r2133 | break; | ||
|
r2412 | } | ||
|
r2133 | case Qt::PreferredSize:{ | ||
|
r2534 | qreal labelWidth = 0.0; | ||
|
r2412 | foreach (const QString& s, ticksList) { | ||
|
r2539 | QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle()); | ||
|
r2412 | labelWidth = qMax(rect.width(), labelWidth); | ||
|
r2133 | } | ||
|
r2534 | width = labelWidth + labelPadding() + base.width() + 1.0; | ||
if (base.width() > 0.0) | ||||
|
r2412 | width += labelPadding(); | ||
sh = QSizeF(width, height); | ||||
|
r2133 | break; | ||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
|
r1965 | } | ||
|
r2306 | #include "moc_chartbarcategoryaxisy_p.cpp" | ||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||