QualityProfilesDropDownMenuModel.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt6.QtCore import Qt, QTimer
  4. import cura.CuraApplication # Imported this way to prevent circular dependencies.
  5. from UM.Logger import Logger
  6. from UM.Qt.ListModel import ListModel
  7. from cura.Machines.ContainerTree import ContainerTree
  8. from cura.Machines.Models.MachineModelUtils import fetchLayerHeight
  9. class QualityProfilesDropDownMenuModel(ListModel):
  10. """QML Model for all built-in quality profiles. This model is used for the drop-down quality menu."""
  11. NameRole = Qt.ItemDataRole.UserRole + 1
  12. QualityTypeRole = Qt.ItemDataRole.UserRole + 2
  13. LayerHeightRole = Qt.ItemDataRole.UserRole + 3
  14. LayerHeightUnitRole = Qt.ItemDataRole.UserRole + 4
  15. AvailableRole = Qt.ItemDataRole.UserRole + 5
  16. QualityGroupRole = Qt.ItemDataRole.UserRole + 6
  17. QualityChangesGroupRole = Qt.ItemDataRole.UserRole + 7
  18. IsExperimentalRole = Qt.ItemDataRole.UserRole + 8
  19. def __init__(self, parent = None):
  20. super().__init__(parent)
  21. self.addRoleName(self.NameRole, "name")
  22. self.addRoleName(self.QualityTypeRole, "quality_type")
  23. self.addRoleName(self.LayerHeightRole, "layer_height")
  24. self.addRoleName(self.LayerHeightUnitRole, "layer_height_unit")
  25. self.addRoleName(self.AvailableRole, "available") #Whether the quality profile is available in our current nozzle + material.
  26. self.addRoleName(self.QualityGroupRole, "quality_group")
  27. self.addRoleName(self.QualityChangesGroupRole, "quality_changes_group")
  28. self.addRoleName(self.IsExperimentalRole, "is_experimental")
  29. application = cura.CuraApplication.CuraApplication.getInstance()
  30. machine_manager = application.getMachineManager()
  31. application.globalContainerStackChanged.connect(self._onChange)
  32. machine_manager.activeQualityGroupChanged.connect(self._onChange)
  33. machine_manager.activeMaterialChanged.connect(self._onChange)
  34. machine_manager.activeVariantChanged.connect(self._onChange)
  35. self._layer_height_unit = "" # This is cached
  36. self._update_timer = QTimer() # type: QTimer
  37. self._update_timer.setInterval(100)
  38. self._update_timer.setSingleShot(True)
  39. self._update_timer.timeout.connect(self._update)
  40. self._onChange()
  41. def _onChange(self) -> None:
  42. self._update_timer.start()
  43. def _update(self):
  44. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  45. # CURA-6836
  46. # LabelBar is a repeater that creates labels for quality layer heights. Because of an optimization in
  47. # UM.ListModel, the model will not remove all items and recreate new ones every time there's an update.
  48. # Because LabelBar uses Repeater with Labels anchoring to "undefined" in certain cases, the anchoring will be
  49. # kept the same as before.
  50. self.setItems([])
  51. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  52. if global_stack is None:
  53. self.setItems([])
  54. Logger.log("d", "No active GlobalStack, set quality profile model as empty.")
  55. return
  56. if not self._layer_height_unit:
  57. unit = global_stack.definition.getProperty("layer_height", "unit")
  58. if not unit:
  59. unit = ""
  60. self._layer_height_unit = unit
  61. # Check for material compatibility
  62. if not cura.CuraApplication.CuraApplication.getInstance().getMachineManager().activeMaterialsCompatible():
  63. Logger.log("d", "No active material compatibility, set quality profile model as empty.")
  64. self.setItems([])
  65. return
  66. quality_group_dict = ContainerTree.getInstance().getCurrentQualityGroups()
  67. item_list = []
  68. for quality_group in quality_group_dict.values():
  69. layer_height = fetchLayerHeight(quality_group)
  70. item = {"name": quality_group.name,
  71. "quality_type": quality_group.quality_type,
  72. "layer_height": layer_height,
  73. "layer_height_unit": self._layer_height_unit,
  74. "available": quality_group.is_available,
  75. "quality_group": quality_group,
  76. "is_experimental": quality_group.is_experimental}
  77. item_list.append(item)
  78. # Sort items based on layer_height
  79. item_list = sorted(item_list, key = lambda x: x["layer_height"])
  80. self.setItems(item_list)