QmlDesigner: Allow dragging image Assets to the Material Browser

When dragging an image from the Assets Library to the Textures section
of the Material Browser, now Textures will be created from those
assets.

Task-number: QDS-8783
Change-Id: Ibbfb8697705a7d1af7714f3a32caff24282ee7a9
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io>
This commit is contained in:
Samuel Ghinet
2023-02-01 12:10:34 +02:00
parent d42af9246f
commit 192ac5db61
10 changed files with 46 additions and 4 deletions

View File

@@ -34,6 +34,9 @@ Item {
var complexSuffixes = rootView.supportedAssetSuffixes(true) var complexSuffixes = rootView.supportedAssetSuffixes(true)
for (const u of drag.urls) { for (const u of drag.urls) {
var url = u.toString() var url = u.toString()
if (assetsModel.urlPathExistsInModel(url))
continue;
var ext = '*.' + url.slice(url.lastIndexOf('.') + 1).toLowerCase() var ext = '*.' + url.slice(url.lastIndexOf('.') + 1).toLowerCase()
if (simpleSuffixes.includes(ext)) if (simpleSuffixes.includes(ext))
root.dropSimpleExtFiles.push(url) root.dropSimpleExtFiles.push(url)

View File

@@ -716,6 +716,8 @@ Item {
onDropEnter: (drag) => { onDropEnter: (drag) => {
drag.accepted = drag.formats[0] === "application/vnd.qtdesignstudio.bundletexture" drag.accepted = drag.formats[0] === "application/vnd.qtdesignstudio.bundletexture"
|| drag.formats[0] === "application/vnd.qtdesignstudio.assets"
highlight = drag.accepted highlight = drag.accepted
} }
@@ -726,7 +728,10 @@ Item {
onDrop: (drag) => { onDrop: (drag) => {
drag.accept() drag.accept()
highlight = false highlight = false
if (drag.formats[0] === "application/vnd.qtdesignstudio.bundletexture")
rootView.acceptBundleTextureDrop() rootView.acceptBundleTextureDrop()
else if (drag.formats[0] === "application/vnd.qtdesignstudio.assets")
rootView.acceptAssetsDrop(drag.urls)
} }
onExpandedChanged: { onExpandedChanged: {

View File

@@ -150,6 +150,12 @@ bool AssetsLibraryModel::addNewFolder(const QString &folderPath)
return dir.mkpath(iterPath); return dir.mkpath(iterPath);
} }
bool AssetsLibraryModel::urlPathExistsInModel(const QUrl &url) const
{
QModelIndex index = indexForPath(url.toLocalFile());
return index.isValid();
}
bool AssetsLibraryModel::deleteFolderRecursively(const QModelIndex &folderIndex) bool AssetsLibraryModel::deleteFolderRecursively(const QModelIndex &folderIndex)
{ {
auto idx = mapToSource(folderIndex); auto idx = mapToSource(folderIndex);

View File

@@ -39,6 +39,7 @@ public:
Q_INVOKABLE QList<QModelIndex> parentIndices(const QModelIndex &index) const; Q_INVOKABLE QList<QModelIndex> parentIndices(const QModelIndex &index) const;
Q_INVOKABLE bool indexIsValid(const QModelIndex &index) const; Q_INVOKABLE bool indexIsValid(const QModelIndex &index) const;
Q_INVOKABLE bool urlPathExistsInModel(const QUrl &url) const;
Q_INVOKABLE QString currentProjectDirPath() const; Q_INVOKABLE QString currentProjectDirPath() const;
Q_INVOKABLE QString contentDirPath() const; Q_INVOKABLE QString contentDirPath() const;
Q_INVOKABLE bool requestDeleteFiles(const QStringList &filePaths); Q_INVOKABLE bool requestDeleteFiles(const QStringList &filePaths);

View File

@@ -72,8 +72,16 @@ bool AssetsLibraryWidget::eventFilter(QObject *obj, QEvent *event)
if ((me->globalPos() - m_dragStartPoint).manhattanLength() > 10) { if ((me->globalPos() - m_dragStartPoint).manhattanLength() > 10) {
QMimeData *mimeData = new QMimeData; QMimeData *mimeData = new QMimeData;
mimeData->setData(Constants::MIME_TYPE_ASSETS, m_assetsToDrag.join(',').toUtf8()); mimeData->setData(Constants::MIME_TYPE_ASSETS, m_assetsToDrag.join(',').toUtf8());
m_model->startDrag(mimeData,
m_assetsIconProvider->requestPixmap(m_assetsToDrag[0], nullptr, {128, 128})); QList<QUrl> urlsToDrag = Utils::transform(m_assetsToDrag, [](const QString &path) {
return QUrl::fromLocalFile(path);
});
mimeData->setUrls(urlsToDrag);
m_model->startDrag(mimeData, m_assetsIconProvider->requestPixmap(m_assetsToDrag[0],
nullptr, {128, 128}));
m_assetsToDrag.clear(); m_assetsToDrag.clear();
} }
} }

View File

@@ -235,7 +235,7 @@ ModelNodePreviewImageOperation DesignerActionManager::modelNodePreviewOperation(
bool DesignerActionManager::externalDragHasSupportedAssets(const QMimeData *mimeData) const bool DesignerActionManager::externalDragHasSupportedAssets(const QMimeData *mimeData) const
{ {
if (!mimeData->hasUrls()) if (!mimeData->hasUrls() || mimeData->hasFormat(Constants::MIME_TYPE_ASSETS))
return false; return false;
QSet<QString> filtersSet; QSet<QString> filtersSet;

View File

@@ -216,6 +216,17 @@ WidgetInfo MaterialBrowserView::widgetInfo()
tr("Material Browser view")); tr("Material Browser view"));
} }
void MaterialBrowserView::createTextures(const QStringList &assetPaths)
{
auto *create = new CreateTextures(this, false);
executeInTransaction("MaterialBrowserView::createTextures", [&]() {
create->execute(assetPaths, AddTextureMode::Texture, m_sceneId);
});
create->deleteLater();
}
void MaterialBrowserView::modelAttached(Model *model) void MaterialBrowserView::modelAttached(Model *model)
{ {
AbstractView::modelAttached(model); AbstractView::modelAttached(model);

View File

@@ -55,6 +55,7 @@ public:
void applyTextureToModel3D(const QmlObjectNode &model3D, const ModelNode &texture = {}); void applyTextureToModel3D(const QmlObjectNode &model3D, const ModelNode &texture = {});
void applyTextureToMaterial(const QList<ModelNode> &materials, const ModelNode &texture); void applyTextureToMaterial(const QList<ModelNode> &materials, const ModelNode &texture);
void createTextures(const QStringList &assetPaths);
Q_INVOKABLE void updatePropsModel(const QString &matId); Q_INVOKABLE void updatePropsModel(const QString &matId);
Q_INVOKABLE void applyTextureToProperty(const QString &matId, const QString &propName); Q_INVOKABLE void applyTextureToProperty(const QString &matId, const QString &propName);

View File

@@ -278,6 +278,12 @@ void MaterialBrowserWidget::acceptBundleTextureDrop()
m_materialBrowserView->model()->endDrag(); m_materialBrowserView->model()->endDrag();
} }
void MaterialBrowserWidget::acceptAssetsDrop(const QList<QUrl> &urls)
{
QStringList assetPaths = Utils::transform(urls, [](const QUrl &url) { return url.toLocalFile(); });
m_materialBrowserView->createTextures(assetPaths);
}
void MaterialBrowserWidget::acceptTextureDropOnMaterial(int matIndex, const QString &texId) void MaterialBrowserWidget::acceptTextureDropOnMaterial(int matIndex, const QString &texId)
{ {
ModelNode mat = m_materialBrowserModel->materialAt(matIndex); ModelNode mat = m_materialBrowserModel->materialAt(matIndex);

View File

@@ -59,6 +59,7 @@ public:
Q_INVOKABLE void startDragTexture(int index, const QPointF &mousePos); Q_INVOKABLE void startDragTexture(int index, const QPointF &mousePos);
Q_INVOKABLE void acceptBundleMaterialDrop(); Q_INVOKABLE void acceptBundleMaterialDrop();
Q_INVOKABLE void acceptBundleTextureDrop(); Q_INVOKABLE void acceptBundleTextureDrop();
Q_INVOKABLE void acceptAssetsDrop(const QList<QUrl> &urls);
Q_INVOKABLE void acceptTextureDropOnMaterial(int matIndex, const QString &texId); Q_INVOKABLE void acceptTextureDropOnMaterial(int matIndex, const QString &texId);
Q_INVOKABLE void focusMaterialSection(bool focusMatSec); Q_INVOKABLE void focusMaterialSection(bool focusMatSec);