PackageModel.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtProperty, QObject
  4. from typing import Any, Dict, Optional
  5. from UM.i18n import i18nCatalog # To translate placeholder names if data is not present.
  6. catalog = i18nCatalog("cura")
  7. class PackageModel(QObject):
  8. """
  9. Represents a package, containing all the relevant information to be displayed about a package.
  10. Effectively this behaves like a glorified named tuple, but as a QObject so that its properties can be obtained from
  11. QML. The model can also be constructed directly from a response received by the API.
  12. """
  13. def __init__(self, package_data: Dict[str, Any], parent: QObject = None) -> None:
  14. """
  15. Constructs a new model for a single package.
  16. :param package_data: The data received from the Marketplace API about the package to create.
  17. :param parent: The parent QML object that controls the lifetime of this model (normally a PackageList).
  18. """
  19. super().__init__(parent)
  20. self._package_id = package_data.get("package_id", "UnknownPackageId")
  21. self._display_name = package_data.get("display_name", catalog.i18nc("@label:property", "Unknown Package"))
  22. self._section_title = package_data.get("section_title", None)
  23. @pyqtProperty(str, constant = True)
  24. def packageId(self) -> str:
  25. return self._package_id
  26. @pyqtProperty(str, constant = True)
  27. def displayName(self) -> str:
  28. return self._display_name
  29. @pyqtProperty(str, constant = True)
  30. def sectionTitle(self) -> Optional[str]:
  31. return self._section_title