-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeviceInterfacesModel.cpp
executable file
·202 lines (170 loc) · 5.3 KB
/
DeviceInterfacesModel.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "DeviceInterfacesModel.h"
DeviceInterfacesModel::DeviceInterfacesModel(QObject *parent) : QAbstractItemModel(parent)
{
}
QVariant DeviceInterfacesModel::data(const QModelIndex& index, int role) const
{
// The (invisible) root node. This should never happen.
if (!index.isValid())
return QVariant();
switch (role)
{
case Qt::DisplayRole:
{
quintptr id = index.internalId();
if (!nodes.contains(id))
return "";
switch (nodes[id].data.type)
{
case NodeType::Device:
return "Device";
case NodeType::EndpointZero:
return "Endpoint 0";
case NodeType::Configuration:
{
int c = nodes[id].data.configuration;
if (c < 0 || c >= descriptor.configurations.size())
return "";
return "bConfigurationValue " + QString::number(descriptor.configurations[c].bConfigurationValue);
}
case NodeType::Interface:
{
int c = nodes[id].data.configuration;
if (c < 0 || c >= descriptor.configurations.size())
return "";
int i = nodes[id].data.interface_;
if (i < 0 || i >= descriptor.configurations[c].interfaces.size())
return "";
return "bInterfaceNumber " + QString::number(descriptor.configurations[c].interfaces[i].bInterfaceNumber);
}
case NodeType::Endpoint:
{
int c = nodes[id].data.configuration;
if (c < 0 || c >= descriptor.configurations.size())
return "";
int i = nodes[id].data.interface_;
if (i < 0 || i >= descriptor.configurations[c].interfaces.size())
return "";
int e = nodes[id].data.endpoint;
if (e < 0 || e >= descriptor.configurations[c].interfaces[i].endpoints.size())
return "";
return "bEndpointAddress " + QString::number(descriptor.configurations[c].interfaces[i].endpoints[e].bEndpointAddress);
}
default:
return "";
}
break;
}
case Qt::UserRole:
{
quintptr id = index.internalId();
if (!nodes.contains(id))
return QVariant();
return QVariant::fromValue(nodes[id].data);
}
default:
break;
}
return QVariant();
}
Qt::ItemFlags DeviceInterfacesModel::flags(const QModelIndex& index) const
{
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
QVariant DeviceInterfacesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return QVariant();
}
QModelIndex DeviceInterfacesModel::index(int row, int column, const QModelIndex& parent) const
{
// This checks the row and column against rowCount/columnCount.
if (!hasIndex(row, column, parent))
return QModelIndex();
// Must be the root device node, which always has an index of 1.
if (!parent.isValid())
return createIndex(row, column, 1);
quintptr id = parent.internalId();
if (!nodes.contains(id))
return QModelIndex();
// We need to return the index of the child item of `parent` at the given row/column.
if (row < 0 || row >= nodes[id].children.size())
return QModelIndex();
return createIndex(row, column, nodes[id].children[row]);
}
QModelIndex DeviceInterfacesModel::parent(const QModelIndex& index) const
{
if (!index.isValid())
return QModelIndex();
quintptr id = index.internalId();
if (!nodes.contains(id))
return QModelIndex();
// Get the row.
quintptr parentId = nodes[id].parent;
if (!nodes.contains(parentId))
return QModelIndex();
for (int r = 0; r < nodes[parentId].children.size(); ++r)
if (nodes[parentId].children[r] == id)
return createIndex(r, 0, parentId);
return QModelIndex();
}
int DeviceInterfacesModel::rowCount(const QModelIndex& parent) const
{
if (parent.column() > 0)
return 0;
// The top level item. We have one device.
if (!parent.isValid())
return 1;
quintptr id = parent.internalId();
if (!nodes.contains(id))
return 0;
return nodes[id].children.size();
}
int DeviceInterfacesModel::columnCount(const QModelIndex& parent) const
{
return 1;
}
void DeviceInterfacesModel::setDescriptors(DeviceDescriptor d)
{
beginResetModel();
descriptor = d;
nodes.clear();
// 0 is like a null pointer. If QModelIndex::internalId() is 0 then QModelIndex::isValid() returns false.
quintptr idx = 1;
// Populate the indices.
quintptr deviceId = idx++;
nodes[deviceId].parent = 0;
nodes[deviceId].data.type = NodeType::Device;
// Add the virtual endpoint 0 node.
quintptr ep0Id = idx++;
nodes[ep0Id].parent = deviceId;
nodes[ep0Id].data.type = NodeType::EndpointZero;
nodes[deviceId].children.append(ep0Id);
for (int c = 0; c < descriptor.configurations.size(); ++c)
{
quintptr configId = idx++;
nodes[configId].data.configuration = c;
nodes[configId].parent = deviceId;
nodes[configId].data.type = NodeType::Configuration;
nodes[deviceId].children.append(configId);
for (int i = 0; i < descriptor.configurations[c].interfaces.size(); ++i)
{
quintptr interfaceId = idx++;
nodes[interfaceId].data.configuration = c;
nodes[interfaceId].data.interface_ = i;
nodes[interfaceId].parent = configId;
nodes[interfaceId].data.type = NodeType::Interface;
nodes[configId].children.append(interfaceId);
for (int e = 0; e < descriptor.configurations[c].interfaces[i].endpoints.size(); ++e)
{
quintptr endpointId = idx++;
nodes[endpointId].data.configuration = c;
nodes[endpointId].data.interface_ = i;
nodes[endpointId].data.endpoint = e;
nodes[endpointId].parent = interfaceId;
nodes[endpointId].data.type = NodeType::Endpoint;
nodes[interfaceId].children.append(endpointId);
}
}
}
endResetModel();
}