File indexing completed on 2024-05-05 16:16:40

0001 /*
0002  * This file is part of KQuickCharts
0003  * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 #include "ChartAxisSource.h"
0009 
0010 #include <QDebug>
0011 #include <QVariant>
0012 
0013 #include "XYChart.h"
0014 
0015 ChartAxisSource::ChartAxisSource(QObject *parent)
0016     : ChartDataSource(parent)
0017 {
0018     connect(this, &ChartAxisSource::itemCountChanged, this, &ChartAxisSource::dataChanged);
0019     connect(this, &ChartAxisSource::chartChanged, this, &ChartAxisSource::dataChanged);
0020     connect(this, &ChartAxisSource::axisChanged, this, &ChartAxisSource::dataChanged);
0021 }
0022 
0023 QVariant ChartAxisSource::item(int index) const
0024 {
0025     if (!m_chart || index < 0 || index > m_itemCount) {
0026         return {};
0027     }
0028 
0029     auto range = m_chart->computedRange();
0030     if (m_axis == Axis::XAxis) {
0031         return range.startX + (range.distanceX / (m_itemCount - 1)) * index;
0032     } else {
0033         return range.startY + (range.distanceY / (m_itemCount - 1)) * index;
0034     }
0035 }
0036 
0037 QVariant ChartAxisSource::minimum() const
0038 {
0039     if (!m_chart) {
0040         return {};
0041     }
0042 
0043     if (m_axis == Axis::XAxis) {
0044         return m_chart->computedRange().startX;
0045     } else {
0046         return m_chart->computedRange().startY;
0047     }
0048 }
0049 
0050 QVariant ChartAxisSource::maximum() const
0051 {
0052     if (!m_chart) {
0053         return {};
0054     }
0055 
0056     if (m_axis == Axis::XAxis) {
0057         return m_chart->computedRange().endX;
0058     } else {
0059         return m_chart->computedRange().endY;
0060     }
0061 }
0062 
0063 XYChart *ChartAxisSource::chart() const
0064 {
0065     return m_chart;
0066 }
0067 
0068 void ChartAxisSource::setChart(XYChart *newChart)
0069 {
0070     if (newChart == m_chart) {
0071         return;
0072     }
0073 
0074     if (m_chart) {
0075         disconnect(m_chart, &XYChart::computedRangeChanged, this, &ChartAxisSource::dataChanged);
0076     }
0077 
0078     m_chart = newChart;
0079     if (m_chart) {
0080         connect(m_chart, &XYChart::computedRangeChanged, this, &ChartAxisSource::dataChanged);
0081     }
0082     Q_EMIT chartChanged();
0083 }
0084 
0085 ChartAxisSource::Axis ChartAxisSource::axis() const
0086 {
0087     return m_axis;
0088 }
0089 
0090 void ChartAxisSource::setAxis(ChartAxisSource::Axis newAxis)
0091 {
0092     if (newAxis == m_axis) {
0093         return;
0094     }
0095 
0096     m_axis = newAxis;
0097     Q_EMIT axisChanged();
0098 }
0099 
0100 int ChartAxisSource::itemCount() const
0101 {
0102     return m_itemCount;
0103 }
0104 
0105 void ChartAxisSource::setItemCount(int newItemCount)
0106 {
0107     if (newItemCount == m_itemCount) {
0108         return;
0109     }
0110 
0111     m_itemCount = newItemCount;
0112     Q_EMIT itemCountChanged();
0113 }
0114 
0115 #include "moc_ChartAxisSource.cpp"