Browse Source

CURA-4606 Ensure the correct material is highlighted in the material menu

Jack Ha 7 years ago
parent
commit
27169e883c

+ 1 - 2
cura/Machines/MaterialManager.py

@@ -4,7 +4,6 @@ from PyQt5.Qt import QTimer, QObject, pyqtSignal
 
 from UM.Logger import Logger
 from UM.Settings import ContainerRegistry
-from UM.Settings import InstanceContainer
 
 from cura.Machines.ContainerNode import ContainerNode
 
@@ -227,7 +226,7 @@ class MaterialManager(QObject):
     #  1. the given machine doesn't have materials;
     #  2. cannot find any material InstanceContainers with the given settings.
     #
-    def getMaterial(self, machine_definition_id: str, variant_name: Optional[str], diameter: float, root_material_id: str) -> Optional["InstanceContainer"]:
+    def getMaterialNode(self, machine_definition_id: str, variant_name: Optional[str], diameter: float, root_material_id: str) -> Optional["InstanceContainer"]:
         # round the diameter to get the approximate diameter
         rounded_diameter = str(round(diameter))
         if rounded_diameter not in self._diameter_machine_variant_material_map:

+ 17 - 1
cura/Settings/MachineManager.py

@@ -135,11 +135,11 @@ class MachineManager(QObject):
 
         ### NEW
         self._current_quality_group = None
+        self._current_root_material_id = {}
 
     ### NEW
     activeQualityGroupChanged = pyqtSignal()
 
-
     globalContainerChanged = pyqtSignal()  # Emitted whenever the global stack is changed (ie: when changing between printers, changing a global profile, but not when changing a value)
     activeMaterialChanged = pyqtSignal()
     activeVariantChanged = pyqtSignal()
@@ -155,6 +155,8 @@ class MachineManager(QObject):
 
     outputDevicesChanged = pyqtSignal()
 
+    rootMaterialChanged = pyqtSignal()
+
     def _onOutputDevicesChanged(self) -> None:
         for printer_output_device in self._printer_output_devices:
             printer_output_device.hotendIdChanged.disconnect(self._onHotendIdChanged)
@@ -1491,6 +1493,15 @@ class MachineManager(QObject):
     #
     # New
     #
+
+    @pyqtProperty("QVariant", notify = rootMaterialChanged)
+    def currentRootMaterialId(self):
+        # initial filling the current_root_material_id
+        for position in self._global_container_stack.extruders:
+            if position not in self._current_root_material_id:
+                self._current_root_material_id[position] = self._global_container_stack.extruders[position].material.getMetaDataEntry("base_file")
+        return self._current_root_material_id
+
     def _setEmptyQuality(self):
         self._current_quality_group = None
         self._global_container_stack.quality = self._empty_quality_container
@@ -1523,6 +1534,11 @@ class MachineManager(QObject):
             self._global_container_stack.extruders[position].material = container_node.getContainer()
         else:
             self._global_container_stack.extruders[position].material = self._empty_material_container
+        # The _current_root_material_id is used in the MaterialMenu to see which material is selected
+        root_material_id = container_node.metadata["base_file"]
+        if root_material_id != self._current_root_material_id[position]:
+            self._current_root_material_id[position] = root_material_id
+            self.rootMaterialChanged.emit()
 
     ## Update current quality type and machine after setting material
     def _updateQualityWithMaterial(self):

+ 2 - 57
resources/qml/Menus/MaterialMenu.qml

@@ -14,48 +14,6 @@ Menu
 
     property int extruderIndex: 0
     property bool printerConnected: Cura.MachineManager.printerOutputDevices.length != 0
-    property bool isClusterPrinter:
-    {
-        if(Cura.MachineManager.printerOutputDevices.length == 0)
-        {
-            return false;
-        }
-        var clusterSize = Cura.MachineManager.printerOutputDevices[0].clusterSize;
-        // This is not a cluster printer or the cluster it is just one printer
-        if(clusterSize == undefined || clusterSize == 1)
-        {
-            return false;
-        }
-        return true;
-    }
-
-    MenuItem
-    {
-        id: automaticMaterial
-        text:
-        {
-            if(visible)
-            {
-                var materialName = Cura.MachineManager.printerOutputDevices[0].materialNames[extruderIndex];
-                return catalog.i18nc("@title:menuitem %1 is the automatically selected material", "Automatic: %1").arg(materialName);
-            }
-            return "";
-        }
-        visible: printerConnected && Cura.MachineManager.printerOutputDevices[0].materialNames != undefined && Cura.MachineManager.printerOutputDevices[0].materialNames.length > extruderIndex && !isClusterPrinter
-        onTriggered:
-        {
-            var materialId = Cura.MachineManager.printerOutputDevices[0].materialIds[extruderIndex];
-            var items = materialsModel.items;
-            for(var i in items)
-            {
-                if (items[i]["metadata"]["GUID"] == materialId)
-                {
-                    Cura.MachineManager.setActiveMaterial(items[i].id);
-                    break;
-                }
-            }
-        }
-    }
 
     MenuSeparator
     {
@@ -69,19 +27,11 @@ Menu
         {
             text: model.name
             checkable: true
-            checked: model.id == Cura.MachineManager.allActiveMaterialIds[Cura.ExtruderManager.extruderIds[extruderIndex]]
+            checked: model.root_material_id == Cura.MachineManager.currentRootMaterialId[Cura.ExtruderManager.activeExtruderIndex]
             exclusiveGroup: group
             onTriggered:
             {
-
-                const container_id = model.id;
-                // This workaround is done because of the application menus for materials and variants for multiextrusion printers.
-                // The extruder menu would always act on the correspoding extruder only, instead of acting on the extruder selected in the UI.
-
                 var activeExtruderIndex = Cura.ExtruderManager.activeExtruderIndex;
-                //Cura.ExtruderManager.setActiveExtruderIndex(extruderIndex);
-                //Cura.MachineManager.setActiveMaterial(container_id);
-                //Cura.ExtruderManager.setActiveExtruderIndex(activeExtruderIndex);
                 Cura.MachineManager.setMaterial(activeExtruderIndex, model.container_node);
             }
         }
@@ -120,13 +70,8 @@ Menu
                             exclusiveGroup: group
                             onTriggered:
                             {
-                                const container_id = model.id;
-                                // This workaround is done because of the application menus for materials and variants for multiextrusion printers.
-                                // The extruder menu would always act on the correspoding extruder only, instead of acting on the extruder selected in the UI.
                                 var activeExtruderIndex = Cura.ExtruderManager.activeExtruderIndex;
-                                Cura.ExtruderManager.setActiveExtruderIndex(extruderIndex);
-                                Cura.MachineManager.setActiveMaterial(container_id);
-                                Cura.ExtruderManager.setActiveExtruderIndex(activeExtruderIndex);
+                                Cura.MachineManager.setMaterial(activeExtruderIndex, model.container_node);
                             }
                         }
                         onObjectAdded: brandMaterialsMenu.insertItem(index, object)