QualitySettingsModel.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt6.QtCore import pyqtProperty, pyqtSignal, Qt
  4. from typing import Set
  5. import cura.CuraApplication
  6. from UM import i18nCatalog
  7. from UM.Logger import Logger
  8. from UM.Qt.ListModel import ListModel
  9. from UM.Settings.ContainerRegistry import ContainerRegistry
  10. import os
  11. class QualitySettingsModel(ListModel):
  12. """This model is used to show details settings of the selected quality in the quality management page."""
  13. KeyRole = Qt.ItemDataRole.UserRole + 1
  14. LabelRole = Qt.ItemDataRole.UserRole + 2
  15. UnitRole = Qt.ItemDataRole.UserRole + 3
  16. ProfileValueRole = Qt.ItemDataRole.UserRole + 4
  17. ProfileValueSourceRole = Qt.ItemDataRole.UserRole + 5
  18. UserValueRole = Qt.ItemDataRole.UserRole + 6
  19. CategoryRole = Qt.ItemDataRole.UserRole + 7
  20. GLOBAL_STACK_POSITION = -1
  21. def __init__(self, parent = None) -> None:
  22. super().__init__(parent = parent)
  23. self.addRoleName(self.KeyRole, "key")
  24. self.addRoleName(self.LabelRole, "label")
  25. self.addRoleName(self.UnitRole, "unit")
  26. self.addRoleName(self.ProfileValueRole, "profile_value")
  27. self.addRoleName(self.ProfileValueSourceRole, "profile_value_source")
  28. self.addRoleName(self.UserValueRole, "user_value")
  29. self.addRoleName(self.CategoryRole, "category")
  30. self._container_registry = ContainerRegistry.getInstance()
  31. self._application = cura.CuraApplication.CuraApplication.getInstance()
  32. self._application.getMachineManager().activeStackChanged.connect(self._update)
  33. # Must be either GLOBAL_STACK_POSITION or an extruder position (0, 1, etc.)
  34. self._selected_position = self.GLOBAL_STACK_POSITION
  35. self._selected_quality_item = None # The selected quality in the quality management page
  36. self._i18n_catalog = None
  37. self._update()
  38. selectedPositionChanged = pyqtSignal()
  39. selectedQualityItemChanged = pyqtSignal()
  40. def setSelectedPosition(self, selected_position: int) -> None:
  41. if selected_position != self._selected_position:
  42. self._selected_position = selected_position
  43. self.selectedPositionChanged.emit()
  44. self._update()
  45. @pyqtProperty(int, fset = setSelectedPosition, notify = selectedPositionChanged)
  46. def selectedPosition(self) -> int:
  47. return self._selected_position
  48. def setSelectedQualityItem(self, selected_quality_item):
  49. if selected_quality_item != self._selected_quality_item:
  50. self._selected_quality_item = selected_quality_item
  51. self.selectedQualityItemChanged.emit()
  52. self._update()
  53. @pyqtProperty("QVariantMap", fset = setSelectedQualityItem, notify = selectedQualityItemChanged)
  54. def selectedQualityItem(self):
  55. return self._selected_quality_item
  56. def _update(self) -> None:
  57. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  58. if not self._selected_quality_item:
  59. self.setItems([])
  60. return
  61. items = []
  62. global_container_stack = self._application.getGlobalContainerStack()
  63. definition_container = global_container_stack.definition
  64. # Try and find a translation catalog for the definition
  65. for file_name in definition_container.getInheritedFiles():
  66. catalog = i18nCatalog(os.path.basename(file_name))
  67. if catalog.hasTranslationLoaded():
  68. self._i18n_catalog = catalog
  69. quality_group = self._selected_quality_item["quality_group"]
  70. quality_changes_group = self._selected_quality_item["quality_changes_group"]
  71. quality_node = None
  72. settings_keys = set() # type: Set[str]
  73. if quality_group:
  74. if self._selected_position == self.GLOBAL_STACK_POSITION:
  75. quality_node = quality_group.node_for_global
  76. else:
  77. quality_node = quality_group.nodes_for_extruders.get(self._selected_position)
  78. settings_keys = quality_group.getAllKeys()
  79. quality_containers = []
  80. if quality_node is not None and quality_node.container is not None:
  81. quality_containers.append(quality_node.container)
  82. # Here, if the user has selected a quality changes, then "quality_changes_group" will not be None, and we fetch
  83. # the settings in that quality_changes_group.
  84. if quality_changes_group is not None:
  85. container_registry = ContainerRegistry.getInstance()
  86. metadata_for_global = quality_changes_group.metadata_for_global
  87. global_containers = container_registry.findContainers(id = metadata_for_global["id"])
  88. global_container = None if len(global_containers) == 0 else global_containers[0]
  89. extruders_containers = {pos: container_registry.findContainers(id = quality_changes_group.metadata_per_extruder[pos]["id"]) for pos in quality_changes_group.metadata_per_extruder}
  90. extruders_container = {pos: None if not containers else containers[0] for pos, containers in extruders_containers.items()}
  91. quality_changes_metadata = None
  92. if self._selected_position == self.GLOBAL_STACK_POSITION and global_container:
  93. quality_changes_metadata = global_container.getMetaData()
  94. else:
  95. extruder = extruders_container.get(self._selected_position)
  96. if extruder:
  97. quality_changes_metadata = extruder.getMetaData()
  98. if quality_changes_metadata is not None: # It can be None if number of extruders are changed during runtime.
  99. container = container_registry.findContainers(id = quality_changes_metadata["id"])
  100. if container:
  101. quality_containers.insert(0, container[0])
  102. if global_container:
  103. settings_keys.update(global_container.getAllKeys())
  104. for container in extruders_container.values():
  105. if container:
  106. settings_keys.update(container.getAllKeys())
  107. # We iterate over all definitions instead of settings in a quality/quality_changes group is because in the GUI,
  108. # the settings are grouped together by categories, and we had to go over all the definitions to figure out
  109. # which setting belongs in which category.
  110. current_category = ""
  111. for definition in definition_container.findDefinitions():
  112. if definition.type == "category":
  113. current_category = definition.label
  114. if self._i18n_catalog:
  115. current_category = self._i18n_catalog.i18nc(definition.key + " label", definition.label)
  116. continue
  117. profile_value = None
  118. profile_value_source = ""
  119. for quality_container in quality_containers:
  120. new_value = quality_container.getProperty(definition.key, "value")
  121. if new_value is not None:
  122. profile_value_source = quality_container.getMetaDataEntry("type")
  123. profile_value = new_value
  124. # Global tab should use resolve (if there is one)
  125. if self._selected_position == self.GLOBAL_STACK_POSITION:
  126. resolve_value = global_container_stack.getProperty(definition.key, "resolve")
  127. if resolve_value is not None and definition.key in settings_keys:
  128. profile_value = resolve_value
  129. if profile_value is not None:
  130. break
  131. if self._selected_position == self.GLOBAL_STACK_POSITION:
  132. user_value = global_container_stack.userChanges.getProperty(definition.key, "value")
  133. else:
  134. extruder_stack = global_container_stack.extruderList[self._selected_position]
  135. user_value = extruder_stack.userChanges.getProperty(definition.key, "value")
  136. if profile_value is None and user_value is None:
  137. continue
  138. label = definition.label
  139. if self._i18n_catalog:
  140. label = self._i18n_catalog.i18nc(definition.key + " label", label)
  141. items.append({
  142. "key": definition.key,
  143. "label": label,
  144. "unit": definition.unit,
  145. "profile_value": "" if profile_value is None else str(profile_value), # it is for display only
  146. "profile_value_source": profile_value_source,
  147. "user_value": "" if user_value is None else str(user_value),
  148. "category": current_category
  149. })
  150. self.setItems(items)