chartcategoryaxisy.cpp
118 lines
| 3.3 KiB
| text/x-c
|
CppLexer
|
r1701 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r1817 | #include "chartcategoryaxisy_p.h" | ||
#include "qcategoryaxis.h" | ||||
|
r1701 | #include "qabstractaxis.h" | ||
#include "chartpresenter_p.h" | ||||
#include <QGraphicsLayout> | ||||
#include <QFontMetrics> | ||||
|
r1735 | #include <qmath.h> | ||
|
r2136 | #include <QDebug> | ||
|
r1735 | |||
|
r1701 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r2273 | ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, QGraphicsItem* item) | ||
: VerticalAxis(axis, item, true), | ||||
|
r2131 | m_axis(axis) | ||
|
r1701 | { | ||
} | ||||
|
r1817 | ChartCategoryAxisY::~ChartCategoryAxisY() | ||
|
r1701 | { | ||
} | ||||
|
r1817 | QVector<qreal> ChartCategoryAxisY::calculateLayout() const | ||
|
r1701 | { | ||
|
r2111 | int tickCount = m_axis->categoriesLabels().count() + 1; | ||
|
r1701 | QVector<qreal> points; | ||
|
r1708 | |||
if (tickCount < 2) | ||||
return points; | ||||
|
r2131 | const QRectF &gridRect = gridGeometry(); | ||
|
r2136 | qreal range = max() - min(); | ||
|
r1846 | if (range > 0) { | ||
points.resize(tickCount); | ||||
|
r2111 | qreal scale = gridRect.height() / range; | ||
|
r2131 | for (int i = 0; i < tickCount; ++i) { | ||
|
r1846 | if (i < tickCount - 1) { | ||
|
r2136 | int y = -(m_axis->startValue(m_axis->categoriesLabels().at(i)) - min()) * scale + gridRect.bottom(); | ||
|
r1846 | points[i] = y; | ||
} else { | ||||
|
r2136 | int y = -(m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - min()) * scale + gridRect.bottom(); | ||
|
r1846 | points[i] = y; | ||
} | ||||
|
r2131 | } | ||
|
r1846 | } | ||
|
r1701 | |||
return points; | ||||
} | ||||
|
r1817 | void ChartCategoryAxisY::updateGeometry() | ||
|
r1701 | { | ||
|
r2131 | setLabels(m_axis->categoriesLabels() << ""); | ||
|
r2111 | VerticalAxis::updateGeometry(); | ||
|
r1701 | } | ||
|
r1817 | void ChartCategoryAxisY::handleAxisUpdated() | ||
|
r1777 | { | ||
updateGeometry(); | ||||
|
r2273 | //TODO:: ChartAxis::handleAxisUpdated(); | ||
|
r1777 | } | ||
|
r2131 | QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
|
r1965 | { | ||
Q_UNUSED(constraint) | ||||
|
r2111 | QFontMetrics fn(font()); | ||
|
r1965 | QSizeF sh; | ||
|
r2138 | QSizeF base = VerticalAxis::sizeHint(which, constraint); | ||
|
r2136 | QStringList ticksList = m_axis->categoriesLabels(); | ||
|
r2131 | qreal width = 0; | ||
qreal height = 0; | ||||
switch (which) { | ||||
case Qt::MinimumSize: | ||||
width = fn.boundingRect("...").width() + labelPadding(); | ||||
height = fn.height(); | ||||
|
r2136 | width += base.width(); | ||
height = qMax(height, base.height());; | ||||
|
r2131 | sh = QSizeF(width, height); | ||
break; | ||||
case Qt::PreferredSize: { | ||||
for (int i = 0; i < ticksList.size(); ++i) { | ||||
QRectF rect = fn.boundingRect(ticksList.at(i)); | ||||
|
r2136 | width = qMax(rect.width() + labelPadding() + 1, width); | ||
|
r2131 | height += rect.height(); | ||
|
r1965 | } | ||
|
r2131 | height = qMax(height, base.height()); | ||
width += base.width(); | ||||
sh = QSizeF(width, height); | ||||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
|
r1965 | } | ||
|
r1701 | QTCOMMERCIALCHART_END_NAMESPACE | ||