ConfigurationModel.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.ExtruderConfigurationModel import ExtruderConfigurationModel
  8. class ConfigurationModel(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):
  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"]):
  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. self._buildplate_configuration = buildplate_configuration
  31. @pyqtProperty(str, fset = setBuildplateConfiguration, notify = configurationChanged)
  32. def buildplateConfiguration(self) -> str:
  33. return self._buildplate_configuration
  34. ## This method is intended to indicate whether the configuration is valid or not.
  35. # The method checks if the mandatory fields are or not set
  36. def isValid(self) -> bool:
  37. if not self._extruder_configurations:
  38. return False
  39. for configuration in self._extruder_configurations:
  40. if configuration is None:
  41. return False
  42. return self._printer_type != ""
  43. def __str__(self):
  44. message_chunks = []
  45. message_chunks.append("Printer type: " + self._printer_type)
  46. message_chunks.append("Extruders: [")
  47. for configuration in self._extruder_configurations:
  48. message_chunks.append(" " + str(configuration))
  49. message_chunks.append("]")
  50. if self._buildplate_configuration is not None:
  51. message_chunks.append("Buildplate: " + self._buildplate_configuration)
  52. return "\n".join(message_chunks)
  53. def __eq__(self, other):
  54. return hash(self) == hash(other)
  55. ## The hash function is used to compare and create unique sets. The configuration is unique if the configuration
  56. # of the extruders is unique (the order of the extruders matters), and the type and buildplate is the same.
  57. def __hash__(self):
  58. extruder_hash = hash(0)
  59. first_extruder = None
  60. for configuration in self._extruder_configurations:
  61. extruder_hash ^= hash(configuration)
  62. if configuration.position == 0:
  63. first_extruder = configuration
  64. # To ensure the correct order of the extruders, we add an "and" operation using the first extruder hash value
  65. if first_extruder:
  66. extruder_hash &= hash(first_extruder)
  67. return hash(self._printer_type) ^ extruder_hash ^ hash(self._buildplate_configuration)