ViewSolutions 1.0.100.30ae2aa
View solutions of qml for quasarapp projects
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
24class __PrvateBaseListModel : public QAbstractListModel{
25 Q_OBJECT
26protected:
27 __PrvateBaseListModel(QObject* parent = nullptr): QAbstractListModel(parent){}
28signals:
30};
31
32
33#define BASE_LIST_MODEL_DATA_PROPERTY(Type) \
34Q_PROPERTY(QList<Type> listDate READ dateList WRITE setDataList NOTIFY listDateChanged FINAL)
35
36
85template<class TYPE>
87{
88
89public:
90 BaseListModel(QObject* parent = nullptr): __PrvateBaseListModel(parent) {
91
92 }
93
94 int rowCount(const QModelIndex &) const override {
95 return m_data.size();
96 }
97
98 QHash<int, QByteArray> roleNames() const override {
99 QHash<int, QByteArray> roles;
100 roles[Qt::EditRole] = "delegateModel";
101 return roles;
102 }
103
104 QVariant data(const QModelIndex &index, int role) const override {
105 if (index.isValid() &&
106 index.row() < m_data.size() &&
107 m_data.size() &&
108 role == Qt::EditRole) {
109
110 if constexpr (std::is_same_v<TYPE, QVariant>) {
111 return m_data[index.row()];
112 } else if constexpr(std::is_base_of_v<QObject, TYPE>) {
113 QQmlEngine::setObjectOwnership(m_data[index.row()], QQmlEngine::CppOwnership);
114
115 return QVariant::fromValue(m_data[index.row()]);
116
117 } else if constexpr (std::is_same_v<TYPE, QSharedPointer<QObject>> ||
118 std::is_base_of_v<QObject, typename TYPE::element_type>) {
119 QObject* ptr = m_data[index.row()].get();
120
121 QQmlEngine::setObjectOwnership(ptr, QQmlEngine::CppOwnership);
122 return QVariant::fromValue(ptr);
123 } else {
124 return QVariant::fromValue(m_data[index.row()]);
125
126 }
127 }
128
129 return {};
130 }
131
136 Q_INVOKABLE void setDataList(const QList<TYPE> &newData) {
137
138 const int diff = newData.size() - m_data.size();
139
140 if (diff > 0) {
141 beginInsertRows(QModelIndex{}, m_data.size(), m_data.size() + diff - 1);
142 m_data = newData;
143 endInsertRows();
144
145 emit dataChanged(index(0, 0),
146 index(m_data.size() - diff - 1, 0));
147 } else if (diff == 0) {
148 m_data = newData;
149
150 emit dataChanged(index(0, 0),
151 index(m_data.size() - diff - 1, 0));
152 } else {
153 beginRemoveRows(QModelIndex{}, m_data.size() + diff, m_data.size() - 1);
154 m_data = newData;
155
156 endRemoveRows();
157
158 emit dataChanged(index(0, 0),
159 index(m_data.size() - 1, 0));
160 }
161
162 emit listDateChanged();
163
164 }
165
166 Q_INVOKABLE const QList<TYPE>& dateList() const {
167 return m_data;
168 }
169
170private:
171 QList<TYPE> m_data;
172
173};
174
175
176
177}
178#endif // BASELISTMODEL_H
QHash< int, QByteArray > roleNames() 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)
int rowCount(const QModelIndex &) const override
__PrvateBaseListModel(QObject *parent=nullptr)
The ViewSolutions namespace.