From 0f966115e65762149d61009b4968598a5bb33c20 Mon Sep 17 00:00:00 2001 From: Lipu Fei Date: Thu, 12 Apr 2018 15:19:18 +0200 Subject: [PATCH] WIP: Make CuraPackageManager aggregate PluginRegistry data CURA-4644 --- cura/CuraApplication.py | 11 +--- cura/CuraPackageManager.py | 75 ++++++++++++++++++++++++++-- plugins/Toolbox/src/PackagesModel.py | 6 ++- plugins/Toolbox/src/Toolbox.py | 11 ++-- resources/qml/Cura.qml | 33 ------------ 5 files changed, 83 insertions(+), 53 deletions(-) diff --git a/cura/CuraApplication.py b/cura/CuraApplication.py index 6e19ab3a30..53b6f68f22 100755 --- a/cura/CuraApplication.py +++ b/cura/CuraApplication.py @@ -100,7 +100,6 @@ from PyQt5.QtWidgets import QMessageBox from PyQt5.QtQml import qmlRegisterUncreatableType, qmlRegisterSingletonType, qmlRegisterType import sys -import os.path import numpy import copy import os @@ -248,6 +247,8 @@ class CuraApplication(QtApplication): self.initialize() + self._cura_package_manager.getAllInstalledPackagesInfo() + # FOR TESTING ONLY if kwargs["parsed_command_line"].get("trigger_early_crash", False): assert not "This crash is triggered by the trigger_early_crash command line argument." @@ -395,8 +396,6 @@ class CuraApplication(QtApplication): self.globalContainerStackChanged.connect(self._onGlobalContainerChanged) self._onGlobalContainerChanged() - self._plugin_registry.addSupportedPluginExtension("curaplugin", "Cura Plugin") - self.getCuraSceneController().setActiveBuildPlate(0) # Initialize self._quality_profile_drop_down_menu_model = None @@ -404,7 +403,6 @@ class CuraApplication(QtApplication): CuraApplication.Created = True - def _onEngineCreated(self): self._engine.addImageProvider("camera", CameraImageProvider.CameraImageProvider()) @@ -508,11 +506,6 @@ class CuraApplication(QtApplication): def getStaticVersion(cls): return CuraVersion - ## Handle removing the unneeded plugins - # \sa PluginRegistry - def _removePlugins(self): - self._plugin_registry.removePlugins() - ## Handle loading of all plugin types (and the backend explicitly) # \sa PluginRegistry def _loadPlugins(self): diff --git a/cura/CuraPackageManager.py b/cura/CuraPackageManager.py index d07a3aced0..abfad86b20 100644 --- a/cura/CuraPackageManager.py +++ b/cura/CuraPackageManager.py @@ -85,10 +85,79 @@ class CuraPackageManager(QObject): def getInstalledPackageInfo(self, package_id: str) -> Optional[dict]: if package_id in self._to_remove_package_set: return None - if package_id in self._to_install_package_dict: - return self._to_install_package_dict[package_id]["package_info"] - return self._installed_package_dict.get(package_id) + if package_id in self._to_install_package_dict: + package_info = self._to_install_package_dict[package_id]["package_info"] + package_info["is_bundled"] = False + return package_info + + if package_id in self._installed_package_dict: + package_info = self._installed_package_dict.get(package_id) + package_info["is_bundled"] = False + return package_info + + # TODO: get from plugin registry + #self._plugin_registry. + + return None + + def getAllInstalledPackagesInfo(self) -> dict: + installed_package_id_set = set(self._installed_package_dict.keys()) | set(self._to_install_package_dict.keys()) + installed_package_id_set = installed_package_id_set.difference(self._to_remove_package_set) + + managed_package_id_set = set(installed_package_id_set) | self._to_remove_package_set + + # map of -> -> + installed_packages_dict = {} + for package_id in installed_package_id_set: + if package_id in self._to_install_package_dict: + package_info = self._to_install_package_dict[package_id]["package_info"] + else: + package_info = self._installed_package_dict[package_id] + package_info["is_bundled"] = False + + package_type = package_info["package_type"] + if package_type not in installed_packages_dict: + installed_packages_dict[package_type] = {} + installed_packages_dict[package_type][package_id] = package_info + + # We also need to get information from the plugin registry such as if a plugin is active + package_info["is_active"] = self._plugin_registry.isActivePlugin(package_id) + + # Also get all bundled plugins + all_metadata = self._plugin_registry.getAllMetaData() + for item in all_metadata: + plugin_package_info = self.__convertPluginMetadataToPackageMetadata(item) + # Only gather the bundled plugins here. + package_id = plugin_package_info["package_id"] + if package_id in managed_package_id_set: + continue + + plugin_package_info["is_bundled"] = True + plugin_package_info["is_active"] = self._plugin_registry.isActivePlugin(package_id) + package_type = "plugin" + if package_type not in installed_packages_dict: + installed_packages_dict[package_type] = {} + installed_packages_dict[package_type][package_id] = plugin_package_info + + return installed_packages_dict + + def __convertPluginMetadataToPackageMetadata(self, plugin_metadata: dict) -> dict: + package_metadata = {"package_id": plugin_metadata["id"], + "package_type": "plugin", + "display_name": plugin_metadata["plugin"]["name"], + "description": plugin_metadata["plugin"].get("description"), + "package_version": plugin_metadata["plugin"]["version"], + "cura_version": int(plugin_metadata["plugin"]["api"]), + "website": "", + "author": { + "name": plugin_metadata["plugin"].get("author", ""), + "email": "", + "website": "", + }, + "tags": ["plugin"], + } + return package_metadata # Checks if the given package is installed. def isPackageInstalled(self, package_id: str) -> bool: diff --git a/plugins/Toolbox/src/PackagesModel.py b/plugins/Toolbox/src/PackagesModel.py index d11dc61029..7bf023f707 100644 --- a/plugins/Toolbox/src/PackagesModel.py +++ b/plugins/Toolbox/src/PackagesModel.py @@ -4,10 +4,12 @@ import re from typing import Dict -from PyQt5.QtCore import Qt, pyqtProperty, pyqtSignal +from PyQt5.QtCore import Qt, pyqtProperty +from UM.Application import Application from UM.Qt.ListModel import ListModel + ## Model that holds cura packages. By setting the filter property the instances held by this model can be changed. class PackagesModel(ListModel): IdRole = Qt.UserRole + 1 @@ -40,7 +42,7 @@ class PackagesModel(ListModel): self.addRoleName(PackagesModel.LastUpdatedRole, "last_updated") # List of filters for queries. The result is the union of the each list of results. - self._filter = {} # type: Dict[str,str] + self._filter = {} # type: Dict[str, str] def setMetadata(self, data): self._metadata = data diff --git a/plugins/Toolbox/src/Toolbox.py b/plugins/Toolbox/src/Toolbox.py index b3b46d283e..92bd5eefec 100644 --- a/plugins/Toolbox/src/Toolbox.py +++ b/plugins/Toolbox/src/Toolbox.py @@ -218,13 +218,13 @@ class Toolbox(QObject, Extension): @pyqtSlot(str) def enablePlugin(self, plugin_id): - self._plugin_registry.enablePlugin(plugin_id) + self._plugin_registry.setPluginEnabled(plugin_id, True) self.metadataChanged.emit() Logger.log("i", "%s was set as 'active'.", plugin_id) @pyqtSlot(str) def disablePlugin(self, plugin_id): - self._plugin_registry.disablePlugin(plugin_id) + self._plugin_registry.setPluginEnabled(plugin_id, False) self.metadataChanged.emit() Logger.log("i", "%s was set as 'deactive'.", plugin_id) @@ -371,7 +371,7 @@ class Toolbox(QObject, Extension): print(json_data) # Create packages model with all packages: if not self._models["packages"]: - self._models["packages"] = PackagesModel() + self._models["packages"] = PackagesModel(self) self._metadata["packages"] = json_data["data"] self._models["packages"].setMetadata(self._metadata["packages"]) self.metadataChanged.emit() @@ -388,7 +388,7 @@ class Toolbox(QObject, Extension): self.metadataChanged.emit() if not self._models["materials_showcase"]: - self._models["materials_showcase"] = AuthorsModel() + self._models["materials_showcase"] = AuthorsModel(self) # TODO: Replace this with a proper API call: self._models["materials_showcase"].setMetadata(self._metadata["materials_showcase"]) self.metadataChanged.emit() @@ -400,8 +400,7 @@ class Toolbox(QObject, Extension): Logger.log("w", "Toolbox: Received invalid JSON for package list.") return - - if reply.url() == self._request_urls["plugins_showcase"]: + if reply.url() == self._request_urls["plugins_showcase"]: try: json_data = json.loads(bytes(reply.readAll()).decode("utf-8")) # Create packages model with all packages: diff --git a/resources/qml/Cura.qml b/resources/qml/Cura.qml index e20a29fe16..23feede6a0 100644 --- a/resources/qml/Cura.qml +++ b/resources/qml/Cura.qml @@ -322,31 +322,6 @@ UM.MainWindow { if (drop.urls.length > 0) { - // As the drop area also supports plugins, first check if it's a plugin that was dropped. - if (drop.urls.length == 1) - { - if (PluginRegistry.isPluginFile(drop.urls[0])) - { - // Try to install plugin & close. - var result = PluginRegistry.installPlugin(drop.urls[0]); - pluginInstallDialog.text = result.message; - if (result.status == "ok") - { - pluginInstallDialog.icon = StandardIcon.Information; - } - else if (result.status == "duplicate") - { - pluginInstallDialog.icon = StandardIcon.Warning; - } - else - { - pluginInstallDialog.icon = StandardIcon.Critical; - } - pluginInstallDialog.open(); - return; - } - } - openDialog.handleOpenFileUrls(drop.urls); } } @@ -813,14 +788,6 @@ UM.MainWindow } } - MessageDialog - { - id: pluginInstallDialog - title: catalog.i18nc("@window:title", "Install Plugin"); - standardButtons: StandardButton.Ok - modality: Qt.ApplicationModal - } - MessageDialog { id: infoMultipleFilesWithGcodeDialog title: catalog.i18nc("@title:window", "Open File(s)")