chartcategoryaxisx.cpp
118 lines
| 3.6 KiB
| text/x-c
|
CppLexer
Marek Rosa
|
r1701 | /**************************************************************************** | ||
** | ||||
Miikka Heikkinen
|
r2432 | ** Copyright (C) 2013 Digia Plc | ||
Marek Rosa
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
Marek Rosa
|
r1817 | #include "chartcategoryaxisx_p.h" | ||
#include "qcategoryaxis.h" | ||||
Marek Rosa
|
r1701 | #include "qabstractaxis.h" | ||
#include "chartpresenter_p.h" | ||||
Miikka Heikkinen
|
r2483 | #include "abstractchartlayout_p.h" | ||
Marek Rosa
|
r1701 | #include <QGraphicsLayout> | ||
Michal Klocek
|
r1735 | #include <qmath.h> | ||
Marek Rosa
|
r1701 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r2273 | ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis, QGraphicsItem* item) | ||
: HorizontalAxis(axis, item, true), | ||||
Jani Honkonen
|
r2131 | m_axis(axis) | ||
Marek Rosa
|
r1701 | { | ||
Miikka Heikkinen
|
r2483 | QObject::connect(axis, SIGNAL(categoriesChanged()), this, SLOT(handleCategoriesChanged())); | ||
Marek Rosa
|
r1701 | } | ||
Marek Rosa
|
r1817 | ChartCategoryAxisX::~ChartCategoryAxisX() | ||
Marek Rosa
|
r1701 | { | ||
} | ||||
Marek Rosa
|
r1817 | QVector<qreal> ChartCategoryAxisX::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.width() / range; | ||
Jani Honkonen
|
r2131 | for (int i = 0; i < tickCount; ++i) { | ||
Marek Rosa
|
r1846 | if (i < tickCount - 1) { | ||
Miikka Heikkinen
|
r2436 | qreal x = (m_axis->startValue(m_axis->categoriesLabels().at(i)) - min()) * scale + gridRect.left(); | ||
Marek Rosa
|
r1846 | points[i] = x; | ||
} else { | ||||
Miikka Heikkinen
|
r2436 | qreal x = (m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - min()) * scale + gridRect.left(); | ||
Marek Rosa
|
r1846 | points[i] = x; | ||
} | ||||
Jani Honkonen
|
r2131 | } | ||
Marek Rosa
|
r1846 | } | ||
Michal Klocek
|
r2111 | |||
Marek Rosa
|
r1701 | return points; | ||
} | ||||
Marek Rosa
|
r1817 | void ChartCategoryAxisX::updateGeometry() | ||
Michal Klocek
|
r2407 | { | ||
Miikka Heikkinen
|
r2477 | //TODO: this is not optimal when many categories :( , create only visible lables | ||
Jani Honkonen
|
r2131 | setLabels(m_axis->categoriesLabels() << ""); | ||
Michal Klocek
|
r2111 | HorizontalAxis::updateGeometry(); | ||
Marek Rosa
|
r1701 | } | ||
Jani Honkonen
|
r2131 | QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
Michal Klocek
|
r1965 | { | ||
Q_UNUSED(constraint) | ||||
QSizeF sh; | ||||
Michal Klocek
|
r2138 | QSizeF base = HorizontalAxis::sizeHint(which, constraint); | ||
Michal Klocek
|
r2136 | QStringList ticksList = m_axis->categoriesLabels(); | ||
Miikka Heikkinen
|
r2443 | qreal width = 0; // Width is irrelevant for X axes with interval labels | ||
Jani Honkonen
|
r2131 | qreal height = 0; | ||
Michal Klocek
|
r1965 | |||
switch (which) { | ||||
Miikka Heikkinen
|
r2412 | case Qt::MinimumSize: { | ||
Miikka Heikkinen
|
r2539 | QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), "...", axis()->labelsAngle()); | ||
Miikka Heikkinen
|
r2534 | height = boundingRect.height() + labelPadding() + base.height() + 1.0; | ||
Jani Honkonen
|
r2131 | sh = QSizeF(width, height); | ||
Michal Klocek
|
r1965 | break; | ||
Miikka Heikkinen
|
r2412 | } | ||
Jani Honkonen
|
r2131 | case Qt::PreferredSize: { | ||
Miikka Heikkinen
|
r2534 | qreal labelHeight = 0.0; | ||
Miikka Heikkinen
|
r2412 | foreach (const QString& s, ticksList) { | ||
Miikka Heikkinen
|
r2539 | QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle()); | ||
Miikka Heikkinen
|
r2412 | labelHeight = qMax(rect.height(), labelHeight); | ||
Michal Klocek
|
r1965 | } | ||
Miikka Heikkinen
|
r2534 | height = labelHeight + labelPadding() + base.height() + 1.0; | ||
Jani Honkonen
|
r2131 | sh = QSizeF(width, height); | ||
break; | ||||
} | ||||
default: | ||||
Michal Klocek
|
r1965 | break; | ||
} | ||||
return sh; | ||||
} | ||||
Miikka Heikkinen
|
r2483 | void ChartCategoryAxisX::handleCategoriesChanged() | ||
{ | ||||
QGraphicsLayoutItem::updateGeometry(); | ||||
presenter()->layout()->invalidate(); | ||||
} | ||||
#include "moc_chartcategoryaxisx_p.cpp" | ||||
Marek Rosa
|
r1701 | QTCOMMERCIALCHART_END_NAMESPACE | ||