| 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 "Chart.h" |
| 9 | #include "datasource/ChartDataSource.h" |
| 10 | |
| 11 | Chart::Chart(QQuickItem *parent) |
| 12 | : QQuickItem(parent) |
| 13 | { |
| 14 | setFlag(flag: ItemHasContents, enabled: true); |
| 15 | connect(sender: this, signal: &Chart::dataChanged, context: this, slot: &Chart::onDataChanged); |
| 16 | } |
| 17 | |
| 18 | ChartDataSource *Chart::nameSource() const |
| 19 | { |
| 20 | return m_nameSource; |
| 21 | } |
| 22 | |
| 23 | void Chart::setNameSource(ChartDataSource *nameSource) |
| 24 | { |
| 25 | if (m_nameSource == nameSource) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | m_nameSource = nameSource; |
| 30 | Q_EMIT dataChanged(); |
| 31 | Q_EMIT nameSourceChanged(); |
| 32 | } |
| 33 | |
| 34 | ChartDataSource *Chart::shortNameSource() const |
| 35 | { |
| 36 | return m_shortNameSource; |
| 37 | } |
| 38 | |
| 39 | void Chart::setShortNameSource(ChartDataSource *shortNameSource) |
| 40 | { |
| 41 | if (m_shortNameSource == shortNameSource) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | m_shortNameSource = shortNameSource; |
| 46 | Q_EMIT dataChanged(); |
| 47 | Q_EMIT shortNameSourceChanged(); |
| 48 | } |
| 49 | |
| 50 | ChartDataSource *Chart::colorSource() const |
| 51 | { |
| 52 | return m_colorSource; |
| 53 | } |
| 54 | |
| 55 | void Chart::setColorSource(ChartDataSource *colorSource) |
| 56 | { |
| 57 | if (m_colorSource == colorSource) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | if (m_colorSource) { |
| 62 | disconnect(sender: m_colorSource, signal: &ChartDataSource::dataChanged, receiver: this, slot: &Chart::dataChanged); |
| 63 | } |
| 64 | |
| 65 | m_colorSource = colorSource; |
| 66 | |
| 67 | if (m_colorSource) { |
| 68 | connect(sender: m_colorSource, signal: &ChartDataSource::dataChanged, context: this, slot: &Chart::dataChanged); |
| 69 | } |
| 70 | |
| 71 | Q_EMIT dataChanged(); |
| 72 | Q_EMIT colorSourceChanged(); |
| 73 | } |
| 74 | |
| 75 | Chart::DataSourcesProperty Chart::valueSourcesProperty() |
| 76 | { |
| 77 | return DataSourcesProperty{ |
| 78 | this, |
| 79 | this, |
| 80 | &Chart::appendSource, |
| 81 | &Chart::sourceCount, |
| 82 | &Chart::source, |
| 83 | &Chart::clearSources, |
| 84 | &Chart::replaceSource, |
| 85 | &Chart::removeLastSource, |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | QList<ChartDataSource *> Chart::valueSources() const |
| 90 | { |
| 91 | return m_valueSources; |
| 92 | } |
| 93 | |
| 94 | void Chart::insertValueSource(int index, ChartDataSource *source) |
| 95 | { |
| 96 | if (index < 0) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | m_valueSources.insert(i: index, t: source); |
| 101 | connect(sender: source, signal: &QObject::destroyed, context: this, slot: qOverload<QObject *>(&Chart::removeValueSource)); |
| 102 | connect(sender: source, signal: &ChartDataSource::dataChanged, context: this, slot: &Chart::dataChanged); |
| 103 | |
| 104 | Q_EMIT dataChanged(); |
| 105 | Q_EMIT valueSourcesChanged(); |
| 106 | } |
| 107 | |
| 108 | void Chart::removeValueSource(int index) |
| 109 | { |
| 110 | if (index < 0 || index >= m_valueSources.count()) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | m_valueSources.at(i: index)->disconnect(receiver: this); |
| 115 | m_valueSources.remove(i: index); |
| 116 | |
| 117 | Q_EMIT dataChanged(); |
| 118 | Q_EMIT valueSourcesChanged(); |
| 119 | } |
| 120 | |
| 121 | void Chart::removeValueSource(QObject *source) |
| 122 | { |
| 123 | auto itr = std::find_if(first: m_valueSources.begin(), last: m_valueSources.end(), pred: [source](QObject *dataSource) { |
| 124 | return dataSource == source; |
| 125 | }); |
| 126 | |
| 127 | if (itr != m_valueSources.end()) { |
| 128 | (*itr)->disconnect(receiver: this); |
| 129 | m_valueSources.erase(pos: itr); |
| 130 | } |
| 131 | |
| 132 | Q_EMIT dataChanged(); |
| 133 | Q_EMIT valueSourcesChanged(); |
| 134 | } |
| 135 | |
| 136 | Chart::IndexingMode Chart::indexingMode() const |
| 137 | { |
| 138 | return m_indexingMode; |
| 139 | } |
| 140 | |
| 141 | void Chart::setIndexingMode(IndexingMode newIndexingMode) |
| 142 | { |
| 143 | if (newIndexingMode == m_indexingMode) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | m_indexingMode = newIndexingMode; |
| 148 | Q_EMIT dataChanged(); |
| 149 | Q_EMIT indexingModeChanged(); |
| 150 | } |
| 151 | |
| 152 | int Chart::highlight() const |
| 153 | { |
| 154 | return m_highlight; |
| 155 | } |
| 156 | |
| 157 | void Chart::setHighlight(int newHighlight) |
| 158 | { |
| 159 | if (newHighlight == m_highlight) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | m_highlight = newHighlight; |
| 164 | Q_EMIT dataChanged(); |
| 165 | Q_EMIT highlightChanged(); |
| 166 | } |
| 167 | |
| 168 | void Chart::resetHighlight() |
| 169 | { |
| 170 | setHighlight(-1); |
| 171 | } |
| 172 | |
| 173 | void Chart::componentComplete() |
| 174 | { |
| 175 | QQuickItem::componentComplete(); |
| 176 | Q_EMIT dataChanged(); |
| 177 | } |
| 178 | |
| 179 | QColor Chart::desaturate(const QColor &input) |
| 180 | { |
| 181 | auto color = input.convertTo(colorSpec: QColor::Hsl); |
| 182 | color.setHslF(h: color.hueF(), s: color.saturationF() * 0.5, l: color.lightnessF() * 0.5, a: color.alphaF() * 0.5); |
| 183 | return color.convertTo(colorSpec: QColor::Rgb); |
| 184 | } |
| 185 | |
| 186 | void Chart::appendSource(Chart::DataSourcesProperty *list, ChartDataSource *source) |
| 187 | { |
| 188 | auto chart = reinterpret_cast<Chart *>(list->data); |
| 189 | chart->insertValueSource(index: chart->valueSources().size(), source); |
| 190 | } |
| 191 | qsizetype Chart::sourceCount(Chart::DataSourcesProperty *list) |
| 192 | { |
| 193 | return reinterpret_cast<Chart *>(list->data)->m_valueSources.count(); |
| 194 | } |
| 195 | |
| 196 | ChartDataSource *Chart::source(Chart::DataSourcesProperty *list, qsizetype index) |
| 197 | { |
| 198 | return reinterpret_cast<Chart *>(list->data)->m_valueSources.at(i: index); |
| 199 | } |
| 200 | |
| 201 | void Chart::clearSources(Chart::DataSourcesProperty *list) |
| 202 | { |
| 203 | auto chart = reinterpret_cast<Chart *>(list->data); |
| 204 | std::for_each(first: chart->m_valueSources.cbegin(), last: chart->m_valueSources.cend(), f: [chart](ChartDataSource *source) { |
| 205 | source->disconnect(receiver: chart); |
| 206 | }); |
| 207 | chart->m_valueSources.clear(); |
| 208 | Q_EMIT chart->dataChanged(); |
| 209 | } |
| 210 | |
| 211 | void Chart::replaceSource(DataSourcesProperty *list, qsizetype index, ChartDataSource *source) |
| 212 | { |
| 213 | auto chart = reinterpret_cast<Chart *>(list->data); |
| 214 | Q_ASSERT(index > 0 && index < chart->m_valueSources.size()); |
| 215 | chart->m_valueSources.at(i: index)->disconnect(receiver: chart); |
| 216 | chart->m_valueSources.replace(i: index, t: source); |
| 217 | connect(sender: source, signal: &QObject::destroyed, context: chart, slot: qOverload<QObject *>(&Chart::removeValueSource)); |
| 218 | connect(sender: source, signal: &ChartDataSource::dataChanged, context: chart, slot: &Chart::dataChanged); |
| 219 | Q_EMIT chart->dataChanged(); |
| 220 | } |
| 221 | |
| 222 | void Chart::removeLastSource(DataSourcesProperty *list) |
| 223 | { |
| 224 | auto chart = reinterpret_cast<Chart *>(list->data); |
| 225 | chart->removeValueSource(index: chart->m_valueSources.size() - 1); |
| 226 | } |
| 227 | |
| 228 | #include "moc_Chart.cpp" |
| 229 | |