Marketplace.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 pyqtProperty, pyqtSignal, 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. from .RestartManager import RestartManager # To register this type with QML.
  13. class Marketplace(Extension, QObject):
  14. """
  15. The main managing object for the Marketplace plug-in.
  16. """
  17. class TabManager(QObject):
  18. def __init__(self) -> None:
  19. super().__init__(parent=CuraApplication.getInstance())
  20. self._tab_shown: int = 0
  21. def getTabShown(self) -> int:
  22. return self._tab_shown
  23. def setTabShown(self, tab_shown: int) -> None:
  24. if tab_shown != self._tab_shown:
  25. self._tab_shown = tab_shown
  26. self.tabShownChanged.emit()
  27. tabShownChanged = pyqtSignal()
  28. tabShown = pyqtProperty(int, fget=getTabShown, fset=setTabShown, notify=tabShownChanged)
  29. def __init__(self, parent: Optional[QObject] = None) -> None:
  30. QObject.__init__(self, parent)
  31. Extension.__init__(self)
  32. self._window: Optional["QObject"] = None # If the window has been loaded yet, it'll be cached in here.
  33. self._plugin_registry: Optional[PluginRegistry] = None
  34. self._tab_manager = Marketplace.TabManager()
  35. self._package_manager = CuraApplication.getInstance().getPackageManager()
  36. self._material_package_list: Optional[RemotePackageList] = None
  37. self._plugin_package_list: Optional[RemotePackageList] = None
  38. # Not entirely the cleanest code, since the localPackage list also checks the server if there are updates
  39. # Since that in turn will trigger notifications to be shown, we do need to construct it here and make sure
  40. # that it checks for updates...
  41. self._local_package_list = LocalPackageList(self)
  42. self._local_package_list.checkForUpdates(self._package_manager.local_packages)
  43. qmlRegisterType(RestartManager, "Marketplace", 1, 0, "RestartManager")
  44. @pyqtProperty(QObject, constant=True)
  45. def MaterialPackageList(self):
  46. if self._material_package_list is None:
  47. self._material_package_list = RemotePackageList()
  48. self._material_package_list.packageTypeFilter = "material"
  49. return self._material_package_list
  50. @pyqtProperty(QObject, constant=True)
  51. def PluginPackageList(self):
  52. if self._plugin_package_list is None:
  53. self._plugin_package_list = RemotePackageList()
  54. self._plugin_package_list.packageTypeFilter = "plugin"
  55. return self._plugin_package_list
  56. @pyqtProperty(QObject, constant=True)
  57. def LocalPackageList(self):
  58. return self._local_package_list
  59. @pyqtSlot()
  60. def show(self) -> None:
  61. """
  62. Opens the window of the Marketplace.
  63. If the window hadn't been loaded yet into Qt, it will be created lazily.
  64. """
  65. if self._window is None:
  66. self._plugin_registry = PluginRegistry.getInstance()
  67. plugin_path = PluginRegistry.getInstance().getPluginPath(self.getPluginId())
  68. if plugin_path is None:
  69. plugin_path = os.path.dirname(__file__)
  70. path = os.path.join(plugin_path, "resources", "qml", "Marketplace.qml")
  71. self._window = CuraApplication.getInstance().createQmlComponent(path, {"tabManager": self._tab_manager, "manager": self})
  72. if self._window is None: # Still None? Failed to load the QML then.
  73. return
  74. self._tab_manager.setTabShown(0)
  75. self._window.show()
  76. self._window.requestActivate() # Bring window into focus, if it was already open in the background.
  77. @pyqtSlot()
  78. def setVisibleTabToMaterials(self) -> None:
  79. """
  80. Set the tab shown to the remote materials one.
  81. Not implemented in a more generic way because it needs the ability to be called with 'callExtensionMethod'.
  82. """
  83. self._tab_manager.setTabShown(1)