Browse Source

Simplify creating, duplicating materials in preferences, extract logic to python, code-style fixes

ChrisTerBeke 7 years ago
parent
commit
3de4940d69
2 changed files with 58 additions and 62 deletions
  1. 16 0
      cura/Settings/ContainerManager.py
  2. 42 62
      resources/qml/Preferences/MaterialsPage.qml

+ 16 - 0
cura/Settings/ContainerManager.py

@@ -816,6 +816,22 @@ class ContainerManager(QObject):
             ContainerRegistry.getInstance().addContainer(container_to_add)
         return self._getMaterialContainerIdForActiveMachine(clone_of_original)
 
+    ##  Create a duplicate of a material or it's original entry
+    #
+    #   \return \type{str} the id of the newly created container.
+    @pyqtSlot(str, result = str)
+    def duplicateOriginalMaterial(self, material_id):
+
+        # check if the given material has a base file (i.e. was shipped by default)
+        base_file = self.getContainerMetaDataEntry(material_id, "base_file")
+
+        if base_file == "":
+            # there is no base file, so duplicate by ID
+            return self.duplicateMaterial(material_id)
+        else:
+            # there is a base file, so duplicate the original material
+            return self.duplicateMaterial(base_file)
+
     ##  Create a new material by cloning Generic PLA for the current material diameter and setting the GUID to something unqiue
     #
     #   \return \type{str} the id of the newly created container.

+ 42 - 62
resources/qml/Preferences/MaterialsPage.qml

@@ -132,93 +132,73 @@ UM.ManagementPage
     }
 
     buttons: [
-        Button
-        {
-            text: catalog.i18nc("@action:button", "Activate");
+
+        // Activate button
+        Button {
+            text: catalog.i18nc("@action:button", "Activate")
             iconName: "list-activate";
             enabled: base.currentItem != null && base.currentItem.id != Cura.MachineManager.activeMaterialId && Cura.MachineManager.hasMaterials
-            onClicked:
-            {
-                forceActiveFocus();
+            onClicked: {
+                forceActiveFocus()
                 Cura.MachineManager.setActiveMaterial(base.currentItem.id)
                 currentItem = base.model.getItem(base.objectList.currentIndex) // Refresh the current item.
             }
         },
-        Button
-        {
+
+        // Create button
+        Button {
             text: catalog.i18nc("@action:button", "Create")
             iconName: "list-add"
-            onClicked:
-            {
-                forceActiveFocus();
-                var material_id = Cura.ContainerManager.createMaterial()
-                if(material_id == "")
-                {
-                    return
-                }
-                if(Cura.MachineManager.hasMaterials)
-                {
-                    Cura.MachineManager.setActiveMaterial(material_id)
-                }
-                base.objectList.currentIndex = base.getIndexById(material_id);
+            onClicked: {
+                forceActiveFocus()
+                Cura.ContainerManager.createMaterial()
             }
         },
-        Button
-        {
+
+        // Duplicate button
+        Button {
             text: catalog.i18nc("@action:button", "Duplicate");
             iconName: "list-add";
             enabled: base.currentItem != null
-            onClicked:
-            {
-                forceActiveFocus();
-                var base_file = Cura.ContainerManager.getContainerMetaDataEntry(base.currentItem.id, "base_file")
-                // We need to copy the base container instead of the specific variant.
-                var material_id = base_file == "" ? Cura.ContainerManager.duplicateMaterial(base.currentItem.id): Cura.ContainerManager.duplicateMaterial(base_file)
-                if(material_id == "")
-                {
-                    return
-                }
-                if(Cura.MachineManager.hasMaterials)
-                {
-                    Cura.MachineManager.setActiveMaterial(material_id)
-                }
-                // TODO: this doesn't work because the source is a bit delayed
-                base.objectList.currentIndex = base.getIndexById(material_id);
+            onClicked: {
+                forceActiveFocus()
+                Cura.ContainerManager.duplicateOriginalMaterial(base.currentItem.id)
             }
         },
-        Button
-        {
-            text: catalog.i18nc("@action:button", "Remove");
-            iconName: "list-remove";
+
+        // Remove button
+        Button {
+            text: catalog.i18nc("@action:button", "Remove")
+            iconName: "list-remove"
             enabled: base.currentItem != null && !base.currentItem.readOnly && !Cura.ContainerManager.isContainerUsed(base.currentItem.id)
-            onClicked:
-            {
-                forceActiveFocus();
-                confirmDialog.open();
+            onClicked: {
+                forceActiveFocus()
+                confirmDialog.open()
             }
         },
-        Button
-        {
-            text: catalog.i18nc("@action:button", "Import");
-            iconName: "document-import";
-            onClicked:
-            {
-                forceActiveFocus();
-                importDialog.open();
+
+        // Import button
+        Button {
+            text: catalog.i18nc("@action:button", "Import")
+            iconName: "document-import"
+            onClicked: {
+                forceActiveFocus()
+                importDialog.open()
             }
-            visible: true;
+            visible: true
         },
-        Button
-        {
+
+        // Export button
+        Button {
             text: catalog.i18nc("@action:button", "Export")
             iconName: "document-export"
-            onClicked:
-            {
-                forceActiveFocus();
-                exportDialog.open();
+            onClicked: {
+                forceActiveFocus()
+                exportDialog.open()
             }
             enabled: currentItem != null
         }
+
     ]
 
     Item {