From 6c0772cd4a8e8d6013edaaaa58bc250921917ca1 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Fri, 4 Oct 2019 14:12:57 +0200 Subject: [PATCH] Provide activeQualityGroupName instead of QualityGroup(QObject) We don't need to inherit from QObject if we expose the name elsewhere. This prevents having workarounds for C++ vs QML ownership, and also allows us to test this while mocking out CuraApplication. Done during Turbo Testing and Tooling. --- cura/Machines/MachineNode.py | 13 +++---------- cura/Machines/QualityGroup.py | 12 ++++++------ cura/Settings/MachineManager.py | 22 ++++++++++++++++++---- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/cura/Machines/MachineNode.py b/cura/Machines/MachineNode.py index 00edca2c6b..235574675c 100644 --- a/cura/Machines/MachineNode.py +++ b/cura/Machines/MachineNode.py @@ -8,6 +8,7 @@ from UM.Signal import Signal from UM.Util import parseBool from UM.Settings.ContainerRegistry import ContainerRegistry # To find all the variants for this machine. +import cura.CuraApplication # Imported like this to prevent circular dependencies. from cura.Machines.ContainerNode import ContainerNode from cura.Machines.QualityChangesGroup import QualityChangesGroup # To construct groups of quality changes profiles that belong together. from cura.Machines.QualityGroup import QualityGroup # To construct groups of quality profiles that belong together. @@ -81,15 +82,7 @@ class MachineNode(ContainerNode): if not global_quality_node.container: Logger.log("w", "Node {0} doesn't have a container.".format(global_quality_node.container_id)) continue - # CURA-6599 - # Same as QualityChangesGroup. - # For some reason, QML will get null or fail to convert type for MachineManager.activeQualityChangesGroup() to - # a QObject. Setting the object ownership to QQmlEngine.CppOwnership doesn't work, but setting the object - # parent to application seems to work. - from cura.CuraApplication import CuraApplication - quality_groups[quality_type] = QualityGroup(name = global_quality_node.container.getMetaDataEntry("name", "Unnamed profile"), - quality_type = quality_type, - parent = CuraApplication.getInstance()) + quality_groups[quality_type] = QualityGroup(name = global_quality_node.container.getMetaDataEntry("name", "Unnamed profile"), quality_type = quality_type) quality_groups[quality_type].node_for_global = global_quality_node for extruder, qualities_per_type in enumerate(qualities_per_type_per_extruder): if quality_type in qualities_per_type: @@ -168,7 +161,7 @@ class MachineNode(ContainerNode): return self.global_qualities.get(self.preferred_quality_type, next(iter(self.global_qualities.values()))) ## (Re)loads all variants under this printer. - def _loadAll(self): + def _loadAll(self) -> None: container_registry = ContainerRegistry.getInstance() if not self.has_variants: self.variants["empty"] = VariantNode("empty_variant", machine = self) diff --git a/cura/Machines/QualityGroup.py b/cura/Machines/QualityGroup.py index b9f2451ddc..48047974a9 100644 --- a/cura/Machines/QualityGroup.py +++ b/cura/Machines/QualityGroup.py @@ -27,11 +27,12 @@ from cura.Machines.ContainerNode import ContainerNode # applied to a configuration, so that when a quality level is selected, the # container can directly be applied to each stack instead of looking them up # again. -class QualityGroup(QObject): - - def __init__(self, name: str, quality_type: str, parent: Optional["QObject"] = None) -> None: - super().__init__(parent) - +class QualityGroup: + ## Constructs a new group. + # \param name The user-visible name for the group. + # \param quality_type The quality level that each profile in this group + # has. + def __init__(self, name: str, quality_type: str) -> None: self.name = name self.node_for_global = None # type: Optional[ContainerNode] self.nodes_for_extruders = {} # type: Dict[int, ContainerNode] @@ -39,7 +40,6 @@ class QualityGroup(QObject): self.is_available = False self.is_experimental = False - @pyqtSlot(result = str) def getName(self) -> str: return self.name diff --git a/cura/Settings/MachineManager.py b/cura/Settings/MachineManager.py index 06f4e9be4e..d315f2fdb0 100755 --- a/cura/Settings/MachineManager.py +++ b/cura/Settings/MachineManager.py @@ -613,9 +613,10 @@ class MachineManager(QObject): global_container_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack() if not global_container_stack: return False - if not self.activeQualityGroup: + active_quality_group = self.activeQualityGroup() + if active_quality_group is None: return False - return self.activeQualityGroup.is_available + return active_quality_group.is_available @pyqtProperty(bool, notify = activeQualityGroupChanged) def isActiveQualityExperimental(self) -> bool: @@ -1646,13 +1647,26 @@ class MachineManager(QObject): else: # No intent had the correct category. extruder.intent = empty_intent_container - @pyqtProperty(QObject, fset = setQualityGroup, notify = activeQualityGroupChanged) + ## Get the currently activated quality group. + # + # If no printer is added yet or the printer doesn't have quality profiles, + # this returns ``None``. + # \return The currently active quality group. def activeQualityGroup(self) -> Optional["QualityGroup"]: global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack() if not global_stack or global_stack.quality == empty_quality_container: return None return ContainerTree.getInstance().getCurrentQualityGroups().get(self.activeQualityType) + ## Get the name of the active quality group. + # \return The name of the active quality group. + @pyqtProperty(str, notify = activeQualityGroupChanged) + def activeQualityGroupName(self) -> str: + quality_group = self.activeQualityGroup() + if quality_group is None: + return "" + return quality_group.getName() + @pyqtSlot(QObject) def setQualityChangesGroup(self, quality_changes_group: "QualityChangesGroup", no_dialog: bool = False) -> None: self.blurSettings.emit() @@ -1668,7 +1682,7 @@ class MachineManager(QObject): if self._global_container_stack is None: return with postponeSignals(*self._getContainerChangedSignals(), compress = CompressTechnique.CompressPerParameterValue): - self._setQualityGroup(self.activeQualityGroup) + self._setQualityGroup(self.activeQualityGroup()) for stack in [self._global_container_stack] + list(self._global_container_stack.extruders.values()): stack.userChanges.clear()