From 5955d46eb9b4e6645be6d52f2734c68cd4162a8e Mon Sep 17 00:00:00 2001
From: Gennady Pospelov <g.pospelov@fz-juelich.de>
Date: Thu, 30 Nov 2017 11:40:43 +0100
Subject: [PATCH] Mouse wheel events interception localized in custom editors.

---
 .../PropertyEditor/ComponentFlatView.cpp      | 19 -------------------
 .../Views/PropertyEditor/ComponentFlatView.h  |  4 ----
 .../Views/PropertyEditor/CustomEditors.cpp    |  3 +++
 .../Views/PropertyEditor/CustomEditors.h      | 10 ++++------
 .../PropertyEditor/PropertyEditorFactory.cpp  |  6 ++++++
 .../PropertyEditor/PropertyWidgetItem.cpp     |  4 ++++
 6 files changed, 17 insertions(+), 29 deletions(-)

diff --git a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
index e2ba1d283dd..4c15fcb35c4 100644
--- a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
+++ b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
@@ -21,7 +21,6 @@
 #include "SessionModel.h"
 #include "LayoutUtils.h"
 #include "PropertyWidgetItem.h"
-#include "CustomEventFilters.h"
 #include <QLabel>
 #include <QVBoxLayout>
 #include <QGridLayout>
@@ -35,7 +34,6 @@ ComponentFlatView::ComponentFlatView(QWidget* parent)
     , m_gridLayout(nullptr)
     , m_model(nullptr)
     , m_show_children(true)
-    , m_wheel_event_filter(new WheelEventEater)
 {
     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 
@@ -170,25 +168,8 @@ PropertyWidgetItem* ComponentFlatView::createWidget(const SessionItem* item)
     if (!editor)
         return nullptr;
 
-    install_custom_filters(editor);
-
     auto result = new PropertyWidgetItem(this);
     result->setItemEditor(item, editor);
 
     return result;
 }
-
-void ComponentFlatView::install_custom_filters(QWidget* editor)
-{
-    editor->installEventFilter(m_wheel_event_filter.get());
-    editor->setFocusPolicy(Qt::StrongFocus);
-
-    for(auto w : editor->findChildren<QAbstractSpinBox *>()) {
-        w->installEventFilter(m_wheel_event_filter.get());
-        w->setFocusPolicy(Qt::StrongFocus);
-    }
-
-    for(auto w : editor->findChildren<QComboBox *>())
-        w->installEventFilter(m_wheel_event_filter.get());
-
-}
diff --git a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.h b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.h
index 71ad5451a7e..a04017420ff 100644
--- a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.h
+++ b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.h
@@ -25,7 +25,6 @@ class SessionModel;
 class QGridLayout;
 class QBoxLayout;
 class PropertyWidgetItem;
-class WheelEventEater;
 
 //! Component property widget for SessionItems. On the contrary to ComponentTreeView
 //! properties are presented as widgets in grid layout.
@@ -58,14 +57,11 @@ private:
     void initGridLayout();
     PropertyWidgetItem* createWidget(const SessionItem* item);
 
-    void install_custom_filters(QWidget* editor);
-
     QBoxLayout* m_mainLayout;
     QGridLayout* m_gridLayout;
     QVector<PropertyWidgetItem*> m_widgetItems;
     SessionModel* m_model;
     bool m_show_children;
-    std::unique_ptr<WheelEventEater> m_wheel_event_filter;
     QVector<const SessionItem*> m_topItems;
 };
 
diff --git a/GUI/coregui/Views/PropertyEditor/CustomEditors.cpp b/GUI/coregui/Views/PropertyEditor/CustomEditors.cpp
index 941b2624ab1..b6b120e98a0 100644
--- a/GUI/coregui/Views/PropertyEditor/CustomEditors.cpp
+++ b/GUI/coregui/Views/PropertyEditor/CustomEditors.cpp
@@ -162,6 +162,7 @@ void ColorPropertyEditor::initEditor()
 CustomComboEditor::CustomComboEditor(QWidget* parent)
     : CustomEditor(parent)
     , m_box(new QComboBox)
+    , m_wheel_event_filter(new WheelEventEater(this))
 {
     setAutoFillBackground(true);
     setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
@@ -171,6 +172,8 @@ CustomComboEditor::CustomComboEditor(QWidget* parent)
     layout->setSpacing(0);
     layout->addWidget(m_box);
 
+    m_box->installEventFilter(m_wheel_event_filter);
+
     setLayout(layout);
     setConnected(true);
 }
diff --git a/GUI/coregui/Views/PropertyEditor/CustomEditors.h b/GUI/coregui/Views/PropertyEditor/CustomEditors.h
index 094de57239a..ea9e1e248ef 100644
--- a/GUI/coregui/Views/PropertyEditor/CustomEditors.h
+++ b/GUI/coregui/Views/PropertyEditor/CustomEditors.h
@@ -109,7 +109,8 @@ protected:
     virtual int internIndex();
     void setConnected(bool isConnected);
 
-    QComboBox* m_box;    
+    QComboBox* m_box;
+    class WheelEventEater* m_wheel_event_filter;
 };
 
 //! Editor for GroupProperty variant.
@@ -151,9 +152,6 @@ protected:
 
 //! Editor for ScientificDoubleProperty variant.
 
-class QLineEdit;
-class QDoubleValidator;
-
 class BA_CORE_API_ ScientificDoublePropertyEditor : public CustomEditor
 {
     Q_OBJECT
@@ -167,8 +165,8 @@ protected:
     void initEditor();
 
 private:
-    QLineEdit* m_lineEdit;
-    QDoubleValidator* m_validator;
+    class QLineEdit* m_lineEdit;
+    class QDoubleValidator* m_validator;
 };
 
 //! Editor for boolean.
diff --git a/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp b/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp
index 84ec2de283f..cbc684ac32d 100644
--- a/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp
+++ b/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp
@@ -22,6 +22,7 @@
 #include "CustomEditors.h"
 #include "ComboProperty.h"
 #include "ColorProperty.h"
+#include "CustomEventFilters.h"
 #include <QDoubleSpinBox>
 #include <QSpinBox>
 #include <QLineEdit>
@@ -183,6 +184,9 @@ QWidget* createCustomDoubleEditor(const SessionItem& item)
     auto result = new QDoubleSpinBox;
     result->setKeyboardTracking(false);
 
+    result->setFocusPolicy(Qt::StrongFocus);
+    result->installEventFilter(new WheelEventEater(result));
+
     result->setMaximum(std::numeric_limits<double>::max());
     result->setMinimum(std::numeric_limits<double>::lowest());
 
@@ -202,6 +206,8 @@ QWidget* createCustomDoubleEditor(const SessionItem& item)
 QWidget* createCustomIntEditor(const SessionItem& item)
 {
     auto result = new QSpinBox;
+    result->setFocusPolicy(Qt::StrongFocus);
+    result->installEventFilter(new WheelEventEater(result));
 
     result->setMaximum(std::numeric_limits<int>::max());
     result->setKeyboardTracking(false);
diff --git a/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp b/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
index 9122b087caf..d2f7c65837c 100644
--- a/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
+++ b/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
@@ -102,6 +102,10 @@ void PropertyWidgetItem::connectEditor(QWidget* editor)
         connect(combo, &ComboPropertyEditor::currentIndexChanged,
                 [=] { m_delegate->commitData(combo); });
 
+        // TODO after merging GroupProperty and ComboProperty
+        // 1) cast to CustomEditor
+        // 2) switch to CustomEditor::dataChanged()
+
     } else if (auto spinbox = dynamic_cast<QSpinBox*>(editor)) {
         // To provide update of the model on valueChanged() and not only on editingFinished()
         connect(spinbox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
-- 
GitLab