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.
This commit is contained in:
Ghostkeeper 2021-10-12 16:06:53 +02:00
parent 8607eb5cff
commit 0fa6f650f6
No known key found for this signature in database
GPG Key ID: 68F39EA88EEED5FF
2 changed files with 28 additions and 5 deletions

View File

@ -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._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_sync_status = {}
self._printer_metadata = {} self._printer_metadata = {}
self.processProgressChanged.connect(self._onProcessProgressChanged)
uploadCompleted = Signal() uploadCompleted = Signal()
processProgressChanged = Signal()
uploadProgressChanged = Signal() uploadProgressChanged = Signal()
def run(self): def run(self):
@ -65,7 +67,7 @@ class UploadMaterialsJob(Job):
archive_file.close() archive_file.close()
self._archive_filename = archive_file.name 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) file_size = os.path.getsize(self._archive_filename)
http = HttpRequestManager.getInstance() http = HttpRequestManager.getInstance()
@ -143,7 +145,10 @@ class UploadMaterialsJob(Job):
else: else:
self._printer_sync_status[printer_id] = "success" 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(): if "failed" in self._printer_sync_status.values():
self.setResult(self.Result.FAILED) 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."))) 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]: def getPrinterSyncStatus(self) -> Dict[str, str]:
return self._printer_sync_status 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): class UploadMaterialsError(Exception):
""" """

View File

@ -3,7 +3,7 @@
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QUrl from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QUrl
from PyQt5.QtGui import QDesktopServices 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 zipfile # To export all materials in a .zip archive.
import cura.CuraApplication # Imported like this to prevent circular imports. import cura.CuraApplication # Imported like this to prevent circular imports.
@ -28,6 +28,7 @@ class CloudMaterialSync(QObject):
self._export_upload_status = "idle" self._export_upload_status = "idle"
self._checkIfNewMaterialsWereInstalled() self._checkIfNewMaterialsWereInstalled()
self._export_progress = 0 self._export_progress = 0
self._printer_status = {}
def _checkIfNewMaterialsWereInstalled(self) -> None: def _checkIfNewMaterialsWereInstalled(self) -> None:
""" """
@ -151,10 +152,14 @@ class CloudMaterialSync(QObject):
self._export_upload_status = "uploading" self._export_upload_status = "uploading"
self.exportUploadStatusChanged.emit() self.exportUploadStatusChanged.emit()
job = UploadMaterialsJob(self) job = UploadMaterialsJob(self)
job.uploadProgressChanged.connect(self.setExportProgress) job.uploadProgressChanged.connect(self._onUploadProgressChanged)
job.uploadCompleted.connect(self.exportUploadCompleted) job.uploadCompleted.connect(self.exportUploadCompleted)
job.start() 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]): def exportUploadCompleted(self, job_result: UploadMaterialsJob.Result, job_error: Optional[Exception]):
if job_result == UploadMaterialsJob.Result.FAILED: if job_result == UploadMaterialsJob.Result.FAILED:
if isinstance(job_error, UploadMaterialsError): if isinstance(job_error, UploadMaterialsError):
@ -175,3 +180,13 @@ class CloudMaterialSync(QObject):
@pyqtProperty(float, fset = setExportProgress, notify = exportProgressChanged) @pyqtProperty(float, fset = setExportProgress, notify = exportProgressChanged)
def exportProgress(self) -> float: def exportProgress(self) -> float:
return self._export_progress 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