ACF $AcfVersion:0$
TWheelFocusEvent.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtWidgets/QWidget>
4#include <QtCore/QEvent>
5#include <QtWidgets/QAbstractSpinBox>
6#include <QtWidgets/QSlider>
7#include <QtWidgets/QDial>
8#include <QtWidgets/QAbstractItemView>
9#include <QtWidgets/QComboBox>
10
11
12namespace iqtgui
13{
14
15
16// static functions
17
18inline static void InstallWheelEventFilter(QObject* filterPtr, QWidget* parentPtr)
19{
20 if (!parentPtr) {
21 return;
22 }
23
24 auto childrenList = parentPtr->findChildren<QWidget*>();
25 for (auto it : childrenList)
26 {
27 if (auto viewPtr = dynamic_cast<QAbstractItemView*>(it))
28 {
29 viewPtr->viewport()->installEventFilter(filterPtr);
30 continue;
31 }
32
33 if (dynamic_cast<QAbstractSpinBox*>(it) ||
34 dynamic_cast<QSlider*>(it) ||
35 dynamic_cast<QDial*>(it) ||
36 dynamic_cast<QComboBox*>(it))
37 {
38 it->installEventFilter(filterPtr);
39 it->setFocusPolicy(Qt::StrongFocus);
40 continue;
41 }
42 }
43}
44
45
46inline static bool HandleWheelEvent(QObject* object, QEvent* event)
47{
48 // #10124: if the event is a wheel event and the editor does not have focus, ignore it
49 // this is needed to prevent the wheel event from being passed to the parent widget
50 // which may cause unwanted behavior (e.g. scrolling in a table view)
51 // this is needed for all widgets that are not ACF classes (e.g. QSpinBox, QSlider, etc.)
52
53 if (event->type() == QEvent::Wheel) {
54 if (auto editorPtr = dynamic_cast<QWidget*>(object)) {
55 if (!editorPtr->hasFocus()) {
56 return true;
57 }
58 }
59 }
60
61 return false;
62}
63
64
65// use this class to install event filter for wheel events for non-ACF classes
66
67template<class Widget = QWidget>
68class TWheelFocusEventFilter : public Widget
69{
70public:
71 TWheelFocusEventFilter(QWidget* parent = nullptr) : Widget(parent)
72 {}
73
74protected:
75 // reimplemented (QObject)
76 virtual bool eventFilter(QObject* object, QEvent* event) override
77 {
78 // #10124
79 if (HandleWheelEvent(object, event)) {
80 return true;
81 }
82 else
83 // pass the event to the parent class
84 return Widget::eventFilter(object, event);
85 }
86};
87
88
89
90// use this class to install event filter for wheel events for ACF classes
91
92template<class GUIClass>
93class TWheelFocusEvent : public GUIClass
94{
95public:
96 typedef GUIClass BaseClass;
97
98 // reimplemented (iqtgui::CGuiComponentBase)
99 virtual void OnGuiCreated() override
100 {
101 BaseClass::OnGuiCreated();
102
103 // install event filter for wheel events
104 InstallWheelEventFilter(this, GUIClass::GetQtWidget());
105 }
106
107protected:
108 // reimplemented (QObject)
109 virtual bool eventFilter(QObject* object, QEvent* event) override
110 {
111 // #10124
112 if (HandleWheelEvent(object, event)) {
113 return true;
114 }
115 else
116 // pass the event to the parent class
117 return BaseClass::eventFilter(object, event);
118 }
119};
120
121
122} // ns
TWheelFocusEventFilter(QWidget *parent=nullptr)
virtual bool eventFilter(QObject *object, QEvent *event) override
virtual bool eventFilter(QObject *object, QEvent *event) override
virtual void OnGuiCreated() override
Standard GUI specific interfaces and components based on Qt.
static void InstallWheelEventFilter(QObject *filterPtr, QWidget *parentPtr)
static bool HandleWheelEvent(QObject *object, QEvent *event)