From ec7c42a38c57ad2ef4ecb4c3e25953108f71729d Mon Sep 17 00:00:00 2001 From: Mike Walters Date: Fri, 12 Aug 2016 15:42:24 +0100 Subject: [PATCH] Display drag/move mouse pointer when dragging cursors Enables mouse tracking on PlotView to get mouse move events even when not dragging. Passes through Leave events to handle the case where the cursor is near the edge of the widget and the mouse leaves the widget without generating a mouse move event that isn't on the cursor. Passes in mouse cursor shape to Cursor to define whether it should be a horizontal resize, vertical resize or move (resize all). Qt handles the case where the cursor is dragged off screen and does not generate a Leave event while draggin (<3 Qt). --- cursor.cpp | 16 ++++++++++++++-- cursor.h | 18 ++++++++++-------- cursors.cpp | 4 ++-- plotview.cpp | 4 +++- tuner.cpp | 6 +++--- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/cursor.cpp b/cursor.cpp index cf32067..da3bd61 100644 --- a/cursor.cpp +++ b/cursor.cpp @@ -17,10 +17,10 @@ * along with this program. If not, see . */ -#include +#include #include "cursor.h" -Cursor::Cursor(Qt::Orientation orientation, QObject * parent) : QObject::QObject(parent), orientation(orientation) +Cursor::Cursor(Qt::Orientation orientation, Qt::CursorShape mouseCursorShape, QObject * parent) : QObject::QObject(parent), orientation(orientation), cursorShape(mouseCursorShape) { } @@ -39,6 +39,18 @@ bool Cursor::pointOverCursor(QPoint point) bool Cursor::mouseEvent(QEvent::Type type, QMouseEvent event) { + // If the mouse pointer moves over a cursor, display a resize pointer + if (pointOverCursor(event.pos()) && type != QEvent::Leave) { + if (!cursorOverrided) { + cursorOverrided = true; + QApplication::setOverrideCursor(QCursor(cursorShape)); + } + // Restore pointer if it moves off the cursor, or leaves the widget + } else if (cursorOverrided) { + cursorOverrided = false; + QApplication::restoreOverrideCursor(); + } + // Start dragging on left mouse button press, if over a cursor if (type == QEvent::MouseButtonPress) { if (event.button() == Qt::LeftButton) { diff --git a/cursor.h b/cursor.h index 2ecef70..aceb608 100644 --- a/cursor.h +++ b/cursor.h @@ -29,19 +29,21 @@ class Cursor : public QObject Q_OBJECT public: - Cursor(Qt::Orientation orientation, QObject * parent); + Cursor(Qt::Orientation orientation, Qt::CursorShape mouseCursorShape, QObject * parent); int pos(); void setPos(int newPos); - bool mouseEvent(QEvent::Type type, QMouseEvent event); + bool mouseEvent(QEvent::Type type, QMouseEvent event); signals: - void posChanged(); + void posChanged(); private: - int fromPoint(QPoint point); - bool pointOverCursor(QPoint point); + int fromPoint(QPoint point); + bool pointOverCursor(QPoint point); - Qt::Orientation orientation; - bool dragging = false; - int cursorPosition = 0; + Qt::Orientation orientation; + Qt::CursorShape cursorShape; + bool dragging = false; + bool cursorOverrided = false; + int cursorPosition = 0; }; diff --git a/cursors.cpp b/cursors.cpp index 4d838e2..15ed35b 100644 --- a/cursors.cpp +++ b/cursors.cpp @@ -22,8 +22,8 @@ Cursors::Cursors(QObject * parent) : QObject::QObject(parent) { - minCursor = new Cursor(Qt::Vertical, this); - maxCursor = new Cursor(Qt::Vertical, this); + minCursor = new Cursor(Qt::Vertical, Qt::SizeHorCursor, this); + maxCursor = new Cursor(Qt::Vertical, Qt::SizeHorCursor, this); connect(minCursor, &Cursor::posChanged, this, &Cursors::cursorMoved); connect(maxCursor, &Cursor::posChanged, this, &Cursors::cursorMoved); } diff --git a/plotview.cpp b/plotview.cpp index 76cfbea..5888c81 100644 --- a/plotview.cpp +++ b/plotview.cpp @@ -30,6 +30,7 @@ PlotView::PlotView(InputSource *input) : cursors(this), viewRange({0, 0}) { mainSampleSource = input; setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); + setMouseTracking(true); enableCursors(false); enableTimeScale(true); connect(&cursors, SIGNAL(cursorsMoved()), this, SLOT(cursorsMoved())); @@ -131,7 +132,8 @@ bool PlotView::eventFilter(QObject * obj, QEvent *event) // Pass mouse events to individual plot objects if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseMove || - event->type() == QEvent::MouseButtonRelease) { + event->type() == QEvent::MouseButtonRelease || + event->type() == QEvent::Leave) { QMouseEvent *mouseEvent = static_cast(event); diff --git a/tuner.cpp b/tuner.cpp index 258e630..90880e2 100644 --- a/tuner.cpp +++ b/tuner.cpp @@ -22,9 +22,9 @@ Tuner::Tuner(QObject * parent) : QObject::QObject(parent) { - minCursor = new Cursor(Qt::Horizontal, this); - cfCursor = new Cursor(Qt::Horizontal, this); - maxCursor = new Cursor(Qt::Horizontal, this); + minCursor = new Cursor(Qt::Horizontal, Qt::SizeVerCursor, this); + cfCursor = new Cursor(Qt::Horizontal, Qt::SizeAllCursor, this); + maxCursor = new Cursor(Qt::Horizontal, Qt::SizeVerCursor, this); connect(minCursor, &Cursor::posChanged, this, &Tuner::cursorMoved); connect(cfCursor, &Cursor::posChanged, this, &Tuner::cursorMoved); connect(maxCursor, &Cursor::posChanged, this, &Tuner::cursorMoved);