PrintJobOutputModel.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtSignal, pyqtProperty, QObject, pyqtSlot
  4. from typing import Optional, TYPE_CHECKING, List
  5. from PyQt5.QtCore import QUrl
  6. from PyQt5.QtGui import QImage
  7. if TYPE_CHECKING:
  8. from cura.PrinterOutput.PrinterOutputController import PrinterOutputController
  9. from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
  10. from cura.PrinterOutput.ConfigurationModel import ConfigurationModel
  11. class PrintJobOutputModel(QObject):
  12. stateChanged = pyqtSignal()
  13. timeTotalChanged = pyqtSignal()
  14. timeElapsedChanged = pyqtSignal()
  15. nameChanged = pyqtSignal()
  16. keyChanged = pyqtSignal()
  17. assignedPrinterChanged = pyqtSignal()
  18. ownerChanged = pyqtSignal()
  19. configurationChanged = pyqtSignal()
  20. previewImageChanged = pyqtSignal()
  21. compatibleMachineFamiliesChanged = pyqtSignal()
  22. def __init__(self, output_controller: "PrinterOutputController", key: str = "", name: str = "", parent=None) -> None:
  23. super().__init__(parent)
  24. self._output_controller = output_controller
  25. self._state = ""
  26. self._time_total = 0
  27. self._time_elapsed = 0
  28. self._name = name # Human readable name
  29. self._key = key # Unique identifier
  30. self._assigned_printer = None # type: Optional[PrinterOutputModel]
  31. self._owner = "" # Who started/owns the print job?
  32. self._configuration = None # type: Optional[ConfigurationModel]
  33. self._compatible_machine_families = [] # type: List[str]
  34. self._preview_image_id = 0
  35. self._preview_image = None # type: Optional[QImage]
  36. @pyqtProperty("QStringList", notify=compatibleMachineFamiliesChanged)
  37. def compatibleMachineFamilies(self):
  38. # Hack; Some versions of cluster will return a family more than once...
  39. return set(self._compatible_machine_families)
  40. def setCompatibleMachineFamilies(self, compatible_machine_families: List[str]) -> None:
  41. if self._compatible_machine_families != compatible_machine_families:
  42. self._compatible_machine_families = compatible_machine_families
  43. self.compatibleMachineFamiliesChanged.emit()
  44. @pyqtProperty(QUrl, notify=previewImageChanged)
  45. def previewImageUrl(self):
  46. self._preview_image_id += 1
  47. # There is an image provider that is called "camera". In order to ensure that the image qml object, that
  48. # requires a QUrl to function, updates correctly we add an increasing number. This causes to see the QUrl
  49. # as new (instead of relying on cached version and thus forces an update.
  50. temp = "image://print_job_preview/" + str(self._preview_image_id) + "/" + self._key
  51. return QUrl(temp, QUrl.TolerantMode)
  52. def getPreviewImage(self) -> Optional[QImage]:
  53. return self._preview_image
  54. def updatePreviewImage(self, preview_image: Optional[QImage]) -> None:
  55. if self._preview_image != preview_image:
  56. self._preview_image = preview_image
  57. self.previewImageChanged.emit()
  58. @pyqtProperty(QObject, notify=configurationChanged)
  59. def configuration(self) -> Optional["ConfigurationModel"]:
  60. return self._configuration
  61. def updateConfiguration(self, configuration: Optional["ConfigurationModel"]) -> None:
  62. if self._configuration != configuration:
  63. self._configuration = configuration
  64. self.configurationChanged.emit()
  65. @pyqtProperty(str, notify=ownerChanged)
  66. def owner(self):
  67. return self._owner
  68. def updateOwner(self, owner):
  69. if self._owner != owner:
  70. self._owner = owner
  71. self.ownerChanged.emit()
  72. @pyqtProperty(QObject, notify=assignedPrinterChanged)
  73. def assignedPrinter(self):
  74. return self._assigned_printer
  75. def updateAssignedPrinter(self, assigned_printer: "PrinterOutputModel"):
  76. if self._assigned_printer != assigned_printer:
  77. old_printer = self._assigned_printer
  78. self._assigned_printer = assigned_printer
  79. if old_printer is not None:
  80. # If the previously assigned printer is set, this job is moved away from it.
  81. old_printer.updateActivePrintJob(None)
  82. self.assignedPrinterChanged.emit()
  83. @pyqtProperty(str, notify=keyChanged)
  84. def key(self):
  85. return self._key
  86. def updateKey(self, key: str):
  87. if self._key != key:
  88. self._key = key
  89. self.keyChanged.emit()
  90. @pyqtProperty(str, notify = nameChanged)
  91. def name(self):
  92. return self._name
  93. def updateName(self, name: str):
  94. if self._name != name:
  95. self._name = name
  96. self.nameChanged.emit()
  97. @pyqtProperty(int, notify = timeTotalChanged)
  98. def timeTotal(self):
  99. return self._time_total
  100. @pyqtProperty(int, notify = timeElapsedChanged)
  101. def timeElapsed(self):
  102. return self._time_elapsed
  103. @pyqtProperty(str, notify=stateChanged)
  104. def state(self):
  105. return self._state
  106. def updateTimeTotal(self, new_time_total):
  107. if self._time_total != new_time_total:
  108. self._time_total = new_time_total
  109. self.timeTotalChanged.emit()
  110. def updateTimeElapsed(self, new_time_elapsed):
  111. if self._time_elapsed != new_time_elapsed:
  112. self._time_elapsed = new_time_elapsed
  113. self.timeElapsedChanged.emit()
  114. def updateState(self, new_state):
  115. if self._state != new_state:
  116. self._state = new_state
  117. self.stateChanged.emit()
  118. @pyqtSlot(str)
  119. def setState(self, state):
  120. self._output_controller.setJobState(self, state)