From 4a720cb3e54d5830b8d53db71e3f6d93f9dc59d3 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Sun, 12 Jan 2025 14:03:56 +0100 Subject: [PATCH] Move the fader of the selected channel Move the fader of the selected channel instead of the fader that has focus when the up/plus or down/minus keys are pressed. Doing so also feels more natural because users can already change the selected channel via the left and right keys and now they can immediately adjust the volume of the currently selected channel while doing so. Key events are now handled in `MixerView::keyPressEvent` instead of `Fader::keyPressEvent` and the latter is removed. `MixerChannelView` now has a method called `fader` which provides the associated fader. This is needed so that the event handler of `MixerView` can act upon the currently selected fader. ## Changes in Fader The `Fader` class provides two new public methods. The `adjust` method takes the modifier key(s) and the adjustment direction and then decides internally how the modifier keys are mapped to increment values. This is done to keep the mapping between modifier keys and increment values consistent across different clients, e.g. the key event of the `MixerView` and the wheel event of the `Fader` itself. The direction is provided by the client because the means to determine the direction can differ between clients and cases, e.g. a wheel event determines the direction differently than a key event does. The method `adjustByDecibelDelta` simply adjusts the fader by the given delta amount. It currently is not really used in a public way but it still makes sense to provide this functionality in case a parent class or client wants to manipulate the faders by its very own logic. Because the `Fader` class does not react itself to key press events anymore the call to `setFocusPolicy` is removed again. --- include/Fader.h | 10 +++++++- include/MixerChannelView.h | 2 ++ src/gui/MixerView.cpp | 18 ++++++++++++++ src/gui/widgets/Fader.cpp | 51 +++++++++++--------------------------- 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/include/Fader.h b/include/Fader.h index 0988ded6b..d5ab22836 100644 --- a/include/Fader.h +++ b/include/Fader.h @@ -93,6 +93,15 @@ public: inline bool getRenderUnityLine() const { return m_renderUnityLine; } inline void setRenderUnityLine(bool value = true) { m_renderUnityLine = value; } + enum class AdjustmentDirection + { + Up, + Down + }; + + void adjust(const Qt::KeyboardModifiers & modifiers, AdjustmentDirection direction); + void adjustByDecibelDelta(float value); + void setDisplayConversion(bool b) { m_conversionFactor = b ? 100.0 : 1.0; @@ -115,7 +124,6 @@ private: void mouseMoveEvent(QMouseEvent* ev) override; void mouseReleaseEvent(QMouseEvent* me) override; void wheelEvent(QWheelEvent* ev) override; - void keyPressEvent(QKeyEvent *event) override; void paintEvent(QPaintEvent* ev) override; void paintLevels(QPaintEvent* ev, QPainter& painter, bool linear = false); diff --git a/include/MixerChannelView.h b/include/MixerChannelView.h index 6716aee09..3d5f4ffb6 100644 --- a/include/MixerChannelView.h +++ b/include/MixerChannelView.h @@ -82,6 +82,8 @@ public: QColor strokeInnerInactive() const { return m_strokeInnerInactive; } void setStrokeInnerInactive(const QColor& c) { m_strokeInnerInactive = c; } + Fader* fader() const { return m_fader; } + public slots: void renameChannel(); void resetColor(); diff --git a/src/gui/MixerView.cpp b/src/gui/MixerView.cpp index 0ba893be0..a24f5f74a 100644 --- a/src/gui/MixerView.cpp +++ b/src/gui/MixerView.cpp @@ -481,6 +481,16 @@ void MixerView::renameChannel(int index) void MixerView::keyPressEvent(QKeyEvent * e) { + auto adjustCurrentFader = [this](const Qt::KeyboardModifiers& modifiers, Fader::AdjustmentDirection direction) + { + auto* mixerChannel = currentMixerChannel(); + + if (mixerChannel) + { + mixerChannel->fader()->adjust(modifiers, direction); + } + }; + switch(e->key()) { case Qt::Key_Delete: @@ -508,6 +518,14 @@ void MixerView::keyPressEvent(QKeyEvent * e) setCurrentMixerChannel(m_currentMixerChannel->channelIndex() + 1); } break; + case Qt::Key_Up: + case Qt::Key_Plus: + adjustCurrentFader(e->modifiers(), Fader::AdjustmentDirection::Up); + break; + case Qt::Key_Down: + case Qt::Key_Minus: + adjustCurrentFader(e->modifiers(), Fader::AdjustmentDirection::Down); + break; case Qt::Key_Insert: if (e->modifiers() & Qt::ShiftModifier) { diff --git a/src/gui/widgets/Fader.cpp b/src/gui/widgets/Fader.cpp index 33368484d..88affea17 100644 --- a/src/gui/widgets/Fader.cpp +++ b/src/gui/widgets/Fader.cpp @@ -86,8 +86,6 @@ Fader::Fader(FloatModel* model, const QString& name, QWidget* parent, bool model m_conversionFactor = 100.0; - setFocusPolicy(Qt::StrongFocus); - if (model) { // We currently assume that the model is not changed later on and only connect here once @@ -107,6 +105,19 @@ Fader::Fader(FloatModel* model, const QString& name, QWidget* parent, const QPix m_knob = knob; } +void Fader::adjust(const Qt::KeyboardModifiers & modifiers, AdjustmentDirection direction) +{ + const auto adjustmentDb = determineAdjustmentDelta(modifiers) * (direction == AdjustmentDirection::Down ? -1. : 1.); + adjustByDecibelDelta(adjustmentDb); +} + +void Fader::adjustByDecibelDelta(float value) +{ + adjustModelByDBDelta(value); + + updateTextFloat(); + s_textFloat->setVisibilityTimeOut(1000); +} void Fader::contextMenuEvent(QContextMenuEvent* ev) { @@ -247,45 +258,11 @@ void Fader::wheelEvent (QWheelEvent* ev) const float increment = determineAdjustmentDelta(ev->modifiers()) * direction; - adjustModelByDBDelta(increment); - - updateTextFloat(); - s_textFloat->setVisibilityTimeOut(1000); + adjustByDecibelDelta(increment); ev->accept(); } -void Fader::keyPressEvent(QKeyEvent *event) -{ - bool handled = false; - - const auto key = event->key(); - const auto modifiers = event->modifiers(); - - if (key == Qt::Key_Up || key == Qt::Key_Plus) - { - const float incrementValue = determineAdjustmentDelta(modifiers); - adjustModelByDBDelta(incrementValue); - handled = true; - } - else if (key == Qt::Key_Down || key == Qt::Key_Minus) - { - const float incrementValue = determineAdjustmentDelta(modifiers); - adjustModelByDBDelta(-incrementValue); - handled = true; - } - - if (handled) - { - updateTextFloat(); - s_textFloat->setVisibilityTimeOut(1000); - } - else - { - QWidget::keyPressEvent(event); - } -} - float Fader::determineAdjustmentDelta(const Qt::KeyboardModifiers & modifiers) const { if (modifiers == Qt::ShiftModifier)