QmlDesigner: Add texture name on creation

Fixes: QDS-13140
Change-Id: Icb5ef4a5f3d82ec9e21231c1cacebcf0f04d234e
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
Reviewed-by: Shrief Gabr <shrief.gabr@qt.io>
This commit is contained in:
Ali Kianian
2024-07-09 15:20:32 +03:00
parent f86c1f5b2f
commit 77ac3392bb
4 changed files with 75 additions and 55 deletions

View File

@@ -16,15 +16,75 @@
#include <coreplugin/messagebox.h> #include <coreplugin/messagebox.h>
#include <QRegularExpression>
#include <QStack>
#include <QTimer> #include <QTimer>
#include <QUrl> #include <QUrl>
namespace QmlDesigner { 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<int> 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) CreateTexture::CreateTexture(AbstractView *view)
: m_view{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) ModelNode CreateTexture::execute(const QString &filePath, AddTextureMode mode, int sceneId)
{ {
Asset asset(filePath); Asset asset(filePath);
@@ -94,8 +154,12 @@ ModelNode CreateTexture::createTextureFromImage(const Utils::FilePath &assetPat
#endif #endif
newTexNode.setIdWithoutRefactoring(m_view->model()->generateNewId(assetPath.baseName())); 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"); VariantProperty sourceProp = newTexNode.variantProperty("source");
sourceProp.setValue(QUrl(textureSource)); sourceProp.setValue(QUrl(textureSource));
matLib.defaultNodeListProperty().reparentHere(newTexNode); matLib.defaultNodeListProperty().reparentHere(newTexNode);
} }

View File

@@ -23,7 +23,10 @@ class CreateTexture : public QObject
public: public:
CreateTexture(AbstractView *view); 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); ModelNode resolveSceneEnv(int sceneId);
void assignTextureAsLightProbe(const ModelNode &texture, int sceneId); void assignTextureAsLightProbe(const ModelNode &texture, int sceneId);

View File

@@ -12,17 +12,18 @@
#include <auxiliarydataproperties.h> #include <auxiliarydataproperties.h>
#include <bindingproperty.h> #include <bindingproperty.h>
#include <createtexture.h>
#include <dynamicpropertiesmodel.h> #include <dynamicpropertiesmodel.h>
#include <externaldependenciesinterface.h> #include <externaldependenciesinterface.h>
#include <nodeinstanceview.h> #include <nodeinstanceview.h>
#include <nodelistproperty.h> #include <nodelistproperty.h>
#include <nodemetainfo.h> #include <nodemetainfo.h>
#include <nodeproperty.h> #include <nodeproperty.h>
#include <rewritingexception.h>
#include <variantproperty.h>
#include <qmldesignerconstants.h> #include <qmldesignerconstants.h>
#include <qmldesignerplugin.h> #include <qmldesignerplugin.h>
#include <qmltimeline.h> #include <qmltimeline.h>
#include <rewritingexception.h>
#include <variantproperty.h>
#include <theme.h> #include <theme.h>
@@ -50,41 +51,6 @@
using namespace Qt::StringLiterals; 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<int> 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 { namespace QmlDesigner {
TextureEditorView::TextureEditorView(AsynchronousImageCache &imageCache, TextureEditorView::TextureEditorView(AsynchronousImageCache &imageCache,
@@ -92,6 +58,7 @@ TextureEditorView::TextureEditorView(AsynchronousImageCache &imageCache,
: AbstractView{externalDependencies} : AbstractView{externalDependencies}
, m_imageCache(imageCache) , m_imageCache(imageCache)
, m_stackedWidget(new QStackedWidget) , m_stackedWidget(new QStackedWidget)
, m_createTexture(new CreateTexture(this))
, m_dynamicPropertiesModel(new DynamicPropertiesModel(true, this)) , m_dynamicPropertiesModel(new DynamicPropertiesModel(true, this))
{ {
m_updateShortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_F12), m_stackedWidget); m_updateShortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_F12), m_stackedWidget);
@@ -413,23 +380,7 @@ void TextureEditorView::handleToolBarAction(int action)
case TextureEditorContextObject::AddNewTexture: { case TextureEditorContextObject::AddNewTexture: {
if (!model()) if (!model())
break; break;
executeInTransaction("TextureEditorView:handleToolBarAction", [&] { m_createTexture->execute();
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);
});
break; break;
} }

View File

@@ -18,6 +18,7 @@ QT_END_NAMESPACE
namespace QmlDesigner { namespace QmlDesigner {
class CreateTexture;
class DynamicPropertiesModel; class DynamicPropertiesModel;
class ModelNode; class ModelNode;
class TextureEditorQmlBackend; class TextureEditorQmlBackend;
@@ -118,6 +119,7 @@ private:
bool m_initializingPreviewData = false; bool m_initializingPreviewData = false;
QPointer<QColorDialog> m_colorDialog; QPointer<QColorDialog> m_colorDialog;
QPointer<CreateTexture> m_createTexture;
DynamicPropertiesModel *m_dynamicPropertiesModel = nullptr; DynamicPropertiesModel *m_dynamicPropertiesModel = nullptr;
}; };