ProfilesModel.py 11 KB

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