QualityProfilesDropDownMenuModel.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import Qt, QTimer
  4. from typing import TYPE_CHECKING
  5. from UM.Logger import Logger
  6. from UM.Qt.ListModel import ListModel
  7. from UM.Settings.SettingFunction import SettingFunction
  8. import cura.CuraApplication # Imported this way to prevent circular dependencies.
  9. from cura.Machines.ContainerTree import ContainerTree
  10. if TYPE_CHECKING:
  11. from cura.Machines import QualityGroup
  12. #
  13. # QML Model for all built-in quality profiles. This model is used for the drop-down quality menu.
  14. #
  15. class QualityProfilesDropDownMenuModel(ListModel):
  16. NameRole = Qt.UserRole + 1
  17. QualityTypeRole = Qt.UserRole + 2
  18. LayerHeightRole = Qt.UserRole + 3
  19. LayerHeightUnitRole = Qt.UserRole + 4
  20. AvailableRole = Qt.UserRole + 5
  21. QualityGroupRole = Qt.UserRole + 6
  22. QualityChangesGroupRole = Qt.UserRole + 7
  23. IsExperimentalRole = Qt.UserRole + 8
  24. def __init__(self, parent = None):
  25. super().__init__(parent)
  26. self.addRoleName(self.NameRole, "name")
  27. self.addRoleName(self.QualityTypeRole, "quality_type")
  28. self.addRoleName(self.LayerHeightRole, "layer_height")
  29. self.addRoleName(self.LayerHeightUnitRole, "layer_height_unit")
  30. self.addRoleName(self.AvailableRole, "available") #Whether the quality profile is available in our current nozzle + material.
  31. self.addRoleName(self.QualityGroupRole, "quality_group")
  32. self.addRoleName(self.QualityChangesGroupRole, "quality_changes_group")
  33. self.addRoleName(self.IsExperimentalRole, "is_experimental")
  34. application = cura.CuraApplication.CuraApplication.getInstance()
  35. machine_manager = application.getMachineManager()
  36. application.globalContainerStackChanged.connect(self._onChange)
  37. machine_manager.activeQualityGroupChanged.connect(self._onChange)
  38. machine_manager.activeStackChanged.connect(self._onChange)
  39. machine_manager.extruderChanged.connect(self._onChange)
  40. self._layer_height_unit = "" # This is cached
  41. self._update_timer = QTimer() # type: QTimer
  42. self._update_timer.setInterval(100)
  43. self._update_timer.setSingleShot(True)
  44. self._update_timer.timeout.connect(self._update)
  45. self._onChange()
  46. def _onChange(self) -> None:
  47. self._update_timer.start()
  48. def _update(self):
  49. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  50. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  51. if global_stack is None:
  52. self.setItems([])
  53. Logger.log("d", "No active GlobalStack, set quality profile model as empty.")
  54. return
  55. # Check for material compatibility
  56. if not cura.CuraApplication.CuraApplication.getInstance().getMachineManager().activeMaterialsCompatible():
  57. Logger.log("d", "No active material compatibility, set quality profile model as empty.")
  58. self.setItems([])
  59. return
  60. quality_group_dict = ContainerTree.getInstance().getCurrentQualityGroups()
  61. item_list = []
  62. for key in quality_group_dict:
  63. quality_group = quality_group_dict[key]
  64. layer_height = self._fetchLayerHeight(quality_group)
  65. item = {"name": quality_group.name,
  66. "quality_type": quality_group.quality_type,
  67. "layer_height": layer_height,
  68. "layer_height_unit": self._layer_height_unit,
  69. "available": quality_group.is_available,
  70. "quality_group": quality_group,
  71. "is_experimental": quality_group.is_experimental}
  72. item_list.append(item)
  73. # Sort items based on layer_height
  74. item_list = sorted(item_list, key = lambda x: x["layer_height"])
  75. self.setItems(item_list)
  76. def _fetchLayerHeight(self, quality_group: "QualityGroup") -> float:
  77. global_stack = cura.CuraApplication.CuraApplication.getInstance().getMachineManager().activeMachine
  78. if not self._layer_height_unit:
  79. unit = global_stack.definition.getProperty("layer_height", "unit")
  80. if not unit:
  81. unit = ""
  82. self._layer_height_unit = unit
  83. default_layer_height = global_stack.definition.getProperty("layer_height", "value")
  84. # Get layer_height from the quality profile for the GlobalStack
  85. if quality_group.node_for_global is None:
  86. return float(default_layer_height)
  87. container = quality_group.node_for_global.container
  88. layer_height = default_layer_height
  89. if container and container.hasProperty("layer_height", "value"):
  90. layer_height = container.getProperty("layer_height", "value")
  91. else:
  92. # Look for layer_height in the GlobalStack from material -> definition
  93. container = global_stack.definition
  94. if container and container.hasProperty("layer_height", "value"):
  95. layer_height = container.getProperty("layer_height", "value")
  96. if isinstance(layer_height, SettingFunction):
  97. layer_height = layer_height(global_stack)
  98. return float(layer_height)