ViewSolutions
Loading...
Searching...
No Matches
baselistmodel.h
Go to the documentation of this file.
1//#
2//# Copyright (C) 2020-2025 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
8#ifndef BASELISTMODEL_H
9#define BASELISTMODEL_H
10
11#include <QAbstractItemModel>
12#include <QQmlEngine>
13
14namespace ViewSolutions {
15
16class __PrvateBaseListModel : public QAbstractListModel{
17 Q_OBJECT
18protected:
19 __PrvateBaseListModel(QObject* parent = nullptr): QAbstractListModel(parent){}
20signals:
22};
23
24
25#define BASE_LIST_MODEL_DATA_PROPERTY(Type) \
26Q_PROPERTY(QList<Type> listDate READ dateList WRITE setDataList NOTIFY listDateChanged FINAL)
27
28
52template<class TYPE>
54{
55
56public:
57 BaseListModel(QObject* parent = nullptr): __PrvateBaseListModel(parent) {
58
59 }
60
61 int rowCount(const QModelIndex &parent) const override {
62 return m_data.size();
63 }
64
65 QHash<int, QByteArray> roleNames() const override {
66 QHash<int, QByteArray> roles;
67 roles[Qt::EditRole] = "delegateModel";
68 return roles;
69 }
70
71 QVariant data(const QModelIndex &index, int role) const override {
72 if (index.isValid() &&
73 index.row() < m_data.size() &&
74 m_data.size() &&
75 role == Qt::EditRole) {
76
77 if constexpr (std::is_same_v<TYPE, QVariant>) {
78 return m_data[index.row()];
79 } else if constexpr(std::is_base_of_v<QObject, TYPE>) {
80 QQmlEngine::setObjectOwnership(m_data[index.row()], QQmlEngine::CppOwnership);
81
82 return QVariant::fromValue(m_data[index.row()]);
83
84 } else if constexpr (std::is_same_v<TYPE, QSharedPointer<QObject>> ||
85 std::is_base_of_v<QObject, typename TYPE::element_type>) {
86 QObject* ptr = m_data[index.row()].get();
87
88 QQmlEngine::setObjectOwnership(ptr, QQmlEngine::CppOwnership);
89 return QVariant::fromValue(ptr);
90 } else {
91 return QVariant::fromValue(m_data[index.row()]);
92
93 }
94 }
95
96 return {};
97 }
98
103 Q_INVOKABLE void setDataList(const QList<TYPE> &newData) {
104
105 const int diff = newData.size() - m_data.size();
106
107 if (diff > 0) {
108 beginInsertRows(QModelIndex{}, m_data.size(), m_data.size() + diff - 1);
109 m_data = newData;
110 endInsertRows();
111
112 emit dataChanged(index(0, 0),
113 index(m_data.size() - diff - 1, 0));
114 } else if (diff == 0) {
115 m_data = newData;
116
117 emit dataChanged(index(0, 0),
118 index(m_data.size() - diff - 1, 0));
119 } else {
120 beginRemoveRows(QModelIndex{}, m_data.size() + diff, m_data.size() - 1);
121 m_data = newData;
122
123 endRemoveRows();
124
125 emit dataChanged(index(0, 0),
126 index(m_data.size() - 1, 0));
127 }
128
129 emit listDateChanged();
130
131 }
132
133 Q_INVOKABLE const QList<TYPE>& dateList() const {
134 return m_data;
135 }
136
137private:
138 QList<TYPE> m_data;
139
140};
141
142
143
144}
145#endif // BASELISTMODEL_H
The BaseListModel class is base class of all GUI list models.
QHash< int, QByteArray > roleNames() const override
int rowCount(const QModelIndex &parent) const override
Q_INVOKABLE const QList< TYPE > & dateList() const
Q_INVOKABLE void setDataList(const QList< TYPE > &newData)
setDataList sets new date of model, and reset all model;
QVariant data(const QModelIndex &index, int role) const override
BaseListModel(QObject *parent=nullptr)
__PrvateBaseListModel(QObject *parent=nullptr)
the ViewSolutions namespace