PrinterConfigurationModel.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtProperty, QObject, pyqtSignal
  4. from typing import List
  5. MYPY = False
  6. if MYPY:
  7. from cura.PrinterOutput.Models.ExtruderConfigurationModel import ExtruderConfigurationModel
  8. class PrinterConfigurationModel(QObject):
  9. configurationChanged = pyqtSignal()
  10. def __init__(self) -> None:
  11. super().__init__()
  12. self._printer_type = ""
  13. self._extruder_configurations = [] # type: List[ExtruderConfigurationModel]
  14. self._buildplate_configuration = ""
  15. def setPrinterType(self, printer_type: str) -> None:
  16. self._printer_type = printer_type
  17. @pyqtProperty(str, fset = setPrinterType, notify = configurationChanged)
  18. def printerType(self) -> str:
  19. return self._printer_type
  20. def setExtruderConfigurations(self, extruder_configurations: List["ExtruderConfigurationModel"]) -> None:
  21. if self._extruder_configurations != extruder_configurations:
  22. self._extruder_configurations = extruder_configurations
  23. for extruder_configuration in self._extruder_configurations:
  24. extruder_configuration.extruderConfigurationChanged.connect(self.configurationChanged)
  25. self.configurationChanged.emit()
  26. @pyqtProperty("QVariantList", fset = setExtruderConfigurations, notify = configurationChanged)
  27. def extruderConfigurations(self):
  28. return self._extruder_configurations
  29. def setBuildplateConfiguration(self, buildplate_configuration: str) -> None:
  30. if self._buildplate_configuration != buildplate_configuration:
  31. self._buildplate_configuration = buildplate_configuration
  32. self.configurationChanged.emit()
  33. @pyqtProperty(str, fset = setBuildplateConfiguration, notify = configurationChanged)
  34. def buildplateConfiguration(self) -> str:
  35. return self._buildplate_configuration
  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. def isValid(self) -> bool:
  39. if not self._extruder_configurations:
  40. return False
  41. for configuration in self._extruder_configurations:
  42. if configuration is None:
  43. return False
  44. return self._printer_type != ""
  45. def __str__(self):
  46. message_chunks = []
  47. message_chunks.append("Printer type: " + self._printer_type)
  48. message_chunks.append("Extruders: [")
  49. for configuration in self._extruder_configurations:
  50. message_chunks.append(" " + str(configuration))
  51. message_chunks.append("]")
  52. if self._buildplate_configuration is not None:
  53. message_chunks.append("Buildplate: " + self._buildplate_configuration)
  54. return "\n".join(message_chunks)
  55. def __eq__(self, other):
  56. if not isinstance(other, PrinterConfigurationModel):
  57. return False
  58. if self.printerType != other.printerType:
  59. return False
  60. if self.buildplateConfiguration != other.buildplateConfiguration:
  61. return False
  62. if len(self.extruderConfigurations) != len(other.extruderConfigurations):
  63. return False
  64. for self_extruder, other_extruder in zip(sorted(self._extruder_configurations, key=lambda x: x.position), sorted(other.extruderConfigurations, key=lambda x: x.position)):
  65. if self_extruder != other_extruder:
  66. return False
  67. return True
  68. ## The hash function is used to compare and create unique sets. The configuration is unique if the configuration
  69. # of the extruders is unique (the order of the extruders matters), and the type and buildplate is the same.
  70. def __hash__(self):
  71. extruder_hash = hash(0)
  72. first_extruder = None
  73. for configuration in self._extruder_configurations:
  74. extruder_hash ^= hash(configuration)
  75. if configuration.position == 0:
  76. first_extruder = configuration
  77. # To ensure the correct order of the extruders, we add an "and" operation using the first extruder hash value
  78. if first_extruder:
  79. extruder_hash &= hash(first_extruder)
  80. return hash(self._printer_type) ^ extruder_hash ^ hash(self._buildplate_configuration)