chartcategoryaxisx.cpp
121 lines
| 3.4 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 "chartcategoryaxisx_p.h" | ||
#include "qcategoryaxis.h" | ||||
|
r1701 | #include "qabstractaxis.h" | ||
#include "chartpresenter_p.h" | ||||
#include <QGraphicsLayout> | ||||
#include <QFontMetrics> | ||||
|
r1735 | #include <qmath.h> | ||
|
r1701 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r2273 | ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis, QGraphicsItem* item) | ||
: HorizontalAxis(axis, item, true), | ||||
|
r2131 | m_axis(axis) | ||
|
r1701 | { | ||
} | ||||
|
r1817 | ChartCategoryAxisX::~ChartCategoryAxisX() | ||
|
r1701 | { | ||
} | ||||
|
r1817 | QVector<qreal> ChartCategoryAxisX::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.width() / range; | ||
|
r2131 | for (int i = 0; i < tickCount; ++i) { | ||
|
r1846 | if (i < tickCount - 1) { | ||
|
r2136 | int x = (m_axis->startValue(m_axis->categoriesLabels().at(i)) - min()) * scale + gridRect.left(); | ||
|
r1846 | points[i] = x; | ||
} else { | ||||
|
r2136 | int x = (m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - min()) * scale + gridRect.left(); | ||
|
r1846 | points[i] = x; | ||
} | ||||
|
r2131 | } | ||
|
r1846 | } | ||
|
r2111 | |||
|
r1701 | return points; | ||
} | ||||
|
r1817 | void ChartCategoryAxisX::updateGeometry() | ||
|
r2407 | { | ||
//TODO: this is not optimal when many categories :( , create only visible lables | ||||
|
r2131 | setLabels(m_axis->categoriesLabels() << ""); | ||
|
r2111 | HorizontalAxis::updateGeometry(); | ||
|
r1701 | } | ||
|
r1817 | void ChartCategoryAxisX::handleAxisUpdated() | ||
|
r1777 | { | ||
updateGeometry(); | ||||
|
r2273 | //TODO :: ChartAxis::handleAxisUpdated(); | ||
|
r1777 | } | ||
|
r2131 | QSizeF ChartCategoryAxisX::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); | ||
|
r2136 | QStringList ticksList = m_axis->categoriesLabels(); | ||
|
r2131 | qreal width = 0; | ||
qreal height = 0; | ||||
|
r1965 | |||
switch (which) { | ||||
|
r2412 | case Qt::MinimumSize: { | ||
QRectF boundingRect = labelBoundingRect(fn, "..."); | ||||
width = qMax(boundingRect.width(), base.width()); | ||||
height = boundingRect.height() + labelPadding(); | ||||
|
r2131 | height += base.height(); | ||
sh = QSizeF(width, height); | ||||
|
r1965 | break; | ||
|
r2412 | } | ||
|
r2131 | case Qt::PreferredSize: { | ||
|
r2412 | int labelHeight = 0; | ||
foreach (const QString& s, ticksList) { | ||||
QRect rect = labelBoundingRect(fn, s); | ||||
labelHeight = qMax(rect.height(), labelHeight); | ||||
|
r2131 | width += rect.width(); | ||
|
r1965 | } | ||
|
r2412 | height = labelHeight + labelPadding(); | ||
|
r2131 | height += base.height(); | ||
|
r2412 | width = qMax(width, base.width()); | ||
|
r2131 | sh = QSizeF(width, height); | ||
break; | ||||
} | ||||
default: | ||||
|
r1965 | break; | ||
} | ||||
return sh; | ||||
} | ||||
|
r1701 | QTCOMMERCIALCHART_END_NAMESPACE | ||