aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMahmoud Badri <mahmoud.badri@qt.io>2024-04-25 11:59:01 +0300
committerMahmoud Badri <mahmoud.badri@qt.io>2024-04-26 09:41:17 +0000
commit17a28ea850604af6e4cf84d5b3a7198d2d03280f (patch)
tree30ed8846cca85f857bc5945121936c5f1fcf8e7c
parent9b4aa051d4effefc5b1a2cb1e1ed4d7a89ea831d (diff)
QmlDesigner: Allow removing a content library material
Fixes: QDS-12541 Change-Id: I8efdd5c5f6185961bd8440e06d0adb60ad9d79f2 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
-rw-r--r--share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibrary.qml12
-rw-r--r--share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryMaterialContextMenu.qml9
-rw-r--r--share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml3
-rw-r--r--share/qtcreator/qmldesigner/contentLibraryQmlSource/DeleteBundleItemDialog.qml65
-rw-r--r--share/qtcreator/qmldesigner/contentLibraryQmlSource/UnimportBundleItemDialog.qml (renamed from share/qtcreator/qmldesigner/contentLibraryQmlSource/UnimportBundleMaterialDialog.qml)2
-rw-r--r--src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.cpp5
-rw-r--r--src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.h1
-rw-r--r--src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp41
-rw-r--r--src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.h6
-rw-r--r--src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp3
10 files changed, 140 insertions, 7 deletions
diff --git a/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibrary.qml b/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibrary.qml
index 2c98b58adc7..d06d942e3f9 100644
--- a/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibrary.qml
+++ b/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibrary.qml
@@ -130,10 +130,14 @@ Item {
}
}
- UnimportBundleMaterialDialog {
+ UnimportBundleItemDialog {
id: confirmUnimportDialog
}
+ DeleteBundleItemDialog {
+ id: confirmDeleteDialog
+ }
+
StackLayout {
id: stackLayout
width: root.width
@@ -246,6 +250,12 @@ Item {
confirmUnimportDialog.open()
}
+ onRemoveFromContentLib: (bundleItem) => {
+ confirmDeleteDialog.targetBundleItem = bundleItem
+ confirmDeleteDialog.targetBundleLabel = "material"
+ confirmDeleteDialog.open()
+ }
+
onCountChanged: root.responsiveResize(stackLayout.width, stackLayout.height)
}
}
diff --git a/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryMaterialContextMenu.qml b/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryMaterialContextMenu.qml
index b67ec311ef0..f1fa81ed76e 100644
--- a/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryMaterialContextMenu.qml
+++ b/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryMaterialContextMenu.qml
@@ -12,12 +12,14 @@ StudioControls.Menu {
property var targetMaterial: null
property bool hasModelSelection: false
property bool importerRunning: false
+ property bool enableRemove: false // true: adds an option to remove targetMaterial
readonly property bool targetAvailable: targetMaterial && !importerRunning
signal unimport();
signal addToProject()
signal applyToSelected(bool add)
+ signal removeFromContentLib()
function popupMenu(targetMaterial = null)
{
@@ -56,4 +58,11 @@ StudioControls.Menu {
onTriggered: root.unimport()
}
+
+ StudioControls.MenuItem {
+ text: qsTr("Remove from Content Library")
+ visible: root.enableRemove && root.targetAvailable
+ height: visible ? implicitHeight : 0
+ onTriggered: root.removeFromContentLib()
+ }
}
diff --git a/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml b/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml
index d711a0fc310..8fd196dbcde 100644
--- a/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml
+++ b/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml
@@ -31,6 +31,7 @@ HelperWidgets.ScrollView {
required property var searchBox
signal unimport(var bundleItem);
+ signal removeFromContentLib(var bundleItem);
function closeContextMenu() {
ctxMenuMaterial.close()
@@ -49,6 +50,7 @@ HelperWidgets.ScrollView {
ContentLibraryMaterialContextMenu {
id: ctxMenuMaterial
+ enableRemove: true
hasModelSelection: ContentLibraryBackend.userModel.hasModelSelection
importerRunning: ContentLibraryBackend.userModel.importerRunning
@@ -56,6 +58,7 @@ HelperWidgets.ScrollView {
onUnimport: root.unimport(ctxMenuMaterial.targetMaterial)
onAddToProject: ContentLibraryBackend.userModel.addToProject(ctxMenuMaterial.targetMaterial)
+ onRemoveFromContentLib: root.removeFromContentLib(ctxMenuMaterial.targetMaterial)
}
ContentLibraryTextureContextMenu {
diff --git a/share/qtcreator/qmldesigner/contentLibraryQmlSource/DeleteBundleItemDialog.qml b/share/qtcreator/qmldesigner/contentLibraryQmlSource/DeleteBundleItemDialog.qml
new file mode 100644
index 00000000000..d8f6551ae59
--- /dev/null
+++ b/share/qtcreator/qmldesigner/contentLibraryQmlSource/DeleteBundleItemDialog.qml
@@ -0,0 +1,65 @@
+// Copyright (C) 2024 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+import QtQuick
+import QtQuick.Controls
+import QtQuick.Layouts
+import HelperWidgets
+import StudioControls as StudioControls
+import StudioTheme as StudioTheme
+import ContentLibraryBackend
+
+StudioControls.Dialog {
+ id: root
+
+ property var targetBundleItem
+ property var targetBundleModel
+ property string targetBundleLabel // "effect" or "material"
+
+ title: qsTr("Remove bundle %1").arg(root.targetBundleLabel)
+ anchors.centerIn: parent
+ closePolicy: Popup.CloseOnEscape
+ implicitWidth: 300
+ modal: true
+
+ onOpened: warningText.forceActiveFocus()
+
+ contentItem: Column {
+ spacing: 20
+ width: parent.width
+
+ Text {
+ id: warningText
+
+ text: qsTr("Are you sure you? The action cannot be undone")
+ color: StudioTheme.Values.themeTextColor
+ wrapMode: Text.WordWrap
+ anchors.right: parent.right
+ anchors.left: parent.left
+ leftPadding: 10
+ rightPadding: 10
+
+ Keys.onEnterPressed: btnRemove.onClicked()
+ Keys.onReturnPressed: btnRemove.onClicked()
+ }
+
+ Row {
+ anchors.right: parent.right
+ Button {
+ id: btnRemove
+
+ text: qsTr("Remove")
+
+ onClicked: {
+ ContentLibraryBackend.userModel.removeFromContentLib(root.targetBundleItem)
+ root.accept()
+ }
+ }
+
+ Button {
+ text: qsTr("Cancel")
+ onClicked: root.reject()
+ }
+ }
+ }
+}
diff --git a/share/qtcreator/qmldesigner/contentLibraryQmlSource/UnimportBundleMaterialDialog.qml b/share/qtcreator/qmldesigner/contentLibraryQmlSource/UnimportBundleItemDialog.qml
index 4385e3bf82e..305e1018e37 100644
--- a/share/qtcreator/qmldesigner/contentLibraryQmlSource/UnimportBundleMaterialDialog.qml
+++ b/share/qtcreator/qmldesigner/contentLibraryQmlSource/UnimportBundleItemDialog.qml
@@ -13,8 +13,8 @@ StudioControls.Dialog {
id: root
property var targetBundleItem
- property var targetBundleLabel // "effect" or "material"
property var targetBundleModel
+ property string targetBundleLabel // "effect" or "material"
title: qsTr("Bundle %1 might be in use").arg(root.targetBundleLabel)
anchors.centerIn: parent
diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.cpp
index 834bc8aa30c..1d30b0217d8 100644
--- a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.cpp
+++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.cpp
@@ -32,6 +32,11 @@ bool ContentLibraryMaterial::filter(const QString &searchText)
return m_visible;
}
+QString ContentLibraryMaterial::name() const
+{
+ return m_name;
+}
+
QUrl ContentLibraryMaterial::icon() const
{
return m_icon;
diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.h b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.h
index 55af2accbd3..a2f53b7e3cf 100644
--- a/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.h
+++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibrarymaterial.h
@@ -37,6 +37,7 @@ public:
Q_INVOKABLE bool isDownloaded() const;
+ QString name() const;
QUrl icon() const;
QString qml() const;
TypeName type() const;
diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp
index 793e3bbe7ad..75ec5c3650f 100644
--- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp
+++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.cpp
@@ -33,9 +33,6 @@ ContentLibraryUserModel::ContentLibraryUserModel(ContentLibraryWidget *parent)
, m_widget(parent)
{
m_userCategories = {tr("Materials"), tr("Textures")/*, tr("3D"), tr("Effects"), tr("2D components")*/}; // TODO
-
- loadMaterialBundle();
- loadTextureBundle();
}
int ContentLibraryUserModel::rowCount(const QModelIndex &) const
@@ -136,6 +133,44 @@ void ContentLibraryUserModel::removeTexture(ContentLibraryTexture *tex)
emit dataChanged(index(texSectionIdx), index(texSectionIdx));
}
+void ContentLibraryUserModel::removeFromContentLib(ContentLibraryMaterial *mat)
+{
+ auto bundlePath = Utils::FilePath::fromString(Paths::bundlesPathSetting() + "/User/materials/");
+
+ QJsonObject matsObj = m_bundleObj.value("materials").toObject();
+
+ // remove qml and icon files
+ Utils::FilePath::fromString(mat->qmlFilePath()).removeFile();
+ Utils::FilePath::fromUrl(mat->icon()).removeFile();
+
+ // remove from the bundle json file
+ matsObj.remove(mat->name());
+ m_bundleObj.insert("materials", matsObj);
+ auto result = bundlePath.pathAppended("user_materials_bundle.json")
+ .writeFileContents(QJsonDocument(m_bundleObj).toJson());
+ if (!result)
+ qWarning() << __FUNCTION__ << result.error();
+
+ // delete dependency files if they are only used by the deleted material
+ QStringList allFiles;
+ for (const QJsonValueConstRef &mat : std::as_const(matsObj))
+ allFiles.append(mat.toObject().value("files").toVariant().toStringList());
+
+ const QStringList matFiles = mat->files();
+ for (const QString &matFile : matFiles) {
+ if (allFiles.count(matFile) == 0) // only used by the deleted material
+ bundlePath.pathAppended(matFile).removeFile();
+ }
+
+ // remove from model
+ m_userMaterials.removeOne(mat);
+ mat->deleteLater();
+
+ // update model
+ int matSectionIdx = 0;
+ emit dataChanged(index(matSectionIdx), index(matSectionIdx));
+}
+
// returns unique library material's name and qml component
QPair<QString, QString> ContentLibraryUserModel::getUniqueLibMaterialNameAndQml(const QString &matName) const
{
diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.h b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.h
index affa9a89120..1bda49674a8 100644
--- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.h
+++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryusermodel.h
@@ -67,12 +67,16 @@ public:
void setBundleObj(const QJsonObject &newBundleObj);
QJsonObject &bundleJsonObjectRef();
+ void loadMaterialBundle();
+ void loadTextureBundle();
+
Internal::ContentLibraryBundleImporter *bundleImporter() const;
Q_INVOKABLE void applyToSelected(QmlDesigner::ContentLibraryMaterial *mat, bool add = false);
Q_INVOKABLE void addToProject(QmlDesigner::ContentLibraryMaterial *mat);
Q_INVOKABLE void removeFromProject(QmlDesigner::ContentLibraryMaterial *mat);
Q_INVOKABLE void removeTexture(QmlDesigner::ContentLibraryTexture *tex);
+ Q_INVOKABLE void removeFromContentLib(QmlDesigner::ContentLibraryMaterial *mat);
signals:
void isEmptyChanged();
@@ -96,8 +100,6 @@ signals:
void matBundleExistsChanged();
private:
- void loadMaterialBundle();
- void loadTextureBundle();
bool isValidIndex(int idx) const;
void createImporter(const QString &bundlePath, const QString &bundleId,
const QStringList &sharedFiles);
diff --git a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp
index dd8a4d9919e..1e0b69099be 100644
--- a/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp
+++ b/src/plugins/qmldesigner/components/contentlibrary/contentlibraryview.cpp
@@ -304,6 +304,9 @@ void ContentLibraryView::modelAttached(Model *model)
m_widget->effectsModel()->loadBundle();
updateBundleEffectsImportedState();
+
+ m_widget->userModel()->loadMaterialBundle();
+ m_widget->userModel()->loadTextureBundle();
}
void ContentLibraryView::modelAboutToBeDetached(Model *model)