diff --git a/cura/PrinterOutputDevice.py b/cura/PrinterOutputDevice.py index f411190fd5..b8a5da6fb9 100644 --- a/cura/PrinterOutputDevice.py +++ b/cura/PrinterOutputDevice.py @@ -3,13 +3,18 @@ from UM.i18n import i18nCatalog from UM.OutputDevice.OutputDevice import OutputDevice -from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QTimer +from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QTimer, pyqtSignal, QUrl +from PyQt5.QtQml import QQmlComponent, QQmlContext from PyQt5.QtWidgets import QMessageBox from enum import IntEnum # For the connection state tracking. from UM.Settings.ContainerRegistry import ContainerRegistry from UM.Logger import Logger from UM.Signal import signalemitter +from UM.PluginRegistry import PluginRegistry +from UM.Application import Application + +import os i18n_catalog = i18nCatalog("cura") @@ -57,6 +62,11 @@ class PrinterOutputDevice(QObject, OutputDevice): self._camera_active = False + self._monitor_view_qml_path = "" + self._monitor_component = None + self._monitor_item = None + self._qml_context = None + def requestWrite(self, nodes, file_name = None, filter_by_machine = False, file_handler = None): raise NotImplementedError("requestWrite needs to be implemented") @@ -111,6 +121,32 @@ class PrinterOutputDevice(QObject, OutputDevice): # Signal to be emitted when some drastic change occurs in the remaining time (not when the time just passes on normally). preheatBedRemainingTimeChanged = pyqtSignal() + @pyqtProperty(QObject, constant=True) + def monitorItem(self): + # Note that we specifically only check if the monitor component is created. + # It could be that it failed to actually create the qml item! If we check if the item was created, it will try to + # create the item (and fail) every time. + if not self._monitor_component: + self._createMonitorViewFromQML() + + return self._monitor_item + + def _createMonitorViewFromQML(self): + path = QUrl.fromLocalFile(os.path.join(PluginRegistry.getInstance().getPluginPath(self.getPluginId()), self._monitor_view_qml_path)) + + # Because of garbage collection we need to keep this referenced by python. + self._monitor_component = QQmlComponent(Application.getInstance()._engine, path) + + # Check if the context was already requested before (Printer output device might have multiple items in the future) + if self._qml_context is None: + self._qml_context = QQmlContext(Application.getInstance()._engine.rootContext()) + self._qml_context.setContextProperty("OutputDevice", self) + + self._monitor_item = self._monitor_component.create(self._qml_context) + if self._monitor_item is None: + Logger.log("e", "QQmlComponent status %s", self._monitor_component.status()) + Logger.log("e", "QQmlComponent error string %s", self._monitor_component.errorString()) + @pyqtProperty(str, notify=printerTypeChanged) def printerType(self): return self._printer_type