[vlc-devel] [RFC 17/82] qt: provide an event filter to process events before a QML item

Pierre Lamot pierre at videolabs.io
Fri Feb 1 14:01:21 CET 2019


---
 modules/gui/qt/Makefile.am             |  3 ++
 modules/gui/qt/util/qmleventfilter.cpp | 70 ++++++++++++++++++++++++
 modules/gui/qt/util/qmleventfilter.hpp | 75 ++++++++++++++++++++++++++
 3 files changed, 148 insertions(+)
 create mode 100644 modules/gui/qt/util/qmleventfilter.cpp
 create mode 100644 modules/gui/qt/util/qmleventfilter.hpp

diff --git a/modules/gui/qt/Makefile.am b/modules/gui/qt/Makefile.am
index ff8c46fa9a..3d218c000d 100644
--- a/modules/gui/qt/Makefile.am
+++ b/modules/gui/qt/Makefile.am
@@ -143,6 +143,8 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/util/buttons/RoundButton.hpp \
 	gui/qt/util/qvlcframe.hpp \
 	gui/qt/util/qvlcapp.hpp \
+	gui/qt/util/qmleventfilter.cpp \
+	gui/qt/util/qmleventfilter.hpp \
 	gui/qt/util/singleton.hpp \
 	gui/qt/util/vlctick.cpp \
 	gui/qt/util/vlctick.hpp \
@@ -232,6 +234,7 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/util/input_slider.moc.cpp \
 	gui/qt/util/timetooltip.moc.cpp \
 	gui/qt/util/customwidgets.moc.cpp \
+	gui/qt/util/qmleventfilter.moc.cpp \
 	gui/qt/util/searchlineedit.moc.cpp \
 	gui/qt/util/qmenuview.moc.cpp \
 	gui/qt/util/qvlcapp.moc.cpp \
diff --git a/modules/gui/qt/util/qmleventfilter.cpp b/modules/gui/qt/util/qmleventfilter.cpp
new file mode 100644
index 0000000000..28f8646955
--- /dev/null
+++ b/modules/gui/qt/util/qmleventfilter.cpp
@@ -0,0 +1,70 @@
+/*****************************************************************************
+ * Copyright (C) 2019 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+#include "qmleventfilter.hpp"
+#include <QEvent>
+
+QmlEventFilter::QmlEventFilter(QQuickItem *parent)
+    : QQuickItem(parent)
+{
+}
+
+QmlEventFilter::~QmlEventFilter()
+{
+    if (m_source != nullptr)
+        m_source->removeEventFilter(this);
+}
+
+void QmlEventFilter::setSource(QObject* source)
+{
+    source->installEventFilter(this);
+    m_source = source;
+}
+
+void QmlEventFilter::keyPressEvent(QKeyEvent* event)
+{
+    // This is actually called when the QML event handler hasn't accepted the event
+    m_qmlAccepted = false;
+    // Ensure the event won't be propagated further
+    event->setAccepted(true);
+}
+
+void QmlEventFilter::keyReleaseEvent(QKeyEvent* event)
+{
+    m_qmlAccepted = false;
+    event->setAccepted(true);
+}
+
+bool QmlEventFilter::eventFilter(QObject* obj, QEvent* event)
+{
+    if (!m_filterEnabled)
+        return false;
+
+    bool ret = false;
+    switch (event->type())
+    {
+    case QEvent::KeyPress:
+    case QEvent::KeyRelease:
+        m_qmlAccepted = true;
+        QCoreApplication::sendEvent(this, event);
+        ret = m_qmlAccepted;
+        break;
+    default:
+        break;
+    }
+    return ret;
+}
diff --git a/modules/gui/qt/util/qmleventfilter.hpp b/modules/gui/qt/util/qmleventfilter.hpp
new file mode 100644
index 0000000000..b22d830af3
--- /dev/null
+++ b/modules/gui/qt/util/qmleventfilter.hpp
@@ -0,0 +1,75 @@
+/*****************************************************************************
+ * Copyright (C) 2019 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * ( at your option ) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+#ifndef QMLEVENTFILTER_HPP
+#define QMLEVENTFILTER_HPP
+
+#include <QQuickItem>
+
+/**
+ * @brief The QmlEventFilter class allows to register an event filter from QML
+ * this might be usefull to process key press before they are processed (and
+ * eventually accepted) by children components
+ *
+ * this is usable as
+ *
+ *  EventFilter {
+ *      filterEnabled: true
+ *      Keys.onPressed: {
+ *          //do stuff
+ *          event.accepted = true //to block the event
+ *      }
+ *  }
+ *
+ *  Component.onCompleted: {
+ *      filter.source = rootWindow
+ *  }
+ *
+ * Note that this solution doesn't work as-is for mouse events as Qml doesn't provide
+ * the equivalent of Keys.onPressed.
+ */
+class QmlEventFilter : public QQuickItem
+{
+    Q_OBJECT
+public:
+    Q_PROPERTY(QObject * source READ getSource WRITE setSource)
+    Q_PROPERTY(bool filterEnabled READ getFilterEnabled WRITE setFilterEnabled)
+
+public:
+    QmlEventFilter(QQuickItem *parent = nullptr);
+    ~QmlEventFilter();
+
+    void setSource(QObject *source);
+
+    inline QObject * getSource() { return m_source; }
+    inline void setFilterEnabled(bool value) { m_filterEnabled = value; }
+    inline bool getFilterEnabled() { return m_filterEnabled; }
+
+private:
+
+    void keyPressEvent(QKeyEvent *event) override;
+
+    void keyReleaseEvent(QKeyEvent *event) override;
+
+    bool eventFilter(QObject *obj, QEvent *event) override;
+
+private:
+    QObject *m_source = nullptr;
+    bool m_filterEnabled = true;
+    bool m_qmlAccepted = false;
+};
+#endif // QMLEVENTFILTER_HPP
-- 
2.19.1



More information about the vlc-devel mailing list