Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Knob: hide cursor on wheel event for .8s #11077

Merged
merged 4 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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