MissingPackageList.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, TYPE_CHECKING, Dict, List
  4. from .PackageModel import PackageModel
  5. from .RemotePackageList import RemotePackageList
  6. from PyQt6.QtCore import pyqtSignal, QObject, pyqtProperty, QCoreApplication
  7. from UM.i18n import i18nCatalog
  8. if TYPE_CHECKING:
  9. from PyQt6.QtCore import QObject, pyqtProperty, pyqtSignal
  10. catalog = i18nCatalog("cura")
  11. class MissingPackageList(RemotePackageList):
  12. def __init__(self, packages_metadata: List[Dict[str, str]], parent: Optional["QObject"] = None) -> None:
  13. super().__init__(parent)
  14. self._packages_metadata: List[Dict[str, str]] = packages_metadata
  15. self._search_type = "package_ids"
  16. self._requested_search_string = ",".join(map(lambda package: package["id"], packages_metadata))
  17. def _parseResponse(self, reply: "QNetworkReply") -> None:
  18. super()._parseResponse(reply)
  19. # At the end of the list we want to show some information about packages the user is missing that can't be found
  20. # This will add cards with some information about the missing packages
  21. if not self.hasMore:
  22. self._addPackagesMissingFromRequest()
  23. def _addPackagesMissingFromRequest(self) -> None:
  24. """Create cards for packages the user needs to install that could not be found"""
  25. returned_packages_ids = [item["package"].packageId for item in self._items]
  26. for package_metadata in self._packages_metadata:
  27. if package_metadata["id"] not in returned_packages_ids:
  28. package_type = package_metadata["type"] if "type" in package_metadata else "material"
  29. # When this feature was originally introduced only missing materials were detected. With the inclusion
  30. # of backend plugins this system was extended to also detect missing plugins. With that change the type
  31. # of the package was added to the metadata. Project files before this change do not have this type. So
  32. # if the type is not present we assume it is a material.
  33. package = PackageModel.fromIncompletePackageInformation(package_metadata["display_name"],
  34. package_metadata["package_version"],
  35. package_type)
  36. self.appendItem({"package": package})
  37. self.itemsChanged.emit()