chartbarcategoryaxisy.cpp
163 lines
| 5.1 KiB
| text/x-c
|
CppLexer
Marek Rosa
|
r1555 | /**************************************************************************** | ||
Michal Klocek
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
Marek Rosa
|
r1555 | |||
Marek Rosa
|
r1810 | #include "chartbarcategoryaxisy_p.h" | ||
Marek Rosa
|
r1555 | #include "chartpresenter_p.h" | ||
Marek Rosa
|
r1810 | #include "qbarcategoryaxis_p.h" | ||
Michal Klocek
|
r1720 | #include <qmath.h> | ||
Michal Klocek
|
r1837 | #include <QDebug> | ||
Marek Rosa
|
r1555 | |||
static int label_padding = 5; | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Marek Rosa
|
r1808 | ChartCategoriesAxisY::ChartCategoriesAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), | ||
Michal Klocek
|
r1716 | m_categoriesAxis(axis) | ||
Marek Rosa
|
r1555 | { | ||
} | ||||
ChartCategoriesAxisY::~ChartCategoriesAxisY() | ||||
{ | ||||
} | ||||
QVector<qreal> ChartCategoriesAxisY::calculateLayout() const | ||||
{ | ||||
Michal Klocek
|
r1725 | int count = m_categoriesAxis->d_ptr->count(); | ||
Q_ASSERT(count>=1); | ||||
Marek Rosa
|
r1555 | |||
QVector<qreal> points; | ||||
Michal Klocek
|
r1725 | points.resize(count+2); | ||
Marek Rosa
|
r1555 | |||
Michal Klocek
|
r1725 | const qreal delta = m_rect.height()/(count); | ||
Michal Klocek
|
r1720 | qreal offset = - m_min - 0.5; | ||
Michal Klocek
|
r1716 | |||
Michal Klocek
|
r1837 | if(delta<1) return points; | ||
Michal Klocek
|
r1720 | if(offset<=0) { | ||
offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta) + delta; | ||||
Michal Klocek
|
r1716 | } | ||
else { | ||||
Michal Klocek
|
r1720 | offset = int(offset * m_rect.height()/(m_max - m_min))%int(delta); | ||
Michal Klocek
|
r1716 | } | ||
Michal Klocek
|
r1720 | points[0] = m_rect.bottom(); | ||
Michal Klocek
|
r1725 | points[count+1] = m_rect.top(); | ||
Michal Klocek
|
r1716 | |||
Michal Klocek
|
r1725 | for (int i = 0; i < count; ++i) { | ||
Michal Klocek
|
r1720 | int y = m_rect.bottom() - i * delta - offset; | ||
points[i+1] = y; | ||||
Marek Rosa
|
r1555 | } | ||
return points; | ||||
} | ||||
Michal Klocek
|
r1720 | QStringList ChartCategoriesAxisY::createCategoryLabels(const QVector<qreal>& layout) const | ||
{ | ||||
QStringList result; | ||||
qreal d = (m_max - m_min)/m_rect.height(); | ||||
for (int i = 0;i < layout.count()-1; ++i) { | ||||
qreal x = qFloor(((m_rect.height()- (layout[i+1] + layout[i])/2 + m_rect.top())*d + m_min+0.5)); | ||||
if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { | ||||
result << m_categoriesAxis->categories().at(x); | ||||
} | ||||
else { | ||||
// No label for x coordinate | ||||
result << ""; | ||||
} | ||||
} | ||||
result << ""; | ||||
return result; | ||||
} | ||||
Marek Rosa
|
r1555 | void ChartCategoriesAxisY::updateGeometry() | ||
{ | ||||
const QVector<qreal>& layout = ChartAxis::layout(); | ||||
m_minWidth = 0; | ||||
m_minHeight = 0; | ||||
if(layout.isEmpty()) return; | ||||
Michal Klocek
|
r1720 | QStringList ticksList = createCategoryLabels(layout); | ||
Marek Rosa
|
r1555 | |||
QList<QGraphicsItem *> lines = m_grid->childItems(); | ||||
QList<QGraphicsItem *> labels = m_labels->childItems(); | ||||
QList<QGraphicsItem *> shades = m_shades->childItems(); | ||||
Michal Klocek
|
r1729 | QList<QGraphicsItem *> axis = m_arrow->childItems(); | ||
Marek Rosa
|
r1555 | |||
Michal Klocek
|
r1716 | Q_ASSERT(labels.size() == ticksList.size()); | ||
Q_ASSERT(layout.size() == ticksList.size()); | ||||
Michal Klocek
|
r1725 | const qreal delta = m_rect.height()/(m_categoriesAxis->d_ptr->count()); | ||
Marek Rosa
|
r1555 | |||
QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | ||||
Michal Klocek
|
r1655 | lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); | ||
Marek Rosa
|
r1555 | |||
Michal Klocek
|
r1716 | qreal height = m_rect.bottom(); | ||
Michal Klocek
|
r1640 | for (int i = 0; i < layout.size(); ++i) { | ||
Marek Rosa
|
r1555 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | ||
sauimone
|
r1682 | lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); | ||
Marek Rosa
|
r1555 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | ||
Michal Klocek
|
r1716 | labelItem->setText(ticksList.at(i)); | ||
Michal Klocek
|
r1577 | const QRectF& rect = labelItem->boundingRect(); | ||
QPointF center = rect.center(); | ||||
labelItem->setTransformOriginPoint(center.x(), center.y()); | ||||
Michal Klocek
|
r1716 | |||
Michal Klocek
|
r1720 | if(i==0) { | ||
labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y()); | ||||
Michal Klocek
|
r1716 | } | ||
else { | ||||
Michal Klocek
|
r1720 | labelItem->setPos(m_rect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y()); | ||
Michal Klocek
|
r1640 | } | ||
Michal Klocek
|
r1716 | if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < m_rect.top()) { | ||
labelItem->setVisible(false); | ||||
} | ||||
else { | ||||
labelItem->setVisible(true); | ||||
height=labelItem->pos().y(); | ||||
} | ||||
Michal Klocek
|
r1837 | m_minWidth=qMax(rect.width()+label_padding,m_minWidth); | ||
m_minHeight+=rect.height(); | ||||
Marek Rosa
|
r1555 | |||
if ((i+1)%2 && i>1) { | ||||
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | ||||
Michal Klocek
|
r1655 | rectItem->setRect(m_rect.left(),layout[i],m_rect.width(),layout[i-1]-layout[i]); | ||
Marek Rosa
|
r1555 | } | ||
lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | ||||
Michal Klocek
|
r1655 | lineItem->setLine(m_rect.left()-5,layout[i],m_rect.left(),layout[i]); | ||
Marek Rosa
|
r1555 | } | ||
} | ||||
Michal Klocek
|
r1643 | void ChartCategoriesAxisY::handleAxisUpdated() | ||
{ | ||||
if(m_categoriesAxis->categories()!=m_categories) | ||||
{ | ||||
m_categories=m_categoriesAxis->categories(); | ||||
Michal Klocek
|
r1725 | if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) { | ||
Michal Klocek
|
r1720 | updateGeometry(); | ||
} | ||||
Michal Klocek
|
r1643 | } | ||
ChartAxis::handleAxisUpdated(); | ||||
} | ||||
Marek Rosa
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||