ProfilesModel.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright (c) 2016 Ultimaker B.V.
  2. # Cura is released under the terms of the AGPLv3 or higher.
  3. from PyQt5.QtCore import Qt
  4. from UM.Application import Application
  5. from UM.Settings.ContainerRegistry import ContainerRegistry
  6. from UM.Settings.Models.InstanceContainersModel import InstanceContainersModel
  7. from cura.QualityManager import QualityManager
  8. from cura.Settings.ExtruderManager import ExtruderManager
  9. ## QML Model for listing the current list of valid quality profiles.
  10. #
  11. class ProfilesModel(InstanceContainersModel):
  12. LayerHeightRole = Qt.UserRole + 1001
  13. def __init__(self, parent = None):
  14. super().__init__(parent)
  15. self.addRoleName(self.LayerHeightRole, "layer_height")
  16. Application.getInstance().globalContainerStackChanged.connect(self._update)
  17. Application.getInstance().getMachineManager().activeVariantChanged.connect(self._update)
  18. Application.getInstance().getMachineManager().activeStackChanged.connect(self._update)
  19. Application.getInstance().getMachineManager().activeMaterialChanged.connect(self._update)
  20. # Factory function, used by QML
  21. @staticmethod
  22. def createProfilesModel(engine, js_engine):
  23. return ProfilesModel.getInstance()
  24. ## Get the singleton instance for this class.
  25. @classmethod
  26. def getInstance(cls) -> "ProfilesModel":
  27. # Note: Explicit use of class name to prevent issues with inheritance.
  28. if not ProfilesModel.__instance:
  29. ProfilesModel.__instance = cls()
  30. return ProfilesModel.__instance
  31. __instance = None # type: "ProfilesModel"
  32. ## Fetch the list of containers to display.
  33. #
  34. # See UM.Settings.Models.InstanceContainersModel._fetchInstanceContainers().
  35. def _fetchInstanceContainers(self):
  36. global_container_stack = Application.getInstance().getGlobalContainerStack()
  37. if global_container_stack is None:
  38. return []
  39. # Get the list of extruders and place the selected extruder at the front of the list.
  40. extruder_manager = ExtruderManager.getInstance()
  41. active_extruder = extruder_manager.getActiveExtruderStack()
  42. extruder_stacks = extruder_manager.getActiveExtruderStacks()
  43. if active_extruder in extruder_stacks:
  44. extruder_stacks.remove(active_extruder)
  45. extruder_stacks = [active_extruder] + extruder_stacks
  46. # Fetch the list of useable qualities across all extruders.
  47. # The actual list of quality profiles come from the first extruder in the extruder list.
  48. return QualityManager.getInstance().findAllUsableQualitiesForMachineAndExtruders(global_container_stack,
  49. extruder_stacks)
  50. ## Re-computes the items in this model, and adds the layer height role.
  51. def _recomputeItems(self):
  52. #Some globals that we can re-use.
  53. global_container_stack = Application.getInstance().getGlobalContainerStack()
  54. if global_container_stack is None:
  55. return
  56. container_registry = ContainerRegistry.getInstance()
  57. machine_manager = Application.getInstance().getMachineManager()
  58. unit = global_container_stack.getBottom().getProperty("layer_height", "unit")
  59. if not unit:
  60. unit = ""
  61. for item in super()._recomputeItems():
  62. profile = container_registry.findContainers(id = item["id"])
  63. if not profile:
  64. item["layer_height"] = "" #Can't update a profile that is unknown.
  65. yield item
  66. continue
  67. #Easy case: This profile defines its own layer height.
  68. profile = profile[0]
  69. if profile.hasProperty("layer_height", "value"):
  70. item["layer_height"] = str(profile.getProperty("layer_height", "value")) + unit
  71. yield item
  72. continue
  73. #Quality-changes profile that has no value for layer height. Get the corresponding quality profile and ask that profile.
  74. quality_type = profile.getMetaDataEntry("quality_type", None)
  75. if quality_type:
  76. quality_results = machine_manager.determineQualityAndQualityChangesForQualityType(quality_type)
  77. for quality_result in quality_results:
  78. if quality_result["stack"] is global_container_stack:
  79. quality = quality_result["quality"]
  80. break
  81. else: #No global container stack in the results:
  82. if quality_results:
  83. quality = quality_results[0]["quality"] #Take any of the extruders.
  84. else:
  85. quality = None
  86. if quality and quality.hasProperty("layer_height", "value"):
  87. item["layer_height"] = str(quality.getProperty("layer_height", "value")) + unit
  88. yield item
  89. continue
  90. #Quality has no value for layer height either. Get the layer height from somewhere lower in the stack.
  91. skip_until_container = global_container_stack.material
  92. if not skip_until_container: #No material in stack.
  93. skip_until_container = global_container_stack.variant
  94. if not skip_until_container: #No variant in stack.
  95. skip_until_container = global_container_stack.getBottom()
  96. item["layer_height"] = str(global_container_stack.getRawProperty("layer_height", "value", skip_until_container = skip_until_container.getId())) + unit #Fall through to the currently loaded material.
  97. yield item