QualitySettingsModel.py 7.9 KB

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