chartbarcategoryaxisy.cpp
144 lines
| 4.3 KiB
| text/x-c
|
CppLexer
|
r1555 | /**************************************************************************** | ||
|
r1716 | ** | ||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r1555 | |||
|
r1810 | #include "chartbarcategoryaxisy_p.h" | ||
|
r1555 | #include "chartpresenter_p.h" | ||
|
r1810 | #include "qbarcategoryaxis_p.h" | ||
|
r2483 | #include "abstractchartlayout_p.h" | ||
|
r1720 | #include <qmath.h> | ||
|
r1965 | #include <QFontMetrics> | ||
|
r2133 | #include <QDebug> | ||
|
r1555 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
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; | ||
qreal offset = (ceil(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 | ||
result << ""; | ||||
} | ||||
|
r1720 | } | ||
result << ""; | ||||
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) | ||||
|
r2483 | QFontMetrics fn(axis()->labelsFont()); | ||
|
r1965 | 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: { | ||||
QRectF boundingRect = labelBoundingRect(fn, "..."); | ||||
width = boundingRect.width() + labelPadding(); | ||||
width += base.width(); | ||||
if (base.width() > 0) | ||||
width += labelPadding(); | ||||
sh = QSizeF(width, height); | ||||
|
r2133 | break; | ||
|
r2412 | } | ||
|
r2133 | case Qt::PreferredSize:{ | ||
|
r2412 | int labelWidth = 0; | ||
foreach (const QString& s, ticksList) { | ||||
QRect rect = labelBoundingRect(fn, s); | ||||
labelWidth = qMax(rect.width(), labelWidth); | ||||
|
r2133 | } | ||
|
r2412 | width = labelWidth + labelPadding() + 1; | ||
width += base.width(); | ||||
if (base.width() > 0) | ||||
width += labelPadding(); | ||||
sh = QSizeF(width, height); | ||||
|
r2133 | break; | ||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
|
r1965 | } | ||
|
r2306 | #include "moc_chartbarcategoryaxisy_p.cpp" | ||
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||