chartbarcategoryaxisy.cpp
140 lines
| 4.3 KiB
| text/x-c
|
CppLexer
Marek Rosa
|
r1555 | /**************************************************************************** | ||
Michal Klocek
|
r1716 | ** | ||
Miikka Heikkinen
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
Michal Klocek
|
r1716 | ** All rights reserved. | ||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Marek Rosa
|
r1555 | |||
Marek Rosa
|
r1810 | #include "chartbarcategoryaxisy_p.h" | ||
Marek Rosa
|
r1555 | #include "chartpresenter_p.h" | ||
Marek Rosa
|
r1810 | #include "qbarcategoryaxis_p.h" | ||
Miikka Heikkinen
|
r2483 | #include "abstractchartlayout_p.h" | ||
Michal Klocek
|
r1720 | #include <qmath.h> | ||
Michal Klocek
|
r2133 | #include <QDebug> | ||
Marek Rosa
|
r1555 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
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; | ||
qreal offset = (ceil(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 | ||
result << ""; | ||||
} | ||||
Michal Klocek
|
r1720 | } | ||
result << ""; | ||||
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: { | ||||
Miikka Heikkinen
|
r2539 | QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", 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" | ||
Marek Rosa
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||