QualityProfilesDropDownMenuModel.py 4.7 KB

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