chartbarcategoryaxisy.cpp
202 lines
| 6.3 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
|
r1965 | #include <QFontMetrics> | ||
Michal Klocek
|
r1837 | #include <QDebug> | ||
Marek Rosa
|
r1555 | |||
static int label_padding = 5; | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Marek Rosa
|
r1918 | ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), | ||
Michal Klocek
|
r1716 | m_categoriesAxis(axis) | ||
Marek Rosa
|
r1555 | { | ||
} | ||||
Marek Rosa
|
r1918 | ChartBarCategoryAxisY::~ChartBarCategoryAxisY() | ||
Marek Rosa
|
r1555 | { | ||
} | ||||
Marek Rosa
|
r1918 | QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const | ||
Marek Rosa
|
r1555 | { | ||
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
|
r1965 | QRectF rect = presenter()->chartsGeometry(); | ||
const qreal delta = 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) { | ||
Michal Klocek
|
r1965 | offset = int(offset * rect.height()/(m_max - m_min))%int(delta) + delta; | ||
Michal Klocek
|
r1716 | } | ||
else { | ||||
Michal Klocek
|
r1965 | offset = int(offset * rect.height()/(m_max - m_min))%int(delta); | ||
Michal Klocek
|
r1716 | } | ||
Michal Klocek
|
r1965 | points[0] = rect.bottom(); | ||
points[count+1] = rect.top(); | ||||
Michal Klocek
|
r1716 | |||
Michal Klocek
|
r1725 | for (int i = 0; i < count; ++i) { | ||
Michal Klocek
|
r1965 | int y = rect.bottom() - i * delta - offset; | ||
Michal Klocek
|
r1720 | points[i+1] = y; | ||
Marek Rosa
|
r1555 | } | ||
return points; | ||||
} | ||||
Marek Rosa
|
r1918 | QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const | ||
Michal Klocek
|
r1720 | { | ||
QStringList result; | ||||
Michal Klocek
|
r1965 | QRectF rect = presenter()->chartsGeometry(); | ||
qreal d = (m_max - m_min)/rect.height(); | ||||
Michal Klocek
|
r1720 | for (int i = 0;i < layout.count()-1; ++i) { | ||
Michal Klocek
|
r1965 | qreal x = qFloor(((rect.height()- (layout[i+1] + layout[i])/2 + rect.top())*d + m_min+0.5)); | ||
Michal Klocek
|
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; | ||||
} | ||||
Marek Rosa
|
r1918 | void ChartBarCategoryAxisY::updateGeometry() | ||
Marek Rosa
|
r1555 | { | ||
const QVector<qreal>& layout = ChartAxis::layout(); | ||||
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
|
r1965 | QRectF chartRect = presenter()->chartsGeometry(); | ||
const qreal delta = chartRect.height()/(m_categoriesAxis->d_ptr->count()); | ||||
Marek Rosa
|
r1555 | |||
QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0)); | ||||
Michal Klocek
|
r1965 | lineItem->setLine(chartRect.left() , chartRect.top(), chartRect.left(), chartRect.bottom()); | ||
Marek Rosa
|
r1555 | |||
Michal Klocek
|
r1965 | qreal height = chartRect.bottom(); | ||
Michal Klocek
|
r1640 | for (int i = 0; i < layout.size(); ++i) { | ||
Marek Rosa
|
r1555 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | ||
Michal Klocek
|
r1965 | lineItem->setLine(chartRect.left() , layout[i], chartRect.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) { | ||
Michal Klocek
|
r1965 | labelItem->setPos(chartRect.left() - rect.width() - label_padding ,layout[i+1] + (delta)/2 - center.y()); | ||
Michal Klocek
|
r1716 | } | ||
else { | ||||
Michal Klocek
|
r1965 | labelItem->setPos(chartRect.left() - rect.width() - label_padding ,layout[i] - (delta)/2 - center.y()); | ||
Michal Klocek
|
r1640 | } | ||
Michal Klocek
|
r1965 | if(labelItem->pos().y()+rect.height()>= height || labelItem->pos().y() < chartRect.top()) { | ||
Michal Klocek
|
r1716 | labelItem->setVisible(false); | ||
} | ||||
else { | ||||
labelItem->setVisible(true); | ||||
height=labelItem->pos().y(); | ||||
} | ||||
Marek Rosa
|
r1555 | if ((i+1)%2 && i>1) { | ||
QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1)); | ||||
Michal Klocek
|
r1965 | rectItem->setRect(chartRect.left(),layout[i],chartRect.width(),layout[i-1]-layout[i]); | ||
Marek Rosa
|
r1555 | } | ||
lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1)); | ||||
Michal Klocek
|
r1965 | lineItem->setLine(chartRect.left()-5,layout[i],chartRect.left(),layout[i]); | ||
Marek Rosa
|
r1555 | } | ||
} | ||||
Marek Rosa
|
r1918 | void ChartBarCategoryAxisY::handleAxisUpdated() | ||
Michal Klocek
|
r1643 | { | ||
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(); | ||||
} | ||||
Michal Klocek
|
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; | ||||
} | ||||
Marek Rosa
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||