diff options
author | Paul Olav Tvete <paul.tvete@qt.io> | 2017-08-29 14:32:23 +0200 |
---|---|---|
committer | Paul Olav Tvete <paul.tvete@qt.io> | 2017-10-05 13:53:03 +0000 |
commit | 37a8e57d7b8ff9f4360d8a32a09618fbd16534be (patch) | |
tree | e34c196de61202067c5030e7b14e05f0542af834 /examples | |
parent | 00a99e631459eb7e52fde822c24d7b9d603008c4 (diff) | |
download | qtwayland-37a8e57d7b8ff9f4360d8a32a09618fbd16534be.tar.gz |
Add non-global object to custom extension example
Show how to create non-global objects on the client side.
Also fix error in the XML file: move the enum outside the
event.
Change-Id: I85b4cae115a57d60eda4a54d652ea98a8cd39548
Reviewed-by: Johan Helsing <johan.helsing@qt.io>
Diffstat (limited to 'examples')
7 files changed, 254 insertions, 11 deletions
diff --git a/examples/wayland/custom-extension/client-common/customextension.cpp b/examples/wayland/custom-extension/client-common/customextension.cpp index 81f524d9..41b4a30e 100644 --- a/examples/wayland/custom-extension/client-common/customextension.cpp +++ b/examples/wayland/custom-extension/client-common/customextension.cpp @@ -109,6 +109,12 @@ void CustomExtension::registerWindow(QWindow *window) sendWindowRegistration(window); } +CustomExtensionObject *CustomExtension::createCustomObject(const QString &color, const QString &text) +{ + auto *obj = create_local_object(color, text); + return new CustomExtensionObject(obj, text); +} + void CustomExtension::sendBounce(QWindow *window, uint ms) { QtWayland::qt_example_extension::bounce(getWlSurface(window), ms); @@ -152,4 +158,24 @@ void CustomExtension::example_extension_set_window_decoration(uint32_t state) } } +CustomExtensionObject::CustomExtensionObject(struct ::qt_example_local_object *wl_object, const QString &text) + : QWaylandClientExtensionTemplate<CustomExtensionObject>(1) + , QtWayland::qt_example_local_object(wl_object) + , m_text(text) +{ + +} + +void CustomExtensionObject::example_local_object_clicked() +{ + qDebug() << "Object clicked:" << m_text; + emit clicked(); +} + +void CustomExtensionObject::setText(const QString &text) +{ + m_text = text; + set_text(text); +} + QT_END_NAMESPACE diff --git a/examples/wayland/custom-extension/client-common/customextension.h b/examples/wayland/custom-extension/client-common/customextension.h index e76682f6..003a5a00 100644 --- a/examples/wayland/custom-extension/client-common/customextension.h +++ b/examples/wayland/custom-extension/client-common/customextension.h @@ -58,6 +58,8 @@ QT_BEGIN_NAMESPACE +class CustomExtensionObject; + class CustomExtension : public QWaylandClientExtensionTemplate<CustomExtension> , public QtWayland::qt_example_extension { @@ -66,6 +68,8 @@ public: CustomExtension(); Q_INVOKABLE void registerWindow(QWindow *window); + CustomExtensionObject *createCustomObject(const QString &color, const QString &text); + public slots: void sendBounce(QWindow *window, uint ms); void sendSpin(QWindow *window, uint ms); @@ -92,6 +96,36 @@ private: bool m_activated; }; +class CustomExtensionObject : public QWaylandClientExtensionTemplate<CustomExtensionObject> + , public QtWayland::qt_example_local_object +{ + Q_OBJECT + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) +public: + CustomExtensionObject(struct ::qt_example_local_object *wl_object, const QString &text); + + QString text() const + { + return m_text; + } + +protected: + void example_local_object_clicked() override; + +public slots: + void setText(const QString &text); + + +signals: + void textChanged(const QString &text); + void clicked(); + +private: + QString m_text; +}; + + + QT_END_NAMESPACE #endif // CUSTOMEXTENSION_H diff --git a/examples/wayland/custom-extension/compositor/customextension.cpp b/examples/wayland/custom-extension/compositor/customextension.cpp index 8304e1af..d9f637a1 100644 --- a/examples/wayland/custom-extension/compositor/customextension.cpp +++ b/examples/wayland/custom-extension/compositor/customextension.cpp @@ -123,3 +123,39 @@ void CustomExtension::example_extension_register_surface(QtWaylandServer::qt_exa qDebug() << "server received new surface" << surface; emit surfaceAdded(surface); } + + +void CustomExtension::example_extension_create_local_object(Resource *resource, uint32_t id, const QString &color, const QString &text) +{ + auto *obj = new CustomExtensionObject(color, text, resource->client(), id, 1); + qDebug() << "Object created" << text << color; + emit customObjectCreated(obj); +} + +CustomExtensionObject::CustomExtensionObject(const QString &color, const QString &text, wl_client *client, int id, int version) + : QtWaylandServer::qt_example_local_object(client, id, version) + , m_color(color) + , m_text(text) +{ + +} + +void CustomExtensionObject::sendClicked() +{ + send_clicked(); +} + +void CustomExtensionObject::example_local_object_destroy_resource(QtWaylandServer::qt_example_local_object::Resource *resource) +{ + Q_UNUSED(resource); + qDebug() << "Object destroyed" << m_text << m_color; + emit resourceDestroyed(); +} + + +void CustomExtensionObject::example_local_object_set_text(QtWaylandServer::qt_example_local_object::Resource *resource, const QString &text) +{ + Q_UNUSED(resource); + qDebug() << "Client changed text from" << m_text << "to" << text; + setText(text); +} diff --git a/examples/wayland/custom-extension/compositor/customextension.h b/examples/wayland/custom-extension/compositor/customextension.h index b8f05b17..de7df6ac 100644 --- a/examples/wayland/custom-extension/compositor/customextension.h +++ b/examples/wayland/custom-extension/compositor/customextension.h @@ -58,6 +58,8 @@ #include <QtWaylandCompositor/QWaylandCompositor> #include "qwayland-server-custom.h" +class CustomExtensionObject; + class CustomExtension : public QWaylandCompositorExtensionTemplate<CustomExtension> , public QtWaylandServer::qt_example_extension { @@ -71,6 +73,8 @@ signals: void bounce(QWaylandSurface *surface, uint ms); void spin(QWaylandSurface *surface, uint ms); + void customObjectCreated(CustomExtensionObject *obj); + public slots: void setFontSize(QWaylandSurface *surface, uint pixelSize); void showDecorations(QWaylandClient *client, bool); @@ -80,6 +84,63 @@ protected: void example_extension_bounce(Resource *resource, wl_resource *surface, uint32_t duration) override; 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; +}; + + +class CustomExtensionObject : public QWaylandCompositorExtensionTemplate<CustomExtensionObject> + , public QtWaylandServer::qt_example_local_object +{ + Q_OBJECT + Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged) + Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) +public: + CustomExtensionObject(const QString &color, const QString &text, struct ::wl_client *client, int id, int version); + + QString color() const + { + return m_color; + } + + QString text() const + { + return m_text; + } + +public slots: + void setColor(const QString &color) + { + if (m_color == color) + return; + + m_color = color; + emit colorChanged(m_color); + } + + void setText(QString text) + { + if (m_text == text) + return; + + m_text = text; + emit textChanged(m_text); + } + void sendClicked(); + +signals: + void colorChanged(const QString &color); + void resourceDestroyed(); + + void textChanged(QString text); + +protected: + void example_local_object_destroy_resource(Resource *resource) override; + void example_local_object_set_text(Resource *resource, const QString &text) override; + +private: + QString m_color; + QString m_text; }; Q_COMPOSITOR_DECLARE_QUICK_EXTENSION_CLASS(CustomExtension) diff --git a/examples/wayland/custom-extension/compositor/qml/main.qml b/examples/wayland/custom-extension/compositor/qml/main.qml index 6739a5c9..a7d76227 100644 --- a/examples/wayland/custom-extension/compositor/qml/main.qml +++ b/examples/wayland/custom-extension/compositor/qml/main.qml @@ -139,6 +139,40 @@ WaylandCompositor { } } + + Component { + id: customObjectComponent + Rectangle { + id: customItem + property QtObject obj + property alias text: label.text + + width: 100 + height: 100 + radius: width/2 + x: Math.random() * (defaultOutput.surfaceArea.width - 100) + y: Math.random() * (defaultOutput.surfaceArea.height - 100) + + Text { + id: label + anchors.centerIn: parent + text: "?" + } + + MouseArea { + anchors.fill: parent + onClicked: obj.sendClicked() + } + + Connections { + target: obj + onResourceDestroyed: { + customItem.destroy() + } + } + } + } + WlShell { id: defaultShell onWlShellSurfaceCreated: { @@ -168,6 +202,9 @@ WaylandCompositor { var item = itemForSurface(surface) item.doSpin(ms) } + onCustomObjectCreated: { + var item = customObjectComponent.createObject(defaultOutput.surfaceArea, { "color": obj.color, "text": obj.text, "obj": obj } ); + } } function setDecorations(shown) { diff --git a/examples/wayland/custom-extension/cpp-client/main.cpp b/examples/wayland/custom-extension/cpp-client/main.cpp index 54c0bcc0..da6230cc 100644 --- a/examples/wayland/custom-extension/cpp-client/main.cpp +++ b/examples/wayland/custom-extension/cpp-client/main.cpp @@ -70,6 +70,7 @@ public: , rect1(50, 50, 100, 100) , rect2(50, 200, 100, 100) , rect3(50, 350, 100, 100) + , rect4(200,350, 100, 100) { m_extension->registerWindow(this); connect(m_extension, &CustomExtension::fontSize, this, &TestWindow::handleSetFontSize); @@ -101,6 +102,14 @@ public slots: w->show(); } + CustomExtensionObject *newObject() + { + m_objectCount++; + QColor col = QColor::fromHsv(0, 511/(m_objectCount+1), 255); + + return m_extension->createCustomObject(col.name(), QString::number(m_objectCount)); + } + void handleSetFontSize(QWindow *w, uint pixelSize) { if (w == this) { @@ -121,6 +130,9 @@ protected: p.drawText(rect2, Qt::TextWordWrap, "Press here to send bounce request."); p.fillRect(rect3, QColor("#7EA")); p.drawText(rect3, Qt::TextWordWrap, "Create new window."); + p.fillRect(rect4, QColor("#7EABA6")); + p.drawText(rect4, Qt::TextWordWrap, "Create custom object."); + } void mousePressEvent(QMouseEvent *ev) override @@ -131,6 +143,8 @@ protected: doBounce(); else if (rect3.contains(ev->pos())) newWindow(); + else if (rect4.contains(ev->pos())) + newObject(); } private: @@ -138,9 +152,15 @@ private: QRect rect1; QRect rect2; QRect rect3; + QRect rect4; QFont m_font; + static int m_objectCount; + static int m_hue; }; +int TestWindow::m_objectCount = 0; +int TestWindow::m_hue; + int main (int argc, char **argv) { QGuiApplication app(argc, argv); diff --git a/examples/wayland/custom-extension/protocol/custom.xml b/examples/wayland/custom-extension/protocol/custom.xml index 9bbe50d6..2e9a8426 100644 --- a/examples/wayland/custom-extension/protocol/custom.xml +++ b/examples/wayland/custom-extension/protocol/custom.xml @@ -40,9 +40,9 @@ <interface name="qt_example_extension" version="1"> - <description summary="example extension for surfaces"> - This example shows how to add extra functionality to the Wayland - through an extension. + <description summary="Example Wayland extension"> + This example shows how to add extra functionality to Wayland + through an extension. This is the global object of the extension. </description> <request name="register_surface"> @@ -85,23 +85,52 @@ <arg name="pixel_size" type="uint"/> </event> + <enum name="decoration_state"> + <description summary="window decoration state"> + Describes whether window decorations should be shown. + </description> + <entry name="hidden" value="0" summary="Decorations are not shown"/> + <entry name="shown" value="1" summary="Decorations are shown"/> + </enum> + <event name="set_window_decoration"> <description summary="turn window decoration on/off"> Ask the client to turn window decoration on/off on all surfaces. </description> - <enum name="decoration_state"> - <description summary="window decoration state"> - Describes whether window decorations should be shown. - </description> - <entry name="hidden" value="0" summary="Decorations are not shown"/> - <entry name="shown" value="1" summary="Decorations are shown"/> - </enum> - <arg name="state" type="uint"/> </event> + <request name="create_local_object"> + <description summary="Create a sily object"> + Create a new object that should be visualized by the compositor + </description> + <arg name="id" type="new_id" interface="qt_example_local_object"/> + <arg name="color" type = "string"/> + <arg name="text" type = "string"/> + </request> </interface> + <interface name="qt_example_local_object" version="1"> + <description summary="An object created on the client side"> + This object should have a visual representation in the compositor. + </description> + + <request name="set_text"> + <description summary="Change the text"> + Tell the compositor that the object's text is changed + </description> + <arg name="text" type="string"/> + </request> + + <event name="clicked"> + <description summary="The object was clicked"> + Notification to the client that the user clicked the representation of + the object in the compositor. + </description> + + </event> + + </interface> </protocol> |