ViewSolutions
Loading...
Searching...
No Matches
listviewmodel.cpp
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#include "listviewmodel.h"
9namespace ViewSolutions {
10
12 QAbstractListModel(ptr){
13
14}
15
19
20int ListViewModel::rowCount(const QModelIndex &) const {
21 if (external())
22 return _externalData->size();
23
24 return _data.size();
25}
26
27QVariant ListViewModel::data(const QModelIndex &index, int role) const {
28
29 if (index.row() < rowCount(index)) {
30 if (static_cast<ListViewModelRoles>(role) == ListViewModelRoles::ModelData) {
31 QObject *item = (external())? _externalData->value(index.row()) : _data.value(index.row());
32 return QVariant::fromValue(item);
33 }
34 }
35
36 return {};
37}
38
39QHash<int, QByteArray> ListViewModel::roleNames() const {
40 QHash<int, QByteArray> roles;
41 roles[static_cast<int>(ListViewModelRoles::ModelData)] = "modelData";
42
43 return roles;
44}
45
46void ListViewModel::setExternalSource(const QList<QObject *> *newData) {
47 if (external()) {
48 beginResetModel();
49 _externalData = newData;
50 endResetModel();
51 } else {
52 _externalData = newData;
53 }
54
55 setExternal(true);
56
57}
58
59void ListViewModel::setSource(const QList<QObject*> &newData) {
60
61 if (!external()) {
62 beginResetModel();
63
64 clear(true);
65 _data = newData;
66
67 endResetModel();
68 } else {
69 clear(true);
70 _data = newData;
71 }
72
73 setExternal(false);
74}
75
76void ListViewModel::addSource(QObject* data) {
77 if (!external()) {
78 beginInsertRows(QModelIndex(), rowCount(), rowCount());
79 _data.push_back(data);
80 endInsertRows();
81 } else {
82 _data.push_back(data);
83 }
84}
85
86void ListViewModel::clear(bool fast) {
87 for (auto i : std::as_const(_data)) {
88 if (fast) {
89 i->deleteLater();
90 } else {
91 delete i;
92 }
93 }
94
95 _data.clear();
96}
97
99 return _external;
100}
101
102void ListViewModel::setExternal(bool external) {
103 if (external != _external) {
104 beginResetModel();
105 _external = external;
106 endResetModel();
107 }
108}
109
110}
QHash< int, QByteArray > roleNames() const
void addSource(QObject *data)
void clear(bool fast=false)
QVariant data(const QModelIndex &index, int role) const
void setExternalSource(const QList< QObject * > *newData)
ListViewModel(QObject *ptr=nullptr)
int rowCount(const QModelIndex &parent=QModelIndex()) const
void setSource(const QList< QObject * > &newData)
the ViewSolutions namespace