diff options
author | Laszlo Agocs <laszlo.p.agocs@nokia.com> | 2011-07-22 15:11:44 +0300 |
---|---|---|
committer | Laszlo Agocs <laszlo.p.agocs@nokia.com> | 2011-07-25 13:33:55 +0300 |
commit | 76b9f821d517dbd4a784a5b35de30b8d35c316c0 (patch) | |
tree | a6fa7ed952462457b99c27ad688c016fbb89ec25 /examples | |
parent | 42ae9a55f099765345d280f35405fd2c36123662 (diff) |
Add drag'n'drop and basic cursor support.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/qwidget-compositor/main.cpp | 65 |
1 files changed, 54 insertions, 11 deletions
diff --git a/examples/qwidget-compositor/main.cpp b/examples/qwidget-compositor/main.cpp index 9cd500676..635db2a80 100644 --- a/examples/qwidget-compositor/main.cpp +++ b/examples/qwidget-compositor/main.cpp @@ -81,7 +81,7 @@ class QWidgetCompositor : public QWidget, public WaylandCompositor { Q_OBJECT public: - QWidgetCompositor() : WaylandCompositor(this), m_dragSurface(0) { + QWidgetCompositor() : WaylandCompositor(this), m_moveSurface(0), m_dragSourceSurface(0) { setMouseTracking(true); setRetainedSelectionEnabled(true); m_background = QImage(QLatin1String("background.jpg")); @@ -160,6 +160,9 @@ protected: } } + if (!m_cursor.isNull()) + p.drawImage(m_cursorPos - m_cursorHotspot, m_cursor); + frameFinished(); #ifdef QT_COMPOSITOR_WAYLAND_GL @@ -185,12 +188,15 @@ protected: } void mousePressEvent(QMouseEvent *e) { + m_cursorPos = e->pos(); + if (!m_cursor.isNull()) + update(); QPoint local; if (WaylandSurface *surface = surfaceAt(e->pos(), &local)) { raise(surface); if (e->modifiers() & Qt::ControlModifier) { - m_dragSurface = surface; - m_dragOffset = local; + m_moveSurface = surface; + m_moveOffset = local; } else { surface->sendMousePressEvent(local, e->button()); } @@ -198,10 +204,27 @@ protected: } void mouseMoveEvent(QMouseEvent *e) { - if (m_dragSurface) { - QRect geometry = m_dragSurface->geometry(); - geometry.moveTo(e->pos() - m_dragOffset); - m_dragSurface->setGeometry(geometry); + m_cursorPos = e->pos(); + if (!m_cursor.isNull()) + update(); + if (isDragging()) { + QPoint global = e->pos(); // "global" here means the window of the compositor + QPoint local; + WaylandSurface *surface = surfaceAt(e->pos(), &local); + if (surface) { + if (!m_dragSourceSurface) + m_dragSourceSurface = surface; + if (m_dragSourceSurface == surface) + m_lastDragSourcePos = local; + raise(surface); + } + sendDragMoveEvent(global, local, surface); + return; + } + if (m_moveSurface) { + QRect geometry = m_moveSurface->geometry(); + geometry.moveTo(e->pos() - m_moveOffset); + m_moveSurface->setGeometry(geometry); update(); return; } @@ -211,8 +234,16 @@ protected: } void mouseReleaseEvent(QMouseEvent *e) { - if (m_dragSurface) { - m_dragSurface = 0; + if (isDragging()) { + sendDragEndEvent(); + if (m_dragSourceSurface) { + // Must send a release event to the source too, no matter where the cursor is now. + m_dragSourceSurface->sendMouseReleaseEvent(m_lastDragSourcePos, e->button()); + m_dragSourceSurface = 0; + } + } + if (m_moveSurface) { + m_moveSurface = 0; return; } QPoint local; @@ -245,14 +276,26 @@ protected: return 0; } + void changeCursor(const QImage &image, int hotspotX, int hotspotY) { + m_cursor = image; + m_cursorHotspot = QPoint(hotspotX, hotspotY); + update(); + } + private: QImage m_background; QPixmap m_backgroundScaled; QList<WaylandSurface *> m_surfaces; - WaylandSurface *m_dragSurface; - QPoint m_dragOffset; + WaylandSurface *m_moveSurface; + QPoint m_moveOffset; + WaylandSurface *m_dragSourceSurface; + QPoint m_lastDragSourcePos; + + QImage m_cursor; + QPoint m_cursorPos; + QPoint m_cursorHotspot; friend class TouchObserver; }; |