Permalink
Browse files

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).
  • Loading branch information...
1 parent 172ddee commit ec7c42a38c57ad2ef4ecb4c3e25953108f71729d @miek committed Aug 12, 2016
Showing with 32 additions and 16 deletions.
  1. +14 −2 cursor.cpp
  2. +10 −8 cursor.h
  3. +2 −2 cursors.cpp
  4. +3 −1 plotview.cpp
  5. +3 −3 tuner.cpp
View
@@ -17,10 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <QDebug>
+#include <QApplication>
#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) {
View
@@ -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;
};
View
@@ -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);
}
View
@@ -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<QMouseEvent *>(event);
View
@@ -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);

0 comments on commit ec7c42a

Please sign in to comment.