DiscoveredCloudPrintersModel.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from typing import Optional, TYPE_CHECKING, List, Dict
  2. from PyQt6.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. """Model used to inform the application about newly added cloud printers, which are discovered from the user's
  8. account """
  9. DeviceKeyRole = Qt.ItemDataRole.UserRole + 1
  10. DeviceNameRole = Qt.ItemDataRole.UserRole + 2
  11. DeviceTypeRole = Qt.ItemDataRole.UserRole + 3
  12. DeviceFirmwareVersionRole = Qt.ItemDataRole.UserRole + 4
  13. cloudPrintersDetectedChanged = pyqtSignal(bool)
  14. def __init__(self, application: "CuraApplication", parent: Optional["QObject"] = None) -> None:
  15. super().__init__(parent)
  16. self.addRoleName(self.DeviceKeyRole, "key")
  17. self.addRoleName(self.DeviceNameRole, "name")
  18. self.addRoleName(self.DeviceTypeRole, "machine_type")
  19. self.addRoleName(self.DeviceFirmwareVersionRole, "firmware_version")
  20. self._discovered_cloud_printers_list = [] # type: List[Dict[str, str]]
  21. self._application = application # type: CuraApplication
  22. def addDiscoveredCloudPrinters(self, new_devices: List[Dict[str, str]]) -> None:
  23. """Adds all the newly discovered cloud printers into the DiscoveredCloudPrintersModel.
  24. Example new_devices entry:
  25. .. code-block:: python
  26. {
  27. "key": "YjW8pwGYcaUvaa0YgVyWeFkX3z",
  28. "name": "NG 001",
  29. "machine_type": "Ultimaker S5",
  30. "firmware_version": "5.5.12.202001"
  31. }
  32. :param new_devices: List of dictionaries which contain information about added cloud printers.
  33. :return: None
  34. """
  35. self._discovered_cloud_printers_list.extend(new_devices)
  36. self._update()
  37. # Inform whether new cloud printers have been detected. If they have, the welcome wizard can close.
  38. self.cloudPrintersDetectedChanged.emit(len(new_devices) > 0)
  39. @pyqtSlot()
  40. def clear(self) -> None:
  41. """Clears the contents of the DiscoveredCloudPrintersModel.
  42. :return: None
  43. """
  44. self._discovered_cloud_printers_list = []
  45. self._update()
  46. self.cloudPrintersDetectedChanged.emit(False)
  47. def _update(self) -> None:
  48. """Sorts the newly discovered cloud printers by name and then updates the ListModel.
  49. :return: None
  50. """
  51. items = self._discovered_cloud_printers_list[:]
  52. items.sort(key = lambda k: k["name"])
  53. self.setItems(items)