File: CustomEventFilters.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (198 lines) | stat: -rw-r--r-- 6,154 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Base/CustomEventFilters.cpp
//! @brief     Defines classes releted to event filtering.
//!
//! @homepage  http://www.bornagainproject.org
//! @license   GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors   Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
//  ************************************************************************************************

#include "GUI/View/Base/CustomEventFilters.h"
#include "Base/Util/Assert.h"
#include <QApplication>
#include <QComboBox>
#include <QKeyEvent>
#include <QString>
#include <utility>

SpaceKeyEater::SpaceKeyEater(QObject* parent)
    : QObject(parent)
{
}

bool SpaceKeyEater::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type() != QEvent::KeyPress)
        return QObject::eventFilter(obj, event);

    auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
    if (keyEvent->key() == Qt::Key_Space)
        return true; /* Always accept space bar */
    return QObject::eventFilter(obj, event);
    // standard event processing
}

// ----------------------------------------------------------------------------

WheelEventEater::WheelEventEater(QObject* parent)
    : QObject(parent)
{
}

void WheelEventEater::install(QObject* obj)
{
    auto* p = new WheelEventEater(obj);
    obj->installEventFilter(p);
}

bool WheelEventEater::eventFilter(QObject* obj, QEvent* event)
{
    if (auto* spinBox = qobject_cast<QAbstractSpinBox*>(obj)) {
        if (event->type() == QEvent::Wheel) {
            if (spinBox->focusPolicy() == Qt::WheelFocus) {
                event->accept();
                return false;
            }
            event->ignore();
            return true;
        }
        if (event->type() == QEvent::FocusIn)
            spinBox->setFocusPolicy(Qt::WheelFocus);
        else if (event->type() == QEvent::FocusOut)
            spinBox->setFocusPolicy(Qt::StrongFocus);
    } else if (qobject_cast<QComboBox*>(obj)) {
        if (event->type() == QEvent::Wheel) {
            event->ignore();
            return true;
        }
        event->accept();
        return false;
    }
    return QObject::eventFilter(obj, event);
}

// ----------------------------------------------------------------------------

bool DeleteEventFilter::eventFilter(QObject* dist, QEvent* event)
{
    Q_UNUSED(dist);
    if (event->type() == QEvent::KeyPress) {
        auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
        ASSERT(keyEvent);
        if (keyEvent->key() == Qt::Key_Delete)
            emit removeItem();
    }
    return QObject::eventFilter(dist, event);
}

// ----------------------------------------------------------------------------

LostFocusFilter::LostFocusFilter(QObject* parent)
    : QObject(parent)
{
}

bool LostFocusFilter::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type() == QEvent::FocusOut)
        return true;

    return QObject::eventFilter(obj, event);
}

// ----------------------------------------------------------------------------

ShortcodeFilter::ShortcodeFilter(const QString& shortcode, QObject* parent)
    : QObject(parent)
    , m_shortcode(shortcode)
    , m_index(0)
{
}

bool ShortcodeFilter::eventFilter(QObject* obj, QEvent* event)
{
    Q_UNUSED(obj);
    if (event->type() == QEvent::KeyPress) {
        auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
        ASSERT(keyEvent);
        if (m_shortcode.at(m_index) == keyEvent->text()) {
            m_index++;
            if (m_index == m_shortcode.length()) {
                emit found();
                m_index = 0;
            }
        } else {
            int right = m_index;
            while (m_index > 0) {
                if (m_shortcode.at(m_index - 1) == keyEvent->text()
                    && m_shortcode.left(m_index - 1)
                           == m_shortcode.mid(right - m_index + 1, m_index - 1))
                    break;
                m_index--;
            }
        }
    }
    return false;
}

RightMouseButtonEater::RightMouseButtonEater(QObject* parent)
    : QObject(parent)
{
}

bool RightMouseButtonEater::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type() != QEvent::MouseButtonPress)
        // standard event processing
        return QObject::eventFilter(obj, event);
    auto* mouseEvent = dynamic_cast<QMouseEvent*>(event);
    ASSERT(mouseEvent);
    if (mouseEvent->button() == Qt::RightButton) {
        event->ignore();
        return true;
    }
    event->accept();
    return false;
}

//! Passing focus-related events from child widget (e.g. QSpinBox) to parent (e.g. IntEditor)
//! to trigger QTreeView delegate's mechanism to switch editors on "tab" press key.
//! https://stackoverflow.com/questions/12145522/why-pressing-of-tab-key-emits-only-qeventshortcutoverride-event

TabFromFocusProxy::TabFromFocusProxy(QWidget* parent)
    : QObject(parent)
    , m_parent(parent)
{
    if (parent->focusProxy())
        parent->focusProxy()->installEventFilter(this);
}

bool TabFromFocusProxy::eventFilter(QObject* obj, QEvent* event)
{
    if (event->type() == QEvent::KeyPress) {
        auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
        ASSERT(keyEvent);
        if (keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Backtab) {
            // we are posting event as if m_parent had "tab" key
            QApplication::postEvent(
                m_parent, new QKeyEvent(keyEvent->type(), keyEvent->key(), keyEvent->modifiers()));

            return false; // let QSpinBox that triggered the event process it
        }
    }

    else if (event->type() == QEvent::FocusOut) {
        auto* focusEvent = dynamic_cast<QFocusEvent*>(event);
        ASSERT(focusEvent);
        QApplication::postEvent(this, new QFocusEvent(focusEvent->type(), focusEvent->reason()));

        return false; // Don't filter because focus can be changed internally in editor
    }

    return QObject::eventFilter(obj, event);
}