ExtruderConfigurationModel.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 PyQt6.QtCore import pyqtProperty, QObject, pyqtSignal
  5. from .MaterialOutputModel import MaterialOutputModel
  6. class ExtruderConfigurationModel(QObject):
  7. extruderConfigurationChanged = pyqtSignal()
  8. def __init__(self, position: int = -1) -> None:
  9. super().__init__()
  10. self._position: int = position
  11. self._material: Optional[MaterialOutputModel] = None
  12. self._hotend_id: Optional[str] = None
  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 material is None or self._material == material:
  20. return
  21. self._material = material
  22. self.extruderConfigurationChanged.emit()
  23. @pyqtProperty(QObject, fset = setMaterial, notify = extruderConfigurationChanged)
  24. def activeMaterial(self) -> Optional[MaterialOutputModel]:
  25. return self._material
  26. @pyqtProperty(QObject, fset = setMaterial, notify = extruderConfigurationChanged)
  27. def material(self) -> Optional[MaterialOutputModel]:
  28. return self._material
  29. def setHotendID(self, hotend_id: Optional[str]) -> None:
  30. if self._hotend_id != hotend_id:
  31. self._hotend_id = hotend_id
  32. self.extruderConfigurationChanged.emit()
  33. @pyqtProperty(str, fset = setHotendID, notify = extruderConfigurationChanged)
  34. def hotendID(self) -> Optional[str]:
  35. return self._hotend_id
  36. def isValid(self) -> bool:
  37. """This method is intended to indicate whether the configuration is valid or not.
  38. The method checks if the mandatory fields are or not set
  39. At this moment is always valid since we allow to have empty material and variants.
  40. """
  41. return True
  42. def __str__(self) -> str:
  43. message_chunks = []
  44. message_chunks.append("Position: " + str(self._position))
  45. message_chunks.append("-")
  46. message_chunks.append("Material: " + self.activeMaterial.type if self.activeMaterial else "empty")
  47. message_chunks.append("-")
  48. message_chunks.append("HotendID: " + self.hotendID if self.hotendID else "empty")
  49. return " ".join(message_chunks)
  50. def __eq__(self, other) -> bool:
  51. if not isinstance(other, ExtruderConfigurationModel):
  52. return False
  53. if self._position != other.position:
  54. return False
  55. # Empty materials should be ignored for comparison
  56. if self.activeMaterial is not None and other.activeMaterial is not None:
  57. if self.activeMaterial.guid != other.activeMaterial.guid:
  58. if self.activeMaterial.guid == "" and other.activeMaterial.guid == "":
  59. # At this point there is no material, so it doesn't matter what the hotend is.
  60. return True
  61. else:
  62. return False
  63. if self.hotendID != other.hotendID:
  64. return False
  65. return True
  66. # Calculating a hash function using the position of the extruder, the material GUID and the hotend id to check if is
  67. # unique within a set
  68. def __hash__(self):
  69. return hash(self._position) ^ (hash(self._material.guid) if self._material is not None else hash(0)) ^ hash(self._hotend_id)