QualitySettingsModel.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.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.UserRole + 1
  14. LabelRole = Qt.UserRole + 2
  15. UnitRole = Qt.UserRole + 3
  16. ProfileValueRole = Qt.UserRole + 4
  17. ProfileValueSourceRole = Qt.UserRole + 5
  18. UserValueRole = Qt.UserRole + 6
  19. CategoryRole = Qt.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(str(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. if self._selected_position == self.GLOBAL_STACK_POSITION and global_container:
  92. quality_changes_metadata = global_container.getMetaData()
  93. else:
  94. quality_changes_metadata = extruders_container.get(str(self._selected_position))
  95. if quality_changes_metadata is not None: # It can be None if number of extruders are changed during runtime.
  96. container = container_registry.findContainers(id = quality_changes_metadata["id"])
  97. if container:
  98. quality_containers.insert(0, container[0])
  99. if global_container:
  100. settings_keys.update(global_container.getAllKeys())
  101. for container in extruders_container.values():
  102. if container:
  103. settings_keys.update(container.getAllKeys())
  104. # We iterate over all definitions instead of settings in a quality/quality_changes group is because in the GUI,
  105. # the settings are grouped together by categories, and we had to go over all the definitions to figure out
  106. # which setting belongs in which category.
  107. current_category = ""
  108. for definition in definition_container.findDefinitions():
  109. if definition.type == "category":
  110. current_category = definition.label
  111. if self._i18n_catalog:
  112. current_category = self._i18n_catalog.i18nc(definition.key + " label", definition.label)
  113. continue
  114. profile_value = None
  115. profile_value_source = ""
  116. for quality_container in quality_containers:
  117. new_value = quality_container.getProperty(definition.key, "value")
  118. if new_value is not None:
  119. profile_value_source = quality_container.getMetaDataEntry("type")
  120. profile_value = new_value
  121. # Global tab should use resolve (if there is one)
  122. if self._selected_position == self.GLOBAL_STACK_POSITION:
  123. resolve_value = global_container_stack.getProperty(definition.key, "resolve")
  124. if resolve_value is not None and definition.key in settings_keys:
  125. profile_value = resolve_value
  126. if profile_value is not None:
  127. break
  128. if self._selected_position == self.GLOBAL_STACK_POSITION:
  129. user_value = global_container_stack.userChanges.getProperty(definition.key, "value")
  130. else:
  131. extruder_stack = global_container_stack.extruderList[self._selected_position]
  132. user_value = extruder_stack.userChanges.getProperty(definition.key, "value")
  133. if profile_value is None and user_value is None:
  134. continue
  135. label = definition.label
  136. if self._i18n_catalog:
  137. label = self._i18n_catalog.i18nc(definition.key + " label", label)
  138. items.append({
  139. "key": definition.key,
  140. "label": label,
  141. "unit": definition.unit,
  142. "profile_value": "" if profile_value is None else str(profile_value), # it is for display only
  143. "profile_value_source": profile_value_source,
  144. "user_value": "" if user_value is None else str(user_value),
  145. "category": current_category
  146. })
  147. self.setItems(items)