ProfilesModel.py 9.9 KB

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