PrinterOutputDevice.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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._address = ""
  57. self._connection_text = ""
  58. self.printersChanged.connect(self._onPrintersChanged)
  59. Application.getInstance().getOutputDeviceManager().outputDevicesChanged.connect(self._updateUniqueConfigurations)
  60. @pyqtProperty(str, notify = connectionTextChanged)
  61. def address(self):
  62. return self._address
  63. def setConnectionText(self, connection_text):
  64. if self._connection_text != connection_text:
  65. self._connection_text = connection_text
  66. self.connectionTextChanged.emit()
  67. @pyqtProperty(str, constant=True)
  68. def connectionText(self):
  69. return self._connection_text
  70. def materialHotendChangedMessage(self, callback):
  71. Logger.log("w", "materialHotendChangedMessage needs to be implemented, returning 'Yes'")
  72. callback(QMessageBox.Yes)
  73. def isConnected(self):
  74. return self._connection_state != ConnectionState.closed and self._connection_state != ConnectionState.error
  75. def setConnectionState(self, connection_state):
  76. if self._connection_state != connection_state:
  77. self._connection_state = connection_state
  78. self.connectionStateChanged.emit(self._id)
  79. @pyqtProperty(str, notify = connectionStateChanged)
  80. def connectionState(self):
  81. return self._connection_state
  82. def _update(self):
  83. pass
  84. def _getPrinterByKey(self, key) -> Optional["PrinterOutputModel"]:
  85. for printer in self._printers:
  86. if printer.key == key:
  87. return printer
  88. return None
  89. def requestWrite(self, nodes, file_name = None, filter_by_machine = False, file_handler = None, **kwargs):
  90. raise NotImplementedError("requestWrite needs to be implemented")
  91. @pyqtProperty(QObject, notify = printersChanged)
  92. def activePrinter(self) -> Optional["PrinterOutputModel"]:
  93. if len(self._printers):
  94. return self._printers[0]
  95. return None
  96. @pyqtProperty("QVariantList", notify = printersChanged)
  97. def printers(self):
  98. return self._printers
  99. @pyqtProperty(QObject, constant=True)
  100. def monitorItem(self):
  101. # Note that we specifically only check if the monitor component is created.
  102. # It could be that it failed to actually create the qml item! If we check if the item was created, it will try to
  103. # create the item (and fail) every time.
  104. if not self._monitor_component:
  105. self._createMonitorViewFromQML()
  106. return self._monitor_item
  107. @pyqtProperty(QObject, constant=True)
  108. def controlItem(self):
  109. if not self._control_component:
  110. self._createControlViewFromQML()
  111. return self._control_item
  112. def _createControlViewFromQML(self):
  113. if not self._control_view_qml_path:
  114. return
  115. if self._control_item is None:
  116. self._control_item = Application.getInstance().createQmlComponent(self._control_view_qml_path, {"OutputDevice": self})
  117. def _createMonitorViewFromQML(self):
  118. if not self._monitor_view_qml_path:
  119. return
  120. if self._monitor_item is None:
  121. self._monitor_item = Application.getInstance().createQmlComponent(self._monitor_view_qml_path, {"OutputDevice": self})
  122. ## Attempt to establish connection
  123. def connect(self):
  124. self.setConnectionState(ConnectionState.connecting)
  125. self._update_timer.start()
  126. ## Attempt to close the connection
  127. def close(self):
  128. self._update_timer.stop()
  129. self.setConnectionState(ConnectionState.closed)
  130. ## Ensure that close gets called when object is destroyed
  131. def __del__(self):
  132. self.close()
  133. @pyqtProperty(bool, notify=acceptsCommandsChanged)
  134. def acceptsCommands(self):
  135. return self._accepts_commands
  136. ## Set a flag to signal the UI that the printer is not (yet) ready to receive commands
  137. def _setAcceptsCommands(self, accepts_commands):
  138. if self._accepts_commands != accepts_commands:
  139. self._accepts_commands = accepts_commands
  140. self.acceptsCommandsChanged.emit()
  141. # Returns the unique configurations of the printers within this output device
  142. @pyqtProperty("QVariantList", notify = uniqueConfigurationsChanged)
  143. def uniqueConfigurations(self):
  144. return self._unique_configurations
  145. def _updateUniqueConfigurations(self):
  146. self._unique_configurations = list(set([printer.printerConfiguration for printer in self._printers if printer.printerConfiguration is not None]))
  147. self._unique_configurations.sort(key = lambda k: k.printerType)
  148. self.uniqueConfigurationsChanged.emit()
  149. def _onPrintersChanged(self):
  150. for printer in self._printers:
  151. printer.configurationChanged.connect(self._updateUniqueConfigurations)
  152. # At this point there may be non-updated configurations
  153. self._updateUniqueConfigurations()
  154. ## The current processing state of the backend.
  155. class ConnectionState(IntEnum):
  156. closed = 0
  157. connecting = 1
  158. connected = 2
  159. busy = 3
  160. error = 4