SubscribedPackagesModel.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt6.QtCore import Qt, pyqtProperty
  4. from UM.PackageManager import PackageManager
  5. from UM.Qt.ListModel import ListModel
  6. from UM.Version import Version
  7. from cura import ApplicationMetadata
  8. from typing import List, Dict, Any
  9. class SubscribedPackagesModel(ListModel):
  10. def __init__(self, parent = None):
  11. super().__init__(parent)
  12. self._items = []
  13. self._metadata = None
  14. self._discrepancies = None
  15. self._sdk_version = ApplicationMetadata.CuraSDKVersion
  16. self.addRoleName(Qt.ItemDataRole.UserRole + 1, "package_id")
  17. self.addRoleName(Qt.ItemDataRole.UserRole + 2, "display_name")
  18. self.addRoleName(Qt.ItemDataRole.UserRole + 3, "icon_url")
  19. self.addRoleName(Qt.ItemDataRole.UserRole + 4, "is_compatible")
  20. self.addRoleName(Qt.ItemDataRole.UserRole + 5, "is_dismissed")
  21. @pyqtProperty(bool, constant=True)
  22. def hasCompatiblePackages(self) -> bool:
  23. for item in self._items:
  24. if item['is_compatible']:
  25. return True
  26. return False
  27. @pyqtProperty(bool, constant=True)
  28. def hasIncompatiblePackages(self) -> bool:
  29. for item in self._items:
  30. if not item['is_compatible']:
  31. return True
  32. return False
  33. def addDiscrepancies(self, discrepancy: List[str]) -> None:
  34. self._discrepancies = discrepancy
  35. def getCompatiblePackages(self) -> List[Dict[str, Any]]:
  36. return [package for package in self._items if package["is_compatible"]]
  37. def getIncompatiblePackages(self) -> List[str]:
  38. return [package["package_id"] for package in self._items if not package["is_compatible"]]
  39. def initialize(self, package_manager: PackageManager, subscribed_packages_payload: List[Dict[str, Any]]) -> None:
  40. self._items.clear()
  41. for item in subscribed_packages_payload:
  42. if item["package_id"] not in self._discrepancies:
  43. continue
  44. package = {
  45. "package_id": item["package_id"],
  46. "display_name": item["display_name"],
  47. "sdk_versions": item["sdk_versions"],
  48. "download_url": item["download_url"],
  49. "md5_hash": item["md5_hash"],
  50. "is_dismissed": False,
  51. }
  52. compatible = any(package_manager.isPackageCompatible(Version(version)) for version in item["sdk_versions"])
  53. package.update({"is_compatible": compatible})
  54. try:
  55. package.update({"icon_url": item["icon_url"]})
  56. except KeyError: # There is no 'icon_url" in the response payload for this package
  57. package.update({"icon_url": ""})
  58. self._items.append(package)
  59. self.setItems(self._items)