1 | /* |
2 | * This file is part of KQuickCharts |
3 | * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl> |
4 | * |
5 | * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
6 | */ |
7 | |
8 | #include "ChartAxisSource.h" |
9 | |
10 | #include <QDebug> |
11 | #include <QVariant> |
12 | |
13 | #include "XYChart.h" |
14 | |
15 | ChartAxisSource::ChartAxisSource(QObject *parent) |
16 | : ChartDataSource(parent) |
17 | { |
18 | connect(sender: this, signal: &ChartAxisSource::itemCountChanged, context: this, slot: &ChartAxisSource::dataChanged); |
19 | connect(sender: this, signal: &ChartAxisSource::chartChanged, context: this, slot: &ChartAxisSource::dataChanged); |
20 | connect(sender: this, signal: &ChartAxisSource::axisChanged, context: this, slot: &ChartAxisSource::dataChanged); |
21 | } |
22 | |
23 | QVariant ChartAxisSource::item(int index) const |
24 | { |
25 | if (!m_chart || index < 0 || index > m_itemCount) { |
26 | return {}; |
27 | } |
28 | |
29 | auto range = m_chart->computedRange(); |
30 | if (m_axis == Axis::XAxis) { |
31 | return range.startX + (range.distanceX / (m_itemCount - 1)) * index; |
32 | } else { |
33 | return range.startY + (range.distanceY / (m_itemCount - 1)) * index; |
34 | } |
35 | } |
36 | |
37 | QVariant ChartAxisSource::minimum() const |
38 | { |
39 | if (!m_chart) { |
40 | return {}; |
41 | } |
42 | |
43 | if (m_axis == Axis::XAxis) { |
44 | return m_chart->computedRange().startX; |
45 | } else { |
46 | return m_chart->computedRange().startY; |
47 | } |
48 | } |
49 | |
50 | QVariant ChartAxisSource::maximum() const |
51 | { |
52 | if (!m_chart) { |
53 | return {}; |
54 | } |
55 | |
56 | if (m_axis == Axis::XAxis) { |
57 | return m_chart->computedRange().endX; |
58 | } else { |
59 | return m_chart->computedRange().endY; |
60 | } |
61 | } |
62 | |
63 | XYChart *ChartAxisSource::chart() const |
64 | { |
65 | return m_chart; |
66 | } |
67 | |
68 | void ChartAxisSource::setChart(XYChart *newChart) |
69 | { |
70 | if (newChart == m_chart) { |
71 | return; |
72 | } |
73 | |
74 | if (m_chart) { |
75 | disconnect(sender: m_chart, signal: &XYChart::computedRangeChanged, receiver: this, slot: &ChartAxisSource::dataChanged); |
76 | } |
77 | |
78 | m_chart = newChart; |
79 | if (m_chart) { |
80 | connect(sender: m_chart, signal: &XYChart::computedRangeChanged, context: this, slot: &ChartAxisSource::dataChanged); |
81 | } |
82 | Q_EMIT chartChanged(); |
83 | } |
84 | |
85 | ChartAxisSource::Axis ChartAxisSource::axis() const |
86 | { |
87 | return m_axis; |
88 | } |
89 | |
90 | void ChartAxisSource::setAxis(ChartAxisSource::Axis newAxis) |
91 | { |
92 | if (newAxis == m_axis) { |
93 | return; |
94 | } |
95 | |
96 | m_axis = newAxis; |
97 | Q_EMIT axisChanged(); |
98 | } |
99 | |
100 | int ChartAxisSource::itemCount() const |
101 | { |
102 | return m_itemCount; |
103 | } |
104 | |
105 | void ChartAxisSource::setItemCount(int newItemCount) |
106 | { |
107 | if (newItemCount == m_itemCount) { |
108 | return; |
109 | } |
110 | |
111 | m_itemCount = newItemCount; |
112 | Q_EMIT itemCountChanged(); |
113 | } |
114 | |
115 | #include "moc_ChartAxisSource.cpp" |
116 | |