PrintJobOutputModel.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # Copyright (c) 2018 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 "print_job_preview". 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: Optional["PrinterOutputModel"]) -> None:
  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) -> int:
  99. return self._time_total
  100. @pyqtProperty(int, notify = timeElapsedChanged)
  101. def timeElapsed(self) -> int:
  102. return self._time_elapsed
  103. @pyqtProperty(int, notify = timeElapsedChanged)
  104. def timeRemaining(self) -> int:
  105. # Never get a negative time remaining
  106. return max(self.timeTotal - self.timeElapsed, 0)
  107. @pyqtProperty(float, notify = timeElapsedChanged)
  108. def progress(self) -> float:
  109. time_elapsed = max(float(self.timeElapsed), 1.0) # Prevent a division by zero exception
  110. result = time_elapsed / self.timeTotal
  111. return min(result, 1.0) # Never get a progress past 1.0
  112. @pyqtProperty(str, notify=stateChanged)
  113. def state(self) -> str:
  114. return self._state
  115. @pyqtProperty(bool, notify=stateChanged)
  116. def isActive(self) -> bool:
  117. inactiveStates = [
  118. "pausing",
  119. "paused",
  120. "resuming",
  121. "wait_cleanup"
  122. ]
  123. if self.state in inactiveStates and self.timeRemaining > 0:
  124. return False
  125. return True
  126. def updateTimeTotal(self, new_time_total):
  127. if self._time_total != new_time_total:
  128. self._time_total = new_time_total
  129. self.timeTotalChanged.emit()
  130. def updateTimeElapsed(self, new_time_elapsed):
  131. if self._time_elapsed != new_time_elapsed:
  132. self._time_elapsed = new_time_elapsed
  133. self.timeElapsedChanged.emit()
  134. def updateState(self, new_state):
  135. if self._state != new_state:
  136. self._state = new_state
  137. self.stateChanged.emit()
  138. @pyqtSlot(str)
  139. def setState(self, state):
  140. self._output_controller.setJobState(self, state)