chartbarcategoryaxisx.cpp
159 lines
| 5.0 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 "chartbarcategoryaxisx_p.h" | ||
|
r1555 | #include "chartpresenter_p.h" | ||
|
r1810 | #include "qbarcategoryaxis_p.h" | ||
|
r1720 | #include <QDebug> | ||
#include <qmath.h> | ||||
|
r1555 | |||
static int label_padding = 5; | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r1808 | ChartCategoriesAxisX::ChartCategoriesAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), | ||
|
r1716 | m_categoriesAxis(axis) | ||
|
r1555 | { | ||
} | ||||
|
r1577 | ChartCategoriesAxisX::~ChartCategoriesAxisX() | ||
|
r1570 | { | ||
} | ||||
|
r1555 | QVector<qreal> ChartCategoriesAxisX::calculateLayout() const | ||
{ | ||||
|
r1725 | int count = m_categoriesAxis->d_ptr->count(); | ||
Q_ASSERT(count>=1); | ||||
|
r1555 | |||
QVector<qreal> points; | ||||
|
r1725 | points.resize(count+2); | ||
|
r1555 | |||
|
r1725 | const qreal delta = m_rect.width()/(count); | ||
|
r1720 | qreal offset =-m_min-0.5; | ||
if(offset<=0) { | ||||
offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta) + delta; | ||||
|
r1716 | } | ||
else { | ||||
|
r1720 | offset = int(offset * m_rect.width()/(m_max - m_min))%int(delta); | ||
|
r1716 | } | ||
|
r1720 | points[0] = m_rect.left(); | ||
|
r1725 | points[count+1] = m_rect.right(); | ||
|
r1716 | |||
|
r1725 | for (int i = 0; i < count; ++i) { | ||
|
r1720 | qreal x = offset + i * delta + m_rect.left(); | ||
points[i+1] = x; | ||||
|
r1555 | } | ||
return points; | ||||
} | ||||
|
r1720 | QStringList ChartCategoriesAxisX::createCategoryLabels(const QVector<qreal>& layout) const | ||
{ | ||||
QStringList result; | ||||
qreal d = (m_max - m_min)/m_rect.width(); | ||||
for (int i = 0;i < layout.count()-1; ++i) { | ||||
qreal x = qFloor((((layout[i+1] + layout[i])/2-m_rect.left())*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; | ||||
} | ||||
|
r1555 | void ChartCategoriesAxisX::updateGeometry() | ||
{ | ||||
const QVector<qreal>& layout = ChartAxis::layout(); | ||||
m_minWidth = 0; | ||||
m_minHeight = 0; | ||||
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()); | ||||
|
r1725 | const qreal delta = m_rect.width()/(m_categoriesAxis->d_ptr->count()); | ||
|
r1555 | |||
QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | ||||
lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); | ||||
|
r1716 | qreal width = m_rect.left(); | ||
|
r1640 | for (int i = 0; i < layout.size(); ++i) { | ||
|
r1555 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | ||
lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); | ||||
|
r1640 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | ||
|
r1716 | labelItem->setText(ticksList.at(i)); | ||
|
r1570 | const QRectF& rect = labelItem->boundingRect(); | ||
QPointF center = rect.center(); | ||||
labelItem->setTransformOriginPoint(center.x(), center.y()); | ||||
|
r1716 | |||
|
r1720 | if(i==0){ | ||
labelItem->setPos(layout[i+1] - (delta)/2 - center.x(), m_rect.bottom() + label_padding); | ||||
|
r1640 | }else{ | ||
|
r1720 | labelItem->setPos(layout[i] + (delta)/2 - center.x(), m_rect.bottom() + label_padding); | ||
|
r1716 | } | ||
if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()>m_rect.right()) { | ||||
|
r1640 | labelItem->setVisible(false); | ||
} | ||||
|
r1716 | else { | ||
labelItem->setVisible(true); | ||||
width=rect.width()+labelItem->pos().x(); | ||||
} | ||||
m_minWidth+=rect.width(); | ||||
m_minHeight=qMax(rect.height()+label_padding,m_minHeight); | ||||
|
r1555 | |||
if ((i+1)%2 && i>1) { | ||||
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | ||||
rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); | ||||
} | ||||
lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | ||||
lineItem->setLine(layout[i],m_rect.bottom(),layout[i],m_rect.bottom()+5); | ||||
} | ||||
} | ||||
|
r1643 | void ChartCategoriesAxisX::handleAxisUpdated() | ||
{ | ||||
if(m_categoriesAxis->categories()!=m_categories) | ||||
{ | ||||
m_categories=m_categoriesAxis->categories(); | ||||
|
r1725 | if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) updateGeometry(); | ||
|
r1643 | } | ||
ChartAxis::handleAxisUpdated(); | ||||
} | ||||
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||