chartcategoryaxisy.cpp
120 lines
| 3.4 KiB
| text/x-c
|
CppLexer
|
r1701 | /**************************************************************************** | ||
** | ||||
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
|
r1701 | ** 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 | { | ||
|
r2407 | updateGeometry(); | ||
//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) { | ||||
|
r2412 | case Qt::MinimumSize: { | ||
QRectF boundingRect = labelBoundingRect(fn, "..."); | ||||
width = boundingRect.width() + labelPadding(); | ||||
|
r2136 | width += base.width(); | ||
|
r2412 | height = qMax(boundingRect.height(), base.height()); | ||
|
r2131 | sh = QSizeF(width, height); | ||
break; | ||||
|
r2412 | } | ||
|
r2131 | case Qt::PreferredSize: { | ||
|
r2412 | int labelWidth = 0; | ||
foreach (const QString& s, ticksList) { | ||||
QRect rect = labelBoundingRect(fn, s); | ||||
labelWidth = qMax(rect.width(), labelWidth); | ||||
|
r2131 | height += rect.height(); | ||
|
r1965 | } | ||
|
r2412 | width = labelWidth + labelPadding() + 1; | ||
|
r2131 | width += base.width(); | ||
|
r2412 | height = qMax(height, base.height()); | ||
|
r2131 | sh = QSizeF(width, height); | ||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
|
r1965 | } | ||
|
r1701 | QTCOMMERCIALCHART_END_NAMESPACE | ||