Browse Source

Add function to find preferred quality profile

Not for global yet, so it doesn't appear as if anything is loaded yet.

Contributes to issue CURA-6600.
Ghostkeeper 5 years ago
parent
commit
fcab800a8d
3 changed files with 33 additions and 4 deletions
  1. 23 0
      cura/Machines/MaterialNode.py
  2. 4 0
      cura/Machines/QualityNode.py
  3. 6 4
      cura/Machines/VariantNode.py

+ 23 - 0
cura/Machines/MaterialNode.py

@@ -3,6 +3,7 @@
 
 from typing import Any, TYPE_CHECKING
 
+from UM.Logger import Logger
 from UM.Settings.ContainerRegistry import ContainerRegistry
 from UM.Settings.Interfaces import ContainerInterface
 from UM.Signal import Signal
@@ -32,6 +33,28 @@ class MaterialNode(ContainerNode):
         container_registry.containerRemoved.connect(self._onRemoved)
         container_registry.containerMetaDataChanged.connect(self._onMetadataChanged)
 
+    ##  Finds the preferred quality for this printer with this material and this
+    #   variant loaded.
+    #
+    #   If the preferred quality is not available, an arbitrary quality is
+    #   returned. If there is a configuration mistake (like a typo in the
+    #   preferred quality) this returns a random available quality. If there are
+    #   no available qualities, this will return the empty quality node.
+    #   \return The node for the preferred quality, or any arbitrary quality if
+    #   there is no match.
+    def preferredQuality(self) -> QualityNode:
+        for quality_id, quality_node in self.qualities.items():
+            if self.variant.machine.preferred_quality_type == quality_node.quality_type:
+                return quality_node
+        fallback = next(iter(self.qualities.values()))  # Should only happen with empty quality node.
+        Logger.log("w", "Could not find preferred quality type {preferred_quality_type} for material {material_id} and variant {variant_id}, falling back to {fallback}.".format(
+            preferred_quality_type = self.variant.machine.preferred_quality_type,
+            material_id = self.container_id,
+            variant_id = self.variant.container_id,
+            fallback = fallback.container_id
+        ))
+        return fallback
+
     def _loadAll(self) -> None:
         container_registry = ContainerRegistry.getInstance()
         # Find all quality profiles that fit on this material.

+ 4 - 0
cura/Machines/QualityNode.py

@@ -20,6 +20,10 @@ class QualityNode(ContainerNode):
         super().__init__(container_id)
         self.parent = parent
         self.intents = {}  # type: Dict[str, IntentNode]
+
+        my_metadata = ContainerRegistry.getInstance().findContainersMetadata(id = container_id)[0]
+        self.quality_type = my_metadata["quality_type"]
+
         self._loadAll()
 
     def _loadAll(self) -> None:

+ 6 - 4
cura/Machines/VariantNode.py

@@ -71,12 +71,14 @@ class VariantNode(ContainerNode):
     ##  Finds the preferred material for this printer with this nozzle in one of
     #   the extruders.
     #
-    #   If there is no material here (because the printer has no materials or
-    #   because there are no matching material profiles), None is returned.
+    #   If the preferred material is not available, an arbitrary material is
+    #   returned. If there is a configuration mistake (like a typo in the
+    #   preferred material) this returns a random available material. If there
+    #   are no available materials, this will return the empty material node.
     #   \param approximate_diameter The desired approximate diameter of the
     #   material.
-    #   \return The node for the preferred material, or None if there is no
-    #   match.
+    #   \return The node for the preferred material, or any arbitrary material
+    #   if there is no match.
     def preferredMaterial(self, approximate_diameter) -> MaterialNode:
         for base_material, material_node in self.materials.items():
             if self.machine.preferred_material in base_material and approximate_diameter == int(material_node.getMetaDataEntry("approximate_diameter")):