2023-08-16 11:35:10 +03:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
|
|
#include "compositionnode.h"
|
|
|
|
|
|
2023-08-18 15:39:40 +03:00
|
|
|
#include "effectutils.h"
|
2023-08-21 11:52:11 +03:00
|
|
|
#include "effectmakeruniformsmodel.h"
|
2023-08-17 18:38:40 +03:00
|
|
|
#include "uniform.h"
|
|
|
|
|
|
2023-08-29 16:24:43 +03:00
|
|
|
#include <QFile>
|
2023-08-17 15:59:46 +03:00
|
|
|
#include <QJsonArray>
|
2023-08-16 11:35:10 +03:00
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
|
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
|
|
|
|
CompositionNode::CompositionNode(const QString &qenPath)
|
|
|
|
|
{
|
|
|
|
|
parse(qenPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CompositionNode::fragmentCode() const
|
|
|
|
|
{
|
|
|
|
|
return m_fragmentCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CompositionNode::vertexCode() const
|
|
|
|
|
{
|
|
|
|
|
return m_vertexCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CompositionNode::description() const
|
|
|
|
|
{
|
|
|
|
|
return m_description;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-21 11:52:11 +03:00
|
|
|
QObject *CompositionNode::uniformsModel()
|
|
|
|
|
{
|
|
|
|
|
return &m_unifomrsModel;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 13:30:36 +03:00
|
|
|
QStringList CompositionNode::requiredNodes() const
|
|
|
|
|
{
|
|
|
|
|
return m_requiredNodes;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 16:43:56 +03:00
|
|
|
bool CompositionNode::isEnabled() const
|
|
|
|
|
{
|
|
|
|
|
return m_isEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CompositionNode::setIsEnabled(bool newIsEnabled)
|
|
|
|
|
{
|
2023-09-06 15:18:02 +03:00
|
|
|
if (newIsEnabled != m_isEnabled) {
|
|
|
|
|
m_isEnabled = newIsEnabled;
|
|
|
|
|
emit isEnabledChanged();
|
|
|
|
|
}
|
2023-09-01 16:43:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CompositionNode::NodeType CompositionNode::type() const
|
|
|
|
|
{
|
|
|
|
|
return m_type;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-16 11:35:10 +03:00
|
|
|
void CompositionNode::parse(const QString &qenPath)
|
|
|
|
|
{
|
2023-08-17 15:59:46 +03:00
|
|
|
QFile qenFile(qenPath);
|
2023-08-16 11:35:10 +03:00
|
|
|
|
2023-08-17 15:59:46 +03:00
|
|
|
if (!qenFile.open(QIODevice::ReadOnly)) {
|
2023-08-16 11:35:10 +03:00
|
|
|
qWarning("Couldn't open effect file.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 15:59:46 +03:00
|
|
|
QByteArray loadData = qenFile.readAll();
|
2023-08-16 11:35:10 +03:00
|
|
|
QJsonParseError parseError;
|
|
|
|
|
QJsonDocument jsonDoc(QJsonDocument::fromJson(loadData, &parseError));
|
|
|
|
|
if (parseError.error != QJsonParseError::NoError) {
|
|
|
|
|
QString error = QString("Error parsing the effect node: %1:").arg(qenPath);
|
|
|
|
|
QString errorDetails = QString("%1: %2").arg(parseError.offset).arg(parseError.errorString());
|
|
|
|
|
qWarning() << qPrintable(error);
|
|
|
|
|
qWarning() << qPrintable(errorDetails);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 15:59:46 +03:00
|
|
|
QJsonObject json = jsonDoc.object().value("QEN").toObject();
|
2023-08-16 11:35:10 +03:00
|
|
|
|
2023-08-17 18:38:40 +03:00
|
|
|
int version = -1;
|
|
|
|
|
if (json.contains("version"))
|
|
|
|
|
version = json["version"].toInt(-1);
|
|
|
|
|
if (version != 1) {
|
|
|
|
|
QString error = QString("Error: Unknown effect version (%1)").arg(version);
|
|
|
|
|
qWarning() << qPrintable(error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 15:39:40 +03:00
|
|
|
m_name = json.value("name").toString();
|
2023-08-17 18:38:40 +03:00
|
|
|
m_description = json.value("description").toString();
|
2023-08-18 15:39:40 +03:00
|
|
|
m_fragmentCode = EffectUtils::codeFromJsonArray(json.value("fragmentCode").toArray());
|
|
|
|
|
m_vertexCode = EffectUtils::codeFromJsonArray(json.value("vertexCode").toArray());
|
2023-08-17 15:59:46 +03:00
|
|
|
|
|
|
|
|
// parse properties
|
2023-08-28 13:30:36 +03:00
|
|
|
QJsonArray jsonProps = json.value("properties").toArray();
|
|
|
|
|
for (const auto /*QJsonValueRef*/ &prop : jsonProps)
|
2023-08-21 11:52:11 +03:00
|
|
|
m_unifomrsModel.addUniform(new Uniform(prop.toObject()));
|
2023-08-30 13:43:24 +03:00
|
|
|
|
|
|
|
|
// Seek through code to get tags
|
|
|
|
|
QStringList shaderCodeLines;
|
|
|
|
|
shaderCodeLines += m_vertexCode.split('\n');
|
|
|
|
|
shaderCodeLines += m_fragmentCode.split('\n');
|
|
|
|
|
for (const QString &codeLine : std::as_const(shaderCodeLines)) {
|
|
|
|
|
QString trimmedLine = codeLine.trimmed();
|
|
|
|
|
if (trimmedLine.startsWith("@requires")) {
|
|
|
|
|
// Get the required node, remove "@requires"
|
|
|
|
|
QString l = trimmedLine.sliced(9).trimmed();
|
|
|
|
|
QString nodeName = trimmedLine.sliced(10);
|
|
|
|
|
if (!nodeName.isEmpty() && !m_requiredNodes.contains(nodeName))
|
|
|
|
|
m_requiredNodes << nodeName;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-17 18:38:40 +03:00
|
|
|
}
|
2023-08-16 11:35:10 +03:00
|
|
|
|
2023-08-17 15:59:46 +03:00
|
|
|
} // namespace QmlDesigner
|