File indexing completed on 2024-12-08 12:23:41

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{this, this, &Chart::appendSource, &Chart::sourceCount, &Chart::source, &Chart::clearSources};
0078 }
0079 
0080 QVector<ChartDataSource *> Chart::valueSources() const
0081 {
0082     return m_valueSources;
0083 }
0084 
0085 void Chart::insertValueSource(int index, ChartDataSource *source)
0086 {
0087     if (index < 0) {
0088         return;
0089     }
0090 
0091     m_valueSources.insert(index, source);
0092     connect(source, &QObject::destroyed, this, qOverload<QObject *>(&Chart::removeValueSource));
0093     connect(source, &ChartDataSource::dataChanged, this, &Chart::dataChanged);
0094 
0095     Q_EMIT dataChanged();
0096     Q_EMIT valueSourcesChanged();
0097 }
0098 
0099 void Chart::removeValueSource(int index)
0100 {
0101     if (index < 0 || index >= m_valueSources.count()) {
0102         return;
0103     }
0104 
0105     m_valueSources.at(index)->disconnect(this);
0106     m_valueSources.remove(index);
0107 
0108     Q_EMIT dataChanged();
0109     Q_EMIT valueSourcesChanged();
0110 }
0111 
0112 void Chart::removeValueSource(QObject *source)
0113 {
0114     auto itr = std::find_if(m_valueSources.begin(), m_valueSources.end(), [source](QObject *dataSource) {
0115         return dataSource == source;
0116     });
0117 
0118     if (itr != m_valueSources.end()) {
0119         (*itr)->disconnect(this);
0120         m_valueSources.erase(itr);
0121     }
0122 
0123     Q_EMIT dataChanged();
0124     Q_EMIT valueSourcesChanged();
0125 }
0126 
0127 Chart::IndexingMode Chart::indexingMode() const
0128 {
0129     return m_indexingMode;
0130 }
0131 
0132 void Chart::setIndexingMode(IndexingMode newIndexingMode)
0133 {
0134     if (newIndexingMode == m_indexingMode) {
0135         return;
0136     }
0137 
0138     m_indexingMode = newIndexingMode;
0139     Q_EMIT dataChanged();
0140     Q_EMIT indexingModeChanged();
0141 }
0142 
0143 void Chart::componentComplete()
0144 {
0145     QQuickItem::componentComplete();
0146     Q_EMIT dataChanged();
0147 }
0148 
0149 void Chart::appendSource(Chart::DataSourcesProperty *list, ChartDataSource *source)
0150 {
0151     auto chart = reinterpret_cast<Chart *>(list->data);
0152     chart->insertValueSource(chart->valueSources().size(), source);
0153 }
0154 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0155 int Chart::sourceCount(Chart::DataSourcesProperty *list)
0156 #else
0157 qsizetype Chart::sourceCount(Chart::DataSourcesProperty *list)
0158 #endif
0159 {
0160     return reinterpret_cast<Chart *>(list->data)->m_valueSources.count();
0161 }
0162 
0163 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0164 ChartDataSource *Chart::source(Chart::DataSourcesProperty *list, int index)
0165 #else
0166 ChartDataSource *Chart::source(Chart::DataSourcesProperty *list, qsizetype index)
0167 #endif
0168 {
0169     return reinterpret_cast<Chart *>(list->data)->m_valueSources.at(index);
0170 }
0171 
0172 void Chart::clearSources(Chart::DataSourcesProperty *list)
0173 {
0174     auto chart = reinterpret_cast<Chart *>(list->data);
0175     std::for_each(chart->m_valueSources.cbegin(), chart->m_valueSources.cend(), [chart](ChartDataSource *source) {
0176         source->disconnect(chart);
0177     });
0178     chart->m_valueSources.clear();
0179     Q_EMIT chart->dataChanged();
0180 }
0181 
0182 #include "moc_Chart.cpp"