Move MouseWheelWidgetAdjustmentGuard

This commit is contained in:
WarmUpTill
2023-03-05 01:42:38 +01:00
committed by WarmUpTill
parent 2cb62eae96
commit be23065988
5 changed files with 47 additions and 31 deletions

View File

@@ -0,0 +1,31 @@
#include "mouse-wheel-guard.hpp"
#include <QEvent>
#include <QScrollBar>
MouseWheelWidgetAdjustmentGuard::MouseWheelWidgetAdjustmentGuard(QObject *parent)
: QObject(parent)
{
}
bool MouseWheelWidgetAdjustmentGuard::eventFilter(QObject *o, QEvent *e)
{
const QWidget *widget = static_cast<QWidget *>(o);
if (e->type() == QEvent::Wheel && widget && !widget->hasFocus()) {
e->ignore();
return true;
}
return QObject::eventFilter(o, e);
}
void PreventMouseWheelAdjustWithoutFocus(QWidget *w)
{
w->setFocusPolicy(Qt::StrongFocus);
// Ignore QScrollBar as there is no danger of accidentally modifying anything
// and long expanded QComboBox would be difficult to interact with otherwise.
if (qobject_cast<QScrollBar *>(w)) {
return;
}
w->installEventFilter(new MouseWheelWidgetAdjustmentGuard(w));
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <QWidget>
class MouseWheelWidgetAdjustmentGuard : public QObject {
public:
explicit MouseWheelWidgetAdjustmentGuard(QObject *parent);
protected:
bool eventFilter(QObject *o, QEvent *e) override;
};
void PreventMouseWheelAdjustWithoutFocus(QWidget *);