ViewSolutions 1.0.100.30ae2aa
View solutions of qml for quasarapp projects
Loading...
Searching...
No Matches
basehashmodel.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 BASEHASHMODEL_H
9#define BASEHASHMODEL_H
10#include <QAbstractItemModel>
11#include <QQmlEngine>
12
13namespace ViewSolutions {
14
57template<class KEY, class DATA>
58class BaseHashModel: public QAbstractListModel
59{
60
61public:
62 BaseHashModel(QObject* parent = nullptr): QAbstractListModel(parent) {
63
64 }
65
66 int rowCount(const QModelIndex &parent) const override {
67 return m_data.size();
68 }
69
70 QHash<int, QByteArray> roleNames() const override {
71 QHash<int, QByteArray> roles;
72 roles[Qt::EditRole] = "delegateModel";
73 return roles;
74 }
75
76 QVariant data(const QModelIndex &index, int role) const override {
77 if (index.isValid() && role == Qt::EditRole) {
78
79 auto iter = std::next(m_data.begin(), index.row());
80 if (iter == m_data.end()) {
81 return {};
82 }
83
84 if constexpr (std::is_same_v<DATA, QVariant>) {
85 return *iter;
86 } else if constexpr(std::is_base_of_v<QObject, DATA>) {
87 QQmlEngine::setObjectOwnership(*iter, QQmlEngine::CppOwnership);
88
89 return QVariant::fromValue(*iter);
90
91 } else if constexpr (std::is_same_v<DATA, QSharedPointer<QObject>> ||
92 std::is_base_of_v<QObject, typename DATA::element_type>) {
93 QObject* ptr = iter->get();
94
95 QQmlEngine::setObjectOwnership(ptr, QQmlEngine::CppOwnership);
96 return QVariant::fromValue(ptr);
97 } else {
98 return QVariant::fromValue(*iter);
99
100 }
101 }
102
103 return {};
104 }
105
111 virtual QVariant getByKey(const KEY& key) {
112 auto&& val = m_data.value(key);
113 if constexpr (std::is_same_v<DATA, QVariant>) {
114 return val;
115 } else if constexpr(std::is_base_of_v<QObject, DATA>) {
116 QQmlEngine::setObjectOwnership(val, QQmlEngine::CppOwnership);
117
118 return QVariant::fromValue(val);
119
120 } else if constexpr (std::is_base_of_v<QSharedPointer<QObject>, DATA>) {
121 QObject* ptr = val.get();
122
123 QQmlEngine::setObjectOwnership(ptr, QQmlEngine::CppOwnership);
124 return QVariant::fromValue(ptr);
125 } else {
126 return QVariant::fromValue(val);
127
128 }
129
130 return {};
131 }
132
133 const QHash<KEY, DATA>& dateList() const {
134 return m_data;
135 }
136
142 virtual void setByKey(const KEY& key, const DATA& data) {
143 auto&& iter = m_data.find(key);
144 if (iter != m_data.end() || *iter != data) {
145
146 int insertIdx = std::distance(m_data.begin(), m_data.insert(key, data));
147 emit dataChanged(index(insertIdx, 0),
148 index(insertIdx, 0));
149 }
150 }
151
152 virtual void removeByKey(const KEY& key) {
153 auto&& iter = m_data.find(key);
154 if (iter != m_data.end()) {
155
156 int removeIdx = std::distance(m_data.begin(), iter);
157 beginRemoveRows(QModelIndex{}, removeIdx, removeIdx);
158 m_data.erase(iter);
159 endRemoveRows();
160 }
161 }
162
167 void setDataList(const QHash<KEY, DATA> &newData) {
168
169 const int diff = newData.size() - m_data.size();
170
171 if (diff > 0) {
172 beginInsertRows(QModelIndex{}, m_data.size(), m_data.size() + diff - 1);
173 m_data = newData;
174 endInsertRows();
175
176 emit dataChanged(index(0, 0),
177 index(m_data.size() - diff - 1, 0));
178 } else if (diff == 0) {
179 m_data = newData;
180
181 emit dataChanged(index(0, 0),
182 index(m_data.size() - diff - 1, 0));
183 } else {
184 beginRemoveRows(QModelIndex{}, m_data.size() + diff, m_data.size() - 1);
185 m_data = newData;
186
187 endRemoveRows();
188
189 emit dataChanged(index(0, 0),
190 index(m_data.size() - 1, 0));
191 }
192 }
193
194private:
195 QHash<KEY, DATA> m_data;
196
197};
198
199
200
201}
202
203
204#endif // BASEHASHMODEL_H
int rowCount(const QModelIndex &parent) const override
QHash< int, QByteArray > roleNames() const override
virtual void removeByKey(const KEY &key)
QVariant data(const QModelIndex &index, int role) const override
BaseHashModel(QObject *parent=nullptr)
virtual QVariant getByKey(const KEY &key)
getByKey returns value by key
void setDataList(const QHash< KEY, DATA > &newData)
setDataList sets new date of model, and reset all model;
virtual void setByKey(const KEY &key, const DATA &data)
setByKey update delegate by key
const QHash< KEY, DATA > & dateList() const
The ViewSolutions namespace.