-
Notifications
You must be signed in to change notification settings - Fork 0
/
participantsmodel.cpp
170 lines (136 loc) · 5.74 KB
/
participantsmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/********************************************************************************************************************************************************
* @file participantsmodel.cpp
*
* @Copyright (C) 2022 i-trace.org
*
* This file is part of iTrace Infrastructure http://www.i-trace.org/.
* iTrace Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* iTrace Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with iTrace Infrastructure. If not, see <https://www.gnu.org/licenses/>.
********************************************************************************************************************************************************/
#include "participantsmodel.h"
#include "database.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
// ParticipantsList
ParticipantsList::ParticipantsList(QObject *parent) : QObject(parent) {}
QVector<Task> ParticipantsList::items() const {
return nTasks;
}
QVector<QString> ParticipantsList::getSelected() const {
QVector<QString> rtn;
for(auto i : items()) {
rtn.push_back(i.sessionID + " - " + (i.selected ? "1" : "0"));
}
return rtn;
}
bool ParticipantsList::setItemAt(int index, const Task& item) {
if(index < 0 || index >= nTasks.size())
return false; // If index is out of bounds return false to notify that nothing has changed
const Task& oldItem = nTasks.at(index);
if(item.selected == oldItem.selected && item.sessionID == oldItem.sessionID)
return false; // If modified item and old item are the same do nothing and return false
nTasks[index] = item;
return true;
}
void ParticipantsList::clearTasks() {
nTasks.clear();
}
void ParticipantsList::appendTask(const QString& sessionID) {
emit preItemAppended();
Task task;
task.selected = true;
task.sessionID = sessionID;
nTasks.append(task);
emit postItemAppended();
}
void ParticipantsList::removeTask(const QString &sessionId) {
for(int i = 0; i < nTasks.size();) {
if(nTasks.at(i).sessionID == sessionId) {
emit preItemRemoved(i);
nTasks.removeAt(i);
emit postItemRemoved();
return;
}
++i;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// ParticipantsModel
ParticipantsModel::ParticipantsModel(QObject *parent) : QAbstractListModel(parent), modelList(nullptr) {
}
int ParticipantsModel::rowCount(const QModelIndex &parent) const {
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid() || !modelList)
return 0;
return modelList->items().size();
}
QVariant ParticipantsModel::data(const QModelIndex &index, int role) const {
if (!index.isValid() || !modelList)
return QVariant();
const Task t = modelList->items().at(index.row());
switch (role) {
case DoneRole:
return QVariant(t.selected);
case DescriptionRole:
return QVariant(t.sessionID);
}
return QVariant();
}
bool ParticipantsModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if(!modelList)
return false;
Task t = modelList->items().at(index.row());
switch (role) {
case DoneRole:
t.selected = value.toBool();
break;
case DescriptionRole:
t.sessionID = value.toString();
break;
}
if (modelList->setItemAt(index.row(), t)) {
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags ParticipantsModel::flags(const QModelIndex &index) const {
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable;
}
QHash<int, QByteArray> ParticipantsModel::roleNames() const {
QHash<int, QByteArray> names;
names[DoneRole] = "done";
names[DescriptionRole] = "description";
return names;
}
ParticipantsList* ParticipantsModel::getModelList() const {
return modelList;
}
void ParticipantsModel::setModelList(ParticipantsList* list) {
beginResetModel(); // Must be called before the source of a model changes
if(modelList) {// If the model is connected to a list already
modelList->disconnect(this); // we disconnect it from the old list to prepare for the new one
}
//delete modelList;
modelList = list;
if(modelList) { // If a valid list was set
connect(modelList, &ParticipantsList::preItemAppended, this, [=]() { // Connects this lambda to the preItemAppendedSignal
const int index = modelList->items().size();
beginInsertRows(QModelIndex(), index, index); // 2nd and 3rd parameters tell which indicies to start and end insertion
// As insert is one beyond the current end of the list, we add to the end of the list
});
connect(modelList, &ParticipantsList::postItemAppended, this, [=]() {
endInsertRows();
});
connect(modelList, &ParticipantsList::preItemRemoved, this, [=](int index) {
beginRemoveRows(QModelIndex(), index, index);
});
connect(modelList, &ParticipantsList::postItemRemoved, this, [=]() {
endRemoveRows();
});
}
endResetModel();
}