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