diff options
author | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> | 2023-02-08 13:38:22 +0100 |
---|---|---|
committer | Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> | 2023-03-02 11:44:51 +0000 |
commit | dfe0f9740620ef2267225eeef445bfd7e3cb3348 (patch) | |
tree | d709048fd51050c6942030c5166ea425fbd00134 /examples/wayland/custom-extension/compositor | |
parent | ee92c05f34e96431e22b5735d3b864d08e2e4ad3 (diff) |
Revamp/document custom-extension example
This is a very useful example which was unfortunately
undocumented.
It also needed some clean-up in the code, and as part of
writing the documentation, it became clear that it would be
an easier demonstration to follow if the QML client was
more similar to the C++ client, so that they were simply two
implementations of the same spec.
Fixes: QTBUG-110919
Change-Id: Ia4476cda34056e28722af6639f95d8c87819681a
Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
Diffstat (limited to 'examples/wayland/custom-extension/compositor')
7 files changed, 34 insertions, 16 deletions
diff --git a/examples/wayland/custom-extension/compositor/CMakeLists.txt b/examples/wayland/custom-extension/compositor/CMakeLists.txt index 4a946e6a6..bb70cf106 100644 --- a/examples/wayland/custom-extension/compositor/CMakeLists.txt +++ b/examples/wayland/custom-extension/compositor/CMakeLists.txt @@ -14,6 +14,7 @@ set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/wayland/custom-extension/composit find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml WaylandCompositor) +qt6_policy(SET QTP0001 NEW) qt_add_executable(custom-extension-compositor customextension.cpp customextension.h main.cpp @@ -50,6 +51,11 @@ qt6_add_resources(custom-extension-compositor "compositor" ${compositor_resource_files} ) +qt6_add_qml_module(custom-extension-compositor + URI io.qt.examples.customextension + VERSION 1.0 +) + install(TARGETS custom-extension-compositor RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" diff --git a/examples/wayland/custom-extension/compositor/compositor.pro b/examples/wayland/custom-extension/compositor/compositor.pro index 283ccdf4b..263b9fbdd 100644 --- a/examples/wayland/custom-extension/compositor/compositor.pro +++ b/examples/wayland/custom-extension/compositor/compositor.pro @@ -2,6 +2,10 @@ QT += core gui qml QT += waylandcompositor +CONFIG += qmltypes +QML_IMPORT_NAME = io.qt.examples.customextension +QML_IMPORT_MAJOR_VERSION = 1 + CONFIG += wayland-scanner CONFIG += c++11 SOURCES += \ diff --git a/examples/wayland/custom-extension/compositor/customextension.cpp b/examples/wayland/custom-extension/compositor/customextension.cpp index e6968adc6..0d19a62e2 100644 --- a/examples/wayland/custom-extension/compositor/customextension.cpp +++ b/examples/wayland/custom-extension/compositor/customextension.cpp @@ -8,7 +8,7 @@ #include <QDebug> CustomExtension::CustomExtension(QWaylandCompositor *compositor) - :QWaylandCompositorExtensionTemplate(compositor) + : QWaylandCompositorExtensionTemplate(compositor) { } @@ -19,6 +19,7 @@ void CustomExtension::initialize() init(compositor->display(), 1); } +//! [setFontSize] void CustomExtension::setFontSize(QWaylandSurface *surface, uint pixelSize) { if (surface) { @@ -29,6 +30,7 @@ void CustomExtension::setFontSize(QWaylandSurface *surface, uint pixelSize) } } } +//! [setFontSize] void CustomExtension::showDecorations(QWaylandClient *client, bool shown) { @@ -39,7 +41,6 @@ void CustomExtension::showDecorations(QWaylandClient *client, bool shown) send_set_window_decoration(target->handle, shown); } } - } void CustomExtension::close(QWaylandSurface *surface) @@ -53,6 +54,7 @@ void CustomExtension::close(QWaylandSurface *surface) } } +//! [example_extension_bounce] void CustomExtension::example_extension_bounce(QtWaylandServer::qt_example_extension::Resource *resource, wl_resource *wl_surface, uint32_t duration) { Q_UNUSED(resource); @@ -60,6 +62,7 @@ void CustomExtension::example_extension_bounce(QtWaylandServer::qt_example_exten qDebug() << "server received bounce" << surface << duration; emit bounce(surface, duration); } +//! [example_extension_bounce] void CustomExtension::example_extension_spin(QtWaylandServer::qt_example_extension::Resource *resource, wl_resource *wl_surface, uint32_t duration) { @@ -90,7 +93,6 @@ CustomExtensionObject::CustomExtensionObject(const QString &color, const QString , m_color(color) , m_text(text) { - } void CustomExtensionObject::sendClicked() diff --git a/examples/wayland/custom-extension/compositor/customextension.h b/examples/wayland/custom-extension/compositor/customextension.h index 330199697..e377d5f57 100644 --- a/examples/wayland/custom-extension/compositor/customextension.h +++ b/examples/wayland/custom-extension/compositor/customextension.h @@ -9,14 +9,18 @@ #include <QtWaylandCompositor/QWaylandCompositorExtensionTemplate> #include <QtWaylandCompositor/QWaylandQuickExtension> #include <QtWaylandCompositor/QWaylandCompositor> +#include <QtWaylandCompositor/QWaylandSurface> #include "qwayland-server-custom.h" class CustomExtensionObject; +//! [CustomExtension] class CustomExtension : public QWaylandCompositorExtensionTemplate<CustomExtension> , public QtWaylandServer::qt_example_extension { Q_OBJECT + QML_ELEMENT +//! [CustomExtension] public: CustomExtension(QWaylandCompositor *compositor = nullptr); void initialize() override; @@ -25,7 +29,6 @@ signals: void surfaceAdded(QWaylandSurface *surface); void bounce(QWaylandSurface *surface, uint ms); void spin(QWaylandSurface *surface, uint ms); - void customObjectCreated(CustomExtensionObject *obj); public slots: @@ -33,11 +36,12 @@ public slots: void showDecorations(QWaylandClient *client, bool); void close(QWaylandSurface *surface); +//! [example_extension_bounce] protected: void example_extension_bounce(Resource *resource, wl_resource *surface, uint32_t duration) override; +//! [example_extension_bounce] void example_extension_spin(Resource *resource, wl_resource *surface, uint32_t duration) override; void example_extension_register_surface(Resource *resource, wl_resource *surface) override; - void example_extension_create_local_object(Resource *resource, uint32_t id, const QString &color, const QString &text) override; }; diff --git a/examples/wayland/custom-extension/compositor/main.cpp b/examples/wayland/custom-extension/compositor/main.cpp index a44027229..458f5b9a3 100644 --- a/examples/wayland/custom-extension/compositor/main.cpp +++ b/examples/wayland/custom-extension/compositor/main.cpp @@ -11,15 +11,9 @@ #include "customextension.h" -static void registerTypes() -{ - qmlRegisterType<CustomExtensionQuickExtension>("io.qt.examples.customextension", 1, 0, "CustomExtension"); -} - int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); - registerTypes(); QQmlApplicationEngine appEngine(QUrl("qrc:///qml/main.qml")); return app.exec(); diff --git a/examples/wayland/custom-extension/compositor/qml/CompositorScreen.qml b/examples/wayland/custom-extension/compositor/qml/CompositorScreen.qml index ec70a4373..ea2cb9b16 100644 --- a/examples/wayland/custom-extension/compositor/qml/CompositorScreen.qml +++ b/examples/wayland/custom-extension/compositor/qml/CompositorScreen.qml @@ -9,6 +9,7 @@ WaylandOutput { id: output property alias surfaceArea: background sizeFollowsWindow: true + window: Window { id: screen diff --git a/examples/wayland/custom-extension/compositor/qml/main.qml b/examples/wayland/custom-extension/compositor/qml/main.qml index 7005af841..9907263da 100644 --- a/examples/wayland/custom-extension/compositor/qml/main.qml +++ b/examples/wayland/custom-extension/compositor/qml/main.qml @@ -91,13 +91,14 @@ WaylandCompositor { bounceAnimation.start() } +//! [setFontSize] onFontSizeChanged: { custom.setFontSize(surface, fontSize) } +//! [setFontSize] } } - Component { id: customObjectComponent Rectangle { @@ -107,7 +108,7 @@ WaylandCompositor { width: 100 height: 100 - radius: width/2 + radius: width / 2 x: Math.random() * (defaultOutput.surfaceArea.width - 100) y: Math.random() * (defaultOutput.surfaceArea.height - 100) @@ -138,12 +139,13 @@ WaylandCompositor { var h = defaultOutput.surfaceArea.height / 2 item.x = Math.random() * w item.y = Math.random() * h - var listCopy = itemList; // List properties cannot be modified through Javascript operations + var listCopy = itemList // List properties cannot be modified through Javascript operations listCopy.push(item) itemList = listCopy } } +//! [CustomExtension] CustomExtension { id: custom @@ -151,26 +153,31 @@ WaylandCompositor { var item = itemForSurface(surface) item.isCustom = true } + onBounce: (surface, ms) => { var item = itemForSurface(surface) item.doBounce(ms) } + onSpin: (surface, ms) => { var item = itemForSurface(surface) item.doSpin(ms) } + onCustomObjectCreated: (obj) => { var item = customObjectComponent.createObject(defaultOutput.surfaceArea, - { "color": obj.color, "text": obj.text, "obj": obj } ) + { "color": obj.color, + "text": obj.text, + "obj": obj } ) } } function setDecorations(shown) { var n = itemList.length for (var i = 0; i < n; i++) { - // TODO: we only need to do it once for each client if (itemList[i].isCustom) custom.showDecorations(itemList[i].surface.client, shown) } } +//! [CustomExtension] } |