BaseMaterialsModel.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. ColorCodeRole = Qt.UserRole + 8
  21. GUIDRole = Qt.UserRole + 9
  22. extruderPositionChanged = pyqtSignal()
  23. def __init__(self, parent = None):
  24. super().__init__(parent)
  25. self._application = Application.getInstance()
  26. self._machine_manager = self._application.getMachineManager()
  27. self.addRoleName(self.RootMaterialIdRole, "root_material_id")
  28. self.addRoleName(self.IdRole, "id")
  29. self.addRoleName(self.GUIDRole, "guid")
  30. self.addRoleName(self.NameRole, "name")
  31. self.addRoleName(self.BrandRole, "brand")
  32. self.addRoleName(self.MaterialRole, "material")
  33. self.addRoleName(self.ColorRole, "color_name")
  34. self.addRoleName(self.ColorCodeRole, "color_code")
  35. self.addRoleName(self.ContainerNodeRole, "container_node")
  36. self._extruder_position = 0
  37. self._extruder_stack = None
  38. # Update the stack and the model data when the machine changes
  39. self._machine_manager.globalContainerChanged.connect(self._updateExtruderStack)
  40. def _updateExtruderStack(self):
  41. global_stack = self._machine_manager.activeMachine
  42. if global_stack is None:
  43. return
  44. if self._extruder_stack is not None:
  45. self._extruder_stack.pyqtContainersChanged.disconnect(self._update)
  46. self._extruder_stack = global_stack.extruders.get(str(self._extruder_position))
  47. if self._extruder_stack is not None:
  48. self._extruder_stack.pyqtContainersChanged.connect(self._update)
  49. # Force update the model when the extruder stack changes
  50. self._update()
  51. def setExtruderPosition(self, position: int):
  52. if self._extruder_stack is None or self._extruder_position != position:
  53. self._extruder_position = position
  54. self._updateExtruderStack()
  55. self.extruderPositionChanged.emit()
  56. @pyqtProperty(int, fset = setExtruderPosition, notify = extruderPositionChanged)
  57. def extruderPosition(self) -> int:
  58. return self._extruder_position
  59. #
  60. # This is an abstract method that needs to be implemented by
  61. #
  62. def _update(self):
  63. pass