ExtruderConfigurationModel.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional
  4. from PyQt5.QtCore import pyqtProperty, QObject, pyqtSignal
  5. from cura.PrinterOutput.MaterialOutputModel import MaterialOutputModel
  6. class ExtruderConfigurationModel(QObject):
  7. extruderConfigurationChanged = pyqtSignal()
  8. def __init__(self, position: int = -1) -> None:
  9. super().__init__()
  10. self._position = position # type: int
  11. self._material = None # type: Optional[MaterialOutputModel]
  12. self._hotend_id = None # type: Optional[str]
  13. def setPosition(self, position: int) -> None:
  14. self._position = position
  15. @pyqtProperty(int, fset = setPosition, notify = extruderConfigurationChanged)
  16. def position(self) -> int:
  17. return self._position
  18. def setMaterial(self, material: Optional[MaterialOutputModel]) -> None:
  19. if self._hotend_id != material:
  20. self._material = material
  21. self.extruderConfigurationChanged.emit()
  22. @pyqtProperty(QObject, fset = setMaterial, notify = extruderConfigurationChanged)
  23. def activeMaterial(self) -> Optional[MaterialOutputModel]:
  24. return self._material
  25. @pyqtProperty(QObject, fset=setMaterial, notify=extruderConfigurationChanged)
  26. def material(self) -> Optional[MaterialOutputModel]:
  27. return self._material
  28. def setHotendID(self, hotend_id: Optional[str]) -> None:
  29. if self._hotend_id != hotend_id:
  30. self._hotend_id = hotend_id
  31. self.extruderConfigurationChanged.emit()
  32. @pyqtProperty(str, fset = setHotendID, notify = extruderConfigurationChanged)
  33. def hotendID(self) -> Optional[str]:
  34. return self._hotend_id
  35. ## This method is intended to indicate whether the configuration is valid or not.
  36. # The method checks if the mandatory fields are or not set
  37. # At this moment is always valid since we allow to have empty material and variants.
  38. def isValid(self) -> bool:
  39. return True
  40. def __str__(self) -> str:
  41. message_chunks = []
  42. message_chunks.append("Position: " + str(self._position))
  43. message_chunks.append("-")
  44. message_chunks.append("Material: " + self.activeMaterial.type if self.activeMaterial else "empty")
  45. message_chunks.append("-")
  46. message_chunks.append("HotendID: " + self.hotendID if self.hotendID else "empty")
  47. return " ".join(message_chunks)
  48. def __eq__(self, other) -> bool:
  49. return hash(self) == hash(other)
  50. # Calculating a hash function using the position of the extruder, the material GUID and the hotend id to check if is
  51. # unique within a set
  52. def __hash__(self):
  53. return hash(self._position) ^ (hash(self._material.guid) if self._material is not None else hash(0)) ^ hash(self._hotend_id)