QualityProfilesDropDownMenuModel.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. 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.UserRole + 1
  12. QualityTypeRole = Qt.UserRole + 2
  13. LayerHeightRole = Qt.UserRole + 3
  14. LayerHeightUnitRole = Qt.UserRole + 4
  15. AvailableRole = Qt.UserRole + 5
  16. QualityGroupRole = Qt.UserRole + 6
  17. QualityChangesGroupRole = Qt.UserRole + 7
  18. IsExperimentalRole = Qt.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. machine_manager.extruderChanged.connect(self._onChange)
  36. extruder_manager = application.getExtruderManager()
  37. extruder_manager.extrudersChanged.connect(self._onChange)
  38. self._layer_height_unit = "" # This is cached
  39. self._update_timer = QTimer() # type: QTimer
  40. self._update_timer.setInterval(100)
  41. self._update_timer.setSingleShot(True)
  42. self._update_timer.timeout.connect(self._update)
  43. self._onChange()
  44. def _onChange(self) -> None:
  45. self._update_timer.start()
  46. def _update(self):
  47. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  48. # CURA-6836
  49. # LabelBar is a repeater that creates labels for quality layer heights. Because of an optimization in
  50. # UM.ListModel, the model will not remove all items and recreate new ones every time there's an update.
  51. # Because LabelBar uses Repeater with Labels anchoring to "undefined" in certain cases, the anchoring will be
  52. # kept the same as before.
  53. self.setItems([])
  54. global_stack = cura.CuraApplication.CuraApplication.getInstance().getGlobalContainerStack()
  55. if global_stack is None:
  56. self.setItems([])
  57. Logger.log("d", "No active GlobalStack, set quality profile model as empty.")
  58. return
  59. if not self._layer_height_unit:
  60. unit = global_stack.definition.getProperty("layer_height", "unit")
  61. if not unit:
  62. unit = ""
  63. self._layer_height_unit = unit
  64. # Check for material compatibility
  65. if not cura.CuraApplication.CuraApplication.getInstance().getMachineManager().activeMaterialsCompatible():
  66. Logger.log("d", "No active material compatibility, set quality profile model as empty.")
  67. self.setItems([])
  68. return
  69. quality_group_dict = ContainerTree.getInstance().getCurrentQualityGroups()
  70. item_list = []
  71. for quality_group in quality_group_dict.values():
  72. layer_height = fetchLayerHeight(quality_group)
  73. item = {"name": quality_group.name,
  74. "quality_type": quality_group.quality_type,
  75. "layer_height": layer_height,
  76. "layer_height_unit": self._layer_height_unit,
  77. "available": quality_group.is_available,
  78. "quality_group": quality_group,
  79. "is_experimental": quality_group.is_experimental}
  80. item_list.append(item)
  81. # Sort items based on layer_height
  82. item_list = sorted(item_list, key = lambda x: x["layer_height"])
  83. self.setItems(item_list)