chartbarcategoryaxisx.cpp
200 lines
| 6.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 "chartbarcategoryaxisx_p.h" | ||
|
r1555 | #include "chartpresenter_p.h" | ||
|
r1810 | #include "qbarcategoryaxis_p.h" | ||
|
r1965 | #include <QFontMetrics> | ||
|
r1720 | #include <QDebug> | ||
#include <qmath.h> | ||||
|
r1555 | |||
static int label_padding = 5; | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r1918 | ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), | ||
|
r1716 | m_categoriesAxis(axis) | ||
|
r1555 | { | ||
} | ||||
|
r1918 | ChartBarCategoryAxisX::~ChartBarCategoryAxisX() | ||
|
r1570 | { | ||
} | ||||
|
r1918 | QVector<qreal> ChartBarCategoryAxisX::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.width()/(count); | ||||
|
r1720 | qreal offset =-m_min-0.5; | ||
|
r1837 | if(delta<1) return points; | ||
|
r1720 | if(offset<=0) { | ||
|
r1965 | offset = int(offset * rect.width()/(m_max - m_min))%int(delta) + delta; | ||
|
r1716 | } | ||
else { | ||||
|
r1965 | offset = int(offset * rect.width()/(m_max - m_min))%int(delta); | ||
|
r1716 | } | ||
|
r1965 | points[0] = rect.left(); | ||
points[count+1] = rect.right(); | ||||
|
r1716 | |||
|
r1725 | for (int i = 0; i < count; ++i) { | ||
|
r1965 | qreal x = offset + i * delta + rect.left(); | ||
|
r1720 | points[i+1] = x; | ||
|
r1555 | } | ||
return points; | ||||
} | ||||
|
r1918 | QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const | ||
|
r1720 | { | ||
QStringList result; | ||||
|
r1965 | QRectF rect = presenter()->chartsGeometry(); | ||
qreal d = (m_max - m_min)/rect.width(); | ||||
|
r1720 | for (int i = 0;i < layout.count()-1; ++i) { | ||
|
r1965 | qreal x = qFloor((((layout[i+1] + layout[i])/2-rect.left())*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 ChartBarCategoryAxisX::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.width()/(m_categoriesAxis->d_ptr->count()); | ||||
|
r1555 | |||
QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | ||||
|
r1965 | lineItem->setLine( chartRect.left(), chartRect.bottom(), chartRect.right(), chartRect.bottom()); | ||
|
r1555 | |||
|
r1965 | qreal width = chartRect.left(); | ||
|
r1640 | for (int i = 0; i < layout.size(); ++i) { | ||
|
r1555 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | ||
|
r1965 | lineItem->setLine(layout[i], chartRect.top(), layout[i], chartRect.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){ | ||
|
r1965 | labelItem->setPos(layout[i+1] - (delta)/2 - center.x(), chartRect.bottom() + label_padding); | ||
|
r1640 | }else{ | ||
|
r1965 | labelItem->setPos(layout[i] + (delta)/2 - center.x(), chartRect.bottom() + label_padding); | ||
|
r1716 | } | ||
|
r1965 | if(labelItem->pos().x()<=width || labelItem->pos().x()+ rect.width()> chartRect.right()) { | ||
|
r1640 | labelItem->setVisible(false); | ||
} | ||||
|
r1716 | else { | ||
labelItem->setVisible(true); | ||||
width=rect.width()+labelItem->pos().x(); | ||||
} | ||||
|
r1555 | if ((i+1)%2 && i>1) { | ||
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | ||||
|
r1965 | rectItem->setRect(layout[i-1], chartRect.top(),layout[i]-layout[i-1], chartRect.height()); | ||
|
r1555 | } | ||
lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | ||||
|
r1965 | lineItem->setLine(layout[i], chartRect.bottom(),layout[i], chartRect.bottom()+5); | ||
|
r1555 | } | ||
} | ||||
|
r1918 | void ChartBarCategoryAxisX::handleAxisUpdated() | ||
|
r1643 | { | ||
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(); | ||||
} | ||||
|
r1965 | QSizeF ChartBarCategoryAxisX::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(); | ||||
height = fn.height()+label_padding; | ||||
width=qMax(width,base.width()); | ||||
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)); | ||||
width+=rect.width(); | ||||
height=qMax(rect.height()+label_padding,height); | ||||
} | ||||
width=qMax(width,base.width()); | ||||
height+=base.height(); | ||||
sh = QSizeF(width,height); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
} | ||||
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||