PrinterOutputDevice.py 11 KB

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