From 8dcccd941b72fc1f9e5324937cecec1eeb2b6234 Mon Sep 17 00:00:00 2001 From: Joey de l'Arago Date: Mon, 28 Nov 2022 12:40:35 +0100 Subject: [PATCH] Add dataclass for ActiveQuality. Move some logic out of MachineManager into ActiveQuality. CURA-9793 --- cura/Settings/ActiveQuality.py | 37 ++++++++++++++++++++++++++ cura/Settings/MachineManager.py | 46 +++++++-------------------------- plugins/UFPWriter/UFPWriter.py | 3 ++- 3 files changed, 49 insertions(+), 37 deletions(-) create mode 100644 cura/Settings/ActiveQuality.py diff --git a/cura/Settings/ActiveQuality.py b/cura/Settings/ActiveQuality.py new file mode 100644 index 0000000000..48af2c5d1a --- /dev/null +++ b/cura/Settings/ActiveQuality.py @@ -0,0 +1,37 @@ +from dataclasses import dataclass +from typing import List + +from UM import i18nCatalog + +catalog = i18nCatalog("cura") + + +@dataclass +class ActiveQuality: + """ Represents the active intent+profile combination, contains all information needed to display active quality. """ + intent_category: str = "" # Name of the base intent. For example "visual" or "engineering". + intent_name: str = "" # Name of the base intent formatted for display. For Example "Visual" or "Engineering" + profile: str = "" # Name of the base profile. For example "Fine" or "Fast" + custom_profile: str = "" # Name of the custom profile, this is based on profile. For example "MyCoolCustomProfile" + layer_height: float = None # Layer height of quality in mm. For example 0.4 + is_experimental: bool = False # If the quality experimental. + + def getStringParts(self) -> List[str]: + string_parts = [] + + if self.custom_profile is not None: + string_parts.append(self.custom_profile) + + if self.intent_category is not "default": + string_parts.append(f"{self.intent_name} - {self.profile}") + else: + string_parts.append(self.profile) + + if self.layer_height: + string_parts.append(f"{self.layer_height}mm") + + if self.is_experimental: + string_parts.append(catalog.i18nc("@label", "Experimental")) + + return string_parts + diff --git a/cura/Settings/MachineManager.py b/cura/Settings/MachineManager.py index fabfc27db8..350cac906a 100755 --- a/cura/Settings/MachineManager.py +++ b/cura/Settings/MachineManager.py @@ -40,6 +40,7 @@ from cura.Settings.cura_empty_instance_containers import (empty_definition_chang empty_material_container, empty_quality_container, empty_quality_changes_container, empty_intent_container) from cura.UltimakerCloud.UltimakerCloudConstants import META_UM_LINKED_TO_ACCOUNT +from .ActiveQuality import ActiveQuality from .CuraStackBuilder import CuraStackBuilder @@ -1633,47 +1634,20 @@ class MachineManager(QObject): # - "my_profile - Engineering - Fine" (based on an intent) @pyqtProperty("QList", notify = activeQualityDisplayNameChanged) def activeQualityDisplayNameStringParts(self) -> List[str]: - result_map = self.activeQualityDisplayNameMap - string_parts = [] - - if result_map["custom_profile"] is not None: - string_parts.append(result_map["custom_profile"]) - - if result_map["intent_category"] is not "default": - string_parts.append(f"""{result_map["intent_name"]} - {result_map["profile"]}""") - else: - string_parts.append(result_map["profile"]) - - if result_map["layer_height"]: - string_parts.append(f"""{result_map["layer_height"]}mm""") - - if result_map["is_experimental"]: - string_parts.append(catalog.i18nc("@label", "Experimental")) - - return string_parts + return self.activeQualityDisplayNameMap.getStringParts() @pyqtProperty("QVariantMap", notify = activeQualityDisplayNameChanged) - def activeQualityDisplayNameMap(self) -> Dict[str, Any]: + def activeQualityDisplayNameMap(self) -> ActiveQuality: global_stack = self._application.getGlobalContainerStack() if global_stack is None: - return { - "profile": "", - "intent_category": "", - "intent": "", - "custom_profile": None, - "is_experimental": False - } + return ActiveQuality() - return { - "profile": global_stack.quality.getName(), - "intent_category": self.activeIntentCategory, - "intent_name": IntentCategoryModel.translation(self.activeIntentCategory, "name", self.activeIntentCategory.title()), - "custom_profile": self.activeQualityOrQualityChangesName \ - if global_stack.qualityChanges is not empty_quality_changes_container \ - else None, - "layer_height": self.activeQualityLayerHeight if self.isActiveQualitySupported else None, - "is_experimental": self.isActiveQualityExperimental and self.isActiveQualitySupported, - } + return ActiveQuality(profile = global_stack.quality.getName(), + intent_category = self.activeIntentCategory, + intent_name = IntentCategoryModel.translation(self.activeIntentCategory, "name", self.activeIntentCategory.title()), + custom_profile = self.activeQualityOrQualityChangesName if global_stack.qualityChanges is not empty_quality_changes_container else None, + layer_height = self.activeQualityLayerHeight if self.isActiveQualitySupported else None, + is_experimental = self.isActiveQualityExperimental and self.isActiveQualitySupported) @pyqtSlot(str) def setIntentByCategory(self, intent_category: str) -> None: diff --git a/plugins/UFPWriter/UFPWriter.py b/plugins/UFPWriter/UFPWriter.py index 3693352c1d..6594557584 100644 --- a/plugins/UFPWriter/UFPWriter.py +++ b/plugins/UFPWriter/UFPWriter.py @@ -1,6 +1,7 @@ # Copyright (c) 2022 Ultimaker B.V. # Cura is released under the terms of the LGPLv3 or higher. import json +from dataclasses import asdict from typing import cast, List, Dict from Charon.VirtualFile import VirtualFile # To open UFP files. @@ -227,7 +228,7 @@ class UFPWriter(MeshWriter): }, "intent": machine_manager.activeIntentCategory, "quality": machine_manager.activeQualityOrQualityChangesName, - "quality_display_name": machine_manager.activeQualityDisplayNameMap, + "quality_display_name": asdict(machine_manager.activeQualityDisplayNameMap), } global_stack = cast(GlobalStack, Application.getInstance().getGlobalContainerStack())