BaseMaterialsModel.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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, pyqtSignal, pyqtProperty
  4. from UM.Application import Application
  5. from UM.Qt.ListModel import ListModel
  6. #
  7. # This is the base model class for GenericMaterialsModel and BrandMaterialsModel
  8. # Those 2 models are used by the material drop down menu to show generic materials and branded materials separately.
  9. # The extruder position defined here is being used to bound a menu to the correct extruder. This is used in the top
  10. # bar menu "Settings" -> "Extruder nr" -> "Material" -> this menu
  11. #
  12. class BaseMaterialsModel(ListModel):
  13. RootMaterialIdRole = Qt.UserRole + 1
  14. IdRole = Qt.UserRole + 2
  15. NameRole = Qt.UserRole + 3
  16. BrandRole = Qt.UserRole + 4
  17. MaterialRole = Qt.UserRole + 5
  18. ColorRole = Qt.UserRole + 6
  19. ContainerNodeRole = Qt.UserRole + 7
  20. extruderPositionChanged = pyqtSignal()
  21. def __init__(self, parent = None):
  22. super().__init__(parent)
  23. self._application = Application.getInstance()
  24. self._machine_manager = self._application.getMachineManager()
  25. self.addRoleName(self.RootMaterialIdRole, "root_material_id")
  26. self.addRoleName(self.IdRole, "id")
  27. self.addRoleName(self.NameRole, "name")
  28. self.addRoleName(self.BrandRole, "brand")
  29. self.addRoleName(self.MaterialRole, "material")
  30. self.addRoleName(self.ColorRole, "color_name")
  31. self.addRoleName(self.ContainerNodeRole, "container_node")
  32. self._extruder_position = 0
  33. self._extruder_stack = None
  34. def _updateExtruderStack(self):
  35. global_stack = self._machine_manager.activeMachine
  36. if global_stack is None:
  37. return
  38. if self._extruder_stack is not None:
  39. self._extruder_stack.pyqtContainersChanged.disconnect(self._update)
  40. self._extruder_stack = global_stack.extruders.get(str(self._extruder_position))
  41. if self._extruder_stack is not None:
  42. self._extruder_stack.pyqtContainersChanged.connect(self._update)
  43. def setExtruderPosition(self, position: int):
  44. if self._extruder_position != position:
  45. self._extruder_position = position
  46. self._updateExtruderStack()
  47. self.extruderPositionChanged.emit()
  48. @pyqtProperty(int, fset = setExtruderPosition, notify = extruderPositionChanged)
  49. def extruderPosition(self) -> int:
  50. return self._extruder_position
  51. #
  52. # This is an abstract method that needs to be implemented by
  53. #
  54. def _update(self):
  55. pass