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 * A model that extracts information from a chart that can be displayed as a legend.
29 */
30class LegendModel : public QAbstractListModel
31{
32 Q_OBJECT
33 QML_ELEMENT
34
35public:
36 enum Roles { NameRole = Qt::UserRole, ShortNameRole, ColorRole, ValueRole };
37
38 enum SourceIndex { UseSourceCount = -2 };
39 Q_ENUM(SourceIndex)
40
41 explicit LegendModel(QObject *parent = nullptr);
42
43 Q_PROPERTY(Chart *chart READ chart WRITE setChart NOTIFY chartChanged)
44 Chart *chart() const;
45 void setChart(Chart *newChart);
46 Q_SIGNAL void chartChanged();
47
48 Q_PROPERTY(int sourceIndex READ sourceIndex WRITE setSourceIndex NOTIFY sourceIndexChanged)
49 int sourceIndex() const;
50 void setSourceIndex(int index);
51 Q_SIGNAL void sourceIndexChanged();
52
53 QHash<int, QByteArray> roleNames() const override;
54 int rowCount(const QModelIndex &parent) const override;
55 QVariant data(const QModelIndex &index, int role) const override;
56
57private:
58 void queueUpdate();
59 void queueDataChange();
60 void update();
61 void updateData();
62 int countItems();
63 QVariant getValueForItem(int item);
64
65 Chart *m_chart = nullptr;
66 int m_sourceIndex = UseSourceCount;
67 bool m_updateQueued = false;
68 bool m_dataChangeQueued = false;
69 std::vector<QMetaObject::Connection> m_connections;
70 std::vector<LegendItem> m_items;
71};
72
73#endif // LEGENDMODEL_H
74

source code of kquickcharts/controls/LegendModel.h