BaseMaterialsModel.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. # Update the stack and the model data when the machine changes
  35. self._machine_manager.globalContainerChanged.connect(self._updateExtruderStack)
  36. def _updateExtruderStack(self):
  37. global_stack = self._machine_manager.activeMachine
  38. if global_stack is None:
  39. return
  40. if self._extruder_stack is not None:
  41. self._extruder_stack.pyqtContainersChanged.disconnect(self._update)
  42. self._extruder_stack = global_stack.extruders.get(str(self._extruder_position))
  43. if self._extruder_stack is not None:
  44. self._extruder_stack.pyqtContainersChanged.connect(self._update)
  45. # Force update the model when the extruder stack changes
  46. self._update()
  47. def setExtruderPosition(self, position: int):
  48. if self._extruder_stack is None or self._extruder_position != position:
  49. self._extruder_position = position
  50. self._updateExtruderStack()
  51. self.extruderPositionChanged.emit()
  52. @pyqtProperty(int, fset = setExtruderPosition, notify = extruderPositionChanged)
  53. def extruderPosition(self) -> int:
  54. return self._extruder_position
  55. #
  56. # This is an abstract method that needs to be implemented by
  57. #
  58. def _update(self):
  59. pass