Extend the parameter support in QShaderNodeLoader for richer types

In particular, go through QMetaType/QMetaEnum to deal with enums.

Change-Id: Idbe16c913c1d471a4a91d219f77876e498c192d9
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
This commit is contained in:
Kevin Ottens 2017-07-12 14:01:31 +02:00 committed by Sean Harmer
parent 3786411f7f
commit 50775424be
2 changed files with 42 additions and 2 deletions

View File

@ -44,6 +44,7 @@
#include <QtCore/qjsonarray.h> #include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h> #include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h> #include <QtCore/qjsonobject.h>
#include <QtCore/qmetaobject.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -151,7 +152,30 @@ void QShaderNodesLoader::load()
if (parametersValue.isObject()) { if (parametersValue.isObject()) {
const auto parametersObject = parametersValue.toObject(); const auto parametersObject = parametersValue.toObject();
for (const auto &parameterName : parametersObject.keys()) { for (const auto &parameterName : parametersObject.keys()) {
node.setParameter(parameterName, parametersObject.value(parameterName).toVariant()); const auto parameterValue = parametersObject.value(parameterName);
if (parameterValue.isObject()) {
const auto parameterObject = parameterValue.toObject();
const auto type = parameterObject.value(QStringLiteral("type")).toString();
const auto typeId = QMetaType::type(type.toUtf8());
const auto value = parameterObject.value(QStringLiteral("value")).toString();
auto variant = QVariant(value);
if (QMetaType::typeFlags(typeId) & QMetaType::IsEnumeration) {
const auto metaObject = QMetaType::metaObjectForType(typeId);
const auto className = metaObject->className();
const auto enumName = type.mid(static_cast<int>(qstrlen(className)) + 2).toUtf8();
const auto metaEnum = metaObject->enumerator(metaObject->indexOfEnumerator(enumName));
const auto enumValue = metaEnum.keyToValue(value.toUtf8());
variant = QVariant(enumValue);
variant.convert(typeId);
} else {
variant.convert(typeId);
}
node.setParameter(parameterName, variant);
} else {
node.setParameter(parameterName, parameterValue.toVariant());
}
} }
} }

View File

@ -32,6 +32,7 @@
#include <QtCore/qbuffer.h> #include <QtCore/qbuffer.h>
#include <QtGui/private/qshadernodesloader_p.h> #include <QtGui/private/qshadernodesloader_p.h>
#include <QtGui/private/qshaderlanguage_p.h>
using QBufferPointer = QSharedPointer<QBuffer>; using QBufferPointer = QSharedPointer<QBuffer>;
Q_DECLARE_METATYPE(QBufferPointer); Q_DECLARE_METATYPE(QBufferPointer);
@ -131,7 +132,19 @@ void tst_QShaderNodesLoader::shouldLoadFromJsonStream_data()
" \"value\"" " \"value\""
" ]," " ],"
" \"parameters\": {" " \"parameters\": {"
" \"name\": \"defaultName\"" " \"name\": \"defaultName\","
" \"qualifier\": {"
" \"type\": \"QShaderLanguage::StorageQualifier\","
" \"value\": \"QShaderLanguage::Uniform\""
" },"
" \"type\": {"
" \"type\": \"QShaderLanguage::VariableType\","
" \"value\": \"QShaderLanguage::Vec3\""
" },"
" \"defaultValue\": {"
" \"type\": \"float\","
" \"value\": \"1.25\""
" }"
" }," " },"
" \"rules\": [" " \"rules\": ["
" {" " {"
@ -225,6 +238,9 @@ void tst_QShaderNodesLoader::shouldLoadFromJsonStream_data()
createPort(QShaderNodePort::Output, "value") createPort(QShaderNodePort::Output, "value")
}); });
inputValue.setParameter("name", "defaultName"); inputValue.setParameter("name", "defaultName");
inputValue.setParameter("qualifier", QVariant::fromValue<QShaderLanguage::StorageQualifier>(QShaderLanguage::Uniform));
inputValue.setParameter("type", QVariant::fromValue<QShaderLanguage::VariableType>(QShaderLanguage::Vec3));
inputValue.setParameter("defaultValue", QVariant(1.25f));
inputValue.addRule(openGLES2, QShaderNode::Rule("highp vec3 $value = $name;", inputValue.addRule(openGLES2, QShaderNode::Rule("highp vec3 $value = $name;",
QByteArrayList() << "varying highp vec3 $name;")); QByteArrayList() << "varying highp vec3 $name;"));
inputValue.addRule(openGL2, QShaderNode::Rule("vec3 $value = $name;", inputValue.addRule(openGL2, QShaderNode::Rule("vec3 $value = $name;",