Skip to content

Commit

Permalink
DlgPrefEQ: prevent scroll events changing unfocused sliders or combob…
Browse files Browse the repository at this point in the history
…oxes
  • Loading branch information
ronso0 committed Aug 9, 2022
1 parent 0b312e5 commit 31d12ad
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/preferences/dialog/dlgprefeq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ DlgPrefEQ::DlgPrefEQ(
m_bEqAutoReset(false),
m_bGainAutoReset(false) {
setupUi(this);
// Set the focus policy for comboboxes and sliders and connect them to the
// custom event filter below so they don't accept focus when we scroll the
// preferences page to avoid undesired value changes.
// Deck EQ & QuickEffect comboxboxes are set up accordingly in slotNumDecksChanged(),
// main EQ sliders in slotMainEqEffectChanged()
SliderHiEQ->setFocusPolicy(Qt::StrongFocus);
SliderHiEQ->installEventFilter(this);
SliderLoEQ->setFocusPolicy(Qt::StrongFocus);
SliderLoEQ->installEventFilter(this);
comboBoxMainEq->setFocusPolicy(Qt::StrongFocus);
comboBoxMainEq->installEventFilter(this);

loadSettings();

Expand Down Expand Up @@ -111,6 +122,21 @@ DlgPrefEQ::~DlgPrefEQ() {
m_deckQuickEffectSelectors.clear();
}

// Catch scroll events over comboboxes and sliders, pass them to the scroll area instead.
bool DlgPrefEQ::eventFilter(QObject* obj, QEvent* e) {
if (e->type() == QEvent::Wheel) {
// Reject scrolling only if widget is unfocused.
// Object to widget cast is needed to check the focus state.
QComboBox* combo = qobject_cast<QComboBox*>(obj);
QSlider* slider = qobject_cast<QSlider*>(obj);
if ((combo && !combo->hasFocus()) || (slider && !slider->hasFocus())) {
QApplication::sendEvent(verticalLayout, e);
return true;
}
}
return QObject::eventFilter(obj, e);
}

void DlgPrefEQ::slotNumDecksChanged(double numDecks) {
int oldDecks = m_deckEqEffectSelectors.size();
while (m_deckEqEffectSelectors.size() < static_cast<int>(numDecks)) {
Expand All @@ -120,6 +146,9 @@ void DlgPrefEQ::slotNumDecksChanged(double numDecks) {

// Create the drop down list for deck EQs
QComboBox* pEqComboBox = new QComboBox(this);
// Ignore scroll events, change value only by dragging (and Up/Down keys?)
pEqComboBox->setFocusPolicy(Qt::StrongFocus);
pEqComboBox->installEventFilter(this);
m_deckEqEffectSelectors.append(pEqComboBox);
connect(pEqComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
Expand All @@ -128,6 +157,8 @@ void DlgPrefEQ::slotNumDecksChanged(double numDecks) {

// Create the drop down list for Quick Effects
QComboBox* pQuickEffectComboBox = new QComboBox(this);
pQuickEffectComboBox->setFocusPolicy(Qt::StrongFocus);
pQuickEffectComboBox->installEventFilter(this);
m_deckQuickEffectSelectors.append(pQuickEffectComboBox);
connect(pQuickEffectComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
Expand Down Expand Up @@ -744,12 +775,21 @@ void DlgPrefEQ::slotMainEqEffectChanged(int effectIndex) {
slider->setMinimumHeight(90);
// Set the index as a property because we need it inside slotUpdateFilter()
slider->setProperty("index", QVariant(i));
// Ignore scroll events, change value only by dragging (and Up/Down keys?)
slider->setFocusPolicy(Qt::StrongFocus);
slider->installEventFilter(this);
slidersGridLayout->addWidget(slider, 1, i + 1, Qt::AlignCenter);
m_mainEQSliders.append(slider);
// catch drag event
connect(slider,
&QSlider::sliderMoved,
this,
&DlgPrefEQ::slotApplyMainEQParameter);
// catch scroll event when slider is focused
connect(slider,
&QSlider::valueChanged,
this,
&DlgPrefEQ::slotApplyMainEQParameter);

QLabel* valueLabel = new QLabel(this);
m_mainEQValues.append(valueLabel);
Expand Down
2 changes: 2 additions & 0 deletions src/preferences/dialog/dlgprefeq.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class DlgPrefEQ : public DlgPreferencePage, public Ui::DlgPrefEQDlg {

void applySelectionsToDecks();

bool eventFilter(QObject* obj, QEvent* e) override;

ControlProxy m_COLoFreq;
ControlProxy m_COHiFreq;
UserSettingsPointer m_pConfig;
Expand Down
2 changes: 1 addition & 1 deletion src/preferences/dialog/dlgprefsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ DlgPrefSound::DlgPrefSound(QWidget* pParent,

// Set the focus policy for QComboBoxes (and wide QDoubleSpinBoxes) and
// connect them to the custom event filter below so they don't accept focus
// when we scroll the preferences page.
// when we scroll the preferences page to avoid undesired value changes.
QObjectList objList = children();
for (int i = 0; i < objList.length(); ++i) {
QComboBox* combo = qobject_cast<QComboBox*>(objList[i]);
Expand Down

0 comments on commit 31d12ad

Please sign in to comment.