BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
CustomEventFilters.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Common/CustomEventFilters.cpp
6 //! @brief Defines classes releted to event filtering
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include <QApplication>
17 #include <QComboBox>
18 #include <QKeyEvent>
19 #include <QString>
20 #include <utility>
21 
23  : QObject(parent)
24 {
25 }
26 
27 bool SpaceKeyEater::eventFilter(QObject* obj, QEvent* event)
28 {
29  if (event->type() != QEvent::KeyPress)
30  return QObject::eventFilter(obj, event);
31 
32  auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
33  if (keyEvent->key() == Qt::Key_Space)
34  return true; /* Always accept space bar */
35  return QObject::eventFilter(obj, event);
36  // standard event processing
37 }
38 
39 // ----------------------------------------------------------------------------
40 
42  : QObject(parent)
43 {
44 }
45 
46 void WheelEventEater::install(QObject* obj)
47 {
48  auto* p = new WheelEventEater(obj);
49  obj->installEventFilter(p);
50 }
51 
52 bool WheelEventEater::eventFilter(QObject* obj, QEvent* event)
53 {
54  if (auto* spinBox = qobject_cast<QAbstractSpinBox*>(obj)) {
55  if (event->type() == QEvent::Wheel) {
56  if (spinBox->focusPolicy() == Qt::WheelFocus) {
57  event->accept();
58  return false;
59  }
60  event->ignore();
61  return true;
62  }
63  if (event->type() == QEvent::FocusIn)
64  spinBox->setFocusPolicy(Qt::WheelFocus);
65  else if (event->type() == QEvent::FocusOut)
66  spinBox->setFocusPolicy(Qt::StrongFocus);
67  } else if (qobject_cast<QComboBox*>(obj)) {
68  if (event->type() == QEvent::Wheel) {
69  event->ignore();
70  return true;
71  }
72  event->accept();
73  return false;
74  }
75  return QObject::eventFilter(obj, event);
76 }
77 
78 // ----------------------------------------------------------------------------
79 
80 bool DeleteEventFilter::eventFilter(QObject* dist, QEvent* event)
81 {
82  Q_UNUSED(dist);
83  if (event->type() == QEvent::KeyPress) {
84  auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
85  if (keyEvent->key() == Qt::Key_Delete)
86  emit removeItem();
87  }
88  return QObject::eventFilter(dist, event);
89 }
90 
91 // ----------------------------------------------------------------------------
92 
94  : QObject(parent)
95 {
96 }
97 
98 bool LostFocusFilter::eventFilter(QObject* obj, QEvent* event)
99 {
100  if (event->type() == QEvent::FocusOut)
101  return true;
102 
103  return QObject::eventFilter(obj, event);
104 }
105 
106 // ----------------------------------------------------------------------------
107 
108 ShortcodeFilter::ShortcodeFilter(QString shortcode, QObject* parent)
109  : QObject(parent)
110  , m_shortcode(std::move(shortcode))
111  , m_index(0)
112 {
113 }
114 
115 bool ShortcodeFilter::eventFilter(QObject* obj, QEvent* event)
116 {
117  Q_UNUSED(obj);
118  if (event->type() == QEvent::KeyPress) {
119  auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
120  if (m_shortcode.at(m_index) == keyEvent->text()) {
121  m_index++;
122  if (m_index == m_shortcode.length()) {
123  emit found();
124  m_index = 0;
125  }
126  } else {
127  int right = m_index;
128  while (m_index > 0) {
129  if (m_shortcode.at(m_index - 1) == keyEvent->text()
130  && m_shortcode.left(m_index - 1)
131  == m_shortcode.mid(right - m_index + 1, m_index - 1))
132  break;
133  m_index--;
134  }
135  }
136  }
137  return false;
138 }
139 
141  : QObject(parent)
142 {
143 }
144 
145 bool RightMouseButtonEater::eventFilter(QObject* obj, QEvent* event)
146 {
147  if (event->type() != QEvent::MouseButtonPress)
148  // standard event processing
149  return QObject::eventFilter(obj, event);
150  auto* mouseEvent = dynamic_cast<QMouseEvent*>(event);
151  if (mouseEvent->button() == Qt::RightButton) {
152  event->ignore();
153  return true;
154  }
155  event->accept();
156  return false;
157 }
158 
159 //! Passing focus-related events from child widget (e.g. QSpinBox) to parent (e.g. IntEditor)
160 //! to trigger QTreeView delegate's mechanism to switch editors on "tab" press key.
161 //! https://stackoverflow.com/questions/12145522/why-pressing-of-tab-key-emits-only-qeventshortcutoverride-event
162 
164  : QObject(parent)
165  , m_parent(parent)
166 {
167  if (parent->focusProxy())
168  parent->focusProxy()->installEventFilter(this);
169 }
170 
171 bool TabFromFocusProxy::eventFilter(QObject* obj, QEvent* event)
172 {
173  if (event->type() == QEvent::KeyPress) {
174  auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
175  if (keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Backtab) {
176  // we are posting event as if m_parent had "tab" key
177  QApplication::postEvent(
178  m_parent, new QKeyEvent(keyEvent->type(), keyEvent->key(), keyEvent->modifiers()));
179 
180  // but still let the origin (QSpinBox) to process it
181  return false; // process
182  }
183  }
184 
185  else if (event->type() == QEvent::FocusOut) {
186  auto* focusEvent = dynamic_cast<QFocusEvent*>(event);
187  QApplication::postEvent(this, new QFocusEvent(focusEvent->type(), focusEvent->reason()));
188 
189  // Don't filter because focus can be changed internally in editor
190  return false;
191  }
192 
193  return QObject::eventFilter(obj, event);
194 }
Defines classes releted to event filtering.
bool eventFilter(QObject *dist, QEvent *event) override
bool eventFilter(QObject *obj, QEvent *event) override
LostFocusFilter(QObject *parent=nullptr)
bool eventFilter(QObject *obj, QEvent *event) override
RightMouseButtonEater(QObject *parent=nullptr)
ShortcodeFilter(QString shortcode, QObject *parent=nullptr)
bool eventFilter(QObject *obj, QEvent *event) override
SpaceKeyEater(QObject *parent=nullptr)
bool eventFilter(QObject *obj, QEvent *event) override
TabFromFocusProxy(QWidget *parent=nullptr)
Passing focus-related events from child widget (e.g. QSpinBox) to parent (e.g. IntEditor) to trigger ...
bool eventFilter(QObject *obj, QEvent *event) override
static void install(QObject *obj)
bool eventFilter(QObject *obj, QEvent *event) override
WheelEventEater(QObject *parent=nullptr)