File indexing completed on 2024-04-14 05:39:49

0001 /*
0002     Copyright © 2014-2017 Harald Sitter <sitter@kde.org>
0003     Copyright © 2016 David Rosca <nowrep@gmail.com>
0004 
0005     This library is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU Lesser General Public
0007     License as published by the Free Software Foundation; either
0008     version 2.1 of the License, or (at your option) version 3, or any
0009     later version accepted by the membership of KDE e.V. (or its
0010     successor approved by the membership of KDE e.V.), which shall
0011     act as a proxy defined in Section 6 of version 3 of the license.
0012 
0013     This library is distributed in the hope that it will be useful,
0014     but WITHOUT ANY WARRANTY; without even the implied warranty of
0015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0016     Lesser General Public License for more details.
0017 
0018     You should have received a copy of the GNU Lesser General Public
0019     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0020 */
0021 
0022 #ifndef ABSTRACTMODEL_H
0023 #define ABSTRACTMODEL_H
0024 
0025 #include <QAbstractListModel>
0026 
0027 class AbstractModel : public QAbstractListModel
0028 {
0029     Q_OBJECT
0030 public:
0031     enum ItemRole {
0032         ObjectRole = Qt::UserRole + 1
0033     };
0034 
0035     AbstractModel(QMap<QString, QObject*> *map, QObject *parent = nullptr);
0036 
0037     QHash<int, QByteArray> roleNames() const Q_DECL_FINAL;
0038     int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_FINAL;
0039     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
0040     // setData removed, PK is not writable. setData reference in plasma-pa repo.
0041 
0042     Q_INVOKABLE int role(const QByteArray &roleName) const;
0043 
0044 protected:
0045     void initRoleNames(const QMetaObject &qobjectMetaObject);
0046     void onDataAdded(int index);
0047     void onDataRemoved(int index);
0048 
0049 private slots:
0050     void propertyChanged();
0051 
0052 private:
0053     QMetaMethod propertyChangedMetaMethod() const;
0054 
0055     QHash<int, QByteArray> m_roles;
0056     QHash<int, int> m_objectProperties;
0057     QHash<int, int> m_signalIndexToProperties;
0058 
0059     QMap<QString, QObject*> *m_map;
0060 
0061 private:
0062     // Prevent leaf-classes from default constructing as we want to enforce
0063     // them passing us a context or explicit nullptrs.
0064     AbstractModel() {}
0065 };
0066 
0067 #endif // ABSTRACTMODEL_H