ExtruderConfigurationModel.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 .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 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. ## This method is intended to indicate whether the configuration is valid or not.
  37. # The method checks if the mandatory fields are or not set
  38. # At this moment is always valid since we allow to have empty material and variants.
  39. def isValid(self) -> bool:
  40. return True
  41. def __str__(self) -> str:
  42. message_chunks = []
  43. message_chunks.append("Position: " + str(self._position))
  44. message_chunks.append("-")
  45. message_chunks.append("Material: " + self.activeMaterial.type if self.activeMaterial else "empty")
  46. message_chunks.append("-")
  47. message_chunks.append("HotendID: " + self.hotendID if self.hotendID else "empty")
  48. return " ".join(message_chunks)
  49. def __eq__(self, other) -> bool:
  50. if not isinstance(other, ExtruderConfigurationModel):
  51. return False
  52. if self._position != other.position:
  53. return False
  54. # Empty materials should be ignored for comparison
  55. if self.activeMaterial is not None and other.activeMaterial is not None:
  56. if self.activeMaterial.guid != other.activeMaterial.guid:
  57. if self.activeMaterial.guid != "" and other.activeMaterial.guid != "":
  58. return False
  59. else:
  60. # At this point there is no material, so it doesn't matter what the hotend is.
  61. return True
  62. if self.hotendID != other.hotendID:
  63. return False
  64. return True
  65. # Calculating a hash function using the position of the extruder, the material GUID and the hotend id to check if is
  66. # unique within a set
  67. def __hash__(self):
  68. return hash(self._position) ^ (hash(self._material.guid) if self._material is not None else hash(0)) ^ hash(self._hotend_id)