diff options
author | Laszlo Agocs <laszlo.p.agocs@nokia.com> | 2011-10-24 11:38:59 +0300 |
---|---|---|
committer | Samuel Rødal <samuel.rodal@nokia.com> | 2011-10-24 11:14:02 +0200 |
commit | d9842e8cbffe9741198f8cce74c89b0692b0e2e4 (patch) | |
tree | 87eb4a4e0257bde682f7466ed2c2c0dee0272e20 /examples | |
parent | b4b085f49c6c7fe5b86ab578ac355cf632f07422 (diff) | |
download | qtwayland-d9842e8cbffe9741198f8cce74c89b0692b0e2e4.tar.gz |
Add basic key, mouse, touch event support to qwindow-compositor.
This brings the capabilities of this compact and lean compositor
example up to the level of the old qwidget-based one.
Change-Id: I58eae567e64e1f06432d23bd7d24579efb36eb84
Reviewed-by: Samuel Rødal <samuel.rodal@nokia.com>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/qwindow-compositor/qwindowcompositor.cpp | 89 | ||||
-rw-r--r-- | examples/qwindow-compositor/qwindowcompositor.h | 3 |
2 files changed, 88 insertions, 4 deletions
diff --git a/examples/qwindow-compositor/qwindowcompositor.cpp b/examples/qwindow-compositor/qwindowcompositor.cpp index fbb25561..d5fcb841 100644 --- a/examples/qwindow-compositor/qwindowcompositor.cpp +++ b/examples/qwindow-compositor/qwindowcompositor.cpp @@ -1,4 +1,7 @@ #include "qwindowcompositor.h" +#include <QMouseEvent> +#include <QKeyEvent> +#include <QTouchEvent> QWindowCompositor::QWindowCompositor(QOpenGLWindow *window) : WaylandCompositor(window, window->context()) @@ -8,13 +11,17 @@ QWindowCompositor::QWindowCompositor(QOpenGLWindow *window) m_renderer = new SurfaceRenderer(m_window->context(), m_window); m_backgroundTexture = m_renderer->textureFromImage(m_backgroundImage); + window->installEventFilter(this); + render(); } void QWindowCompositor::surfaceDestroyed(QObject *object) { WaylandSurface *surface = static_cast<WaylandSurface *>(object); - m_surfaces.removeAll(surface); + m_surfaces.removeOne(surface); + if (inputFocus() == surface || !inputFocus()) // typically reset to 0 already in Compositor::surfaceDestroyed() + setInputFocus(m_surfaces.isEmpty() ? 0 : m_surfaces.last()); render(); } @@ -27,10 +34,11 @@ void QWindowCompositor::surfaceMapped(const QSize &size) uint py = 1 + (qrand() % (m_window->height() - size.height() - 2)); pos = QPoint(px, py); surface->setGeometry(QRect(pos, size)); - m_surfaces.append(surface); } else { surface->setGeometry(QRect(window()->geometry().topLeft(),size)); + m_surfaces.removeOne(surface); } + m_surfaces.append(surface); setInputFocus(surface); render(); } @@ -56,12 +64,17 @@ void QWindowCompositor::surfaceCreated(WaylandSurface *surface) render(); } -WaylandSurface * QWindowCompositor::surfaceAt(const QPoint &point, QPoint *local) +QPointF QWindowCompositor::toSurface(WaylandSurface *surface, const QPointF &pos) const +{ + return pos - surface->geometry().topLeft(); +} + +WaylandSurface *QWindowCompositor::surfaceAt(const QPoint &point, QPoint *local) { for (int i = m_surfaces.size() - 1; i >= 0; --i) { if (m_surfaces.at(i)->geometry().contains(point)) { if (local) - *local = point - m_surfaces.at(i)->geometry().topLeft(); + *local = toSurface(m_surfaces.at(i), point).toPoint(); return m_surfaces.at(i); } } @@ -97,3 +110,71 @@ void QWindowCompositor::render() m_window->context()->doneCurrent(); } +bool QWindowCompositor::eventFilter(QObject *obj, QEvent *event) +{ + if (obj != m_window) + return false; + + switch (event->type()) { + case QEvent::MouseButtonPress: { + QPoint local; + QMouseEvent *me = static_cast<QMouseEvent *>(event); + WaylandSurface *targetSurface = surfaceAt(me->pos(), &local); + if (targetSurface) { + if (inputFocus() != targetSurface) { + setInputFocus(targetSurface); + m_surfaces.removeOne(targetSurface); + m_surfaces.append(targetSurface); + render(); + } + targetSurface->sendMousePressEvent(local, me->button()); + } + break; + } + case QEvent::MouseButtonRelease: { + WaylandSurface *targetSurface = inputFocus(); + if (targetSurface) { + QMouseEvent *me = static_cast<QMouseEvent *>(event); + targetSurface->sendMouseReleaseEvent(toSurface(targetSurface, me->pos()).toPoint(), me->button()); + } + break; + } + case QEvent::KeyPress: { + QKeyEvent *ke = static_cast<QKeyEvent *>(event); + WaylandSurface *targetSurface = inputFocus(); + if (targetSurface) + targetSurface->sendKeyPressEvent(ke->nativeScanCode()); + break; + } + case QEvent::KeyRelease: { + QKeyEvent *ke = static_cast<QKeyEvent *>(event); + WaylandSurface *targetSurface = inputFocus(); + if (targetSurface) + targetSurface->sendKeyReleaseEvent(ke->nativeScanCode()); + break; + } + case QEvent::TouchBegin: + case QEvent::TouchUpdate: + case QEvent::TouchEnd: + { + QSet<WaylandSurface *> targets; + QTouchEvent *te = static_cast<QTouchEvent *>(event); + QList<QTouchEvent::TouchPoint> points = te->touchPoints(); + for (int i = 0; i < points.count(); ++i) { + const QTouchEvent::TouchPoint &tp(points.at(i)); + QPoint local; + WaylandSurface *targetSurface = surfaceAt(tp.pos().toPoint(), &local); + if (targetSurface) { + targetSurface->sendTouchPointEvent(tp.id(), local.x(), local.y(), tp.state()); + targets.insert(targetSurface); + } + } + foreach (WaylandSurface *surface, targets) + surface->sendTouchFrameEvent(); + break; + } + default: + break; + } + return false; +} diff --git a/examples/qwindow-compositor/qwindowcompositor.h b/examples/qwindow-compositor/qwindowcompositor.h index efb448ee..f6c32e2e 100644 --- a/examples/qwindow-compositor/qwindowcompositor.h +++ b/examples/qwindow-compositor/qwindowcompositor.h @@ -26,6 +26,9 @@ protected: void render(); + bool eventFilter(QObject *obj, QEvent *event); + QPointF toSurface(WaylandSurface *surface, const QPointF &pos) const; + private: QOpenGLWindow *m_window; QImage m_backgroundImage; |