Browse Source

Fix typing and deduplicate fetch_layer_height

CURA-6840
Nino van Hooff 5 years ago
parent
commit
88e0a57374

+ 6 - 35
cura/Machines/Models/IntentModel.py

@@ -7,9 +7,10 @@ from PyQt5.QtCore import Qt, QObject, pyqtProperty, pyqtSignal
 import cura.CuraApplication
 from UM.Qt.ListModel import ListModel
 from UM.Settings.ContainerRegistry import ContainerRegistry
-from UM.Settings.SettingFunction import SettingFunction
-from cura.Machines import MaterialNode, QualityGroup
 from cura.Machines.ContainerTree import ContainerTree
+from cura.Machines.MaterialNode import MaterialNode
+from cura.Machines.Models.MachineModelUtils import fetch_layer_height
+from cura.Machines.QualityGroup import QualityGroup
 
 
 class IntentModel(ListModel):
@@ -78,7 +79,7 @@ class IntentModel(ListModel):
         for quality_tuple, quality_group in quality_groups.items():
             # Add the intents that are of the correct category
             if quality_tuple[0] != self._intent_category:
-                layer_height = self._fetchLayerHeight(quality_group)
+                layer_height = fetch_layer_height(quality_group)
                 if layer_height not in layer_heights_added:
                     new_items.append({"name": "Unavailable",
                                       "quality_type": "",
@@ -95,7 +96,7 @@ class IntentModel(ListModel):
         global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
         container_tree = ContainerTree.getInstance()
         machine_node = container_tree.machines[global_stack.definition.getId()]
-        nodes = set([])
+        nodes = set()  # type: Set[MaterialNode]
 
         for extruder in global_stack.extruderList:
             active_variant_name = extruder.variant.getMetaDataEntry("name")
@@ -112,7 +113,7 @@ class IntentModel(ListModel):
             if quality_node.quality_type not in quality_groups:  # Don't add the empty quality type (or anything else that would crash, defensively).
                 continue
             quality_group = quality_groups[quality_node.quality_type]
-            layer_height = self._fetchLayerHeight(quality_group)
+            layer_height = fetch_layer_height(quality_group)
 
             for intent_id, intent_node in quality_node.intents.items():
                 if intent_node.intent_category != self._intent_category:
@@ -125,35 +126,5 @@ class IntentModel(ListModel):
                                   })
         return extruder_intents
 
-    #TODO: Copied this from QualityProfilesDropdownMenuModel for the moment. This code duplication should be fixed.
-    def _fetchLayerHeight(self, quality_group: QualityGroup) -> float:
-        global_stack = cura.CuraApplication.CuraApplication.getInstance().getMachineManager().activeMachine
-        if not self._layer_height_unit:
-            unit = global_stack.definition.getProperty("layer_height", "unit")
-            if not unit:
-                unit = ""
-            self._layer_height_unit = unit
-
-        default_layer_height = global_stack.definition.getProperty("layer_height", "value")
-
-        # Get layer_height from the quality profile for the GlobalStack
-        if quality_group.node_for_global is None:
-            return float(default_layer_height)
-        container = quality_group.node_for_global.container
-
-        layer_height = default_layer_height
-        if container and container.hasProperty("layer_height", "value"):
-            layer_height = container.getProperty("layer_height", "value")
-        else:
-            # Look for layer_height in the GlobalStack from material -> definition
-            container = global_stack.definition
-            if container and container.hasProperty("layer_height", "value"):
-                layer_height = container.getProperty("layer_height", "value")
-
-        if isinstance(layer_height, SettingFunction):
-            layer_height = layer_height(global_stack)
-
-        return float(layer_height)
-
     def __repr__(self):
         return str(self.items)

+ 33 - 0
cura/Machines/Models/MachineModelUtils.py

@@ -0,0 +1,33 @@
+from typing import TYPE_CHECKING
+
+from UM.Settings.SettingFunction import SettingFunction
+
+if TYPE_CHECKING:
+    from cura.Machines.QualityGroup import QualityGroup
+
+layer_height_unit = ""
+
+def fetch_layer_height(quality_group: "QualityGroup") -> float:
+    from cura.CuraApplication import CuraApplication
+    global_stack = CuraApplication.getInstance().getMachineManager().activeMachine
+
+    default_layer_height = global_stack.definition.getProperty("layer_height", "value")
+
+    # Get layer_height from the quality profile for the GlobalStack
+    if quality_group.node_for_global is None:
+        return float(default_layer_height)
+    container = quality_group.node_for_global.container
+
+    layer_height = default_layer_height
+    if container and container.hasProperty("layer_height", "value"):
+        layer_height = container.getProperty("layer_height", "value")
+    else:
+        # Look for layer_height in the GlobalStack from material -> definition
+        container = global_stack.definition
+        if container and container.hasProperty("layer_height", "value"):
+            layer_height = container.getProperty("layer_height", "value")
+
+    if isinstance(layer_height, SettingFunction):
+        layer_height = layer_height(global_stack)
+
+    return float(layer_height)

+ 9 - 37
cura/Machines/Models/QualityProfilesDropDownMenuModel.py

@@ -2,17 +2,12 @@
 # Cura is released under the terms of the LGPLv3 or higher.
 
 from PyQt5.QtCore import Qt, QTimer
-from typing import TYPE_CHECKING
 
+import cura.CuraApplication  # Imported this way to prevent circular dependencies.
 from UM.Logger import Logger
 from UM.Qt.ListModel import ListModel
-from UM.Settings.SettingFunction import SettingFunction
-
-import cura.CuraApplication  # Imported this way to prevent circular dependencies.
 from cura.Machines.ContainerTree import ContainerTree
-
-if TYPE_CHECKING:
-    from cura.Machines.QualityGroup import QualityGroup
+from cura.Machines.Models.MachineModelUtils import fetch_layer_height
 
 
 #
@@ -76,6 +71,12 @@ class QualityProfilesDropDownMenuModel(ListModel):
             Logger.log("d", "No active GlobalStack, set quality profile model as empty.")
             return
 
+        if not self._layer_height_unit:
+            unit = global_stack.definition.getProperty("layer_height", "unit")
+            if not unit:
+                unit = ""
+            self._layer_height_unit = unit
+
         # Check for material compatibility
         if not cura.CuraApplication.CuraApplication.getInstance().getMachineManager().activeMaterialsCompatible():
             Logger.log("d", "No active material compatibility, set quality profile model as empty.")
@@ -86,7 +87,7 @@ class QualityProfilesDropDownMenuModel(ListModel):
 
         item_list = []
         for quality_group in quality_group_dict.values():
-            layer_height = self._fetchLayerHeight(quality_group)
+            layer_height = fetch_layer_height(quality_group)
 
             item = {"name": quality_group.name,
                     "quality_type": quality_group.quality_type,
@@ -102,32 +103,3 @@ class QualityProfilesDropDownMenuModel(ListModel):
         item_list = sorted(item_list, key = lambda x: x["layer_height"])
 
         self.setItems(item_list)
-
-    def _fetchLayerHeight(self, quality_group: "QualityGroup") -> float:
-        global_stack = cura.CuraApplication.CuraApplication.getInstance().getMachineManager().activeMachine
-        if not self._layer_height_unit:
-            unit = global_stack.definition.getProperty("layer_height", "unit")
-            if not unit:
-                unit = ""
-            self._layer_height_unit = unit
-
-        default_layer_height = global_stack.definition.getProperty("layer_height", "value")
-
-        # Get layer_height from the quality profile for the GlobalStack
-        if quality_group.node_for_global is None:
-            return float(default_layer_height)
-        container = quality_group.node_for_global.container
-
-        layer_height = default_layer_height
-        if container and container.hasProperty("layer_height", "value"):
-            layer_height = container.getProperty("layer_height", "value")
-        else:
-            # Look for layer_height in the GlobalStack from material -> definition
-            container = global_stack.definition
-            if container and container.hasProperty("layer_height", "value"):
-                layer_height = container.getProperty("layer_height", "value")
-
-        if isinstance(layer_height, SettingFunction):
-            layer_height = layer_height(global_stack)
-
-        return float(layer_height)