chartbarcategoryaxisy.cpp
202 lines
| 6.3 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> | ||
|
r1837 | #include <QDebug> | ||
|
r1555 | |||
static int label_padding = 5; | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r1918 | ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), | ||
|
r1716 | m_categoriesAxis(axis) | ||
|
r1555 | { | ||
} | ||||
|
r1918 | ChartBarCategoryAxisY::~ChartBarCategoryAxisY() | ||
|
r1555 | { | ||
} | ||||
|
r1918 | QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const | ||
|
r1555 | { | ||
|
r1725 | int count = m_categoriesAxis->d_ptr->count(); | ||
Q_ASSERT(count>=1); | ||||
|
r1555 | |||
QVector<qreal> points; | ||||
|
r1725 | points.resize(count+2); | ||
|
r1555 | |||
|
r1965 | QRectF rect = presenter()->chartsGeometry(); | ||
const qreal delta = rect.height()/(count); | ||||
|
r1720 | qreal offset = - m_min - 0.5; | ||
|
r1716 | |||
|
r1837 | if(delta<1) return points; | ||
|
r1720 | if(offset<=0) { | ||
|
r1965 | offset = int(offset * rect.height()/(m_max - m_min))%int(delta) + delta; | ||
|
r1716 | } | ||
else { | ||||
|
r1965 | offset = int(offset * rect.height()/(m_max - m_min))%int(delta); | ||
|
r1716 | } | ||
|
r1965 | points[0] = rect.bottom(); | ||
points[count+1] = rect.top(); | ||||
|
r1716 | |||
|
r1725 | for (int i = 0; i < count; ++i) { | ||
|
r1965 | int y = rect.bottom() - i * delta - offset; | ||
|
r1720 | points[i+1] = y; | ||
|
r1555 | } | ||
return points; | ||||
} | ||||
|
r1918 | QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const | ||
|
r1720 | { | ||
QStringList result; | ||||
|
r1965 | QRectF rect = presenter()->chartsGeometry(); | ||
qreal d = (m_max - m_min)/rect.height(); | ||||
|
r1720 | for (int i = 0;i < layout.count()-1; ++i) { | ||
|
r1965 | qreal x = qFloor(((rect.height()- (layout[i+1] + layout[i])/2 + rect.top())*d + m_min+0.5)); | ||
|
r1720 | if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { | ||
result << m_categoriesAxis->categories().at(x); | ||||
} | ||||
else { | ||||
// No label for x coordinate | ||||
result << ""; | ||||
} | ||||
} | ||||
result << ""; | ||||
return result; | ||||
} | ||||
|
r1918 | void ChartBarCategoryAxisY::updateGeometry() | ||
|
r1555 | { | ||
const QVector<qreal>& layout = ChartAxis::layout(); | ||||
if(layout.isEmpty()) return; | ||||
|
r1720 | QStringList ticksList = createCategoryLabels(layout); | ||
|
r1555 | |||
QList<QGraphicsItem *> lines = m_grid->childItems(); | ||||
QList<QGraphicsItem *> labels = m_labels->childItems(); | ||||
QList<QGraphicsItem *> shades = m_shades->childItems(); | ||||
|
r1729 | QList<QGraphicsItem *> axis = m_arrow->childItems(); | ||
|
r1555 | |||
|
r1716 | Q_ASSERT(labels.size() == ticksList.size()); | ||
Q_ASSERT(layout.size() == ticksList.size()); | ||||
|
r1965 | QRectF chartRect = presenter()->chartsGeometry(); | ||
const qreal delta = chartRect.height()/(m_categoriesAxis->d_ptr->count()); | ||||
|
r1555 | |||
QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | ||||
|
r1965 | lineItem->setLine(chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom()); | ||
|
r1555 | |||
|
r1965 | qreal height = chartRect.bottom(); | ||
|
r1640 | for (int i = 0; i < layout.size(); ++i) { | ||
|
r1555 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | ||
|
r1965 | lineItem->setLine(chartRect.left() , layout[i], chartRect.right(), layout[i]); | ||
|
r1555 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | ||
|
r1716 | labelItem->setText(ticksList.at(i)); | ||
|
r1577 | const QRectF& rect = labelItem->boundingRect(); | ||
QPointF center = rect.center(); | ||||
labelItem->setTransformOriginPoint(center.x(), center.y()); | ||||
|
r1716 | |||
|
r1720 | if(i==0) { | ||
|
r1965 | labelItem->setPos(chartRect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y()); | ||
|
r1716 | } | ||
else { | ||||
|
r1965 | labelItem->setPos(chartRect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y()); | ||
|
r1640 | } | ||
|
r1965 | if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < chartRect.top()) { | ||
|
r1716 | labelItem->setVisible(false); | ||
} | ||||
else { | ||||
labelItem->setVisible(true); | ||||
height=labelItem->pos().y(); | ||||
} | ||||
|
r1555 | if ((i+1)%2 && i>1) { | ||
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | ||||
|
r1965 | rectItem->setRect(chartRect.left(),layout[i],chartRect.width(),layout[i-1]-layout[i]); | ||
|
r1555 | } | ||
lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | ||||
|
r1965 | lineItem->setLine(chartRect.left()-5,layout[i],chartRect.left(),layout[i]); | ||
|
r1555 | } | ||
} | ||||
|
r1918 | void ChartBarCategoryAxisY::handleAxisUpdated() | ||
|
r1643 | { | ||
if(m_categoriesAxis->categories()!=m_categories) | ||||
{ | ||||
m_categories=m_categoriesAxis->categories(); | ||||
|
r1725 | if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) { | ||
|
r1720 | updateGeometry(); | ||
} | ||||
|
r1643 | } | ||
ChartAxis::handleAxisUpdated(); | ||||
} | ||||
|
r1965 | QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const | ||
{ | ||||
Q_UNUSED(constraint) | ||||
QFontMetrics fn(m_font); | ||||
QSizeF sh; | ||||
QSizeF base = ChartAxis::sizeHint(which, constraint); | ||||
QStringList ticksList = createCategoryLabels(ChartAxis::layout()); | ||||
qreal width=0; | ||||
qreal height=0; | ||||
switch (which) { | ||||
case Qt::MinimumSize: | ||||
width = fn.boundingRect("...").width() + label_padding; | ||||
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(); | ||||
width=qMax(rect.width()+label_padding,width); | ||||
} | ||||
height=qMax(height,base.height()); | ||||
width+=base.width(); | ||||
sh = QSizeF(width,height); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
} | ||||
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||