summaryrefslogtreecommitdiffstats
path: root/tools/qqem/addnodemodel.cpp
blob: 09574ff977aefc81d80f64687a1da853f4900b45 (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
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
// Qt-Security score:critical reason:data-parser

#include "addnodemodel.h"
#include "nodesmodel.h"
#include "effectmanager.h"
#include <QDir>

bool operator==(const AddNodeModel::NodeData &a, const AddNodeModel::NodeData &b) noexcept
{
    return a.name == b.name;
}

AddNodeModel::AddNodeModel(QObject *effectManager)
    : QAbstractListModel(effectManager)
{
    m_effectManager = static_cast<EffectManager *>(effectManager);
    connect(this, &QAbstractListModel::modelReset, this, &AddNodeModel::rowCountChanged);
    updateNodesList();
}

int AddNodeModel::rowCount(const QModelIndex &) const
{
    return m_modelList.size();
}

QHash<int, QByteArray> AddNodeModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[Name] = "name";
    roles[Description] = "description";
    roles[File] = "file";
    roles[Group] = "group";
    roles[Properties] = "properties";
    roles[CanBeAdded] = "canBeAdded";
    roles[Show] = "show";
    roles[RequiredNodes] = "requires";
    return roles;
}

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

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

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

    if (role == Name)
        return QVariant::fromValue(node.name);
    else if (role == Description)
        return QVariant::fromValue(node.description);
    else if (role == File)
        return QVariant::fromValue(node.file);
    else if (role == Group)
        return QVariant::fromValue(node.group);
    else if (role == Properties)
        return QVariant::fromValue(node.properties);
    else if (role == CanBeAdded)
        return QVariant::fromValue(node.canBeAdded);
    else if (role == Show)
        return QVariant::fromValue(node.show);
    else if (role == RequiredNodes)
        return QVariant::fromValue(node.requiredNodes.join(", "));

    return QVariant();
}

void AddNodeModel::loadNodesFromPath(const QString &path) {
    qInfo() << "Loading nodes from:" << path;
    QList<NodeData> nodes;

    QDir rootDirectory(path);
    QStringList dirList;
    dirList << path;
    // List subdirectories, in our preferred order
    QStringList subDirList = rootDirectory.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name);
    for (auto &subDir : subDirList) {
        QString dirPath = (path + "/" + subDir);
        // Trick to get "common" nodes first in list
        if (subDir == "common")
            dirList.prepend(dirPath);
        else
            dirList.append(dirPath);
    }
    // Load nodes from all subdirectories
    for (const auto &dirPath : dirList) {
        QDir directory(dirPath);
        QStringList nodeList = directory.entryList(QStringList() << "*.qen" << "*.QEN", QDir::Files, QDir::Name);
        for (auto &filename : nodeList) {
            QString filePath = directory.path() + "/" + filename;
            auto node = m_effectManager->loadEffectNode(filePath);
            if (!node.name.isEmpty()) {
                NodeData data;
                data.file = filePath;
                data.name = node.name;
                if (!node.jsonUniforms.isEmpty()) {
                    for (const auto &u : node.jsonUniforms) {
                        NodeDataProperty property;
                        property.m_name = u.name;
                        if (u.type == UniformModel::Uniform::Type::Define)
                            property.m_type = QStringLiteral("define");
                        else
                            property.m_type = UniformModel::typeToProperty(u.type);
                        QVariant varProperty;
                        varProperty.setValue(property);
                        data.properties << varProperty;
                    }
                }
                data.description = node.description;
                data.group = directory.dirName();
                // Seek through code to get tags
                QStringList shaderCodeLines;
                shaderCodeLines += node.vertexCode.split('\n');
                shaderCodeLines += node.fragmentCode.split('\n');
                for (const auto &codeLine : shaderCodeLines) {
                    QString trimmedLine = codeLine.trimmed();
                    if (trimmedLine.startsWith("@requires")) {
                        // Get the required node, remove "@requires"
                        QString l = trimmedLine.sliced(9).trimmed();
                        QString nodeName = l.split(' ').first();
                        if (!nodeName.isEmpty() && !data.requiredNodes.contains(nodeName))
                            data.requiredNodes << nodeName;
                    }
                }
                if (!m_modelList.contains(data))
                    nodes << data;
            }
        }
    }

    // Add all nodes into model
    for (const auto &node : nodes)
        m_modelList << node;
}

void AddNodeModel::updateCanBeAdded(const QStringList &propertyNames)
{
    beginResetModel();
    // Check which nodes contain overlapping properties
    for (auto &nodeData : m_modelList) {
        for (const auto &variant : nodeData.properties) {
            NodeDataProperty property = variant.value<NodeDataProperty>();
            if (propertyNames.contains(property.m_name)) {
                nodeData.canBeAdded = false;
            } else {
                nodeData.canBeAdded = true;
            }
        }
    }
    endResetModel();
}

void AddNodeModel::updateShowHide(const QString &groupName, bool show)
{
    int i = 0;
    for (auto &nodeData : m_modelList) {
        if (nodeData.group == groupName) {
            nodeData.show = show;
            Q_EMIT dataChanged(QAbstractItemModel::createIndex(0, 0),
                               QAbstractItemModel::createIndex(i, 0));
        }
        i++;
    }
}

void AddNodeModel::updateNodesList()
{
    beginResetModel();

    m_modelList.clear();

    QString defaultNodePath = m_effectManager->settings()->defaultResourcePath() + "/defaultnodes";
    loadNodesFromPath(defaultNodePath);

    auto customNodesPaths = m_effectManager->settings()->customNodesPaths();
    for (const auto &nodesPath : std::as_const(customNodesPaths))
         loadNodesFromPath(nodesPath);

    static QString envCustomNodePath = qEnvironmentVariable("QQEM_CUSTOM_NODES_PATH");
    if (!envCustomNodePath.isEmpty())
        loadNodesFromPath(envCustomNodePath);

    endResetModel();
}