From 15a716d107db98519672a1e34efe4bd0ca7fa014 Mon Sep 17 00:00:00 2001 From: Linuxero Date: Sun, 15 Nov 2026 17:06:22 +0100 Subject: [PATCH] QRC and GUI Test #4 --- main.py | 63 +++++++- plugins/example-readonly/plugin.json | 2 +- pyproject.toml | 2 +- qml/Main.qml | 17 +++ qml/PluginCard.qml | 41 ++++++ qml/PluginList.qml | 52 +++++++ rc_resources.py | 196 +++++++++++++++++++++++++ resources.qrc | 7 + src/mcc/core/static_plugin_provider.py | 23 +++ src/mcc/resources_rc.py | 196 +++++++++++++++++++++++++ tests/test_plugin_registry.py | 35 +++++ 11 files changed, 628 insertions(+), 6 deletions(-) create mode 100644 qml/Main.qml create mode 100644 qml/PluginCard.qml create mode 100644 qml/PluginList.qml create mode 100644 rc_resources.py create mode 100644 resources.qrc create mode 100644 src/mcc/core/static_plugin_provider.py create mode 100644 src/mcc/resources_rc.py diff --git a/main.py b/main.py index a2f1c62..6565ba9 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,64 @@ -# This Python file uses the following encoding: utf-8 +"""Application entry point.""" + +from __future__ import annotations + +import logging import sys -from PySide6.QtWidgets import QApplication +from pathlib import Path + +from PySide6.QtCore import QUrl +from PySide6.QtGui import QGuiApplication +from PySide6.QtQml import QQmlApplicationEngine + +import mcc.resources_rc # noqa: F401 — registers the compiled Qt resources +from mcc.core.plugin_registry import PluginRegistry, RegistryEntry +from mcc.core.static_plugin_provider import StaticRegistryProvider + + +def load_plugins() -> list[dict[str, object]]: + """Load explicitly registered plugins and convert them for QML.""" + project_root = Path(__file__).resolve().parent + example_manifest = project_root / "plugins" / "example-readonly" / "plugin.json" + + provider = StaticRegistryProvider( + registered_entries=( + RegistryEntry( + plugin_id="org.mageia.mcc.example", + manifest_path=str(example_manifest), + ), + ) + ) + + result = PluginRegistry(provider).load() + + for error in result.errors: + logging.warning("Could not load plugin %s: %s", error.plugin_id, error.message) + + logging.info("Loaded %d plugin(s).", len(result.plugins)) + + return [ + { + "pluginId": plugin.plugin_id, + "name": plugin.name, + "description": plugin.description, + "category": plugin.category, + "subcategory": plugin.subcategory, + "frontends": list(plugin.frontends), + "capabilities": list(plugin.capabilities), + } + for plugin in result.plugins + ] if __name__ == "__main__": - app = QApplication(sys.argv) - # ... + logging.basicConfig(level=logging.INFO) + + app = QGuiApplication(sys.argv) + engine = QQmlApplicationEngine() + engine.rootContext().setContextProperty("registeredPlugins", load_plugins()) + engine.load(QUrl("qrc:/qml/Main.qml")) + + if not engine.rootObjects(): + sys.exit(1) + sys.exit(app.exec()) diff --git a/plugins/example-readonly/plugin.json b/plugins/example-readonly/plugin.json index 0fe70a6..acec4fb 100644 --- a/plugins/example-readonly/plugin.json +++ b/plugins/example-readonly/plugin.json @@ -1,6 +1,6 @@ { "schema_version": 1, - "id": "org.mageia.mcc.example-readonly", + "id": "org.mageia.mcc.example", "api_version": "1", "name": "Example", "description": "An example read-only control-center plugin.", diff --git a/pyproject.toml b/pyproject.toml index ea3bee3..653ed04 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ requires = ["uv_build>=0.13.8,<0.14"] build-backend = "uv_build" [tool.pyside6-project] -files = ["Doxyfile", "README.md", "docs/CONTRIBUTING.md", "docs/architecture.md", "main.py", "plugins/example-readonly/plugin.json", "src/mcc/__init__.py", "src/mcc/core/__init__.py", "src/mcc/core/plugin_manifest.py", "src/mcc/core/plugin_registry.py", "tests/test_plugin_registry.py"] +files = ["Doxyfile", "README.md", "docs/CONTRIBUTING.md", "docs/architecture.md", "main.py", "plugins/example-readonly/plugin.json", "qml/Main.qml", "qml/PluginCard.qml", "qml/PluginList.qml", "resources.qrc", "src/mcc/__init__.py", "src/mcc/core/__init__.py", "src/mcc/core/plugin_manifest.py", "src/mcc/core/plugin_registry.py", "src/mcc/core/static_plugin_provider.py", "tests/test_plugin_registry.py"] [tool.uv.build-backend] module-name = "mcc" diff --git a/qml/Main.qml b/qml/Main.qml new file mode 100644 index 0000000..e4faf28 --- /dev/null +++ b/qml/Main.qml @@ -0,0 +1,17 @@ +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + visible: true + width: 760 + height: 560 + minimumWidth: 480 + minimumHeight: 360 + title: "Mageia Control Center" + + PluginList { + anchors.fill: parent + anchors.margins: 16 + plugins: registeredPlugins + } +} diff --git a/qml/PluginCard.qml b/qml/PluginCard.qml new file mode 100644 index 0000000..47b4cd7 --- /dev/null +++ b/qml/PluginCard.qml @@ -0,0 +1,41 @@ +import QtQuick +import QtQuick.Controls +import QtQuick.Layouts + +Frame { + id: root + + required property var plugin + + implicitHeight: content.implicitHeight + topPadding + bottomPadding + padding: 14 + + ColumnLayout { + id: content + + width: parent.width + spacing: 6 + + Label { + Layout.fillWidth: true + text: root.plugin.name + font.pixelSize: 18 + font.bold: true + wrapMode: Text.Wrap + } + + Label { + Layout.fillWidth: true + text: root.plugin.description + wrapMode: Text.Wrap + } + + Label { + Layout.fillWidth: true + text: root.plugin.category + " / " + root.plugin.subcategory + color: palette.placeholderText + font.pixelSize: 12 + wrapMode: Text.Wrap + } + } +} diff --git a/qml/PluginList.qml b/qml/PluginList.qml new file mode 100644 index 0000000..4f52387 --- /dev/null +++ b/qml/PluginList.qml @@ -0,0 +1,52 @@ +import QtQuick +import QtQuick.Controls + +Column { + id: root + + property var plugins: [] + + spacing: 12 + + Label { + width: parent.width + text: "Available tools" + font.pixelSize: 24 + font.bold: true + } + + Label { + width: parent.width + text: root.plugins.length === 1 + ? "1 tool" + : root.plugins.length + " tools" + color: palette.placeholderText + } + + ListView { + id: pluginView + + width: parent.width + height: Math.max(0, root.height - y) + clip: true + spacing: 10 + model: root.plugins + boundsBehavior: Flickable.StopAtBounds + + delegate: PluginCard { + required property var modelData + + width: pluginView.width + plugin: modelData + } + + ScrollBar.vertical: ScrollBar {} + } + + Label { + anchors.centerIn: parent + visible: root.plugins.length === 0 + text: "No tools are currently available." + color: palette.placeholderText + } +} diff --git a/rc_resources.py b/rc_resources.py new file mode 100644 index 0000000..c6cdca9 --- /dev/null +++ b/rc_resources.py @@ -0,0 +1,196 @@ +# Resource object code (Python 3) +# Created by: object code +# Created by: The Resource Compiler for Qt version 6.12.0 +# WARNING! All changes made in this file will be lost! + +from PySide6 import QtCore + +qt_resource_data = b"\ +\x00\x00\x013\ +i\ +mport QtQuick\x0aim\ +port QtQuick.Con\ +trols\x0a\x0aApplicati\ +onWindow {\x0a v\ +isible: true\x0a \ + width: 760\x0a \ +height: 560\x0a \ +minimumWidth: 48\ +0\x0a minimumHei\ +ght: 360\x0a tit\ +le: \x22Mageia Cont\ +rol Center\x22\x0a\x0a \ + PluginList {\x0a \ + anchors.fi\ +ll: parent\x0a \ + anchors.margi\ +ns: 16\x0a p\ +lugins: register\ +edPlugins\x0a }\x0a\ +}\x0a\ +\x00\x00\x03d\ +i\ +mport QtQuick\x0aim\ +port QtQuick.Con\ +trols\x0aimport QtQ\ +uick.Layouts\x0a\x0aFr\ +ame {\x0a id: ro\ +ot\x0a\x0a required\ + property var pl\ +ugin\x0a\x0a implic\ +itHeight: conten\ +t.implicitHeight\ + + topPadding + \ +bottomPadding\x0a \ + padding: 14\x0a\x0a \ + ColumnLayout \ +{\x0a id: co\ +ntent\x0a\x0a w\ +idth: parent.wid\ +th\x0a spaci\ +ng: 6\x0a\x0a L\ +abel {\x0a \ + Layout.fillWi\ +dth: true\x0a \ + text: root\ +.plugin.name\x0a \ + font.pi\ +xelSize: 18\x0a \ + font.bol\ +d: true\x0a \ + wrapMode: Te\ +xt.Wrap\x0a \ +}\x0a\x0a Label\ + {\x0a L\ +ayout.fillWidth:\ + true\x0a \ + text: root.plu\ +gin.description\x0a\ + wrap\ +Mode: Text.Wrap\x0a\ + }\x0a\x0a \ + Label {\x0a \ + Layout.fi\ +llWidth: true\x0a \ + text: \ +root.plugin.cate\ +gory + \x22 / \x22 + r\ +oot.plugin.subca\ +tegory\x0a \ + color: palett\ +e.placeholderTex\ +t\x0a fo\ +nt.pixelSize: 12\ +\x0a wra\ +pMode: Text.Wrap\ +\x0a }\x0a }\ +\x0a}\x0a\ +\x00\x00\x04\x1b\ +i\ +mport QtQuick\x0aim\ +port QtQuick.Con\ +trols\x0a\x0aColumn {\x0a\ + id: root\x0a\x0a \ + property var p\ +lugins: []\x0a\x0a \ +spacing: 12\x0a\x0a \ + Label {\x0a \ + width: parent.w\ +idth\x0a tex\ +t: \x22Available to\ +ols\x22\x0a fon\ +t.pixelSize: 24\x0a\ + font.bol\ +d: true\x0a }\x0a\x0a \ + Label {\x0a \ + width: parent\ +.width\x0a t\ +ext: root.plugin\ +s.length === 1\x0a \ + ? \x22\ +1 tool\x22\x0a \ + : root.plu\ +gins.length + \x22 \ +tools\x22\x0a c\ +olor: palette.pl\ +aceholderText\x0a \ + }\x0a\x0a ListVie\ +w {\x0a id: \ +pluginView\x0a\x0a \ + width: paren\ +t.width\x0a \ +height: Math.max\ +(0, root.height \ +- y)\x0a cli\ +p: true\x0a \ +spacing: 10\x0a \ + model: root.\ +plugins\x0a \ +boundsBehavior: \ +Flickable.StopAt\ +Bounds\x0a\x0a \ +delegate: Plugin\ +Card {\x0a \ + required prop\ +erty var modelDa\ +ta\x0a\x0a \ +width: pluginVie\ +w.width\x0a \ + plugin: mode\ +lData\x0a }\x0a\ +\x0a ScrollB\ +ar.vertical: Scr\ +ollBar {}\x0a }\x0a\ +\x0a Label {\x0a \ + anchors.cen\ +terIn: parent\x0a \ + visible: r\ +oot.plugins.leng\ +th === 0\x0a \ + text: \x22No tools\ + are currently a\ +vailable.\x22\x0a \ + color: palett\ +e.placeholderTex\ +t\x0a }\x0a}\x0a\ +" + +qt_resource_name = b"\ +\x00\x03\ +\x00\x00x<\ +\x00q\ +\x00m\x00l\ +\x00\x08\ +\x08\x01^\x5c\ +\x00M\ +\x00a\x00i\x00n\x00.\x00q\x00m\x00l\ +\x00\x0e\ +\x0f\xfbH\xdc\ +\x00P\ +\x00l\x00u\x00g\x00i\x00n\x00C\x00a\x00r\x00d\x00.\x00q\x00m\x00l\ +\x00\x0e\ +\x07\xdbG\x9c\ +\x00P\ +\x00l\x00u\x00g\x00i\x00n\x00L\x00i\x00s\x00t\x00.\x00q\x00m\x00l\ +" + +qt_resource_struct = b"\ +\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\ +\x00\x00\x00\x00\x00\x00\x00\x00\ +\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x02\ +\x00\x00\x00\x00\x00\x00\x00\x00\ +\x00\x00\x00D\x00\x00\x00\x00\x00\x01\x00\x00\x04\x9f\ +\x00\x00\x01\xa1\xdf\xe8\xbe\xe8\ +\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\ +\x00\x00\x01\xa1\xdf\xe8\xbe\xd8\ +\x00\x00\x00\x22\x00\x00\x00\x00\x00\x01\x00\x00\x017\ +\x00\x00\x01\xa1\xdf\xe8\xbe\xe6\ +" + +def qInitResources(): + QtCore.qRegisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data) + +def qCleanupResources(): + QtCore.qUnregisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data) + +qInitResources() diff --git a/resources.qrc b/resources.qrc new file mode 100644 index 0000000..7c4889b --- /dev/null +++ b/resources.qrc @@ -0,0 +1,7 @@ + + + qml/Main.qml + qml/PluginList.qml + qml/PluginCard.qml + + diff --git a/src/mcc/core/static_plugin_provider.py b/src/mcc/core/static_plugin_provider.py new file mode 100644 index 0000000..7c692e9 --- /dev/null +++ b/src/mcc/core/static_plugin_provider.py @@ -0,0 +1,23 @@ +"""Static plugin registry provider.""" + +from __future__ import annotations + +from dataclasses import dataclass + +from mcc.core.plugin_registry import RegistryEntry + + +@dataclass(frozen=True) +class StaticRegistryProvider: + """Provides plugin entries declared explicitly in application code. + + This is the initial metadata source. It does not discover plugins by + scanning directories; a later package-installed provider can implement + the same RegistryProvider interface. + """ + + registered_entries: tuple[RegistryEntry, ...] + + def entries(self) -> list[RegistryEntry]: + """Return the explicitly configured plugin entries.""" + return list(self.registered_entries) diff --git a/src/mcc/resources_rc.py b/src/mcc/resources_rc.py new file mode 100644 index 0000000..c6cdca9 --- /dev/null +++ b/src/mcc/resources_rc.py @@ -0,0 +1,196 @@ +# Resource object code (Python 3) +# Created by: object code +# Created by: The Resource Compiler for Qt version 6.12.0 +# WARNING! All changes made in this file will be lost! + +from PySide6 import QtCore + +qt_resource_data = b"\ +\x00\x00\x013\ +i\ +mport QtQuick\x0aim\ +port QtQuick.Con\ +trols\x0a\x0aApplicati\ +onWindow {\x0a v\ +isible: true\x0a \ + width: 760\x0a \ +height: 560\x0a \ +minimumWidth: 48\ +0\x0a minimumHei\ +ght: 360\x0a tit\ +le: \x22Mageia Cont\ +rol Center\x22\x0a\x0a \ + PluginList {\x0a \ + anchors.fi\ +ll: parent\x0a \ + anchors.margi\ +ns: 16\x0a p\ +lugins: register\ +edPlugins\x0a }\x0a\ +}\x0a\ +\x00\x00\x03d\ +i\ +mport QtQuick\x0aim\ +port QtQuick.Con\ +trols\x0aimport QtQ\ +uick.Layouts\x0a\x0aFr\ +ame {\x0a id: ro\ +ot\x0a\x0a required\ + property var pl\ +ugin\x0a\x0a implic\ +itHeight: conten\ +t.implicitHeight\ + + topPadding + \ +bottomPadding\x0a \ + padding: 14\x0a\x0a \ + ColumnLayout \ +{\x0a id: co\ +ntent\x0a\x0a w\ +idth: parent.wid\ +th\x0a spaci\ +ng: 6\x0a\x0a L\ +abel {\x0a \ + Layout.fillWi\ +dth: true\x0a \ + text: root\ +.plugin.name\x0a \ + font.pi\ +xelSize: 18\x0a \ + font.bol\ +d: true\x0a \ + wrapMode: Te\ +xt.Wrap\x0a \ +}\x0a\x0a Label\ + {\x0a L\ +ayout.fillWidth:\ + true\x0a \ + text: root.plu\ +gin.description\x0a\ + wrap\ +Mode: Text.Wrap\x0a\ + }\x0a\x0a \ + Label {\x0a \ + Layout.fi\ +llWidth: true\x0a \ + text: \ +root.plugin.cate\ +gory + \x22 / \x22 + r\ +oot.plugin.subca\ +tegory\x0a \ + color: palett\ +e.placeholderTex\ +t\x0a fo\ +nt.pixelSize: 12\ +\x0a wra\ +pMode: Text.Wrap\ +\x0a }\x0a }\ +\x0a}\x0a\ +\x00\x00\x04\x1b\ +i\ +mport QtQuick\x0aim\ +port QtQuick.Con\ +trols\x0a\x0aColumn {\x0a\ + id: root\x0a\x0a \ + property var p\ +lugins: []\x0a\x0a \ +spacing: 12\x0a\x0a \ + Label {\x0a \ + width: parent.w\ +idth\x0a tex\ +t: \x22Available to\ +ols\x22\x0a fon\ +t.pixelSize: 24\x0a\ + font.bol\ +d: true\x0a }\x0a\x0a \ + Label {\x0a \ + width: parent\ +.width\x0a t\ +ext: root.plugin\ +s.length === 1\x0a \ + ? \x22\ +1 tool\x22\x0a \ + : root.plu\ +gins.length + \x22 \ +tools\x22\x0a c\ +olor: palette.pl\ +aceholderText\x0a \ + }\x0a\x0a ListVie\ +w {\x0a id: \ +pluginView\x0a\x0a \ + width: paren\ +t.width\x0a \ +height: Math.max\ +(0, root.height \ +- y)\x0a cli\ +p: true\x0a \ +spacing: 10\x0a \ + model: root.\ +plugins\x0a \ +boundsBehavior: \ +Flickable.StopAt\ +Bounds\x0a\x0a \ +delegate: Plugin\ +Card {\x0a \ + required prop\ +erty var modelDa\ +ta\x0a\x0a \ +width: pluginVie\ +w.width\x0a \ + plugin: mode\ +lData\x0a }\x0a\ +\x0a ScrollB\ +ar.vertical: Scr\ +ollBar {}\x0a }\x0a\ +\x0a Label {\x0a \ + anchors.cen\ +terIn: parent\x0a \ + visible: r\ +oot.plugins.leng\ +th === 0\x0a \ + text: \x22No tools\ + are currently a\ +vailable.\x22\x0a \ + color: palett\ +e.placeholderTex\ +t\x0a }\x0a}\x0a\ +" + +qt_resource_name = b"\ +\x00\x03\ +\x00\x00x<\ +\x00q\ +\x00m\x00l\ +\x00\x08\ +\x08\x01^\x5c\ +\x00M\ +\x00a\x00i\x00n\x00.\x00q\x00m\x00l\ +\x00\x0e\ +\x0f\xfbH\xdc\ +\x00P\ +\x00l\x00u\x00g\x00i\x00n\x00C\x00a\x00r\x00d\x00.\x00q\x00m\x00l\ +\x00\x0e\ +\x07\xdbG\x9c\ +\x00P\ +\x00l\x00u\x00g\x00i\x00n\x00L\x00i\x00s\x00t\x00.\x00q\x00m\x00l\ +" + +qt_resource_struct = b"\ +\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\ +\x00\x00\x00\x00\x00\x00\x00\x00\ +\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x02\ +\x00\x00\x00\x00\x00\x00\x00\x00\ +\x00\x00\x00D\x00\x00\x00\x00\x00\x01\x00\x00\x04\x9f\ +\x00\x00\x01\xa1\xdf\xe8\xbe\xe8\ +\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\ +\x00\x00\x01\xa1\xdf\xe8\xbe\xd8\ +\x00\x00\x00\x22\x00\x00\x00\x00\x00\x01\x00\x00\x017\ +\x00\x00\x01\xa1\xdf\xe8\xbe\xe6\ +" + +def qInitResources(): + QtCore.qRegisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data) + +def qCleanupResources(): + QtCore.qUnregisterResourceData(0x03, qt_resource_struct, qt_resource_name, qt_resource_data) + +qInitResources() diff --git a/tests/test_plugin_registry.py b/tests/test_plugin_registry.py index 7a17f30..0f33427 100644 --- a/tests/test_plugin_registry.py +++ b/tests/test_plugin_registry.py @@ -8,6 +8,41 @@ from mcc.core.plugin_registry import ( RegistryEntry, ) +from mcc.core.static_plugin_provider import StaticRegistryProvider + + +def test_static_provider_returns_configured_entries() -> None: + """The static provider returns only the entries configured in code.""" + entries = ( + RegistryEntry( + plugin_id="org.mageia.mcc.example", + manifest_path="/plugins/example-readonly/plugin.json", + ), + ) + provider = StaticRegistryProvider(registered_entries=entries) + + assert provider.entries() == list(entries) + + +def test_registry_loads_plugin_from_static_provider(tmp_path: Path) -> None: + """The registry can load a manifest supplied by the static provider.""" + plugin_id = "org.mageia.mcc.example" + manifest_path = tmp_path / "plugin.json" + write_manifest(manifest_path, plugin_id) + provider = StaticRegistryProvider( + registered_entries=( + RegistryEntry( + plugin_id=plugin_id, + manifest_path=str(manifest_path), + ), + ) + ) + + result = PluginRegistry(provider).load() + + assert [plugin.plugin_id for plugin in result.plugins] == [plugin_id] + assert result.errors == () + class FakeProvider: """Test provider that returns a fixed list of registered entries."""