QmlDesigner: Fix textures created from assets can have invalid source

When creating a texture from an image in the asset library, the "source"
for the texture was wrong for images outside of the "content/images"
directory of the project.

Task-number: QDS-8535
Change-Id: I30a28a7cb63748ce0fb81396ca59c7ccc9e83340
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
Samuel Ghinet
2022-12-14 19:35:36 +02:00
parent aaf2e6f0f7
commit 0feeb37ef7

View File

@@ -65,19 +65,27 @@ ModelNode CreateTexture::createTextureFromImage(const QString &assetPath, AddTex
NodeMetaInfo metaInfo = m_view->model()->qtQuick3DTextureMetaInfo();
Utils::FilePath imagePath = ModelNodeOperations::getImagesDefaultDirectory()
.pathAppended(Utils::FilePath::fromString(assetPath).fileName());
QString sourceVal = imagePath.relativePathFrom(
QmlDesigner::DocumentManager::currentFilePath()).toString();
Utils::FilePath currentDocumentPath = QmlDesigner::DocumentManager::currentFilePath();
Utils::FilePath imageTargetPath;
if (m_importFile) {
QString assetName = Utils::FilePath::fromString(assetPath).fileName();
// if the asset had to be imported from somewhere else, then assetPath is the source where
// the asset was taken from, and we have to compute where it was placed in the project.
imageTargetPath = ModelNodeOperations::getImagesDefaultDirectory().pathAppended(assetName);
} else {
imageTargetPath = Utils::FilePath::fromString(assetPath);
}
ModelNode newTexNode = m_view->getTextureDefaultInstance(sourceVal);
QString textureSource = imageTargetPath.relativePathFrom(currentDocumentPath).toString();
ModelNode newTexNode = m_view->getTextureDefaultInstance(textureSource);
if (!newTexNode.isValid()) {
newTexNode = m_view->createModelNode("QtQuick3D.Texture",
metaInfo.majorVersion(),
metaInfo.minorVersion());
newTexNode.validId();
VariantProperty sourceProp = newTexNode.variantProperty("source");
sourceProp.setValue(sourceVal);
sourceProp.setValue(textureSource);
matLib.defaultNodeListProperty().reparentHere(newTexNode);
}