Marketplace.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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, QObject
  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, QObject):
  15. """
  16. The main managing object for the Marketplace plug-in.
  17. """
  18. def __init__(self, parent: Optional[QObject] = None) -> None:
  19. QObject.__init__(self, parent = parent)
  20. Extension.__init__(self)
  21. self._window: Optional[QObject] = None # If the window has been loaded yet, it'll be cached in here.
  22. self.plugin_registry: Optional[PluginRegistry] = None
  23. qmlRegisterType(RemotePackageList, "Marketplace", 1, 0, "RemotePackageList")
  24. qmlRegisterType(LocalPackageList, "Marketplace", 1, 0, "LocalPackageList")
  25. @pyqtSlot()
  26. def show(self) -> None:
  27. """
  28. Opens the window of the Marketplace.
  29. If the window hadn't been loaded yet into Qt, it will be created lazily.
  30. """
  31. if self._window is None:
  32. self.plugin_registry = PluginRegistry.getInstance()
  33. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  34. if plugin_path is None:
  35. plugin_path = os.path.dirname(__file__)
  36. path = os.path.join(plugin_path, "resources", "qml", "Marketplace.qml")
  37. self._window = CuraApplication.getInstance().createQmlComponent(path, {"plugin_registry": self.plugin_registry})
  38. if self._window is None: # Still None? Failed to load the QML then.
  39. return
  40. self._window.show()
  41. self._window.requestActivate() # Bring window into focus, if it was already open in the background.