Sort the different sections and packages

The order in which UX defined the different sections are:
- Installed Cura Plugins
- Installed Materials
- Bundled Plugins
- Bundled Materials

All packages need to be order at least by section, but I also took
the liberty to sort the packages in these sections by Alphabet.

Contributes to CURA-8558
This commit is contained in:
j.spijker@ultimaker.com 2021-11-02 16:20:25 +01:00 committed by Jelle Spijker
parent 3f700e5d0c
commit 1b7b0b9caf
No known key found for this signature in database
GPG key ID: 6662DC033BE6B99A

View file

@ -20,6 +20,18 @@ catalog = i18nCatalog("cura")
class LocalPackageList(PackageList): class LocalPackageList(PackageList):
PackageRole = Qt.UserRole + 1 PackageRole = Qt.UserRole + 1
PACKAGE_SECTION_HEADER = {
"installed":
{
"plugin": catalog.i18nc("@label:property", "Installed Cura Plugins"),
"material": catalog.i18nc("@label:property", "Installed Materials")
},
"bundled":
{
"plugin": catalog.i18nc("@label:property", "Bundled Plugins"),
"material": catalog.i18nc("@label:property", "Bundled Materials")
}
}
def __init__(self, parent: "QObject" = None) -> None: def __init__(self, parent: "QObject" = None) -> None:
super().__init__(parent) super().__init__(parent)
@ -38,16 +50,29 @@ class LocalPackageList(PackageList):
self._getLocalPackages() self._getLocalPackages()
def _getLocalPackages(self) -> None: def _getLocalPackages(self) -> None:
plugin_registry = self._application.getPluginRegistry() sorted_sections = {}
package_manager = self._application.getPackageManager() for section in self._getSections():
packages = filter(lambda p: p["section_title"] == section, self._allPackageInfo())
sorted_sections[section] = sorted(packages, key = lambda p: p["display_name"])
for section in sorted_sections.values():
for package_data in section:
package = PackageModel(package_data, parent = self)
self.appendItem({"package": package})
bundled = plugin_registry.getInstalledPlugins()
for b in bundled:
package = PackageModel({"package_id": b, "display_name": b, "section_title": "bundled"}, parent = self)
self.appendItem({"package": package})
packages = package_manager.getInstalledPackageIDs()
for p in packages:
package = PackageModel({"package_id": p, "display_name": p, "section_title": "package"}, parent = self)
self.appendItem({"package": package})
self.setIsLoading(False) self.setIsLoading(False)
self.setHasMore(False) self.setHasMore(False)
def _getSections(self):
for package_type in self.PACKAGE_SECTION_HEADER.values():
for section in package_type.values():
yield section
def _allPackageInfo(self):
manager = self._application.getPackageManager()
for package_id in manager.getAllInstalledPackageIDs():
package_data = manager.getInstalledPackageInfo(package_id)
bundled_or_installed = "bundled" if package_data["is_bundled"] else "installed"
package_type = package_data["package_type"]
package_data["section_title"] = self.PACKAGE_SECTION_HEADER[bundled_or_installed][package_type]
yield package_data