From 0fa6f650f6b3b3f686b692d3ea79db87b8b7956c Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Tue, 12 Oct 2021 16:06:53 +0200 Subject: [PATCH] Expose printer status updates via progress update signal This way we can ask the printer status from QML even if it's updated via a job on a different thread and different class and all that. Contributes to issue CURA-8609. --- cura/PrinterOutput/UploadMaterialsJob.py | 12 ++++++++++-- cura/UltimakerCloud/CloudMaterialSync.py | 21 ++++++++++++++++++--- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cura/PrinterOutput/UploadMaterialsJob.py b/cura/PrinterOutput/UploadMaterialsJob.py index f53476ccb2..a65406aaf1 100644 --- a/cura/PrinterOutput/UploadMaterialsJob.py +++ b/cura/PrinterOutput/UploadMaterialsJob.py @@ -46,8 +46,10 @@ class UploadMaterialsJob(Job): self._archive_remote_id = None # type: Optional[str] # ID that the server gives to this archive. Used to communicate about the archive to the server. self._printer_sync_status = {} self._printer_metadata = {} + self.processProgressChanged.connect(self._onProcessProgressChanged) uploadCompleted = Signal() + processProgressChanged = Signal() uploadProgressChanged = Signal() def run(self): @@ -65,7 +67,7 @@ class UploadMaterialsJob(Job): archive_file.close() self._archive_filename = archive_file.name - self._material_sync.exportAll(QUrl.fromLocalFile(self._archive_filename), notify_progress = self.uploadProgressChanged) + self._material_sync.exportAll(QUrl.fromLocalFile(self._archive_filename), notify_progress = self.processProgressChanged) file_size = os.path.getsize(self._archive_filename) http = HttpRequestManager.getInstance() @@ -143,7 +145,10 @@ class UploadMaterialsJob(Job): else: self._printer_sync_status[printer_id] = "success" - if "uploading" not in self._printer_sync_status.values(): # This is the last response to be processed. + still_uploading = len([val for val in self._printer_sync_status.values() if val == "uploading"]) + self.uploadProgressChanged.emit(0.8 + (len(self._printer_sync_status) - still_uploading) / len(self._printer_sync_status), self.getPrinterSyncStatus()) + + if still_uploading == 0: # This is the last response to be processed. if "failed" in self._printer_sync_status.values(): self.setResult(self.Result.FAILED) self.setError(UploadMaterialsError(catalog.i18nc("@text:error", "Failed to connect to Digital Factory to sync materials with some of the printers."))) @@ -160,6 +165,9 @@ class UploadMaterialsJob(Job): def getPrinterSyncStatus(self) -> Dict[str, str]: return self._printer_sync_status + def _onProcessProgressChanged(self, progress: float) -> None: + self.uploadProgressChanged.emit(progress * 0.8, self.getPrinterSyncStatus()) # The processing is 80% of the progress bar. + class UploadMaterialsError(Exception): """ diff --git a/cura/UltimakerCloud/CloudMaterialSync.py b/cura/UltimakerCloud/CloudMaterialSync.py index d3cc479a0a..99282d7eb1 100644 --- a/cura/UltimakerCloud/CloudMaterialSync.py +++ b/cura/UltimakerCloud/CloudMaterialSync.py @@ -3,7 +3,7 @@ from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QUrl from PyQt5.QtGui import QDesktopServices -from typing import Optional, TYPE_CHECKING +from typing import Dict, Optional, TYPE_CHECKING import zipfile # To export all materials in a .zip archive. import cura.CuraApplication # Imported like this to prevent circular imports. @@ -28,6 +28,7 @@ class CloudMaterialSync(QObject): self._export_upload_status = "idle" self._checkIfNewMaterialsWereInstalled() self._export_progress = 0 + self._printer_status = {} def _checkIfNewMaterialsWereInstalled(self) -> None: """ @@ -151,10 +152,14 @@ class CloudMaterialSync(QObject): self._export_upload_status = "uploading" self.exportUploadStatusChanged.emit() job = UploadMaterialsJob(self) - job.uploadProgressChanged.connect(self.setExportProgress) + job.uploadProgressChanged.connect(self._onUploadProgressChanged) job.uploadCompleted.connect(self.exportUploadCompleted) job.start() + def _onUploadProgressChanged(self, progress: float, printers_status: Dict[str, str]): + self.setExportProgress(progress) + self.setPrinterStatus(printers_status) + def exportUploadCompleted(self, job_result: UploadMaterialsJob.Result, job_error: Optional[Exception]): if job_result == UploadMaterialsJob.Result.FAILED: if isinstance(job_error, UploadMaterialsError): @@ -174,4 +179,14 @@ class CloudMaterialSync(QObject): @pyqtProperty(float, fset = setExportProgress, notify = exportProgressChanged) def exportProgress(self) -> float: - return self._export_progress \ No newline at end of file + return self._export_progress + + printerStatusChanged = pyqtSignal() + + def setPrinterStatus(self, new_status: Dict[str, str]) -> None: + self._printer_status = new_status + self.printerStatusChanged.emit() + + @pyqtProperty("QVariantMap", fset = setPrinterStatus, notify = printerStatusChanged) + def printerStatus(self) -> Dict[str, str]: + return self._printer_status \ No newline at end of file