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