DiscoveredCloudPrintersModel.py 2.6 KB

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