Skip to content

Commit

Permalink
Merge pull request #11077 from ronso0/knob-wheel-blank-cursor
Browse files Browse the repository at this point in the history
Knob: hide cursor on wheel event for .8s
  • Loading branch information
daschuer authored Dec 8, 2022
2 parents 8f5ed3f + 9fdf0c1 commit 1edfbde
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
46 changes: 38 additions & 8 deletions src/widget/knobeventhandler.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#pragma once

#include <QMouseEvent>
#include <QWheelEvent>
#include <QColor>
#include <QCursor>
#include <QPoint>
#include <QMouseEvent>
#include <QPixmap>
#include <QPoint>
#include <QTimer>
#include <QWheelEvent>

#include "util/math.h"

// duration (ms) the cursor is blanked after a mouse wheel event
// 800 ms is the duration the parameter value is shown in the parameter name widget
// src/widget/weffectparameternamebase.cpp
constexpr int wheelEventCursorTimeout = 800;

template <class T>
class KnobEventHandler {
public:
KnobEventHandler()
: m_bRightButtonPressed(false) {
QPixmap blankPixmap(32, 32);
blankPixmap.fill(QColor(0, 0, 0, 0));
m_blankCursor = QCursor(blankPixmap);
: m_bRightButtonPressed(false),
m_pWheelCursorTimer(nullptr) {
QPixmap blankPixmap(32, 32);
blankPixmap.fill(Qt::transparent);
m_blankCursor = QCursor(blankPixmap);
}

double valueFromMouseEvent(T* pWidget, QMouseEvent* e) {
Expand Down Expand Up @@ -103,6 +109,9 @@ class KnobEventHandler {
}

void wheelEvent(T* pWidget, QWheelEvent* e) {
// Hide/blank the cursor so the parameter value below the knob is not obscured.
// Restore the cursor when the timer runs out, or when the cursor leaves the widget.
pWidget->setCursor(m_blankCursor);
// For legacy (MIDI) reasons this is tuned to 127.
double wheelDirection = e->angleDelta().y() / (120.0 * 127.0);
double newValue = pWidget->getControlParameter() + wheelDirection;
Expand All @@ -113,6 +122,26 @@ class KnobEventHandler {
pWidget->setControlParameter(newValue);
pWidget->inputActivity();
e->accept();
if (!m_pWheelCursorTimer) {
m_pWheelCursorTimer = new QTimer(pWidget);
m_pWheelCursorTimer->setSingleShot(true);
m_pWheelCursorTimer->setInterval(wheelEventCursorTimeout);
}
m_pWheelCursorTimer->start();
m_pWheelCursorTimer->callOnTimeout(
[pWidget]() {
if (pWidget) {
pWidget->unsetCursor();
}
});
}

void leaveEvent(T* pWidget, QEvent* e) {
if (m_pWheelCursorTimer && m_pWheelCursorTimer->isActive()) {
m_pWheelCursorTimer->stop();
pWidget->unsetCursor();
}
e->accept();
}

private:
Expand All @@ -123,4 +152,5 @@ class KnobEventHandler {
QPoint m_startPos;
QPoint m_prevPos;
QCursor m_blankCursor;
QTimer* m_pWheelCursorTimer;
};
4 changes: 4 additions & 0 deletions src/widget/wknob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ void WKnob::wheelEvent(QWheelEvent* e) {
m_handler.wheelEvent(this, e);
}

void WKnob::leaveEvent(QEvent* e) {
m_handler.leaveEvent(this, e);
}

void WKnob::inputActivity() {
#ifdef __APPLE__
m_renderTimer.activity();
Expand Down
1 change: 1 addition & 0 deletions src/widget/wknob.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class WKnob : public WDisplay {

protected:
void wheelEvent(QWheelEvent *e) override;
void leaveEvent(QEvent* e) override;
void mouseMoveEvent(QMouseEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseDoubleClickEvent(QMouseEvent* e) override;
Expand Down
4 changes: 4 additions & 0 deletions src/widget/wknobcomposed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ void WKnobComposed::wheelEvent(QWheelEvent* e) {
m_handler.wheelEvent(this, e);
}

void WKnobComposed::leaveEvent(QEvent* e) {
m_handler.leaveEvent(this, e);
}

void WKnobComposed::inputActivity() {
#ifdef __APPLE__
m_renderTimer.activity();
Expand Down
1 change: 1 addition & 0 deletions src/widget/wknobcomposed.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class WKnobComposed : public WWidget {

protected:
void wheelEvent(QWheelEvent *e) override;
void leaveEvent(QEvent* e) override;
void mouseMoveEvent(QMouseEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseDoubleClickEvent(QMouseEvent* e) override;
Expand Down

0 comments on commit 1edfbde

Please sign in to comment.