Skip to content

Commit

Permalink
Knob: create wheel event cursor timer only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ronso0 committed Nov 22, 2022
1 parent 7532a24 commit f359e4a
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/widget/knobeventhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@

#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_wheelCursorTimer.setSingleShot(true);
m_wheelCursorTimer.setInterval(800);
: m_bRightButtonPressed(false),
m_wheelCursorTimer(nullptr) {
QPixmap blankPixmap(32, 32);
blankPixmap.fill(QColor(0, 0, 0, 0));
m_blankCursor = QCursor(blankPixmap);
}

double valueFromMouseEvent(T* pWidget, QMouseEvent* e) {
Expand Down Expand Up @@ -106,6 +110,8 @@ 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);
Expand All @@ -117,8 +123,13 @@ class KnobEventHandler {
pWidget->setControlParameter(newValue);
pWidget->inputActivity();
e->accept();
m_wheelCursorTimer.start();
m_wheelCursorTimer.callOnTimeout(
if (!m_wheelCursorTimer) {
m_wheelCursorTimer = new QTimer();
m_wheelCursorTimer->setSingleShot(true);
m_wheelCursorTimer->setInterval(wheelEventCursorTimeout);
}
m_wheelCursorTimer->start();
m_wheelCursorTimer->callOnTimeout(
[pWidget]() {
if (pWidget) {
pWidget->unsetCursor();
Expand All @@ -127,8 +138,8 @@ class KnobEventHandler {
}

void leaveEvent(T* pWidget, QEvent* e) {
if (m_wheelCursorTimer.isActive()) {
m_wheelCursorTimer.stop();
if (m_wheelCursorTimer && m_wheelCursorTimer->isActive()) {
m_wheelCursorTimer->stop();
pWidget->unsetCursor();
}
e->accept();
Expand All @@ -142,5 +153,5 @@ class KnobEventHandler {
QPoint m_startPos;
QPoint m_prevPos;
QCursor m_blankCursor;
QTimer m_wheelCursorTimer;
QTimer* m_wheelCursorTimer;
};

0 comments on commit f359e4a

Please sign in to comment.