chartcategoryaxisy.cpp
128 lines
| 4.3 KiB
| text/x-c
|
CppLexer
|
r2854 | /**************************************************************************** | ||
|
r1701 | ** | ||
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
|
r1701 | ** | ||
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
|
r1701 | ** | ||
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
|
r2845 | ** Commercial License Usage | ||
** Licensees holding valid commercial Qt licenses may use this file in | ||||
** accordance with the commercial license agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and The Qt Company. For licensing terms | ||||
|
r2854 | ** and conditions see https://www.qt.io/terms-conditions. For further | ||
** information use the contact form at https://www.qt.io/contact-us. | ||||
** | ||||
** GNU General Public License Usage | ||||
** Alternatively, this file may be used under the terms of the GNU | ||||
** General Public License version 3 or (at your option) any later version | ||||
** approved by the KDE Free Qt Foundation. The licenses are as published by | ||||
** the Free Software Foundation and appearing in the file LICENSE.GPL3 | ||||
** included in the packaging of this file. Please review the following | ||||
** information to ensure the GNU General Public License requirements will | ||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html. | ||||
|
r1701 | ** | ||
|
r2845 | ** $QT_END_LICENSE$ | ||
** | ||||
|
r2854 | ****************************************************************************/ | ||
|
r1701 | |||
|
r2714 | #include <private/chartcategoryaxisy_p.h> | ||
#include <QtCharts/QCategoryAxis> | ||||
#include <QtCharts/QAbstractAxis> | ||||
#include <private/chartpresenter_p.h> | ||||
#include <private/abstractchartlayout_p.h> | ||||
#include <QtWidgets/QGraphicsLayout> | ||||
#include <QtCore/QtMath> | ||||
#include <QtCore/QDebug> | ||||
|
r1735 | |||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r1701 | |||
|
r2273 | ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, QGraphicsItem* item) | ||
: VerticalAxis(axis, item, true), | ||||
|
r2131 | m_axis(axis) | ||
|
r1701 | { | ||
|
r2483 | QObject::connect(axis, SIGNAL(categoriesChanged()), this, SLOT(handleCategoriesChanged())); | ||
|
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) { | ||
|
r2436 | qreal y = -(m_axis->startValue(m_axis->categoriesLabels().at(i)) - min()) * scale + gridRect.bottom(); | ||
|
r1846 | points[i] = y; | ||
} else { | ||||
|
r2436 | qreal 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 | { | ||
|
r2712 | setLabels(m_axis->categoriesLabels() << QString()); | ||
|
r2111 | VerticalAxis::updateGeometry(); | ||
|
r1701 | } | ||
|
r2131 | QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const | ||
|
r1965 | { | ||
Q_UNUSED(constraint) | ||||
QSizeF sh; | ||||
|
r2138 | QSizeF base = VerticalAxis::sizeHint(which, constraint); | ||
|
r2136 | QStringList ticksList = m_axis->categoriesLabels(); | ||
|
r2131 | qreal width = 0; | ||
|
r2443 | qreal height = 0; // Height is irrelevant for Y axes with interval labels | ||
|
r2131 | |||
switch (which) { | ||||
|
r2412 | case Qt::MinimumSize: { | ||
|
r2712 | QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(), | ||
QStringLiteral("..."), | ||||
axis()->labelsAngle()); | ||||
|
r2534 | width = boundingRect.width() + labelPadding() + base.width() + 1.0; | ||
|
r2131 | sh = QSizeF(width, height); | ||
break; | ||||
|
r2412 | } | ||
|
r2131 | case Qt::PreferredSize: { | ||
|
r2534 | qreal labelWidth = 0.0; | ||
|
r2412 | foreach (const QString& s, ticksList) { | ||
|
r2539 | QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle()); | ||
|
r2412 | labelWidth = qMax(rect.width(), labelWidth); | ||
|
r1965 | } | ||
|
r2534 | width = labelWidth + labelPadding() + base.width() + 1.0; | ||
|
r2131 | sh = QSizeF(width, height); | ||
break; | ||||
} | ||||
default: | ||||
break; | ||||
} | ||||
return sh; | ||||
|
r1965 | } | ||
|
r2483 | void ChartCategoryAxisY::handleCategoriesChanged() | ||
{ | ||||
QGraphicsLayoutItem::updateGeometry(); | ||||
presenter()->layout()->invalidate(); | ||||
} | ||||
#include "moc_chartcategoryaxisy_p.cpp" | ||||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||