ViewSolutions
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
41template<class KEY, class DATA>
42class BaseHashModel: public QAbstractListModel
43{
44
45public:
46 BaseHashModel(QObject* parent = nullptr): QAbstractListModel(parent) {
47
48 }
49
50 int rowCount(const QModelIndex &parent) const override {
51 return m_data.size();
52 }
53
54 QHash<int, QByteArray> roleNames() const override {
55 QHash<int, QByteArray> roles;
56 roles[Qt::EditRole] = "delegateModel";
57 return roles;
58 }
59
60 QVariant data(const QModelIndex &index, int role) const override {
61 if (index.isValid() && role == Qt::EditRole) {
62
63 auto iter = std::next(m_data.begin(), index.row());
64 if (iter == m_data.end()) {
65 return {};
66 }
67
68 if constexpr (std::is_same_v<DATA, QVariant>) {
69 return *iter;
70 } else if constexpr(std::is_base_of_v<QObject, DATA>) {
71 QQmlEngine::setObjectOwnership(*iter, QQmlEngine::CppOwnership);
72
73 return QVariant::fromValue(*iter);
74
75 } else if constexpr (std::is_same_v<DATA, QSharedPointer<QObject>> ||
76 std::is_base_of_v<QObject, typename DATA::element_type>) {
77 QObject* ptr = iter->get();
78
79 QQmlEngine::setObjectOwnership(ptr, QQmlEngine::CppOwnership);
80 return QVariant::fromValue(ptr);
81 } else {
82 return QVariant::fromValue(*iter);
83
84 }
85 }
86
87 return {};
88 }
89
95 virtual QVariant getByKey(const KEY& key) {
96 auto&& val = m_data.value(key);
97 if constexpr (std::is_same_v<DATA, QVariant>) {
98 return val;
99 } else if constexpr(std::is_base_of_v<QObject, DATA>) {
100 QQmlEngine::setObjectOwnership(val, QQmlEngine::CppOwnership);
101
102 return QVariant::fromValue(val);
103
104 } else if constexpr (std::is_base_of_v<QSharedPointer<QObject>, DATA>) {
105 QObject* ptr = val.get();
106
107 QQmlEngine::setObjectOwnership(ptr, QQmlEngine::CppOwnership);
108 return QVariant::fromValue(ptr);
109 } else {
110 return QVariant::fromValue(val);
111
112 }
113
114 return {};
115 }
116
117 const QHash<KEY, DATA>& dateList() const {
118 return m_data;
119 }
120
126 virtual void setByKey(const KEY& key, const DATA& data) {
127 auto&& iter = m_data.find(key);
128 if (iter != m_data.end() || *iter != data) {
129
130 int insertIdx = std::distance(m_data.begin(), m_data.insert(key, data));
131 emit dataChanged(index(insertIdx, 0),
132 index(insertIdx, 0));
133 }
134 }
135
136 virtual void removeByKey(const KEY& key) {
137 auto&& iter = m_data.find(key);
138 if (iter != m_data.end()) {
139
140 int removeIdx = std::distance(m_data.begin(), iter);
141 beginRemoveRows(QModelIndex{}, removeIdx, removeIdx);
142 m_data.erase(iter);
143 endRemoveRows();
144 }
145 }
146
151 void setDataList(const QHash<KEY, DATA> &newData) {
152
153 const int diff = newData.size() - m_data.size();
154
155 if (diff > 0) {
156 beginInsertRows(QModelIndex{}, m_data.size(), m_data.size() + diff - 1);
157 m_data = newData;
158 endInsertRows();
159
160 emit dataChanged(index(0, 0),
161 index(m_data.size() - diff - 1, 0));
162 } else if (diff == 0) {
163 m_data = newData;
164
165 emit dataChanged(index(0, 0),
166 index(m_data.size() - diff - 1, 0));
167 } else {
168 beginRemoveRows(QModelIndex{}, m_data.size() + diff, m_data.size() - 1);
169 m_data = newData;
170
171 endRemoveRows();
172
173 emit dataChanged(index(0, 0),
174 index(m_data.size() - 1, 0));
175 }
176 }
177
178private:
179 QHash<KEY, DATA> m_data;
180
181};
182
183
184
185}
186
187
188#endif // BASEHASHMODEL_H
The BaseHashModel class is base class of all GUI list models.
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