chartbarcategoryaxisy.cpp
146 lines
| 4.2 KiB
| text/x-c
|
CppLexer
|
r1555 | /**************************************************************************** | ||
|
r1716 | ** | ||
** Copyright (C) 2012 Digia Plc | ||||
** 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" | ||
|
r1720 | #include <qmath.h> | ||
|
r1965 | #include <QFontMetrics> | ||
|
r2133 | #include <QDebug> | ||
|
r1555 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2131 | ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter) | ||
: VerticalAxis(axis, presenter, true), | ||||
m_categoriesAxis(axis) | ||||
|
r1555 | { | ||
} | ||||
|
r1918 | ChartBarCategoryAxisY::~ChartBarCategoryAxisY() | ||
|
r1555 | { | ||
} | ||||
|
r1918 | QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const | ||
|
r1555 | { | ||
|
r1725 | int count = m_categoriesAxis->d_ptr->count(); | ||
|
r2131 | Q_ASSERT(count >= 1); | ||
|
r1555 | |||
QVector<qreal> points; | ||||
|
r2131 | points.resize(count + 2); | ||
|
r1555 | |||
|
r2131 | const QRectF &gridRect = gridGeometry(); | ||
|
r2111 | |||
|
r2131 | const qreal delta = gridRect.height() / (count); | ||
|
r2111 | qreal offset = - min() - 0.5; | ||
|
r1716 | |||
|
r2131 | if (delta < 1) | ||
return points; | ||||
|
r1837 | |||
|
r2131 | if (offset < 0) | ||
offset = int(offset * gridRect.height() / (max() - min())) % int(delta) + delta; | ||||
else | ||||
offset = int(offset * gridRect.height() / (max() - min())) % int(delta); | ||||
|
r1716 | |||
|
r2131 | for (int i = -1; i < count + 1; ++i) { | ||
|
r2111 | int y = gridRect.bottom() - i * delta - offset; | ||
|
r2131 | points[i + 1] = y; | ||
|
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(); | ||||
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 | { | ||
const QVector<qreal>& layout = ChartAxis::layout(); | ||||
|
r2131 | if (layout.isEmpty()) | ||
return; | ||||
|
r2111 | setLabels(createCategoryLabels(layout)); | ||
VerticalAxis::updateGeometry(); | ||||
|
r1555 | } | ||
|
r1918 | void ChartBarCategoryAxisY::handleAxisUpdated() | ||
|
r1643 | { | ||
|
r2131 | if (m_categoriesAxis->categories() != m_categories) { | ||
m_categories = m_categoriesAxis->categories(); | ||||
if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2) | ||||
|
r1720 | updateGeometry(); | ||
|
r1643 | } | ||
ChartAxis::handleAxisUpdated(); | ||||
} | ||||
|
r2131 | QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
|
r1965 | { | ||
Q_UNUSED(constraint) | ||||
|
r2111 | QFontMetrics fn(font()); | ||
|
r1965 | QSizeF sh; | ||
|
r2138 | QSizeF base = VerticalAxis::sizeHint(which, constraint); | ||
|
r1965 | QStringList ticksList = createCategoryLabels(ChartAxis::layout()); | ||
|
r2133 | qreal width=0; | ||
qreal height=0; | ||||
switch (which) { | ||||
case Qt::MinimumSize: | ||||
width = fn.boundingRect("...").width() + labelPadding(); | ||||
height = fn.height(); | ||||
width+=base.width(); | ||||
height=qMax(height,base.height()); | ||||
sh = QSizeF(width,height); | ||||
break; | ||||
case Qt::PreferredSize:{ | ||||
for (int i = 0; i < ticksList.size(); ++i) | ||||
{ | ||||
QRectF rect = fn.boundingRect(ticksList.at(i)); | ||||
height+=rect.height(); | ||||
|
r2136 | width=qMax(rect.width()+labelPadding() + 1 ,width); //one pixel torelance | ||
|
r2133 | } | ||
height=qMax(height,base.height()); | ||||
width+=base.width(); | ||||
sh = QSizeF(width,height); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
|
r1965 | } | ||
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||