File: pointergesturestest.cpp

package info (click to toggle)
kwin 4%3A6.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,376 kB
  • sloc: cpp: 240,899; javascript: 2,225; xml: 2,096; ansic: 775; sh: 37; python: 15; makefile: 8
file content (148 lines) | stat: -rw-r--r-- 4,152 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
    SPDX-FileCopyrightText: 2016 Martin Gräßlin <mgraesslin@kde.org>

    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/

#include <QGuiApplication>
#include <QQuickItem>
#include <QQuickView>

#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/pointer.h>
#include <KWayland/Client/pointergestures.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/seat.h>

using namespace KWayland::Client;

class PinchGesture : public QQuickItem
{
    Q_OBJECT
    Q_PROPERTY(qreal scale READ scale NOTIFY scaleChanged)
    Q_PROPERTY(qreal progressScale READ progressScale NOTIFY progressScaleChanged)

public:
    explicit PinchGesture(QQuickItem *parent = nullptr);
    ~PinchGesture() override;

    qreal scale() const
    {
        return m_scale;
    }

    qreal progressScale() const
    {
        return m_progressScale;
    }

protected:
    void componentComplete() override;

Q_SIGNALS:
    void progressScaleChanged();

private:
    void initWayland();
    void setupGesture();

    Pointer *m_pointer = nullptr;
    PointerGestures *m_pointerGestures = nullptr;
    PointerPinchGesture *m_gesture = nullptr;

    qreal m_scale = 1.0;
    qreal m_progressScale = 1.0;
};

PinchGesture::PinchGesture(QQuickItem *parent)
    : QQuickItem(parent)
{
}

PinchGesture::~PinchGesture() = default;

void PinchGesture::componentComplete()
{
    QQuickItem::componentComplete();
    initWayland();
}

void PinchGesture::initWayland()
{
    auto c = ConnectionThread::fromApplication(this);
    Registry *r = new Registry(c);
    r->create(c);

    connect(r, &Registry::interfacesAnnounced, this,
            [this, r] {
                const auto gi = r->interface(Registry::Interface::PointerGesturesUnstableV1);
                if (gi.name == 0) {
                    return;
                }
                m_pointerGestures = r->createPointerGestures(gi.name, gi.version, this);

                // now create seat
                const auto si = r->interface(Registry::Interface::Seat);
                if (si.name == 0) {
                    return;
                }
                auto seat = r->createSeat(si.name, si.version, this);
                connect(seat, &Seat::hasKeyboardChanged, this,
                        [this, seat](bool hasPointer) {
                            if (hasPointer) {
                                m_pointer = seat->createPointer(this);
                                setupGesture();
                            } else {
                                delete m_pointer;
                                delete m_gesture;
                                m_pointer = nullptr;
                                m_gesture = nullptr;
                            }
                        });
            });

    r->setup();
    c->roundtrip();
}

void PinchGesture::setupGesture()
{
    if (m_gesture || !m_pointerGestures || !m_pointer) {
        return;
    }
    m_gesture = m_pointerGestures->createPinchGesture(m_pointer, this);
    connect(m_gesture, &PointerPinchGesture::updated, this,
            [this](const QSizeF &delta, qreal scale) {
                m_progressScale = scale;
                Q_EMIT progressScaleChanged();
            });
    connect(m_gesture, &PointerPinchGesture::ended, this,
            [this] {
                m_scale = m_scale * m_progressScale;
                m_progressScale = 1.0;
                Q_EMIT scaleChanged();
                Q_EMIT progressScaleChanged();
            });
    connect(m_gesture, &PointerPinchGesture::cancelled, this,
            [this] {
                m_progressScale = 1.0;
                Q_EMIT progressScaleChanged();
            });
}

int main(int argc, char *argv[])
{
    qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("wayland"));
    QGuiApplication app(argc, argv);

    qmlRegisterType<PinchGesture>("org.kde.kwin.tests", 1, 0, "PinchGesture");

    QQuickView view;
    view.setSource(QUrl::fromLocalFile(QStringLiteral(DIR) + QStringLiteral("/pointergesturestest.qml")));

    view.show();

    return app.exec();
}

#include "pointergesturestest.moc"