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#ifndef LEGENDMODEL_H
9#define LEGENDMODEL_H
10
11#include <vector>
12
13#include <QAbstractListModel>
14#include <QColor>
15#include <qqmlregistration.h>
16
17class Chart;
18class ChartDataSource;
19
20struct LegendItem {
21 QString name;
22 QString shortName;
23 QColor color;
24 QVariant value;
25};
26
27/*!
28 * \qmltype LegendModel
29 * \inqmlmodule org.kde.quickcharts.controls
30 * \brief A model that extracts information from a chart that can be displayed as a legend.
31 */
32class LegendModel : public QAbstractListModel
33{
34 Q_OBJECT
35 QML_ELEMENT
36
37public:
38 enum Roles { NameRole = Qt::UserRole, ShortNameRole, ColorRole, ValueRole };
39
40 enum SourceIndex { UseSourceCount = -2 };
41 Q_ENUM(SourceIndex)
42
43 explicit LegendModel(QObject *parent = nullptr);
44
45 /*!
46 * \qmlproperty Chart LegendModel::chart
47 */
48 Q_PROPERTY(Chart *chart READ chart WRITE setChart NOTIFY chartChanged)
49 Chart *chart() const;
50 void setChart(Chart *newChart);
51 Q_SIGNAL void chartChanged();
52
53 /*!
54 * \qmlproperty int LegendModel::sourceIndex
55 */
56 Q_PROPERTY(int sourceIndex READ sourceIndex WRITE setSourceIndex NOTIFY sourceIndexChanged)
57 int sourceIndex() const;
58 void setSourceIndex(int index);
59 Q_SIGNAL void sourceIndexChanged();
60
61 QHash<int, QByteArray> roleNames() const override;
62 int rowCount(const QModelIndex &parent) const override;
63 QVariant data(const QModelIndex &index, int role) const override;
64
65private:
66 void queueUpdate();
67 void queueDataChange();
68 void update();
69 void updateData();
70 int countItems();
71 QVariant getValueForItem(int item);
72 void onChartDestroyed();
73
74 Chart *m_chart = nullptr;
75 int m_sourceIndex = UseSourceCount;
76 bool m_updateQueued = false;
77 bool m_dataChangeQueued = false;
78 std::vector<QMetaObject::Connection> m_connections;
79 std::vector<LegendItem> m_items;
80};
81
82#endif // LEGENDMODEL_H
83

source code of kquickcharts/controls/LegendModel.h