File indexing completed on 2024-10-06 03:40:52

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 "Chart.h"
0009 #include "datasource/ChartDataSource.h"
0010 
0011 Chart::Chart(QQuickItem *parent)
0012     : QQuickItem(parent)
0013 {
0014     setFlag(ItemHasContents, true);
0015     connect(this, &Chart::dataChanged, this, &Chart::onDataChanged);
0016 }
0017 
0018 ChartDataSource *Chart::nameSource() const
0019 {
0020     return m_nameSource;
0021 }
0022 
0023 void Chart::setNameSource(ChartDataSource *nameSource)
0024 {
0025     if (m_nameSource == nameSource) {
0026         return;
0027     }
0028 
0029     m_nameSource = nameSource;
0030     Q_EMIT dataChanged();
0031     Q_EMIT nameSourceChanged();
0032 }
0033 
0034 ChartDataSource *Chart::shortNameSource() const
0035 {
0036     return m_shortNameSource;
0037 }
0038 
0039 void Chart::setShortNameSource(ChartDataSource *shortNameSource)
0040 {
0041     if (m_shortNameSource == shortNameSource) {
0042         return;
0043     }
0044 
0045     m_shortNameSource = shortNameSource;
0046     Q_EMIT dataChanged();
0047     Q_EMIT shortNameSourceChanged();
0048 }
0049 
0050 ChartDataSource *Chart::colorSource() const
0051 {
0052     return m_colorSource;
0053 }
0054 
0055 void Chart::setColorSource(ChartDataSource *colorSource)
0056 {
0057     if (m_colorSource == colorSource) {
0058         return;
0059     }
0060 
0061     if (m_colorSource) {
0062         disconnect(m_colorSource, &ChartDataSource::dataChanged, this, &Chart::dataChanged);
0063     }
0064 
0065     m_colorSource = colorSource;
0066 
0067     if (m_colorSource) {
0068         connect(m_colorSource, &ChartDataSource::dataChanged, this, &Chart::dataChanged);
0069     }
0070 
0071     Q_EMIT dataChanged();
0072     Q_EMIT colorSourceChanged();
0073 }
0074 
0075 Chart::DataSourcesProperty Chart::valueSourcesProperty()
0076 {
0077     return DataSourcesProperty{
0078         this,
0079         this,
0080         &Chart::appendSource,
0081         &Chart::sourceCount,
0082         &Chart::source,
0083         &Chart::clearSources,
0084         &Chart::replaceSource,
0085         &Chart::removeLastSource,
0086     };
0087 }
0088 
0089 QList<ChartDataSource *> Chart::valueSources() const
0090 {
0091     return m_valueSources;
0092 }
0093 
0094 void Chart::insertValueSource(int index, ChartDataSource *source)
0095 {
0096     if (index < 0) {
0097         return;
0098     }
0099 
0100     m_valueSources.insert(index, source);
0101     connect(source, &QObject::destroyed, this, qOverload<QObject *>(&Chart::removeValueSource));
0102     connect(source, &ChartDataSource::dataChanged, this, &Chart::dataChanged);
0103 
0104     Q_EMIT dataChanged();
0105     Q_EMIT valueSourcesChanged();
0106 }
0107 
0108 void Chart::removeValueSource(int index)
0109 {
0110     if (index < 0 || index >= m_valueSources.count()) {
0111         return;
0112     }
0113 
0114     m_valueSources.at(index)->disconnect(this);
0115     m_valueSources.remove(index);
0116 
0117     Q_EMIT dataChanged();
0118     Q_EMIT valueSourcesChanged();
0119 }
0120 
0121 void Chart::removeValueSource(QObject *source)
0122 {
0123     auto itr = std::find_if(m_valueSources.begin(), m_valueSources.end(), [source](QObject *dataSource) {
0124         return dataSource == source;
0125     });
0126 
0127     if (itr != m_valueSources.end()) {
0128         (*itr)->disconnect(this);
0129         m_valueSources.erase(itr);
0130     }
0131 
0132     Q_EMIT dataChanged();
0133     Q_EMIT valueSourcesChanged();
0134 }
0135 
0136 Chart::IndexingMode Chart::indexingMode() const
0137 {
0138     return m_indexingMode;
0139 }
0140 
0141 void Chart::setIndexingMode(IndexingMode newIndexingMode)
0142 {
0143     if (newIndexingMode == m_indexingMode) {
0144         return;
0145     }
0146 
0147     m_indexingMode = newIndexingMode;
0148     Q_EMIT dataChanged();
0149     Q_EMIT indexingModeChanged();
0150 }
0151 
0152 void Chart::componentComplete()
0153 {
0154     QQuickItem::componentComplete();
0155     Q_EMIT dataChanged();
0156 }
0157 
0158 void Chart::appendSource(Chart::DataSourcesProperty *list, ChartDataSource *source)
0159 {
0160     auto chart = reinterpret_cast<Chart *>(list->data);
0161     chart->insertValueSource(chart->valueSources().size(), source);
0162 }
0163 qsizetype Chart::sourceCount(Chart::DataSourcesProperty *list)
0164 {
0165     return reinterpret_cast<Chart *>(list->data)->m_valueSources.count();
0166 }
0167 
0168 ChartDataSource *Chart::source(Chart::DataSourcesProperty *list, qsizetype index)
0169 {
0170     return reinterpret_cast<Chart *>(list->data)->m_valueSources.at(index);
0171 }
0172 
0173 void Chart::clearSources(Chart::DataSourcesProperty *list)
0174 {
0175     auto chart = reinterpret_cast<Chart *>(list->data);
0176     std::for_each(chart->m_valueSources.cbegin(), chart->m_valueSources.cend(), [chart](ChartDataSource *source) {
0177         source->disconnect(chart);
0178     });
0179     chart->m_valueSources.clear();
0180     Q_EMIT chart->dataChanged();
0181 }
0182 
0183 void Chart::replaceSource(DataSourcesProperty *list, qsizetype index, ChartDataSource *source)
0184 {
0185     auto chart = reinterpret_cast<Chart *>(list->data);
0186     Q_ASSERT(index > 0 && index < chart->m_valueSources.size());
0187     chart->m_valueSources.at(index)->disconnect(chart);
0188     chart->m_valueSources.replace(index, source);
0189     connect(source, &QObject::destroyed, chart, qOverload<QObject *>(&Chart::removeValueSource));
0190     connect(source, &ChartDataSource::dataChanged, chart, &Chart::dataChanged);
0191     Q_EMIT chart->dataChanged();
0192 }
0193 
0194 void Chart::removeLastSource(DataSourcesProperty *list)
0195 {
0196     auto chart = reinterpret_cast<Chart *>(list->data);
0197     chart->removeValueSource(chart->m_valueSources.size() - 1);
0198 }
0199 
0200 #include "moc_Chart.cpp"