chartbarcategoryaxisx.cpp
142 lines
| 4.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" | ||
|
r2306 | #include "chartlayout_p.h" | ||
|
r1965 | #include <QFontMetrics> | ||
|
r1720 | #include <QDebug> | ||
#include <qmath.h> | ||||
|
r1555 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2273 | ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, QGraphicsItem* item) | ||
: HorizontalAxis(axis, item, true), | ||||
|
r2131 | m_categoriesAxis(axis) | ||
|
r1555 | { | ||
|
r2306 | QObject::connect(m_categoriesAxis,SIGNAL(categoriesChanged()),this, SLOT(handleCategoriesChanged())); | ||
handleCategoriesChanged(); | ||||
|
r1555 | } | ||
|
r1918 | ChartBarCategoryAxisX::~ChartBarCategoryAxisX() | ||
|
r1570 | { | ||
} | ||||
|
r1918 | QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const | ||
|
r1555 | { | ||
|
r2133 | QVector<qreal> points; | ||
const QRectF& gridRect = gridGeometry(); | ||||
qreal range = max() - min(); | ||||
const qreal delta = gridRect.width()/range; | ||||
|
r1555 | |||
|
r2133 | if(delta<2) return points; | ||
qreal offset =-min()-0.5; | ||||
offset = int(offset * delta)%int(delta); | ||||
|
r2111 | |||
|
r2133 | int count = qFloor(range); | ||
if(count < 1 ) return points; | ||||
|
r1720 | |||
|
r2133 | points.resize(count+2); | ||
|
r1716 | |||
|
r2133 | for (int i = 0; i < count+2; ++i) { | ||
|
r2145 | points[i] = offset + i * delta + gridRect.left(); | ||
|
r1555 | } | ||
|
r2133 | |||
|
r1555 | return points; | ||
} | ||||
|
r1918 | QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const | ||
|
r1720 | { | ||
|
r2111 | QStringList result ; | ||
|
r2131 | const QRectF &gridRect = gridGeometry(); | ||
qreal d = (max() - min()) / gridRect.width(); | ||||
|
r2145 | |||
|
r2131 | for (int i = 0; i < layout.count() - 1; ++i) { | ||
qreal x = qFloor((((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5)); | ||||
|
r2105 | if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) { | ||
|
r1720 | result << m_categoriesAxis->categories().at(x); | ||
|
r2131 | } else { | ||
|
r2105 | // No label for x coordinate | ||
result << ""; | ||||
} | ||||
|
r1720 | } | ||
result << ""; | ||||
return result; | ||||
} | ||||
|
r1918 | void ChartBarCategoryAxisX::updateGeometry() | ||
|
r1555 | { | ||
const QVector<qreal>& layout = ChartAxis::layout(); | ||||
|
r2131 | if (layout.isEmpty()) | ||
return; | ||||
|
r2111 | setLabels(createCategoryLabels(layout)); | ||
HorizontalAxis::updateGeometry(); | ||||
|
r1555 | } | ||
|
r2306 | void ChartBarCategoryAxisX::handleCategoriesChanged() | ||
|
r1643 | { | ||
|
r2306 | if(presenter()) presenter()->layout()->invalidate(); | ||
|
r1643 | } | ||
|
r2131 | QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
|
r1965 | { | ||
Q_UNUSED(constraint) | ||||
|
r2111 | QFontMetrics fn(font()); | ||
|
r1965 | QSizeF sh; | ||
|
r2138 | QSizeF base = HorizontalAxis::sizeHint(which, constraint); | ||
|
r2306 | QStringList ticksList = m_categoriesAxis->categories(); | ||
|
r1965 | |||
|
r2133 | qreal width=0; | ||
qreal height=0; | ||||
switch (which) { | ||||
case Qt::MinimumSize: | ||||
width = fn.boundingRect("...").width(); | ||||
height = fn.height()+labelPadding(); | ||||
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 = fn.height()+labelPadding(); | ||||
width = qMax(width,base.width()); | ||||
height += base.height(); | ||||
sh = QSizeF(width,height); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
|
r1965 | } | ||
|
r2306 | #include "moc_chartbarcategoryaxisx_p.cpp" | ||
|
r1555 | QTCOMMERCIALCHART_END_NAMESPACE | ||