ConfigurationModel.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. 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. return hash(self) == hash(other)
  57. ## The hash function is used to compare and create unique sets. The configuration is unique if the configuration
  58. # of the extruders is unique (the order of the extruders matters), and the type and buildplate is the same.
  59. def __hash__(self):
  60. extruder_hash = hash(0)
  61. first_extruder = None
  62. for configuration in self._extruder_configurations:
  63. extruder_hash ^= hash(configuration)
  64. if configuration.position == 0:
  65. first_extruder = configuration
  66. # To ensure the correct order of the extruders, we add an "and" operation using the first extruder hash value
  67. if first_extruder:
  68. extruder_hash &= hash(first_extruder)
  69. return hash(self._printer_type) ^ extruder_hash ^ hash(self._buildplate_configuration)