PrinterOutputDevice.py 8.0 KB

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