Marketplace.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os.path
  4. from PyQt5.QtCore import pyqtSlot
  5. from PyQt5.QtQml import qmlRegisterType
  6. from typing import Optional, TYPE_CHECKING
  7. from cura.CuraApplication import CuraApplication # Creating QML objects and managing packages.
  8. from UM.Extension import Extension # We are implementing the main object of an extension here.
  9. from UM.PluginRegistry import PluginRegistry # To find out where we are stored (the proper way).
  10. from .RemotePackageList import RemotePackageList # To register this type with QML.
  11. from .LocalPackageList import LocalPackageList # To register this type with QML.
  12. if TYPE_CHECKING:
  13. from PyQt5.QtCore import QObject
  14. class Marketplace(Extension):
  15. """
  16. The main managing object for the Marketplace plug-in.
  17. """
  18. def __init__(self) -> None:
  19. super().__init__()
  20. self._window: Optional["QObject"] = None # If the window has been loaded yet, it'll be cached in here.
  21. self.plugin_registry: Optional[PluginRegistry] = None
  22. qmlRegisterType(RemotePackageList, "Marketplace", 1, 0, "RemotePackageList")
  23. qmlRegisterType(LocalPackageList, "Marketplace", 1, 0, "LocalPackageList")
  24. @pyqtSlot()
  25. def show(self) -> None:
  26. """
  27. Opens the window of the Marketplace.
  28. If the window hadn't been loaded yet into Qt, it will be created lazily.
  29. """
  30. if self._window is None:
  31. self.plugin_registry = PluginRegistry.getInstance()
  32. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  33. if plugin_path is None:
  34. plugin_path = os.path.dirname(__file__)
  35. path = os.path.join(plugin_path, "resources", "qml", "Marketplace.qml")
  36. self._window = CuraApplication.getInstance().createQmlComponent(path, {"plugin_registry": self.plugin_registry})
  37. if self._window is None: # Still None? Failed to load the QML then.
  38. return
  39. self._window.show()
  40. self._window.requestActivate() # Bring window into focus, if it was already open in the background.