1/*
2 * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6
7#include "MapProxySource.h"
8
9MapProxySource::MapProxySource(QObject *parent)
10 : ChartDataSource(parent)
11{
12 connect(sender: this, signal: &MapProxySource::sourceChanged, context: this, slot: &ChartDataSource::dataChanged);
13 connect(sender: this, signal: &MapProxySource::mapChanged, context: this, slot: &ChartDataSource::dataChanged);
14}
15
16int MapProxySource::itemCount() const
17{
18 if (m_source) {
19 return m_source->itemCount();
20 }
21
22 return 0;
23}
24
25QVariant MapProxySource::minimum() const
26{
27 auto itr = std::min_element(first: m_map.cbegin(), last: m_map.cend(), comp: variantCompare);
28 if (itr != m_map.cend()) {
29 return *itr;
30 }
31 return QVariant{};
32}
33
34QVariant MapProxySource::maximum() const
35{
36 auto itr = std::max_element(first: m_map.cbegin(), last: m_map.cend(), comp: variantCompare);
37 if (itr != m_map.cend()) {
38 return *itr;
39 }
40 return QVariant{};
41}
42
43QVariant MapProxySource::item(int index) const
44{
45 if (!m_source) {
46 return QVariant{};
47 }
48
49 auto mapIndex = m_source->item(index).toString();
50 if (mapIndex.isEmpty()) {
51 return QVariant{};
52 }
53
54 return m_map.value(key: mapIndex);
55}
56
57ChartDataSource *MapProxySource::source() const
58{
59 return m_source;
60}
61
62void MapProxySource::setSource(ChartDataSource *newSource)
63{
64 if (newSource == m_source) {
65 return;
66 }
67
68 if (m_source) {
69 m_source->disconnect(receiver: this);
70 }
71
72 m_source = newSource;
73 if (m_source) {
74 connect(sender: m_source, signal: &ChartDataSource::dataChanged, context: this, slot: &ChartDataSource::dataChanged);
75 }
76 Q_EMIT sourceChanged();
77}
78
79QVariantMap MapProxySource::map() const
80{
81 return m_map;
82}
83
84void MapProxySource::setMap(const QVariantMap &newMap)
85{
86 if (newMap == m_map) {
87 return;
88 }
89
90 m_map = newMap;
91
92 Q_EMIT mapChanged();
93}
94
95#include "moc_MapProxySource.cpp"
96

source code of kquickcharts/src/datasource/MapProxySource.cpp