UM3PrintJobOutputModel.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import List, Optional
  4. from PyQt6.QtCore import pyqtProperty, pyqtSignal
  5. from PyQt6.QtGui import QImage
  6. from PyQt6.QtNetwork import QNetworkReply, QNetworkRequest
  7. from UM.Logger import Logger
  8. from UM.TaskManagement.HttpRequestManager import HttpRequestManager
  9. from cura.PrinterOutput.Models.PrintJobOutputModel import PrintJobOutputModel
  10. from cura.PrinterOutput.PrinterOutputController import PrinterOutputController
  11. from .ConfigurationChangeModel import ConfigurationChangeModel
  12. class UM3PrintJobOutputModel(PrintJobOutputModel):
  13. configurationChangesChanged = pyqtSignal()
  14. def __init__(self, output_controller: PrinterOutputController, key: str = "", name: str = "", parent=None) -> None:
  15. super().__init__(output_controller, key, name, parent)
  16. self._configuration_changes = [] # type: List[ConfigurationChangeModel]
  17. @pyqtProperty("QVariantList", notify=configurationChangesChanged)
  18. def configurationChanges(self) -> List[ConfigurationChangeModel]:
  19. return self._configuration_changes
  20. def updateConfigurationChanges(self, changes: List[ConfigurationChangeModel]) -> None:
  21. if len(self._configuration_changes) == 0 and len(changes) == 0:
  22. return
  23. self._configuration_changes = changes
  24. self.configurationChangesChanged.emit()
  25. def updatePreviewImageData(self, data: bytes) -> None:
  26. image = QImage()
  27. image.loadFromData(data)
  28. self.updatePreviewImage(image)
  29. def loadPreviewImageFromUrl(self, url: str) -> None:
  30. HttpRequestManager.getInstance().get(url=url, callback=self._onImageLoaded, error_callback=self._onImageLoaded)
  31. def _onImageLoaded(self, reply: QNetworkReply, error: Optional["QNetworkReply.NetworkError"] = None) -> None:
  32. if not HttpRequestManager.replyIndicatesSuccess(reply, error):
  33. Logger.warning("Requesting preview image failed, response code {0} while trying to connect to {1}".format(
  34. reply.attribute(QNetworkRequest.Attribute.HttpStatusCodeAttribute), reply.url()))
  35. return
  36. self.updatePreviewImageData(reply.readAll())