QualityProfilesDropDownMenuModel.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import Qt
  4. from UM.Application import Application
  5. from UM.Logger import Logger
  6. from UM.Qt.ListModel import ListModel
  7. from UM.Settings.SettingFunction import SettingFunction
  8. from cura.Machines.QualityManager import QualityGroup
  9. #
  10. # QML Model for all built-in quality profiles. This model is used for the drop-down quality menu.
  11. #
  12. class QualityProfilesDropDownMenuModel(ListModel):
  13. NameRole = Qt.UserRole + 1
  14. QualityTypeRole = Qt.UserRole + 2
  15. LayerHeightRole = Qt.UserRole + 3
  16. LayerHeightUnitRole = Qt.UserRole + 4
  17. AvailableRole = Qt.UserRole + 5
  18. QualityGroupRole = Qt.UserRole + 6
  19. QualityChangesGroupRole = Qt.UserRole + 7
  20. def __init__(self, parent = None):
  21. super().__init__(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._application = Application.getInstance()
  30. self._machine_manager = self._application.getMachineManager()
  31. self._quality_manager = Application.getInstance().getQualityManager()
  32. self._application.globalContainerStackChanged.connect(self._update)
  33. self._machine_manager.activeQualityGroupChanged.connect(self._update)
  34. self._machine_manager.extruderChanged.connect(self._update)
  35. self._quality_manager.qualitiesUpdated.connect(self._update)
  36. self._layer_height_unit = "" # This is cached
  37. self._update()
  38. def _update(self):
  39. Logger.log("d", "Updating {model_class_name}.".format(model_class_name = self.__class__.__name__))
  40. global_stack = self._machine_manager.activeMachine
  41. if global_stack is None:
  42. self.setItems([])
  43. Logger.log("d", "No active GlobalStack, set quality profile model as empty.")
  44. return
  45. # Check for material compatibility
  46. if not self._machine_manager.activeMaterialsCompatible():
  47. Logger.log("d", "No active material compatibility, set quality profile model as empty.")
  48. self.setItems([])
  49. return
  50. quality_group_dict = self._quality_manager.getQualityGroups(global_stack)
  51. item_list = []
  52. for key in sorted(quality_group_dict):
  53. quality_group = quality_group_dict[key]
  54. layer_height = self._fetchLayerHeight(quality_group)
  55. item = {"name": quality_group.name,
  56. "quality_type": quality_group.quality_type,
  57. "layer_height": layer_height,
  58. "layer_height_unit": self._layer_height_unit,
  59. "available": quality_group.is_available,
  60. "quality_group": quality_group}
  61. item_list.append(item)
  62. # Sort items based on layer_height
  63. item_list = sorted(item_list, key = lambda x: x["layer_height"])
  64. self.setItems(item_list)
  65. def _fetchLayerHeight(self, quality_group: "QualityGroup") -> float:
  66. global_stack = self._machine_manager.activeMachine
  67. if not self._layer_height_unit:
  68. unit = global_stack.definition.getProperty("layer_height", "unit")
  69. if not unit:
  70. unit = ""
  71. self._layer_height_unit = unit
  72. default_layer_height = global_stack.definition.getProperty("layer_height", "value")
  73. # Get layer_height from the quality profile for the GlobalStack
  74. if quality_group.node_for_global is None:
  75. return float(default_layer_height)
  76. container = quality_group.node_for_global.getContainer()
  77. layer_height = default_layer_height
  78. if container and container.hasProperty("layer_height", "value"):
  79. layer_height = container.getProperty("layer_height", "value")
  80. else:
  81. # Look for layer_height in the GlobalStack from material -> definition
  82. container = global_stack.definition
  83. if container and container.hasProperty("layer_height", "value"):
  84. layer_height = container.getProperty("layer_height", "value")
  85. if isinstance(layer_height, SettingFunction):
  86. layer_height = layer_height(global_stack)
  87. return float(layer_height)