PrinterOutputDevice.py 8.2 KB

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