Effect Maker: Enable helper nodes

Helper nodes are nodes that another node depends on and are added
automatically when the depending node is added. Helper nodes are
placed before all other nodes. Helper nodes that do not contain
any properties are not shown. Helper nodes keep reference count
and are removed when last referring node is removed.

Task-number: QDS-11193
Change-Id: I036019afb1414ec6e98b2f949a18bd217753a910
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
This commit is contained in:
Mahmoud Badri
2023-12-07 18:32:55 +02:00
committed by Miikka Heikkinen
parent 6b6f74ccad
commit dc42b62ddf
12 changed files with 264 additions and 33 deletions

View File

@@ -14,7 +14,8 @@
namespace EffectMaker {
CompositionNode::CompositionNode(const QString &effectName, const QString &qenPath, const QJsonObject &jsonObject)
CompositionNode::CompositionNode(const QString &effectName, const QString &qenPath,
const QJsonObject &jsonObject)
{
QJsonObject json;
if (jsonObject.isEmpty()) {
@@ -58,6 +59,11 @@ QString CompositionNode::description() const
return m_description;
}
QString CompositionNode::id() const
{
return m_id;
}
QObject *CompositionNode::uniformsModel()
{
return &m_unifomrsModel;
@@ -81,6 +87,11 @@ void CompositionNode::setIsEnabled(bool newIsEnabled)
}
}
bool CompositionNode::isDependency() const
{
return m_refCount > 0;
}
CompositionNode::NodeType CompositionNode::type() const
{
return m_type;
@@ -102,6 +113,13 @@ void CompositionNode::parse(const QString &effectName, const QString &qenPath, c
m_fragmentCode = EffectUtils::codeFromJsonArray(json.value("fragmentCode").toArray());
m_vertexCode = EffectUtils::codeFromJsonArray(json.value("vertexCode").toArray());
m_id = json.value("id").toString();
if (m_id.isEmpty() && !qenPath.isEmpty()) {
QString fileName = qenPath.split('/').last();
fileName.chop(4); // remove ".qen"
m_id = fileName;
}
// parse properties
QJsonArray jsonProps = json.value("properties").toArray();
for (const auto /*QJsonValueRef*/ &prop : jsonProps) {
@@ -118,8 +136,7 @@ void CompositionNode::parse(const QString &effectName, const QString &qenPath, c
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();
// Get the required node, remove "@requires "
QString nodeName = trimmedLine.sliced(10);
if (!nodeName.isEmpty() && !m_requiredNodes.contains(nodeName))
m_requiredNodes << nodeName;
@@ -132,6 +149,36 @@ QList<Uniform *> CompositionNode::uniforms() const
return m_uniforms;
}
int CompositionNode::incRefCount()
{
++m_refCount;
if (m_refCount == 1)
emit isDepencyChanged();
return m_refCount;
}
int CompositionNode::decRefCount()
{
--m_refCount;
if (m_refCount == 0)
emit isDepencyChanged();
return m_refCount;
}
void CompositionNode::setRefCount(int count)
{
bool notifyChange = (m_refCount > 0 && count == 0) || (m_refCount <= 0 && count > 0);
m_refCount = count;
if (notifyChange)
emit isDepencyChanged();
}
QString CompositionNode::name() const
{
return m_name;

View File

@@ -15,7 +15,8 @@ class CompositionNode : public QObject
Q_OBJECT
Q_PROPERTY(QString nodeName MEMBER m_name CONSTANT)
Q_PROPERTY(bool nodeEnabled READ isEnabled WRITE setIsEnabled NOTIFY isEnabledChanged)
Q_PROPERTY(bool nodeEnabled READ isEnabled WRITE setIsEnabled NOTIFY isEnabledChanged)
Q_PROPERTY(bool isDependency READ isDependency NOTIFY isDepencyChanged)
Q_PROPERTY(QObject *nodeUniformsModel READ uniformsModel NOTIFY uniformsModelChanged)
public:
@@ -30,6 +31,7 @@ public:
QString fragmentCode() const;
QString vertexCode() const;
QString description() const;
QString id() const;
QObject *uniformsModel();
@@ -40,13 +42,20 @@ public:
bool isEnabled() const;
void setIsEnabled(bool newIsEnabled);
bool isDependency() const;
QString name() const;
QList<Uniform *> uniforms() const;
int incRefCount();
int decRefCount();
void setRefCount(int count);
signals:
void uniformsModelChanged();
void isEnabledChanged();
void isDepencyChanged();
private:
void parse(const QString &effectName, const QString &qenPath, const QJsonObject &json);
@@ -57,7 +66,9 @@ private:
QString m_vertexCode;
QString m_description;
QStringList m_requiredNodes;
QString m_id;
bool m_isEnabled = true;
int m_refCount = 0;
QList<Uniform*> m_uniforms;

View File

@@ -4,6 +4,8 @@
#include "effectmakermodel.h"
#include "compositionnode.h"
#include "effectutils.h"
#include "propertyhandler.h"
#include "syntaxhighlighterdata.h"
#include "uniform.h"
@@ -58,6 +60,7 @@ QHash<int, QByteArray> EffectMakerModel::roleNames() const
roles[NameRole] = "nodeName";
roles[EnabledRole] = "nodeEnabled";
roles[UniformsRole] = "nodeUniformsModel";
roles[Dependency] = "isDependency";
return roles;
}
@@ -104,14 +107,29 @@ void EffectMakerModel::setIsEmpty(bool val)
void EffectMakerModel::addNode(const QString &nodeQenPath)
{
beginInsertRows({}, m_nodes.size(), m_nodes.size());
auto *node = new CompositionNode("", nodeQenPath);
beginResetModel();
auto *node = new CompositionNode({}, nodeQenPath);
connect(qobject_cast<EffectMakerUniformsModel *>(node->uniformsModel()),
&EffectMakerUniformsModel::dataChanged, this, [this] {
setHasUnsavedChanges(true);
});
setHasUnsavedChanges(true);
});
const QList<QString> requiredNodes = node->requiredNodes();
if (requiredNodes.size() > 0) {
for (const QString &requiredId : requiredNodes) {
if (auto reqNode = findNodeById(requiredId)) {
reqNode->incRefCount();
continue;
}
const QString path = EffectUtils::nodesSourcesPath() + "/common/" + requiredId + ".qen";
auto requiredNode = new CompositionNode({}, path);
requiredNode->setRefCount(1);
m_nodes.prepend(requiredNode);
}
}
m_nodes.append(node);
endInsertRows();
endResetModel();
setIsEmpty(false);
@@ -121,6 +139,15 @@ void EffectMakerModel::addNode(const QString &nodeQenPath)
emit nodesChanged();
}
CompositionNode *EffectMakerModel::findNodeById(const QString &id) const
{
for (CompositionNode *node : std::as_const(m_nodes)) {
if (node->id() == id)
return node;
}
return {};
}
void EffectMakerModel::moveNode(int fromIdx, int toIdx)
{
if (fromIdx == toIdx)
@@ -137,10 +164,20 @@ void EffectMakerModel::moveNode(int fromIdx, int toIdx)
void EffectMakerModel::removeNode(int idx)
{
beginRemoveRows({}, idx, idx);
beginResetModel();
CompositionNode *node = m_nodes.takeAt(idx);
const QStringList reqNodes = node->requiredNodes();
for (const QString &reqId : reqNodes) {
CompositionNode *depNode = findNodeById(reqId);
if (depNode && depNode->decRefCount() <= 0) {
m_nodes.removeOne(depNode);
delete depNode;
}
}
delete node;
endRemoveRows();
endResetModel();
if (m_nodes.isEmpty())
setIsEmpty(true);
@@ -442,6 +479,8 @@ QJsonObject nodeToJson(const CompositionNode &node)
nodeObject.insert("description", node.description());
nodeObject.insert("enabled", node.isEnabled());
nodeObject.insert("version", 1);
nodeObject.insert("id", node.id());
// Add properties
QJsonArray propertiesArray;
const QList<Uniform *> uniforms = node.uniforms();
@@ -549,7 +588,17 @@ QString EffectMakerModel::getQmlEffectString()
s += '\n';
}
//TODO: Blue stuff goes here
if (m_shaderFeatures.enabled(ShaderFeatures::BlurSources)) {
s += " BlurHelper {\n";
s += " id: blurHelper\n";
s += " anchors.fill: parent\n";
int blurMax = 32;
if (g_propertyData.contains("BLUR_HELPER_MAX_LEVEL"))
blurMax = g_propertyData["BLUR_HELPER_MAX_LEVEL"].toInt();
s += QString(" property int blurMax: %1\n").arg(blurMax);
s += " property real blurMultiplier: rootItem.blurMultiplier\n";
s += " }\n";
}
s += getQmlComponentString(true);
s += "}\n";
@@ -643,18 +692,30 @@ void EffectMakerModel::openComposition(const QString &path)
}
if (json.contains("nodes") && json["nodes"].isArray()) {
beginResetModel();
QHash<QString, int> refCounts;
const QJsonArray nodesArray = json["nodes"].toArray();
for (const auto &nodeElement : nodesArray) {
beginInsertRows({}, m_nodes.size(), m_nodes.size());
auto *node = new CompositionNode(effectName, "", nodeElement.toObject());
auto *node = new CompositionNode(effectName, {}, nodeElement.toObject());
connect(qobject_cast<EffectMakerUniformsModel *>(node->uniformsModel()),
&EffectMakerUniformsModel::dataChanged, this, [this] {
setHasUnsavedChanges(true);
});
m_nodes.append(node);
endInsertRows();
const QStringList reqIds = node->requiredNodes();
for (const QString &reqId : reqIds)
++refCounts[reqId];
}
for (auto it = refCounts.cbegin(), end = refCounts.cend(); it != end; ++it) {
CompositionNode *depNode = findNodeById(it.key());
if (depNode)
depNode->setRefCount(it.value());
}
endResetModel();
setIsEmpty(m_nodes.isEmpty());
bakeShaders();
}
@@ -748,6 +809,19 @@ void EffectMakerModel::saveResources(const QString &name)
}
}
if (m_shaderFeatures.enabled(ShaderFeatures::BlurSources)) {
QString blurHelperFilename("BlurHelper.qml");
QString blurFsFilename("bluritems.frag.qsb");
QString blurVsFilename("bluritems.vert.qsb");
QString blurHelperPath(EffectUtils::nodesSourcesPath() + "/common/");
sources.append(blurHelperPath + blurHelperFilename);
sources.append(blurHelperPath + blurFsFilename);
sources.append(blurHelperPath + blurVsFilename);
dests.append(blurHelperFilename);
dests.append(blurFsFilename);
dests.append(blurVsFilename);
}
for (int i = 0; i < sources.count(); ++i) {
Utils::FilePath source = Utils::FilePath::fromString(sources[i]);
Utils::FilePath target = Utils::FilePath::fromString(effectsResPath + dests[i]);
@@ -1505,6 +1579,13 @@ QStringList EffectMakerModel::uniformNames() const
return usedList;
}
bool EffectMakerModel::isDependencyNode(int index) const
{
if (m_nodes.size() > index)
return m_nodes[index]->isDependency();
return false;
}
void EffectMakerModel::updateQmlComponent()
{
// Clear possible QML runtime errors

View File

@@ -7,10 +7,10 @@
#include <utils/filepath.h>
#include <QAbstractListModel>
#include <QFileSystemWatcher>
#include <QMap>
#include <QRegularExpression>
#include <QStandardItemModel>
#include <QTemporaryFile>
namespace ProjectExplorer {
@@ -62,6 +62,8 @@ public:
void addNode(const QString &nodeQenPath);
CompositionNode *findNodeById(const QString &id) const;
Q_INVOKABLE void moveNode(int fromIdx, int toIdx);
Q_INVOKABLE void removeNode(int idx);
Q_INVOKABLE void clear();
@@ -96,6 +98,8 @@ public:
QStringList uniformNames() const;
Q_INVOKABLE bool isDependencyNode(int index) const;
signals:
void isEmptyChanged();
void selectedIndexChanged(int idx);
@@ -112,7 +116,8 @@ private:
enum Roles {
NameRole = Qt::UserRole + 1,
EnabledRole,
UniformsRole
UniformsRole,
Dependency
};
enum ErrorTypes {

View File

@@ -2,8 +2,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "effectmakernodesmodel.h"
#include <coreplugin/icore.h>
#include "effectutils.h"
#include <utils/filepath.h>
#include <utils/hostosinfo.h>
@@ -41,21 +40,12 @@ QVariant EffectMakerNodesModel::data(const QModelIndex &index, int role) const
return m_categories.at(index.row())->property(roleNames().value(role));
}
QString EffectMakerNodesModel::nodesSourcesPath() const
{
#ifdef SHARE_QML_PATH
if (Utils::qtcEnvironmentVariableIsSet("LOAD_QML_FROM_SOURCE"))
return QLatin1String(SHARE_QML_PATH) + "/effectMakerNodes";
#endif
return Core::ICore::resourcePath("qmldesigner/effectMakerNodes").toString();
}
void EffectMakerNodesModel::loadModel()
{
if (m_modelLoaded)
return;
auto nodesPath = Utils::FilePath::fromString(nodesSourcesPath());
auto nodesPath = Utils::FilePath::fromString(EffectUtils::nodesSourcesPath());
if (!nodesPath.exists()) {
qWarning() << __FUNCTION__ << "Effects not found.";

View File

@@ -7,6 +7,7 @@
#include "effectmakermodel.h"
#include "effectmakernodesmodel.h"
#include "effectmakerview.h"
#include "effectutils.h"
#include "propertyhandler.h"
//#include "qmldesigner/designercore/imagecache/midsizeimagecacheprovider.h"
@@ -61,6 +62,7 @@ EffectMakerWidget::EffectMakerWidget(EffectMakerView *view)
m_quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);
QmlDesigner::Theme::setupTheme(m_quickWidget->engine());
m_quickWidget->engine()->addImportPath(propertyEditorResourcesPath() + "/imports");
m_quickWidget->engine()->addImportPath(EffectUtils::nodesSourcesPath() + "/common");
m_quickWidget->setClearColor(QmlDesigner::Theme::getColor(
QmlDesigner::Theme::Color::QmlDesigner_BackgroundColorDarkAlternate));
@@ -76,6 +78,10 @@ EffectMakerWidget::EffectMakerWidget(EffectMakerView *view)
m_quickWidget->rootContext()->setContextProperty("g_propertyData", &g_propertyData);
QString blurPath = "file:" + EffectUtils::nodesSourcesPath() + "/common/";
g_propertyData.insert(QString("blur_vs_path"), QString(blurPath + "bluritems.vert.qsb"));
g_propertyData.insert(QString("blur_fs_path"), QString(blurPath + "bluritems.frag.qsb"));
auto map = m_quickWidget->registerPropertyMap("EffectMakerBackend");
map->setProperties({{"effectMakerNodesModel", QVariant::fromValue(m_effectMakerNodesModel.data())},
{"effectMakerModel", QVariant::fromValue(m_effectMakerModel.data())},

View File

@@ -3,6 +3,8 @@
#include "effectutils.h"
#include <coreplugin/icore.h>
#include <QJsonArray>
namespace EffectMaker {
@@ -20,5 +22,14 @@ QString EffectUtils::codeFromJsonArray(const QJsonArray &codeArray)
return codeString;
}
QString EffectUtils::nodesSourcesPath()
{
#ifdef SHARE_QML_PATH
if (Utils::qtcEnvironmentVariableIsSet("LOAD_QML_FROM_SOURCE"))
return QLatin1String(SHARE_QML_PATH) + "/effectMakerNodes";
#endif
return Core::ICore::resourcePath("qmldesigner/effectMakerNodes").toString();
}
} // namespace EffectMaker

View File

@@ -15,6 +15,8 @@ public:
EffectUtils() = delete;
static QString codeFromJsonArray(const QJsonArray &codeArray);
static QString nodesSourcesPath();
};
} // namespace EffectMaker