chartbarcategoryaxisy.cpp
144 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 | { | ||
QVector<qreal> points; | ||||
|
r2145 | const QRectF& gridRect = gridGeometry(); | ||
qreal range = max() - min(); | ||||
const qreal delta = gridRect.height()/range; | ||||
|
r1555 | |||
|
r2145 | if(delta<2) return points; | ||
|
r2111 | |||
qreal offset = - min() - 0.5; | ||||
|
r2145 | offset = int(offset * delta)%int(delta); | ||
|
r1716 | |||
|
r2145 | int count = qFloor(range); | ||
if(count < 1 ) return points; | ||||
|
r1837 | |||
|
r2145 | points.resize(count+2); | ||
|
r1716 | |||
|
r2145 | for (int i = 0; i < count+2; ++i) { | ||
points[i] = gridRect.bottom() - i * delta -offset; | ||||
|
r1555 | } | ||
|
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 | { | ||
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(); | ||||
|
r2146 | if(base.width()>0) width+=labelPadding(); | ||
|
r2133 | 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(); | ||||
|
r2146 | width=qMax(rect.width()+labelPadding(),width); //one pixel torelance | ||
|
r2133 | } | ||
height=qMax(height,base.height()); | ||||
width+=base.width(); | ||||
|
r2146 | if(base.width()>0) width+=labelPadding(); | ||
|
r2133 | sh = QSizeF(width,height); | ||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
|
r1965 | } | ||
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||