-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremotecall_model.cpp
124 lines (97 loc) · 3.45 KB
/
remotecall_model.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
/*
* This file is part of the ct-Bot remote viewer tool.
* Copyright (c) 2020-2022 Timo Sandmann
*
* This program 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, version 3.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file remotecall_model.cpp
* @brief QML model for remotecall viewer
* @author Timo Sandmann
* @date 27.01.2020
*/
#include <QDebug>
#include "remotecall_model.h"
RCModel::RCModel(QObject* parent) : QAbstractListModel { parent }, list_ {} {}
int RCModel::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() || !list_) {
return 0;
}
return list_->items().size();
}
QVariant RCModel::data(const QModelIndex& index, int role) const {
if (!index.isValid() || !list_) {
return QVariant {};
}
const RCItem item { list_->items().at(index.row()) };
switch (role) {
case Name: return QVariant(item.name);
case Parameter1: return QVariant(item.parameter1);
case Parameter2: return QVariant(item.parameter2);
case Parameter3: return QVariant(item.parameter3);
}
return QVariant {};
}
bool RCModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (!list_) {
return false;
}
RCItem item { list_->items().at(index.row()) };
// qDebug() << "ValueModel::setData(): item={\"" << item.name << "\", " << item.value << "}";
switch (role) {
case Name: item.name = value.toString(); break;
case Parameter1: item.parameter1 = value.toString(); break;
case Parameter2: item.parameter2 = value.toString(); break;
case Parameter3: item.parameter3 = value.toString(); break;
}
if (list_->setItemAt(index.row(), item)) {
// qDebug() << "ValueModel::setData(): item={" << item.name << ", " << item.value << "}";
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return false;
}
Qt::ItemFlags RCModel::flags(const QModelIndex& index) const {
if (!index.isValid()) {
return Qt::NoItemFlags;
}
return Qt::ItemIsEnabled;
}
QHash<int, QByteArray> RCModel::roleNames() const {
QHash<int, QByteArray> names;
names[Name] = "name";
names[Parameter1] = "parameter1";
names[Parameter2] = "parameter2";
names[Parameter3] = "parameter3";
return names;
}
RCList* RCModel::list() const {
return list_;
}
void RCModel::setList(RCList* list) {
beginResetModel();
if (list_) {
list_->disconnect(this);
}
list_ = list;
if (list_) {
connect(list_, &RCList::preItemAppended, this, [=]() {
const int index = list_->items().size();
beginInsertRows(QModelIndex(), index, index);
});
connect(list_, &RCList::postItemAppended, this, [=]() { endInsertRows(); });
}
endResetModel();
}