ConfigurationModel.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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):
  11. super().__init__()
  12. self._printer_type = None
  13. self._extruder_configurations = [] # type: List[ExtruderConfigurationModel]
  14. self._buildplate_configuration = None
  15. def setPrinterType(self, printer_type):
  16. self._printer_type = printer_type
  17. @pyqtProperty(str, fset = setPrinterType, notify = configurationChanged)
  18. def printerType(self):
  19. return self._printer_type
  20. def setExtruderConfigurations(self, extruder_configurations):
  21. self._extruder_configurations = extruder_configurations
  22. @pyqtProperty("QVariantList", fset = setExtruderConfigurations, notify = configurationChanged)
  23. def extruderConfigurations(self):
  24. return self._extruder_configurations
  25. def setBuildplateConfiguration(self, buildplate_configuration):
  26. self._buildplate_configuration = buildplate_configuration
  27. @pyqtProperty(str, fset = setBuildplateConfiguration, notify = configurationChanged)
  28. def buildplateConfiguration(self):
  29. return self._buildplate_configuration
  30. ## This method is intended to indicate whether the configuration is valid or not.
  31. # The method checks if the mandatory fields are or not set
  32. def isValid(self):
  33. if not self._extruder_configurations:
  34. return False
  35. for configuration in self._extruder_configurations:
  36. if configuration is None:
  37. return False
  38. return self._printer_type is not None
  39. def __str__(self):
  40. message_chunks = []
  41. message_chunks.append("Printer type: " + self._printer_type)
  42. message_chunks.append("Extruders: [")
  43. for configuration in self._extruder_configurations:
  44. message_chunks.append(" " + str(configuration))
  45. message_chunks.append("]")
  46. if self._buildplate_configuration is not None:
  47. message_chunks.append("Buildplate: " + self._buildplate_configuration)
  48. return "\n".join(message_chunks)
  49. def __eq__(self, other):
  50. return hash(self) == hash(other)
  51. ## The hash function is used to compare and create unique sets. The configuration is unique if the configuration
  52. # of the extruders is unique (the order of the extruders matters), and the type and buildplate is the same.
  53. def __hash__(self):
  54. extruder_hash = hash(0)
  55. first_extruder = None
  56. for configuration in self._extruder_configurations:
  57. extruder_hash ^= hash(configuration)
  58. if configuration.position == 0:
  59. first_extruder = configuration
  60. # To ensure the correct order of the extruders, we add an "and" operation using the first extruder hash value
  61. if first_extruder:
  62. extruder_hash &= hash(first_extruder)
  63. return hash(self._printer_type) ^ extruder_hash ^ hash(self._buildplate_configuration)