PrinterConfigurationModel.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. 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. """
  40. if not self._extruder_configurations:
  41. return False
  42. for configuration in self._extruder_configurations:
  43. if configuration is None:
  44. return False
  45. return self._printer_type != ""
  46. def hasAnyMaterialLoaded(self) -> bool:
  47. if not self.isValid():
  48. return False
  49. for configuration in self._extruder_configurations:
  50. if configuration.activeMaterial and configuration.activeMaterial.type != "empty":
  51. return True
  52. return False
  53. def __str__(self):
  54. message_chunks = []
  55. message_chunks.append("Printer type: " + self._printer_type)
  56. message_chunks.append("Extruders: [")
  57. for configuration in self._extruder_configurations:
  58. message_chunks.append(" " + str(configuration))
  59. message_chunks.append("]")
  60. if self._buildplate_configuration is not None:
  61. message_chunks.append("Buildplate: " + self._buildplate_configuration)
  62. return "\n".join(message_chunks)
  63. def __eq__(self, other):
  64. if not isinstance(other, PrinterConfigurationModel):
  65. return False
  66. if self.printerType != other.printerType:
  67. return False
  68. if self.buildplateConfiguration != other.buildplateConfiguration:
  69. return False
  70. if len(self.extruderConfigurations) != len(other.extruderConfigurations):
  71. return False
  72. for self_extruder, other_extruder in zip(sorted(self._extruder_configurations, key=lambda x: x.position), sorted(other.extruderConfigurations, key=lambda x: x.position)):
  73. if self_extruder != other_extruder:
  74. return False
  75. return True
  76. def __hash__(self):
  77. """The hash function is used to compare and create unique sets. The configuration is unique if the configuration
  78. of the extruders is unique (the order of the extruders matters), and the type and buildplate is the same.
  79. """
  80. extruder_hash = hash(0)
  81. first_extruder = None
  82. for configuration in self._extruder_configurations:
  83. extruder_hash ^= hash(configuration)
  84. if configuration.position == 0:
  85. first_extruder = configuration
  86. # To ensure the correct order of the extruders, we add an "and" operation using the first extruder hash value
  87. if first_extruder:
  88. extruder_hash &= hash(first_extruder)
  89. return hash(self._printer_type) ^ extruder_hash ^ hash(self._buildplate_configuration)