-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
qtozwassociationmodel_p.cpp
170 lines (156 loc) · 7.18 KB
/
qtozwassociationmodel_p.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
//-----------------------------------------------------------------------------
//
// qtozwassocationmodel_p.cpp
//
// Association Groups Model - Used Internally to Manage the Model
//
// Copyright (c) 2019 Justin Hammond <Justin@dynam.ac>
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of QT-OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include <QBitArray>
#include "qtozw_logging.h"
#include "qtozwassociationmodel_p.h"
QTOZW_Associations_internal::QTOZW_Associations_internal(QObject *parent)
: QTOZW_Associations(parent)
{
}
void QTOZW_Associations_internal::addGroup(quint8 _nodeID, quint8 _groupIDX) {
if (getassocationRow(_nodeID, _groupIDX) != -1) {
qCWarning(associationModel) << "addGroup: Group " << _groupIDX << "already exists on node " << _nodeID;
return;
}
QMap<associationColumns, QVariant> newNode;
newNode[associationColumns::NodeID] = _nodeID;
newNode[associationColumns::GroupID] = _groupIDX;
newNode[associationColumns::Members] = QStringList();
QBitArray flags(static_cast<int>(associationFlags::FlagCount));
newNode[associationColumns::Flags] = flags;
this->beginInsertRows(QModelIndex(), this->rowCount(QModelIndex()), this->rowCount(QModelIndex()));
this->m_associationData[this->rowCount(QModelIndex())] = newNode;
this->endInsertRows();
return;
}
void QTOZW_Associations_internal::setGroupData(quint8 _nodeID, quint8 _groupIDX, QTOZW_Associations::associationColumns column, QVariant data) {
switch (column) {
case NodeID:
case GroupID:
case Members:
case Flags:
{
qCWarning(associationModel) << "Can Not Set Group Data for " << static_cast<associationColumns>(column);
return;
}
default:
break;
}
qint32 row = getassocationRow(_nodeID, _groupIDX);
if (row == -1) {
qCWarning(associationModel) << "setGroupData: Can't Find Group " << _groupIDX << "on node " << _nodeID;
return;
}
this->m_associationData[row][column] = data;
QVector<int> roles;
roles << Qt::DisplayRole;
this->dataChanged(this->createIndex(row, column), this->createIndex(row, column), roles);
}
void QTOZW_Associations_internal::setGroupFlags(quint8 _nodeID, quint8 _groupIDX, QTOZW_Associations::associationFlags _flags, bool _value) {
qint32 row = getassocationRow(_nodeID, _groupIDX);
if (row == -1) {
qCWarning(associationModel) << "setGroupFlags: Can't Find Group " << _groupIDX << "on node " << _nodeID;
return;
}
QBitArray flags(static_cast<int>(associationFlags::FlagCount));
flags = this->m_associationData[row][associationColumns::Flags].toBitArray();
flags.setBit(_flags, _value);
this->m_associationData[row][associationColumns::Flags] = flags;
QVector<int> roles;
roles << Qt::DisplayRole;
this->dataChanged(this->createIndex(row, associationColumns::Flags), this->createIndex(row, associationColumns::Flags), roles);
};
void QTOZW_Associations_internal::delNode(quint8 _nodeID) {
QMap<qint32, QMap<QTOZW_Associations::associationColumns, QVariant> >::Iterator it;
for (it = this->m_associationData.begin(); it != this->m_associationData.end();) {
if (it.value()[associationColumns::NodeID] == _nodeID) {
qCDebug(associationModel) << "Removing Node " << it.value()[associationColumns::NodeID] << it.key();
this->beginRemoveRows(QModelIndex(), it.key(), it.key());
it = this->m_associationData.erase(it);
this->endRemoveRows();
} else {
it++;
}
}
}
void QTOZW_Associations_internal::addAssociation(quint8 _nodeID, quint8 _groupIDX, quint8 _targetNode, quint8 _targetInstance) {
qint32 row = getassocationRow(_nodeID, _groupIDX);
if (row == -1) {
qCWarning(associationModel) << "addAssciation: Can't Find Group " << _groupIDX << "for node " << _nodeID;
return;
}
QString target;
target.append(QString::number(_targetNode)).append(".").append(QString::number(_targetInstance));
QStringList targetlist = this->m_associationData[row][associationColumns::Members].toStringList();
if (targetlist.contains(target)) {
qCWarning(associationModel) << "addAssociation: Target " << target << "already exists in Group " << _groupIDX << "for node " << _nodeID;
return;
}
targetlist << target;
this->m_associationData[row][associationColumns::Members] = targetlist;
QVector<int> roles;
roles << Qt::DisplayRole;
this->dataChanged(this->createIndex(row, associationColumns::Members), this->createIndex(row, associationColumns::Members), roles);
}
void QTOZW_Associations_internal::delAssociation(quint8 _nodeID, quint8 _groupIDX, quint8 _targetNode, quint8 _targetInstance) {
qint32 row = getassocationRow(_nodeID, _groupIDX);
if (row == -1) {
qCWarning(associationModel) << "delAssciation: Can't Find Group " << _groupIDX << "for node " << _nodeID;
return;
}
QString target;
target.append(QString::number(_targetNode)).append(".").append(QString::number(_targetInstance));
QStringList targetlist = this->m_associationData[row][associationColumns::Members].toStringList();
if (!targetlist.contains(target)) {
qCWarning(associationModel) << "delAssociation: Target " << target << "does not exist in Group " << _groupIDX << "for node " << _nodeID;
return;
}
targetlist.removeAll(target);
this->m_associationData[row][associationColumns::Members] = targetlist;
QVector<int> roles;
roles << Qt::DisplayRole;
this->dataChanged(this->createIndex(row, associationColumns::Members), this->createIndex(row, associationColumns::Members), roles);
}
bool QTOZW_Associations_internal::findAssociation(quint8 _nodeID, quint8 _groupIDX, quint8 _targetNode, quint8 _targetInstance) {
qint32 row = getassocationRow(_nodeID, _groupIDX);
if (row == -1) {
qCWarning(associationModel) << "delAssciation: Can't Find Group " << _groupIDX << "for node " << _nodeID;
return false;
}
QString target;
target.append(QString::number(_targetNode)).append(".").append(QString::number(_targetInstance));
QStringList targetlist = this->m_associationData[row][associationColumns::Members].toStringList();
if (!targetlist.contains(target)) {
return false;
}
return true;
}
void QTOZW_Associations_internal::resetModel() {
this->beginRemoveRows(QModelIndex(), 0, this->m_associationData.count());
this->m_associationData.clear();
this->endRemoveRows();
}