File indexing completed on 2024-04-14 14:27:07

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 #ifndef CHART_H
0009 #define CHART_H
0010 
0011 #include "datasource/ChartDataSource.h"
0012 #include <QQuickItem>
0013 
0014 /**
0015  * Abstract base class for all charts.
0016  */
0017 class Chart : public QQuickItem
0018 {
0019     Q_OBJECT
0020     /**
0021      * The data source to use for names of chart items.
0022      */
0023     Q_PROPERTY(ChartDataSource *nameSource READ nameSource WRITE setNameSource NOTIFY nameSourceChanged)
0024     /**
0025      * The data source to use for short names of chart items.
0026      */
0027     Q_PROPERTY(ChartDataSource *shortNameSource READ shortNameSource WRITE setShortNameSource NOTIFY shortNameSourceChanged)
0028     /**
0029      * The data source to use for colors of chart items.
0030      */
0031     Q_PROPERTY(ChartDataSource *colorSource READ colorSource WRITE setColorSource NOTIFY colorSourceChanged)
0032     /**
0033      * The data sources providing the data this chart needs to render.
0034      */
0035     Q_PROPERTY(QQmlListProperty<ChartDataSource> valueSources READ valueSourcesProperty NOTIFY valueSourcesChanged)
0036     /**
0037      * The indexing mode used for indexing colors and names.
0038      */
0039     Q_PROPERTY(IndexingMode indexingMode READ indexingMode WRITE setIndexingMode NOTIFY indexingModeChanged)
0040 
0041 public:
0042     using DataSourcesProperty = QQmlListProperty<ChartDataSource>;
0043 
0044     /**
0045      * How to index color and name sources relative to the different value sources.
0046      */
0047     enum IndexingMode {
0048         IndexSourceValues = 1, ///< Index each value, restart indexing for each value source.
0049         IndexEachSource, ///< Index each value source, never index individual values.
0050         IndexAllValues ///< Index each value, continuing with the index for each value source.
0051     };
0052     Q_ENUM(IndexingMode)
0053 
0054     explicit Chart(QQuickItem *parent = nullptr);
0055     ~Chart() override = default;
0056 
0057     ChartDataSource *nameSource() const;
0058     void setNameSource(ChartDataSource *nameSource);
0059     Q_SIGNAL void nameSourceChanged();
0060 
0061     ChartDataSource *shortNameSource() const;
0062     void setShortNameSource(ChartDataSource *shortNameSource);
0063     Q_SIGNAL void shortNameSourceChanged();
0064 
0065     ChartDataSource *colorSource() const;
0066     void setColorSource(ChartDataSource *colorSource);
0067     Q_SIGNAL void colorSourceChanged();
0068 
0069     DataSourcesProperty valueSourcesProperty();
0070     QVector<ChartDataSource *> valueSources() const;
0071     Q_SIGNAL void valueSourcesChanged();
0072     Q_INVOKABLE void insertValueSource(int index, ChartDataSource *source);
0073     Q_INVOKABLE void removeValueSource(int index);
0074     Q_INVOKABLE void removeValueSource(QObject *source);
0075 
0076     IndexingMode indexingMode() const;
0077     void setIndexingMode(IndexingMode newIndexingMode);
0078     Q_SIGNAL void indexingModeChanged();
0079 
0080     Q_SIGNAL void dataChanged();
0081 
0082 protected:
0083     /**
0084      * Called when the data of a value source changes.
0085      *
0086      * This method should be reimplemented by subclasses. It is called whenever
0087      * the data of one of the value sources changes. Subclasses should use this
0088      * to make sure that they update whatever internal state they use for
0089      * rendering, then call update() to schedule rendering the item.
0090      */
0091     virtual void onDataChanged() = 0;
0092     void componentComplete() override;
0093 
0094 private:
0095     static void appendSource(DataSourcesProperty *list, ChartDataSource *source);
0096 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0097     static int sourceCount(DataSourcesProperty *list);
0098     static ChartDataSource *source(DataSourcesProperty *list, int index);
0099 #else
0100     static qsizetype sourceCount(DataSourcesProperty *list);
0101     static ChartDataSource *source(DataSourcesProperty *list, qsizetype index);
0102 #endif
0103     static void clearSources(DataSourcesProperty *list);
0104 
0105     ChartDataSource *m_nameSource = nullptr;
0106     ChartDataSource *m_shortNameSource = nullptr;
0107     ChartDataSource *m_colorSource = nullptr;
0108     QVector<ChartDataSource *> m_valueSources;
0109     IndexingMode m_indexingMode = IndexEachSource;
0110 };
0111 
0112 #endif // CHART_H