PrinterOutputDevice.py 7.3 KB

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