PrinterOutputDevice.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from enum import IntEnum
  4. from typing import Callable, List, Optional, Union
  5. from PyQt5.QtCore import pyqtProperty, pyqtSignal, QObject, QTimer, QUrl
  6. from PyQt5.QtWidgets import QMessageBox
  7. from UM.Logger import Logger
  8. from UM.Signal import signalemitter
  9. from UM.Qt.QtApplication import QtApplication
  10. from UM.FlameProfiler import pyqtSlot
  11. from UM.Decorators import deprecated
  12. from UM.i18n import i18nCatalog
  13. from UM.OutputDevice.OutputDevice import OutputDevice
  14. MYPY = False
  15. if MYPY:
  16. from UM.FileHandler.FileHandler import FileHandler
  17. from UM.Scene.SceneNode import SceneNode
  18. from .Models.PrinterOutputModel import PrinterOutputModel
  19. from .Models.PrinterConfigurationModel import PrinterConfigurationModel
  20. from .FirmwareUpdater import FirmwareUpdater
  21. i18n_catalog = i18nCatalog("cura")
  22. ## The current processing state of the backend.
  23. class ConnectionState(IntEnum):
  24. Closed = 0
  25. Connecting = 1
  26. Connected = 2
  27. Busy = 3
  28. Error = 4
  29. class ConnectionType(IntEnum):
  30. NotConnected = 0
  31. UsbConnection = 1
  32. NetworkConnection = 2
  33. CloudConnection = 3
  34. ## Printer output device adds extra interface options on top of output device.
  35. #
  36. # The assumption is made the printer is a FDM printer.
  37. #
  38. # Note that a number of settings are marked as "final". This is because decorators
  39. # are not inherited by children. To fix this we use the private counter part of those
  40. # functions to actually have the implementation.
  41. #
  42. # For all other uses it should be used in the same way as a "regular" OutputDevice.
  43. @signalemitter
  44. class PrinterOutputDevice(QObject, OutputDevice):
  45. printersChanged = pyqtSignal()
  46. connectionStateChanged = pyqtSignal(str)
  47. acceptsCommandsChanged = pyqtSignal()
  48. # Signal to indicate that the material of the active printer on the remote changed.
  49. materialIdChanged = pyqtSignal()
  50. # # Signal to indicate that the hotend of the active printer on the remote changed.
  51. hotendIdChanged = pyqtSignal()
  52. # Signal to indicate that the info text about the connection has changed.
  53. connectionTextChanged = pyqtSignal()
  54. # Signal to indicate that the configuration of one of the printers has changed.
  55. uniqueConfigurationsChanged = pyqtSignal()
  56. def __init__(self, device_id: str, connection_type: "ConnectionType" = ConnectionType.NotConnected, parent: QObject = None) -> None:
  57. super().__init__(device_id = device_id, parent = parent) # type: ignore # MyPy complains with the multiple inheritance
  58. self._printers = [] # type: List[PrinterOutputModel]
  59. self._unique_configurations = [] # type: List[PrinterConfigurationModel]
  60. self._monitor_view_qml_path = "" # type: str
  61. self._monitor_component = None # type: Optional[QObject]
  62. self._monitor_item = None # type: Optional[QObject]
  63. self._control_view_qml_path = "" # type: str
  64. self._control_component = None # type: Optional[QObject]
  65. self._control_item = None # type: Optional[QObject]
  66. self._accepts_commands = False # type: bool
  67. self._update_timer = QTimer() # type: QTimer
  68. self._update_timer.setInterval(2000) # TODO; Add preference for update interval
  69. self._update_timer.setSingleShot(False)
  70. self._update_timer.timeout.connect(self._update)
  71. self._connection_state = ConnectionState.Closed # type: ConnectionState
  72. self._connection_type = connection_type # type: ConnectionType
  73. self._firmware_updater = None # type: Optional[FirmwareUpdater]
  74. self._firmware_name = None # type: Optional[str]
  75. self._address = "" # type: str
  76. self._connection_text = "" # type: str
  77. self.printersChanged.connect(self._onPrintersChanged)
  78. QtApplication.getInstance().getOutputDeviceManager().outputDevicesChanged.connect(self._updateUniqueConfigurations)
  79. @pyqtProperty(str, notify = connectionTextChanged)
  80. def address(self) -> str:
  81. return self._address
  82. def setConnectionText(self, connection_text):
  83. if self._connection_text != connection_text:
  84. self._connection_text = connection_text
  85. self.connectionTextChanged.emit()
  86. @pyqtProperty(str, constant=True)
  87. def connectionText(self) -> str:
  88. return self._connection_text
  89. def materialHotendChangedMessage(self, callback: Callable[[int], None]) -> None:
  90. Logger.log("w", "materialHotendChangedMessage needs to be implemented, returning 'Yes'")
  91. callback(QMessageBox.Yes)
  92. def isConnected(self) -> bool:
  93. return self._connection_state != ConnectionState.Closed and self._connection_state != ConnectionState.Error
  94. def setConnectionState(self, connection_state: "ConnectionState") -> None:
  95. if self._connection_state != connection_state:
  96. self._connection_state = connection_state
  97. self.connectionStateChanged.emit(self._id)
  98. @pyqtProperty(int, constant = True)
  99. def connectionType(self) -> "ConnectionType":
  100. return self._connection_type
  101. @pyqtProperty(int, notify = connectionStateChanged)
  102. def connectionState(self) -> "ConnectionState":
  103. return self._connection_state
  104. def _update(self) -> None:
  105. pass
  106. def _getPrinterByKey(self, key: str) -> Optional["PrinterOutputModel"]:
  107. for printer in self._printers:
  108. if printer.key == key:
  109. return printer
  110. return None
  111. def requestWrite(self, nodes: List["SceneNode"], file_name: Optional[str] = None, limit_mimetypes: bool = False,
  112. file_handler: Optional["FileHandler"] = None, filter_by_machine: bool = False, **kwargs) -> None:
  113. raise NotImplementedError("requestWrite needs to be implemented")
  114. @pyqtProperty(QObject, notify = printersChanged)
  115. def activePrinter(self) -> Optional["PrinterOutputModel"]:
  116. if len(self._printers):
  117. return self._printers[0]
  118. return None
  119. @pyqtProperty("QVariantList", notify = printersChanged)
  120. def printers(self) -> List["PrinterOutputModel"]:
  121. return self._printers
  122. @pyqtProperty(QObject, constant = True)
  123. def monitorItem(self) -> QObject:
  124. # Note that we specifically only check if the monitor component is created.
  125. # It could be that it failed to actually create the qml item! If we check if the item was created, it will try to
  126. # create the item (and fail) every time.
  127. if not self._monitor_component:
  128. self._createMonitorViewFromQML()
  129. return self._monitor_item
  130. @pyqtProperty(QObject, constant = True)
  131. def controlItem(self) -> QObject:
  132. if not self._control_component:
  133. self._createControlViewFromQML()
  134. return self._control_item
  135. def _createControlViewFromQML(self) -> None:
  136. if not self._control_view_qml_path:
  137. return
  138. if self._control_item is None:
  139. self._control_item = QtApplication.getInstance().createQmlComponent(self._control_view_qml_path, {"OutputDevice": self})
  140. def _createMonitorViewFromQML(self) -> None:
  141. if not self._monitor_view_qml_path:
  142. return
  143. if self._monitor_item is None:
  144. self._monitor_item = QtApplication.getInstance().createQmlComponent(self._monitor_view_qml_path, {"OutputDevice": self})
  145. ## Attempt to establish connection
  146. def connect(self) -> None:
  147. self.setConnectionState(ConnectionState.Connecting)
  148. self._update_timer.start()
  149. ## Attempt to close the connection
  150. def close(self) -> None:
  151. self._update_timer.stop()
  152. self.setConnectionState(ConnectionState.Closed)
  153. ## Ensure that close gets called when object is destroyed
  154. def __del__(self) -> None:
  155. self.close()
  156. @pyqtProperty(bool, notify = acceptsCommandsChanged)
  157. def acceptsCommands(self) -> bool:
  158. return self._accepts_commands
  159. @deprecated("Please use the protected function instead", "3.2")
  160. def setAcceptsCommands(self, accepts_commands: bool) -> None:
  161. self._setAcceptsCommands(accepts_commands)
  162. ## Set a flag to signal the UI that the printer is not (yet) ready to receive commands
  163. def _setAcceptsCommands(self, accepts_commands: bool) -> None:
  164. if self._accepts_commands != accepts_commands:
  165. self._accepts_commands = accepts_commands
  166. self.acceptsCommandsChanged.emit()
  167. # Returns the unique configurations of the printers within this output device
  168. @pyqtProperty("QVariantList", notify = uniqueConfigurationsChanged)
  169. def uniqueConfigurations(self) -> List["PrinterConfigurationModel"]:
  170. return self._unique_configurations
  171. def _updateUniqueConfigurations(self) -> None:
  172. all_configurations = set()
  173. for printer in self._printers:
  174. if printer.printerConfiguration is not None and printer.printerConfiguration.hasAnyMaterialLoaded():
  175. all_configurations.add(printer.printerConfiguration)
  176. all_configurations.update(printer.availableConfigurations)
  177. new_configurations = sorted(all_configurations, key = lambda config: config.printerType)
  178. if new_configurations != self._unique_configurations:
  179. self._unique_configurations = new_configurations
  180. self.uniqueConfigurationsChanged.emit()
  181. # Returns the unique configurations of the printers within this output device
  182. @pyqtProperty("QStringList", notify = uniqueConfigurationsChanged)
  183. def uniquePrinterTypes(self) -> List[str]:
  184. return list(sorted(set([configuration.printerType for configuration in self._unique_configurations])))
  185. def _onPrintersChanged(self) -> None:
  186. for printer in self._printers:
  187. printer.configurationChanged.connect(self._updateUniqueConfigurations)
  188. printer.availableConfigurationsChanged.connect(self._updateUniqueConfigurations)
  189. # At this point there may be non-updated configurations
  190. self._updateUniqueConfigurations()
  191. ## Set the device firmware name
  192. #
  193. # \param name The name of the firmware.
  194. def _setFirmwareName(self, name: str) -> None:
  195. self._firmware_name = name
  196. ## Get the name of device firmware
  197. #
  198. # This name can be used to define device type
  199. def getFirmwareName(self) -> Optional[str]:
  200. return self._firmware_name
  201. def getFirmwareUpdater(self) -> Optional["FirmwareUpdater"]:
  202. return self._firmware_updater
  203. @pyqtSlot(str)
  204. def updateFirmware(self, firmware_file: Union[str, QUrl]) -> None:
  205. if not self._firmware_updater:
  206. return
  207. self._firmware_updater.updateFirmware(firmware_file)