diff --git a/src/plugins/qmldesigner/components/createtexture.cpp b/src/plugins/qmldesigner/components/createtexture.cpp index 56056e3fd2f..c6cbe64e5df 100644 --- a/src/plugins/qmldesigner/components/createtexture.cpp +++ b/src/plugins/qmldesigner/components/createtexture.cpp @@ -16,15 +16,75 @@ #include +#include +#include #include #include namespace QmlDesigner { +using namespace Qt::StringLiterals; + +static QString nameFromId(const QString &id, const QString &defaultName) +{ + if (id.isEmpty()) + return defaultName; + + QString newName = id; + static const QRegularExpression sideUnderscores{R"((?:^_+)|(?:_+$))"}; + static const QRegularExpression underscores{R"((?:_+))"}; + static const QRegularExpression camelCases{R"((?:[A-Z](?=[a-z]))|(?:(?<=[a-z])[A-Z]))"}; + + newName.remove(sideUnderscores); + + // Insert underscore to camel case edges + QRegularExpressionMatchIterator caseMatch = camelCases.globalMatch(newName); + QStack camelCaseIndexes; + while (caseMatch.hasNext()) + camelCaseIndexes.push(caseMatch.next().capturedStart()); + while (!camelCaseIndexes.isEmpty()) + newName.insert(camelCaseIndexes.pop(), '_'); + + // Replace underscored joints with space + newName.replace(underscores, " "); + newName = newName.trimmed(); + + if (newName.isEmpty()) + return defaultName; + + newName[0] = newName[0].toUpper(); + return newName; +} + CreateTexture::CreateTexture(AbstractView *view) : m_view{view} {} +ModelNode CreateTexture::execute() +{ + ModelNode matLib = Utils3D::materialLibraryNode(m_view); + if (!matLib.isValid()) + return {}; + + ModelNode newTextureNode; + m_view->executeInTransaction(__FUNCTION__, [&]() { +#ifdef QDS_USE_PROJECTSTORAGE + newTextureNode = m_view->createModelNode("Texture"); +#else + NodeMetaInfo metaInfo = m_view->model()->qtQuick3DTextureMetaInfo(); + newTextureNode = m_view->createModelNode("QtQuick3D.Texture", + metaInfo.majorVersion(), + metaInfo.minorVersion()); +#endif + newTextureNode.ensureIdExists(); + VariantProperty textureName = newTextureNode.variantProperty("objectName"); + textureName.setValue(nameFromId(newTextureNode.id(), "Texture"_L1)); + matLib.defaultNodeListProperty().reparentHere(newTextureNode); + }); + + return newTextureNode; +} + ModelNode CreateTexture::execute(const QString &filePath, AddTextureMode mode, int sceneId) { Asset asset(filePath); @@ -94,8 +154,12 @@ ModelNode CreateTexture::createTextureFromImage(const Utils::FilePath &assetPat #endif newTexNode.setIdWithoutRefactoring(m_view->model()->generateNewId(assetPath.baseName())); + VariantProperty textureName = newTexNode.variantProperty("objectName"); + textureName.setValue(nameFromId(newTexNode.id(), "Texture"_L1)); + VariantProperty sourceProp = newTexNode.variantProperty("source"); sourceProp.setValue(QUrl(textureSource)); + matLib.defaultNodeListProperty().reparentHere(newTexNode); } diff --git a/src/plugins/qmldesigner/components/createtexture.h b/src/plugins/qmldesigner/components/createtexture.h index 326696c67de..0f276cdbcd2 100644 --- a/src/plugins/qmldesigner/components/createtexture.h +++ b/src/plugins/qmldesigner/components/createtexture.h @@ -23,7 +23,10 @@ class CreateTexture : public QObject public: CreateTexture(AbstractView *view); - ModelNode execute(const QString &filePath, AddTextureMode mode = AddTextureMode::Texture, int sceneId = -1); + ModelNode execute(); + ModelNode execute(const QString &filePath, + AddTextureMode mode = AddTextureMode::Texture, + int sceneId = -1); ModelNode resolveSceneEnv(int sceneId); void assignTextureAsLightProbe(const ModelNode &texture, int sceneId); diff --git a/src/plugins/qmldesigner/components/textureeditor/textureeditorview.cpp b/src/plugins/qmldesigner/components/textureeditor/textureeditorview.cpp index e7538913699..1c3f01daf7a 100644 --- a/src/plugins/qmldesigner/components/textureeditor/textureeditorview.cpp +++ b/src/plugins/qmldesigner/components/textureeditor/textureeditorview.cpp @@ -12,17 +12,18 @@ #include #include +#include #include #include #include #include #include #include -#include -#include #include #include #include +#include +#include #include @@ -50,41 +51,6 @@ using namespace Qt::StringLiterals; -namespace { - -QString nameFromId(const QString &id, const QString &defaultName) -{ - if (id.isEmpty()) - return defaultName; - - QString newName = id; - static const QRegularExpression sideUnderscores{R"((?:^_+)|(?:_+$))"}; - static const QRegularExpression underscores{R"((?:_+))"}; - static const QRegularExpression camelCases{R"((?:[A-Z](?=[a-z]))|(?:(?<=[a-z])[A-Z]))"}; - - newName.remove(sideUnderscores); - - // Insert underscore to camel case edges - QRegularExpressionMatchIterator caseMatch = camelCases.globalMatch(newName); - QStack camelCaseIndexes; - while (caseMatch.hasNext()) - camelCaseIndexes.push(caseMatch.next().capturedStart()); - while (!camelCaseIndexes.isEmpty()) - newName.insert(camelCaseIndexes.pop(), '_'); - - // Replace underscored joints with space - newName.replace(underscores, " "); - newName = newName.trimmed(); - - if (newName.isEmpty()) - return defaultName; - - newName[0] = newName[0].toUpper(); - return newName; -} - -} // namespace - namespace QmlDesigner { TextureEditorView::TextureEditorView(AsynchronousImageCache &imageCache, @@ -92,6 +58,7 @@ TextureEditorView::TextureEditorView(AsynchronousImageCache &imageCache, : AbstractView{externalDependencies} , m_imageCache(imageCache) , m_stackedWidget(new QStackedWidget) + , m_createTexture(new CreateTexture(this)) , m_dynamicPropertiesModel(new DynamicPropertiesModel(true, this)) { m_updateShortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_F12), m_stackedWidget); @@ -413,23 +380,7 @@ void TextureEditorView::handleToolBarAction(int action) case TextureEditorContextObject::AddNewTexture: { if (!model()) break; - executeInTransaction("TextureEditorView:handleToolBarAction", [&] { - ModelNode matLib = Utils3D::materialLibraryNode(this); - if (!matLib.isValid()) - return; -#ifdef QDS_USE_PROJECTSTORAGE - ModelNode newTextureNode = createModelNode("Texture"); -#else - NodeMetaInfo metaInfo = model()->metaInfo("QtQuick3D.Texture"); - ModelNode newTextureNode = createModelNode("QtQuick3D.Texture", - metaInfo.majorVersion(), - metaInfo.minorVersion()); -#endif - newTextureNode.ensureIdExists(); - VariantProperty textureName = newTextureNode.variantProperty("objectName"); - textureName.setValue(nameFromId(newTextureNode.id(), "Texture"_L1)); - matLib.defaultNodeListProperty().reparentHere(newTextureNode); - }); + m_createTexture->execute(); break; } diff --git a/src/plugins/qmldesigner/components/textureeditor/textureeditorview.h b/src/plugins/qmldesigner/components/textureeditor/textureeditorview.h index dd5e0b0cc98..31378544a3f 100644 --- a/src/plugins/qmldesigner/components/textureeditor/textureeditorview.h +++ b/src/plugins/qmldesigner/components/textureeditor/textureeditorview.h @@ -18,6 +18,7 @@ QT_END_NAMESPACE namespace QmlDesigner { +class CreateTexture; class DynamicPropertiesModel; class ModelNode; class TextureEditorQmlBackend; @@ -118,6 +119,7 @@ private: bool m_initializingPreviewData = false; QPointer m_colorDialog; + QPointer m_createTexture; DynamicPropertiesModel *m_dynamicPropertiesModel = nullptr; };