DiscoveredCloudPrintersModel.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from typing import Optional, TYPE_CHECKING, List, Dict
  2. from PyQt5.QtCore import QObject, pyqtSlot, Qt, pyqtSignal, pyqtProperty
  3. from UM.Qt.ListModel import ListModel
  4. if TYPE_CHECKING:
  5. from cura.CuraApplication import CuraApplication
  6. class DiscoveredCloudPrintersModel(ListModel):
  7. DeviceKeyRole = Qt.UserRole + 1
  8. DeviceNameRole = Qt.UserRole + 2
  9. DeviceTypeRole = Qt.UserRole + 3
  10. DeviceFirmwareVersionRole = Qt.UserRole + 4
  11. cloudPrintersDetectedChanged = pyqtSignal(bool)
  12. def __init__(self, application: "CuraApplication", parent: Optional["QObject"] = None) -> None:
  13. super().__init__(parent)
  14. self.addRoleName(self.DeviceKeyRole, "key")
  15. self.addRoleName(self.DeviceNameRole, "name")
  16. self.addRoleName(self.DeviceTypeRole, "machine_type")
  17. self.addRoleName(self.DeviceFirmwareVersionRole, "firmware_version")
  18. self._discovered_ultimaker_cloud_printers_list = [] # type: List[Dict[str, str]]
  19. self._application = application # type: CuraApplication
  20. def addDiscoveredCloudPrinters(self, new_devices: List[Dict[str, str]]) -> None:
  21. self._discovered_ultimaker_cloud_printers_list.extend(new_devices)
  22. self._update()
  23. # Inform whether new cloud printers have been detected. If they have, the welcome wizard can close.
  24. self.cloudPrintersDetectedChanged.emit(len(new_devices) > 0)
  25. @pyqtSlot()
  26. def clear(self) -> None:
  27. self._discovered_ultimaker_cloud_printers_list = []
  28. self._update()
  29. self.cloudPrintersDetectedChanged.emit(False)
  30. def _update(self) -> None:
  31. items = self._discovered_ultimaker_cloud_printers_list[:]
  32. items.sort(key = lambda k: k["name"])
  33. self.setItems(items)