Modifier keys for mouse wheel adjustments (#6769) (#6770)

* Modifier keys for mouse wheel adjustments (#6769)

Give the users the option to use modifier keys (Shift, Ctrl, Alt) to
switch between different scales of adjustment when changing knob values
using the mouse wheel.

The commit implements the following behaviour:

* Using the mouse wheel without any modifier keys makes coarser
  adjustments than the current default, i.e. the range of the
  parameter can be swept with 100 mouse wheel events instead of 2000.
* Pressing the "Shift" key while using the mouse wheel allows making
  coarser adjustments than the default. The range can be swept with
  10 mouse wheel events.
* Pressing the "Ctrl" key allows making finer adjustments than the
  default. The range is swept with 1000 events.
* Pressing the "Alt" key allows even finer adjustments. The range is
  swept with 2000 events (the current default).

Most of these scales are organized in magnitudes (10, 100, 1000) which
should give a very natural feeling to "zone in" on a value.

* Fix indentation of comments

Fix the indention of comments as Qt Creator seems to be incapable of
copy-pasting code in a sensible way.

* Fix comments

Fix the comments by describing better the ideas instead of referencing values.

* Fix format in src/gui/widgets/Knob.cpp

---------

Co-authored-by: saker <sakertooth@gmail.com>
This commit is contained in:
Michael Gregorius
2023-08-26 04:11:03 +02:00
committed by GitHub
parent a9f0a533a0
commit 7000afb2ea

View File

@@ -689,10 +689,52 @@ void Knob::paintEvent( QPaintEvent * _me )
void Knob::wheelEvent(QWheelEvent * we)
{
we->accept();
const float stepMult = model()->range() / 2000 / model()->step<float>();
const int inc = ((we->angleDelta().y() > 0 ) ? 1 : -1) * ((stepMult < 1 ) ? 1 : stepMult);
model()->incValue( inc );
const int deltaY = we->angleDelta().y();
float direction = deltaY > 0 ? 1 : -1;
auto * m = model();
float const step = m->step<float>();
float const range = m->range();
// This is the default number of steps or mouse wheel events that it takes to sweep
// from the lowest value to the highest value.
// It might be modified if the user presses modifier keys. See below.
float numberOfStepsForFullSweep = 100.;
auto const modKeys = we->modifiers();
if (modKeys == Qt::ShiftModifier)
{
// The shift is intended to go through the values in very coarse steps as in:
// "Shift into overdrive"
numberOfStepsForFullSweep = 10;
}
else if (modKeys == Qt::ControlModifier)
{
// The control key gives more control, i.e. it enables more fine-grained adjustments
numberOfStepsForFullSweep = 1000;
}
else if (modKeys == Qt::AltModifier)
{
// The alt key enables even finer adjustments
numberOfStepsForFullSweep = 2000;
// It seems that on some systems pressing Alt with mess with the directions,
// i.e. scrolling the mouse wheel is interpreted as pressing the mouse wheel
// left and right. Account for this quirk.
if (deltaY == 0)
{
int const deltaX = we->angleDelta().x();
if (deltaX != 0)
{
direction = deltaX > 0 ? 1 : -1;
}
}
}
// Compute the number of steps but make sure that we always do at least one step
const float stepMult = std::max(range / numberOfStepsForFullSweep / step, 1.f);
const int inc = direction * stepMult;
model()->incValue(inc);
s_textFloat->setText( displayValue() );
s_textFloat->moveGlobal( this, QPoint( width() + 2, 0 ) );