-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreemodel.cpp
More file actions
565 lines (487 loc) · 17 KB
/
treemodel.cpp
File metadata and controls
565 lines (487 loc) · 17 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// SPDX-FileCopyrightText: 2025 Marius Dege <marius.dege@basyskom.com>
// SPDX-FileCopyrightText: 2024 Marius Dege <marius.dege@basyskom.com>
// SPDX-FileCopyrightText: 2024 basysKom GmbH
// SPDX-License-Identifier: LGPL-3.0-or-later
// treemodel.cpp
#include "treemodel.h"
#include "Util/Utils.h"
TreeModel::TreeModel(QObject* parent)
: QAbstractItemModel(parent)
{
m_rootItem = std::make_shared<TreeItem>();
}
TreeModel::~TreeModel()
{
m_currentItem.reset();
m_rootItem.reset();
}
QModelIndex TreeModel::index(int row, int column, const QModelIndex& parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
TreeItem* parentItem;
if (!parent.isValid())
parentItem = m_rootItem.get();
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
TreeItem* childItem = parentItem->child(row).get();
if (childItem)
return createIndex(row, column, childItem);
return QModelIndex();
}
QModelIndex TreeModel::parent(const QModelIndex& index) const
{
if (!index.isValid())
return QModelIndex();
TreeItem* childItem = static_cast<TreeItem*>(index.internalPointer());
TreeItem* parentItem = childItem->parentItem().get();
if (parentItem == m_rootItem.get())
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int TreeModel::rowCount(const QModelIndex& parent) const
{
TreeItem* parentItem;
if (parent.column() > 0)
return 0;
if (!parent.isValid())
parentItem = m_rootItem.get();
else
parentItem = static_cast<TreeItem*>(parent.internalPointer());
return parentItem->childCount();
}
int TreeModel::columnCount(const QModelIndex& parent) const
{
return 1;
}
QVariant TreeModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
// TODO check if we need all those roles. If not, remove them and maybe go and remove them from the UANode as well.
switch (role) {
case Qt::DisplayRole:
return item->displayName();
case NodeIdRole:
return item->nodeId();
case BrowseNameRole:
return item->browseName();
case DisplayNameRole:
return item->displayName();
case DescriptionRole:
return item->description();
case ReferencesRole:
return QVariant::fromValue(item->references());
case ParentNodeIdRole:
return item->parentNodeId();
case DefinitionNameRole:
return item->definitionName();
case DefinitionFieldsRole:
return QVariant::fromValue(item->definitionFields());
case DataTypeRole:
return QVariant::fromValue(item->dataType());
case IsAbstractRole:
return item->isAbstract();
case ReferenceTypeRole:
return item->referenceType();
case TargetNodeIdRole:
return item->targetNodeId();
case IsForwardRole:
return item->isForward();
case ReferenceNodeRole:
return QVariant::fromValue(item->referenceNode());
case NamespaceStringRole:
return item->namespaceString();
case IsOptionalRole:
return item->isOptional();
case TypeNameRole:
return item->typeName();
case IsRootNodeRole:
return item->isRootNode();
case IsSelectedRole:
return item->isSelected();
case NodeIdVariableNameRole:
return item->nodeVariableName();
case UserInputMaskRole:
return item->userInputMask();
case IsParentSelectedRole:
return item->isParentSelected();
case ItemRole:
return QVariant::fromValue(item);
}
return QVariant();
}
bool TreeModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (!index.isValid())
return false;
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
switch (role) {
case Qt::DisplayRole:
item->setDisplayName(value.toString());
emit dataChanged(index, index, {role});
return true;
case NodeIdRole:
item->setNodeId(value.toString());
emit dataChanged(index, index, {role});
return true;
case BrowseNameRole:
item->setBrowseName(value.toString());
emit dataChanged(index, index, {role});
return true;
case DisplayNameRole:
item->setDisplayName(value.toString());
emit dataChanged(index, index, {role});
return true;
case DescriptionRole:
item->setDescription(value.toString());
emit dataChanged(index, index, {role});
return true;
case ParentNodeIdRole:
item->setParentNodeId(value.toString());
emit dataChanged(index, index, {role});
return true;
case DefinitionNameRole:
item->setDefinitionName(value.toString());
emit dataChanged(index, index, {role});
return true;
case DataTypeRole:
item->setDataType(value.value<UADataType>());
emit dataChanged(index, index, {role});
return true;
case IsAbstractRole:
item->setIsAbstract(value.toBool());
emit dataChanged(index, index, {role});
return true;
case ReferenceTypeRole:
item->setReferenceType(value.toString());
emit dataChanged(index, index, {role});
return true;
case TargetNodeIdRole:
item->setTargetNodeId(value.toString());
emit dataChanged(index, index, {role});
return true;
case IsForwardRole:
item->setIsForward(value.toBool());
emit dataChanged(index, index, {role});
return true;
case ReferenceNodeRole:
item->setReferenceNode(value.value<UANode*>());
emit dataChanged(index, index, {role});
return true;
case NamespaceStringRole:
item->setNamespaceString(value.toString());
emit dataChanged(index, index, {role});
return true;
case IsOptionalRole:
item->setIsOptional(value.toBool());
emit dataChanged(index, index, {role});
return true;
case TypeNameRole:
//
return true;
case IsRootNodeRole:
item->setIsRootNode(value.toBool());
emit dataChanged(index, index, {role});
return true;
case IsSelectedRole:
item->setSelected(value.toBool());
emit dataChanged(
index,
index.sibling(index.row(), columnCount() - 1),
{IsSelectedRole, IsParentSelectedRole});
// If the item has children, emit dataChanged for all descendants
if (rowCount(index) > 0) {
emit dataChanged(
getIndexFromItem(item->child(0).get()),
getIndexFromItem(item->child(rowCount(index) - 1).get()),
{IsSelectedRole, IsParentSelectedRole});
}
return true;
// TODO Do we allow this? Rightt now the varaible name depends on the browsename and nodeid
case NodeIdVariableNameRole:
qWarning() << "Setting the variable name is not supported right now!";
return false;
case IsParentSelectedRole:
item->setIsParentSelected(value.toBool());
emit dataChanged(index, index, {role});
}
return false;
}
std::shared_ptr<TreeItem> TreeModel::getCurrentItem() const
{
return m_currentItem;
}
void TreeModel::setCurrentItem(int indexInteger)
{
if (indexInteger < 0 || indexInteger >= m_rootItem->childCount()) {
qDebug() << "Index is out of range!";
return;
}
QModelIndex index = createIndex(indexInteger, 0, m_rootItem->child(indexInteger).get());
if (index.isValid()) {
TreeItem* item = static_cast<TreeItem*>(index.internalPointer());
if (item) {
m_currentItem = std::shared_ptr<TreeItem>(item);
return;
}
} else {
qDebug() << "Index is not valid!";
}
}
QModelIndex TreeModel::getIndexFromItem(TreeItem* item) const
{
return createIndex(item->row(), 0, item);
}
TreeItem* TreeModel::getItemFromIndex(const QModelIndex& index) const
{
if (!index.isValid())
return nullptr;
return static_cast<TreeItem*>(index.internalPointer());
}
int TreeModel::getRoleByName(const QString& roleName) const
{
auto roles = roleNames();
for (auto [key, value] : roles.asKeyValueRange()) {
if (value == roleName.toUtf8()) {
return key;
}
}
return -1;
}
std::shared_ptr<TreeItem> TreeModel::rootItem() const
{
return m_rootItem;
}
std::shared_ptr<TreeItem> TreeModel::getItemByName(const QString& name) const
{
auto found = findItemByBrowseNameRecursive(m_rootItem, name);
return found;
}
bool TreeModel::isBrowseNameUnique(const QString& name) const
{
return findBrowseNameRecursive(m_rootItem, name) == QStringLiteral("");
}
QString TreeModel::findBrowseNameRecursive(std::shared_ptr<TreeItem> parent, const QString& name) const
{
if (parent->getNode() != nullptr)
if (parent->browseName() == name)
return parent->browseName();
for (int i = 0; i < parent->childCount(); ++i) {
QString foundName = findBrowseNameRecursive(parent->child(i), name);
if (!foundName.isEmpty()) {
return foundName;
}
}
// Not found
return QStringLiteral("");
}
std::shared_ptr<TreeItem> TreeModel::findItemByBrowseNameRecursive(
std::shared_ptr<TreeItem> parent, const QString& name) const
{
if (parent->getNode() != nullptr)
if (parent->getNode()->browseName() == name)
return parent;
for (int i = 0; i < parent->childCount(); ++i) {
std::shared_ptr<TreeItem> foundItem = findItemByBrowseNameRecursive(parent->child(i), name);
if (foundItem != nullptr) {
return foundItem;
}
}
// Not found
return nullptr;
}
QHash<int, QByteArray> TreeModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NodeIdRole] = "nodeId";
roles[BrowseNameRole] = "browseName";
roles[DisplayNameRole] = "displayName";
roles[DescriptionRole] = "description";
roles[ReferencesRole] = "references";
roles[ParentNodeIdRole] = "parentNodeId";
roles[ParentNode] = "parentNode";
roles[DefinitionNameRole] = "definitionName";
roles[DefinitionFieldsRole] = "definitionFields";
roles[DataTypeRole] = "dataType";
roles[IsAbstractRole] = "isAbstract";
roles[ReferenceTypeRole] = "referenceType";
roles[TargetNodeIdRole] = "targetNodeId";
roles[IsForwardRole] = "isForward";
roles[ReferenceNodeRole] = "referenceNode";
roles[NamespaceStringRole] = "namespaceString";
roles[IsOptionalRole] = "isOptional";
roles[TypeNameRole] = "typeName";
roles[IsRootNodeRole] = "isRootNode";
roles[IsSelectedRole] = "isSelected";
roles[NodeIdVariableNameRole] = "name";
roles[UserInputMaskRole] = "userInputMask";
roles[IsParentSelectedRole] = "isParentSelected";
roles[ItemRole] = "item";
return roles;
}
void TreeModel::setupModelData(std::shared_ptr<UANodeSet> nodeSet)
{
beginResetModel();
m_rootItem.reset();
m_rootItem = std::make_shared<TreeItem>();
for (const auto& node : nodeSet->nodes()) {
// NOTE we want to skip the abstract nodes and only take ObjectType nodes
if (node->typeName() == XmlTags::UAObjectType) {
std::shared_ptr<UAObjectType> object = std::dynamic_pointer_cast<UAObjectType>(node);
if (object->isAbstract())
continue;
addRootNode(node, false, false);
}
}
endResetModel();
}
void TreeModel::addRootNode(
std::shared_ptr<UANode> node,
bool resolveSelection,
bool useUniqueBrowseNames,
bool safeOriginalBrowseName)
{
if (useUniqueBrowseNames) {
node->setBrowseName(makeBrowseNameUnique(node->browseName()));
if (safeOriginalBrowseName)
node->setUniqueBaseBrowseName(node->browseName());
}
node->setIsRootNode(true);
std::shared_ptr<TreeItem> rootItem = std::make_shared<TreeItem>(node, m_rootItem);
connect(rootItem.get(), &TreeItem::forceUpdate, this, &TreeModel::onForceUpdate);
m_rootItem->appendChild(rootItem);
addChildNodes(rootItem, useUniqueBrowseNames, safeOriginalBrowseName);
// Collect and add inherited nodes
QSet<std::shared_ptr<UANode>> inheritedNodes;
collectInheritedNodes(node, inheritedNodes);
for (const std::shared_ptr<UANode>& inheritedNode : std::as_const(inheritedNodes)) {
if (!inheritedNode)
continue;
for (const auto& reference : inheritedNode->references()) {
// FIXME we should allow to generate optional nodes but then there are duplicates...
if (isValidChildNode(reference) /* && !reference->node()->isOptional()*/) {
addNodeToTree(
rootItem, reference->node(), useUniqueBrowseNames, safeOriginalBrowseName);
}
}
}
if (resolveSelection) {
// Setting selected state for root node and children
rootItem->setSelected(true);
}
}
void TreeModel::addRootNodeToSelection(std::shared_ptr<UANode> node)
{
int newRowIndex = m_rootItem->childCount();
beginInsertRows(QModelIndex(), newRowIndex, newRowIndex);
addRootNode(node, true, true, true);
endInsertRows();
}
void TreeModel::removeRootNodeFromSelection(const int index)
{
if (index >= 0 && index < m_rootItem->childCount()) {
beginRemoveRows(QModelIndex(), index, index);
m_rootItem->removeChild(index);
endRemoveRows();
}
}
void TreeModel::resetModel()
{
beginResetModel();
m_rootItem = std::make_shared<TreeItem>();
m_currentItem.reset();
endResetModel();
}
void TreeModel::collectInheritedNodes(
std::shared_ptr<UANode> node, QSet<std::shared_ptr<UANode>>& nodes)
{
if (!node)
return;
for (const auto& reference : node->references()) {
if (shouldAddInheritedNode(reference)) {
std::shared_ptr<UANode> refNode = reference->node();
if (!nodes.contains(refNode)) {
nodes.insert(refNode);
collectInheritedNodes(refNode, nodes);
}
}
}
}
void TreeModel::addChildNodes(
std::shared_ptr<TreeItem> parent, bool useUniqueBrowseNames, bool safeOriginalBrowseName)
{
if (!parent)
return;
auto node = parent->getNode();
if (!node)
return;
static QSet<QString> visitedNodes;
QString nodeId = node->nodeId();
if (visitedNodes.contains(nodeId))
return;
visitedNodes.insert(nodeId);
for (const auto& reference : node->references()) {
if (isValidChildNode(reference) && reference->isForward()) {
addNodeToTree(parent, reference->node(), useUniqueBrowseNames, safeOriginalBrowseName);
}
}
visitedNodes.remove(nodeId);
}
bool TreeModel::isValidChildNode(const std::shared_ptr<Reference> reference) const
{
if (!reference->node())
return false;
const QString& typeName = reference->node()->typeName();
if (typeName != XmlTags::UAObject && typeName != XmlTags::UAMethod
&& typeName != XmlTags::UAVariable)
return false;
const QString& browseName = reference->node()->browseName();
if (browseName.contains(XmlTags::Mandatory) || browseName.contains(XmlTags::Optional)
|| browseName.contains(XmlTags::Arguments))
return false;
return true;
}
bool TreeModel::shouldAddInheritedNode(const std::shared_ptr<Reference> reference) const
{
if (reference->isForward())
return false;
const QString& refType = reference->referenceType();
return (refType == XmlTags::HasSubtype || refType == XmlTags::HasTypeDefinition);
}
void TreeModel::addNodeToTree(
std::shared_ptr<TreeItem> parentItem,
std::shared_ptr<UANode> childNode,
bool useUniqueBrowseNames,
bool safeOriginalBrowseName)
{
if (!parentItem || !childNode)
return;
auto childNodeCopy = childNode->clone();
if (useUniqueBrowseNames)
childNodeCopy->setBrowseName(makeBrowseNameUnique(childNodeCopy->browseName()));
if (safeOriginalBrowseName)
childNodeCopy->setUniqueBaseBrowseName(childNodeCopy->browseName());
auto parentNamespaceMap = Utils::instance()->currentNameSpaceMaps().value(
parentItem->namespaceString());
if (parentItem->namespaceString() != childNodeCopy->namespaceString()) {
childNodeCopy->changeNamespaceId(parentNamespaceMap.key(childNodeCopy->namespaceString()));
}
std::shared_ptr<TreeItem> childItem = std::make_shared<TreeItem>(childNodeCopy, parentItem);
connect(childItem.get(), &TreeItem::forceUpdate, this, &TreeModel::onForceUpdate);
parentItem->appendChild(childItem);
addChildNodes(childItem, useUniqueBrowseNames, safeOriginalBrowseName);
}
QString TreeModel::makeBrowseNameUnique(const QString& browseName) const
{
QString baseBrowseName = browseName;
QString currentBrowseName = baseBrowseName;
int browseNameSuffix = 1;
while (!isBrowseNameUnique(currentBrowseName)) {
currentBrowseName = baseBrowseName + QStringLiteral("_")
+ QString::number(browseNameSuffix);
browseNameSuffix++;
}
return currentBrowseName;
}