PrinterOutputDevice.py 6.3 KB

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