ExtruderConfigurationModel.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 self._material != 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. if not isinstance(other, ExtruderConfigurationModel):
  50. return False
  51. if self._position != other.position:
  52. return False
  53. # Empty materials should be ignored for comparison
  54. if self.activeMaterial is not None and other.activeMaterial is not None:
  55. if self.activeMaterial.guid != other.activeMaterial.guid:
  56. if self.activeMaterial.guid != "" and other.activeMaterial.guid != "":
  57. return False
  58. else:
  59. # At this point there is no material, so it doesn't matter what the hotend is.
  60. return True
  61. if self.hotendID != other.hotendID:
  62. return False
  63. return True
  64. # Calculating a hash function using the position of the extruder, the material GUID and the hotend id to check if is
  65. # unique within a set
  66. def __hash__(self):
  67. return hash(self._position) ^ (hash(self._material.guid) if self._material is not None else hash(0)) ^ hash(self._hotend_id)