1/****************************************************************************
2**
3** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qshadernode_p.h"
41
42QT_BEGIN_NAMESPACE
43
44QShaderNode::Type QShaderNode::type() const noexcept
45{
46 int inputCount = 0;
47 int outputCount = 0;
48 for (const auto &port : qAsConst(m_ports)) {
49 switch (port.direction) {
50 case QShaderNodePort::Input:
51 inputCount++;
52 break;
53 case QShaderNodePort::Output:
54 outputCount++;
55 break;
56 }
57 }
58
59 return (inputCount == 0 && outputCount == 0) ? Invalid
60 : (inputCount > 0 && outputCount == 0) ? Output
61 : (inputCount == 0 && outputCount > 0) ? Input
62 : Function;
63}
64
65QUuid QShaderNode::uuid() const noexcept
66{
67 return m_uuid;
68}
69
70void QShaderNode::setUuid(const QUuid &uuid) noexcept
71{
72 m_uuid = uuid;
73}
74
75QStringList QShaderNode::layers() const noexcept
76{
77 return m_layers;
78}
79
80void QShaderNode::setLayers(const QStringList &layers) noexcept
81{
82 m_layers = layers;
83}
84
85QList<QShaderNodePort> QShaderNode::ports() const noexcept
86{
87 return m_ports;
88}
89
90void QShaderNode::addPort(const QShaderNodePort &port)
91{
92 removePort(port);
93 m_ports.append(port);
94}
95
96void QShaderNode::removePort(const QShaderNodePort &port)
97{
98 const auto it = std::find_if(m_ports.begin(), m_ports.end(),
99 [port](const QShaderNodePort &p) {
100 return p.name == port.name;
101 });
102 if (it != m_ports.end())
103 m_ports.erase(it);
104}
105
106QStringList QShaderNode::parameterNames() const
107{
108 return m_parameters.keys();
109}
110
111QVariant QShaderNode::parameter(const QString &name) const
112{
113 return m_parameters.value(name);
114}
115
116void QShaderNode::setParameter(const QString &name, const QVariant &value)
117{
118 m_parameters.insert(name, value);
119}
120
121void QShaderNode::clearParameter(const QString &name)
122{
123 m_parameters.remove(name);
124}
125
126void QShaderNode::addRule(const QShaderFormat &format, const QShaderNode::Rule &rule)
127{
128 removeRule(format);
129 m_rules << qMakePair(format, rule);
130}
131
132void QShaderNode::removeRule(const QShaderFormat &format)
133{
134 const auto it = std::find_if(m_rules.begin(), m_rules.end(),
135 [format](const QPair<QShaderFormat, Rule> &entry) {
136 return entry.first == format;
137 });
138 if (it != m_rules.end())
139 m_rules.erase(it);
140}
141
142QList<QShaderFormat> QShaderNode::availableFormats() const
143{
144 auto res = QList<QShaderFormat>();
145 std::transform(m_rules.cbegin(), m_rules.cend(),
146 std::back_inserter(res),
147 [](const QPair<QShaderFormat, Rule> &entry) { return entry.first; });
148 return res;
149}
150
151QShaderNode::Rule QShaderNode::rule(const QShaderFormat &format) const
152{
153 const auto it = std::find_if(m_rules.crbegin(), m_rules.crend(),
154 [format](const QPair<QShaderFormat, Rule> &entry) {
155 return format.supports(entry.first);
156 });
157 return it != m_rules.crend() ? it->second : Rule();
158}
159
160QShaderNode::Rule::Rule(const QByteArray &subs, const QByteArrayList &snippets) noexcept
161 : substitution(subs),
162 headerSnippets(snippets)
163{
164}
165
166bool operator==(const QShaderNode::Rule &lhs, const QShaderNode::Rule &rhs) noexcept
167{
168 return lhs.substitution == rhs.substitution
169 && lhs.headerSnippets == rhs.headerSnippets;
170}
171
172QT_END_NAMESPACE
173