summaryrefslogtreecommitdiffstats
path: root/tools/qqem/nodesmodel.cpp
blob: 01463f5f0acf6141e880c66939d0fca4822c3e09 (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "nodesmodel.h"

NodesModel::NodesModel(QObject *parent)
    : QAbstractListModel(parent)
{
    connect(this, &QAbstractListModel::rowsInserted, this, &NodesModel::rowCountChanged);
    connect(this, &QAbstractListModel::rowsRemoved, this, &NodesModel::rowCountChanged);
    connect(this, &QAbstractListModel::modelReset, this, &NodesModel::rowCountChanged);
}

int NodesModel::rowCount(const QModelIndex &) const
{
    return m_nodesList.size();
}

QHash<int, QByteArray> NodesModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[Type] = "type";
    roles[Name] = "name";
    roles[X] = "x";
    roles[Y] = "y";
    roles[Width] = "width";
    roles[Height] = "height";
    roles[Selected] = "selected";
    roles[NodeId] = "nodeId";
    roles[Description] = "description";
    roles[Disabled] = "disabled";
    return roles;
}

QVariant NodesModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() >= m_nodesList.size())
        return false;

    const auto &node = (m_nodesList)[index.row()];

    if (role == Type)
        return QVariant::fromValue(node.type);
    else if (role == Name)
        return QVariant::fromValue(node.name);
    else if (role == X)
        return QVariant::fromValue(node.x);
    else if (role == Y)
        return QVariant::fromValue(node.y);
    else if (role == Width)
        return QVariant::fromValue(node.width);
    else if (role == Height)
        return QVariant::fromValue(node.height);
    else if (role == Selected)
        return QVariant::fromValue(node.selected);
    else if (role == NodeId)
        return QVariant::fromValue(node.nodeId);
    else if (role == Description)
        return QVariant::fromValue(node.description);
    else if (role == Disabled)
        return QVariant::fromValue(node.disabled);

    return QVariant();
}

bool NodesModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    if (index.row() >= m_nodesList.size())
        return false;

    auto &uniform = (m_nodesList)[index.row()];

    if (role == Disabled) {
        uniform.disabled = value.toBool();
    }

    emit dataChanged(index, index, {role});

    return true;
}

NodesModel::Node *NodesModel::getNodeWithId(int id)
{
    for (auto &node : m_nodesList) {
        if (node.nodeId == id)
            return &node;
    }
    return nullptr;
}

void NodesModel::setSelectedNode(Node *node)
{
    if (m_selectedNode == node)
        return;

    m_selectedNode = node;
    Q_EMIT selectedNodeChanged();
}