File indexing completed on 2024-04-28 03:56:45
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 LEGENDMODEL_H 0009 #define LEGENDMODEL_H 0010 0011 #include <vector> 0012 0013 #include <QAbstractListModel> 0014 #include <QColor> 0015 #include <qqmlregistration.h> 0016 0017 class Chart; 0018 class ChartDataSource; 0019 0020 struct LegendItem { 0021 QString name; 0022 QString shortName; 0023 QColor color; 0024 QVariant value; 0025 }; 0026 0027 /** 0028 * A model that extracts information from a chart that can be displayed as a legend. 0029 */ 0030 class LegendModel : public QAbstractListModel 0031 { 0032 Q_OBJECT 0033 QML_ELEMENT 0034 0035 public: 0036 enum Roles { NameRole = Qt::UserRole, ShortNameRole, ColorRole, ValueRole }; 0037 0038 enum SourceIndex { UseSourceCount = -2 }; 0039 Q_ENUM(SourceIndex) 0040 0041 explicit LegendModel(QObject *parent = nullptr); 0042 0043 Q_PROPERTY(Chart *chart READ chart WRITE setChart NOTIFY chartChanged) 0044 Chart *chart() const; 0045 void setChart(Chart *newChart); 0046 Q_SIGNAL void chartChanged(); 0047 0048 Q_PROPERTY(int sourceIndex READ sourceIndex WRITE setSourceIndex NOTIFY sourceIndexChanged) 0049 int sourceIndex() const; 0050 void setSourceIndex(int index); 0051 Q_SIGNAL void sourceIndexChanged(); 0052 0053 QHash<int, QByteArray> roleNames() const override; 0054 int rowCount(const QModelIndex &parent) const override; 0055 QVariant data(const QModelIndex &index, int role) const override; 0056 0057 private: 0058 void queueUpdate(); 0059 void queueDataChange(); 0060 void update(); 0061 void updateData(); 0062 int countItems(); 0063 QVariant getValueForItem(int item); 0064 0065 Chart *m_chart = nullptr; 0066 int m_sourceIndex = UseSourceCount; 0067 bool m_updateQueued = false; 0068 bool m_dataChangeQueued = false; 0069 std::vector<QMetaObject::Connection> m_connections; 0070 std::vector<LegendItem> m_items; 0071 }; 0072 0073 #endif // LEGENDMODEL_H