File indexing completed on 2024-04-28 03:56:47

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