1 | // Copyright (C) 2022 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "nodesmodel.h" |
5 | |
6 | NodesModel::NodesModel(QObject *parent) |
7 | : QAbstractListModel(parent) |
8 | { |
9 | connect(sender: this, signal: &QAbstractListModel::rowsInserted, context: this, slot: &NodesModel::rowCountChanged); |
10 | connect(sender: this, signal: &QAbstractListModel::rowsRemoved, context: this, slot: &NodesModel::rowCountChanged); |
11 | connect(sender: this, signal: &QAbstractListModel::modelReset, context: this, slot: &NodesModel::rowCountChanged); |
12 | } |
13 | |
14 | int NodesModel::rowCount(const QModelIndex &) const |
15 | { |
16 | return m_nodesList.size(); |
17 | } |
18 | |
19 | QHash<int, QByteArray> NodesModel::roleNames() const |
20 | { |
21 | QHash<int, QByteArray> roles; |
22 | roles[Type] = "type" ; |
23 | roles[Name] = "name" ; |
24 | roles[X] = "x" ; |
25 | roles[Y] = "y" ; |
26 | roles[Width] = "width" ; |
27 | roles[Height] = "height" ; |
28 | roles[Selected] = "selected" ; |
29 | roles[NodeId] = "nodeId" ; |
30 | roles[Description] = "description" ; |
31 | roles[Disabled] = "disabled" ; |
32 | return roles; |
33 | } |
34 | |
35 | QVariant NodesModel::data(const QModelIndex &index, int role) const |
36 | { |
37 | if (!index.isValid()) |
38 | return QVariant(); |
39 | |
40 | if (index.row() >= m_nodesList.size()) |
41 | return false; |
42 | |
43 | const auto &node = (m_nodesList)[index.row()]; |
44 | |
45 | if (role == Type) |
46 | return QVariant::fromValue(value: node.type); |
47 | else if (role == Name) |
48 | return QVariant::fromValue(value: node.name); |
49 | else if (role == X) |
50 | return QVariant::fromValue(value: node.x); |
51 | else if (role == Y) |
52 | return QVariant::fromValue(value: node.y); |
53 | else if (role == Width) |
54 | return QVariant::fromValue(value: node.width); |
55 | else if (role == Height) |
56 | return QVariant::fromValue(value: node.height); |
57 | else if (role == Selected) |
58 | return QVariant::fromValue(value: node.selected); |
59 | else if (role == NodeId) |
60 | return QVariant::fromValue(value: node.nodeId); |
61 | else if (role == Description) |
62 | return QVariant::fromValue(value: node.description); |
63 | else if (role == Disabled) |
64 | return QVariant::fromValue(value: node.disabled); |
65 | |
66 | return QVariant(); |
67 | } |
68 | |
69 | bool NodesModel::setData(const QModelIndex &index, const QVariant &value, int role) |
70 | { |
71 | if (!index.isValid()) |
72 | return false; |
73 | |
74 | if (index.row() >= m_nodesList.size()) |
75 | return false; |
76 | |
77 | auto &uniform = (m_nodesList)[index.row()]; |
78 | |
79 | if (role == Disabled) { |
80 | uniform.disabled = value.toBool(); |
81 | } |
82 | |
83 | emit dataChanged(topLeft: index, bottomRight: index, roles: {role}); |
84 | |
85 | return true; |
86 | } |
87 | |
88 | NodesModel::Node *NodesModel::getNodeWithId(int id) |
89 | { |
90 | for (auto &node : m_nodesList) { |
91 | if (node.nodeId == id) |
92 | return &node; |
93 | } |
94 | return nullptr; |
95 | } |
96 | |
97 | void NodesModel::setSelectedNode(Node *node) |
98 | { |
99 | if (m_selectedNode == node) |
100 | return; |
101 | |
102 | m_selectedNode = node; |
103 | Q_EMIT selectedNodeChanged(); |
104 | } |
105 | |