PrinterOutputDevice.py 8.9 KB

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