ProfilesModel.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from collections import OrderedDict
  4. from PyQt5.QtCore import Qt
  5. from UM.Application import Application
  6. from UM.Settings.ContainerRegistry import ContainerRegistry
  7. from UM.Settings.Models.InstanceContainersModel import InstanceContainersModel
  8. from cura.QualityManager import QualityManager
  9. from cura.Settings.ExtruderManager import ExtruderManager
  10. from typing import List, TYPE_CHECKING
  11. if TYPE_CHECKING:
  12. from cura.Settings.ExtruderStack import ExtruderStack
  13. ## QML Model for listing the current list of valid quality profiles.
  14. #
  15. class ProfilesModel(InstanceContainersModel):
  16. LayerHeightRole = Qt.UserRole + 1001
  17. LayerHeightWithoutUnitRole = Qt.UserRole + 1002
  18. AvailableRole = Qt.UserRole + 1003
  19. def __init__(self, parent = None):
  20. super().__init__(parent)
  21. self.addRoleName(self.LayerHeightRole, "layer_height")
  22. self.addRoleName(self.LayerHeightWithoutUnitRole, "layer_height_without_unit")
  23. self.addRoleName(self.AvailableRole, "available")
  24. Application.getInstance().globalContainerStackChanged.connect(self._update)
  25. Application.getInstance().getMachineManager().activeVariantChanged.connect(self._update)
  26. Application.getInstance().getMachineManager().activeStackChanged.connect(self._update)
  27. Application.getInstance().getMachineManager().activeMaterialChanged.connect(self._update)
  28. # Factory function, used by QML
  29. @staticmethod
  30. def createProfilesModel(engine, js_engine):
  31. return ProfilesModel.getInstance()
  32. ## Get the singleton instance for this class.
  33. @classmethod
  34. def getInstance(cls) -> "ProfilesModel":
  35. # Note: Explicit use of class name to prevent issues with inheritance.
  36. if not ProfilesModel.__instance:
  37. ProfilesModel.__instance = cls()
  38. return ProfilesModel.__instance
  39. __instance = None # type: "ProfilesModel"
  40. ## Fetch the list of containers to display.
  41. #
  42. # See UM.Settings.Models.InstanceContainersModel._fetchInstanceContainers().
  43. def _fetchInstanceContainers(self):
  44. global_container_stack = Application.getInstance().getGlobalContainerStack()
  45. if global_container_stack is None:
  46. return []
  47. global_stack_definition = global_container_stack.definition
  48. # Get the list of extruders and place the selected extruder at the front of the list.
  49. extruder_stacks = self._getOrderedExtruderStacksList()
  50. materials = [extruder.material for extruder in extruder_stacks]
  51. # Fetch the list of usable qualities across all extruders.
  52. # The actual list of quality profiles come from the first extruder in the extruder list.
  53. result = QualityManager.getInstance().findAllUsableQualitiesForMachineAndExtruders(global_container_stack, extruder_stacks)
  54. # The usable quality types are set
  55. quality_type_set = set([x.getMetaDataEntry("quality_type") for x in result])
  56. # Fetch all qualities available for this machine and the materials selected in extruders
  57. all_qualities = QualityManager.getInstance().findAllQualitiesForMachineAndMaterials(global_stack_definition, materials)
  58. # If in the all qualities there is some of them that are not available due to incompatibility with materials
  59. # we also add it so that they will appear in the slide quality bar. However in recomputeItems will be marked as
  60. # not available so they will be shown in gray
  61. for quality in all_qualities:
  62. if quality.getMetaDataEntry("quality_type") not in quality_type_set:
  63. result.append(quality)
  64. # if still profiles are found, add a single empty_quality ("Not supported") instance to the drop down list
  65. if len(result) == 0:
  66. # If not qualities are found we dynamically create a not supported container for this machine + material combination
  67. not_supported_container = ContainerRegistry.getInstance().findContainers(id = "empty_quality")[0]
  68. result.append(not_supported_container)
  69. return result
  70. ## Re-computes the items in this model, and adds the layer height role.
  71. def _recomputeItems(self):
  72. # Some globals that we can re-use.
  73. global_container_stack = Application.getInstance().getGlobalContainerStack()
  74. if global_container_stack is None:
  75. return
  76. extruder_stacks = self._getOrderedExtruderStacksList()
  77. container_registry = ContainerRegistry.getInstance()
  78. # Get a list of usable/available qualities for this machine and material
  79. qualities = QualityManager.getInstance().findAllUsableQualitiesForMachineAndExtruders(global_container_stack, extruder_stacks)
  80. unit = global_container_stack.getBottom().getProperty("layer_height", "unit")
  81. if not unit:
  82. unit = ""
  83. # group all quality items according to quality_types, so we know which profile suits the currently
  84. # active machine and material, and later yield the right ones.
  85. tmp_all_quality_items = OrderedDict()
  86. for item in super()._recomputeItems():
  87. profile = container_registry.findContainers(id=item["id"])
  88. quality_type = profile[0].getMetaDataEntry("quality_type") if profile else ""
  89. if quality_type not in tmp_all_quality_items:
  90. tmp_all_quality_items[quality_type] = {"suitable_container": None, "all_containers": []}
  91. tmp_all_quality_items[quality_type]["all_containers"].append(item)
  92. if tmp_all_quality_items[quality_type]["suitable_container"] is None:
  93. tmp_all_quality_items[quality_type]["suitable_container"] = item
  94. # reverse the ordering (finest first, coarsest last)
  95. all_quality_items = OrderedDict()
  96. for key in reversed(tmp_all_quality_items.keys()):
  97. all_quality_items[key] = tmp_all_quality_items[key]
  98. # First the suitable containers are set in the model
  99. containers = []
  100. for data_item in all_quality_items.values():
  101. suitable_item = data_item["suitable_container"]
  102. if suitable_item is not None:
  103. containers.append(suitable_item)
  104. # Once the suitable containers are collected, the rest of the containers are appended
  105. for data_item in all_quality_items.values():
  106. for item in data_item["all_containers"]:
  107. if item not in containers:
  108. containers.append(item)
  109. # Now all the containers are set
  110. for item in containers:
  111. profile = container_registry.findContainers(id = item["id"])
  112. # When for some reason there is no profile container in the registry
  113. if not profile:
  114. self._setItemLayerHeight(item, "", "")
  115. item["available"] = False
  116. yield item
  117. continue
  118. profile = profile[0]
  119. # When there is a profile but it's an empty quality should. It's shown in the list (they are "Not Supported" profiles)
  120. if profile.getId() == "empty_quality":
  121. self._setItemLayerHeight(item, "", "")
  122. item["available"] = True
  123. yield item
  124. continue
  125. item["available"] = profile in qualities
  126. # Easy case: This profile defines its own layer height.
  127. if profile.hasProperty("layer_height", "value"):
  128. self._setItemLayerHeight(item, profile.getProperty("layer_height", "value"), unit)
  129. yield item
  130. continue
  131. machine_manager = Application.getInstance().getMachineManager()
  132. # Quality-changes profile that has no value for layer height. Get the corresponding quality profile and ask that profile.
  133. quality_type = profile.getMetaDataEntry("quality_type", None)
  134. if quality_type:
  135. quality_results = machine_manager.determineQualityAndQualityChangesForQualityType(quality_type)
  136. for quality_result in quality_results:
  137. if quality_result["stack"] is global_container_stack:
  138. quality = quality_result["quality"]
  139. break
  140. else:
  141. # No global container stack in the results:
  142. if quality_results:
  143. # Take any of the extruders.
  144. quality = quality_results[0]["quality"]
  145. else:
  146. quality = None
  147. if quality and quality.hasProperty("layer_height", "value"):
  148. self._setItemLayerHeight(item, quality.getProperty("layer_height", "value"), unit)
  149. yield item
  150. continue
  151. # Quality has no value for layer height either. Get the layer height from somewhere lower in the stack.
  152. skip_until_container = global_container_stack.material
  153. if not skip_until_container or skip_until_container == ContainerRegistry.getInstance().getEmptyInstanceContainer(): # No material in stack.
  154. skip_until_container = global_container_stack.variant
  155. if not skip_until_container or skip_until_container == ContainerRegistry.getInstance().getEmptyInstanceContainer(): # No variant in stack.
  156. skip_until_container = global_container_stack.getBottom()
  157. self._setItemLayerHeight(item, global_container_stack.getRawProperty("layer_height", "value", skip_until_container = skip_until_container.getId()), unit) # Fall through to the currently loaded material.
  158. yield item
  159. ## Get a list of extruder stacks with the active extruder at the front of the list.
  160. @staticmethod
  161. def _getOrderedExtruderStacksList() -> List["ExtruderStack"]:
  162. extruder_manager = ExtruderManager.getInstance()
  163. extruder_stacks = extruder_manager.getActiveExtruderStacks()
  164. active_extruder = extruder_manager.getActiveExtruderStack()
  165. if active_extruder in extruder_stacks:
  166. extruder_stacks.remove(active_extruder)
  167. extruder_stacks = [active_extruder] + extruder_stacks
  168. return extruder_stacks
  169. @staticmethod
  170. def _setItemLayerHeight(item, value, unit):
  171. item["layer_height"] = str(value) + unit
  172. item["layer_height_without_unit"] = str(value)