Browse Source

WIP: Make material removal work in material management dialog

Lipu Fei 7 years ago
parent
commit
779f49f545
2 changed files with 43 additions and 17 deletions
  1. 16 6
      cura/Settings/ContainerManager.py
  2. 27 11
      resources/qml/Preferences/MaterialsPage.qml

+ 16 - 6
cura/Settings/ContainerManager.py

@@ -759,6 +759,19 @@ class ContainerManager(QObject):
 
         return new_change_instances
 
+    @pyqtSlot("QVariant")
+    def removeMaterial(self, material_node):
+        root_material_id = material_node.metadata["base_file"]
+        material_group = self._material_manager.getMaterialGroup(root_material_id)
+        if not material_group:
+            Logger.log("d", "Unable to remove the material with id %s, because it doesn't exist.", root_material_id)
+            return
+
+        nodes_to_remove = [material_group.root_material_node] + material_group.derived_material_node_list
+        for node in nodes_to_remove:
+            self._container_registry.removeContainer(node.metadata["id"])
+
+
     ##  Create a duplicate of a material, which has the same GUID and base_file metadata
     #
     #   \return \type{str} the id of the newly created container.
@@ -769,7 +782,7 @@ class ContainerManager(QObject):
         material_group = self._material_manager.getMaterialGroup(root_material_id)
         if not material_group:
             Logger.log("d", "Unable to duplicate the material with id %s, because it doesn't exist.", root_material_id)
-            return ""
+            return
 
         base_container = material_group.root_material_node.getContainer()
         containers_to_copy = []
@@ -787,10 +800,9 @@ class ContainerManager(QObject):
         new_base_container.getMetaData()["base_file"] = new_base_id
         new_containers.append(new_base_container)
 
-        #Clone all of them.
-        clone_of_original = None #Keeping track of which one is the clone of the original material, since we need to return that.
+        # Clone all of them.
         for container_to_copy in containers_to_copy:
-            #Create unique IDs for every clone.
+            # Create unique IDs for every clone.
             current_id = container_to_copy.getId()
             new_id = new_base_id
             if container_to_copy.getMetaDataEntry("definition") != "fdmprinter":
@@ -798,8 +810,6 @@ class ContainerManager(QObject):
                 if container_to_copy.getMetaDataEntry("variant_name"):
                     variant_name = container_to_copy.getMetaDataEntry("variant_name")
                     new_id += "_" + variant_name.replace(" ", "_")
-            if current_id == root_material_id:
-                clone_of_original = new_id
 
             new_container = copy.deepcopy(container_to_copy)
             new_container.getMetaData()["id"] = new_id

+ 27 - 11
resources/qml/Preferences/MaterialsPage.qml

@@ -4,6 +4,7 @@
 import QtQuick 2.8
 import QtQuick.Controls 1.4
 import QtQuick.Layouts 1.3
+import QtQuick.Dialogs 1.3
 
 import UM 1.2 as UM
 import Cura 1.0 as Cura
@@ -34,16 +35,14 @@ Item
         text: catalog.i18nc("@title:tab", "Materials")
     }
 
-    property var hasCurrentItem: materialListView.currentItem != null;
+    property var hasCurrentItem: materialListView.currentItem != null
 
-    property var currentItem:
-    {
+    property var currentItem: {
         var current_index = materialListView.currentIndex;
         return materialsModel.getItem(current_index);
     }
 
-    property var isCurrentItemActivated:
-    {
+    property var isCurrentItemActivated: {
         const extruder_position = Cura.ExtruderManager.activeExtruderIndex;
         const root_material_id = Cura.MachineManager.currentRootMaterialId[extruder_position];
         return base.currentItem.root_material_id == root_material_id;
@@ -88,8 +87,7 @@ Item
             iconName: "list-add"
             enabled: base.hasCurrentItem
             onClicked: {
-                forceActiveFocus()
-
+                forceActiveFocus();
                 Cura.ContainerManager.duplicateMaterial(base.currentItem.container_node);
             }
         }
@@ -98,11 +96,10 @@ Item
         Button {
             text: catalog.i18nc("@action:button", "Remove")
             iconName: "list-remove"
-            //enabled: base.currentItem != null && !base.currentItem.readOnly && !Cura.ContainerManager.isContainerUsed(base.currentItem.id)
-            enabled: true // TODO
+            enabled: base.hasCurrentItem && !base.currentItem.is_read_only && !base.isCurrentItemActivated
             onClicked: {
-                forceActiveFocus()
-                // TODO
+                forceActiveFocus();
+                confirmRemoveMaterialDialog.open();
             }
         }
 
@@ -129,6 +126,25 @@ Item
         }
     }
 
+    MessageDialog
+    {
+        id: confirmRemoveMaterialDialog
+
+        icon: StandardIcon.Question;
+        title: catalog.i18nc("@title:window", "Confirm Remove")
+        text: catalog.i18nc("@label (%1 is object name)", "Are you sure you wish to remove %1? This cannot be undone!").arg(base.currentItem.name)
+        standardButtons: StandardButton.Yes | StandardButton.No
+        modality: Qt.ApplicationModal
+
+        onYes:
+        {
+            Cura.ContainerManager.removeMaterial(base.currentItem.container_node);
+            // reset current item to the first if available
+            materialListView.currentIndex = 0;
+        }
+    }
+
+
     Item {
         id: contentsItem