[vlc-commits] [Git][videolan/vlc][master] 3 commits: qt: add MouseEventFilter

Jean-Baptiste Kempf (@jbk) gitlab at videolan.org
Sun Nov 21 18:52:23 UTC 2021



Jean-Baptiste Kempf pushed to branch master at VideoLAN / VLC


Commits:
f1bda18e by Fatih Uzunoglu at 2021-11-21T18:19:22+00:00
qt: add MouseEventFilter

- - - - -
6e67493a by Fatih Uzunoglu at 2021-11-21T18:19:22+00:00
qt: register type MouseEventFilter

- - - - -
53ec7adc by Fatih Uzunoglu at 2021-11-21T18:19:22+00:00
qml: use MouseEventFilter

- - - - -


8 changed files:

- modules/gui/qt/Makefile.am
- modules/gui/qt/maininterface/mainui.cpp
- modules/gui/qt/playlist/qml/PlaylistListView.qml
- + modules/gui/qt/util/mouse_event_filter.cpp
- + modules/gui/qt/util/mouse_event_filter.hpp
- modules/gui/qt/widgets/qml/ExpandGridView.qml
- modules/gui/qt/widgets/qml/KeyNavigableListView.qml
- modules/gui/qt/widgets/qml/KeyNavigableTableView.qml


Changes:

=====================================
modules/gui/qt/Makefile.am
=====================================
@@ -261,6 +261,8 @@ libqt_plugin_la_SOURCES = \
 	gui/qt/util/vlctick.cpp \
 	gui/qt/util/vlctick.hpp \
 	gui/qt/util/qmlinputitem.hpp \
+	gui/qt/util/mouse_event_filter.cpp \
+	gui/qt/util/mouse_event_filter.hpp \
 	gui/qt/widgets/native/animators.cpp \
 	gui/qt/widgets/native/animators.hpp \
 	gui/qt/widgets/native/customwidgets.cpp gui/qt/widgets/native/customwidgets.hpp \
@@ -417,6 +419,7 @@ nodist_libqt_plugin_la_SOURCES = \
 	gui/qt/util/navigation_history.moc.cpp \
 	gui/qt/util/qml_main_context.moc.cpp \
 	gui/qt/util/qmleventfilter.moc.cpp \
+	gui/qt/util/mouse_event_filter.moc.cpp \
 	gui/qt/util/qvlcapp.moc.cpp \
 	gui/qt/util/renderer_manager.moc.cpp \
 	gui/qt/util/selectable_list_model.moc.cpp \


=====================================
modules/gui/qt/maininterface/mainui.cpp
=====================================
@@ -37,6 +37,7 @@
 #include "util/sortfilterproxymodel.hpp"
 #include "util/navigation_history.hpp"
 #include "util/qmlinputitem.hpp"
+#include "util/mouse_event_filter.hpp"
 
 #include "dialogs/help/aboutmodel.hpp"
 #include "dialogs/dialogs_provider.hpp"
@@ -211,6 +212,7 @@ void MainUI::registerQMLTypes()
         qRegisterMetaType<DialogId>();
 
         qmlRegisterType<QmlEventFilter>( uri, versionMajor, versionMinor, "EventFilter" );
+        qmlRegisterType<MouseEventFilter>( uri, versionMajor, versionMinor, "MouseEventFilter" );
 
         qmlRegisterUncreatableType<ControlbarProfileModel>(uri, versionMajor, versionMinor, "ControlbarProfileModel", "");
         qmlRegisterUncreatableType<ControlbarProfile>(uri, versionMajor, versionMinor, "ControlbarProfile", "");


=====================================
modules/gui/qt/playlist/qml/PlaylistListView.qml
=====================================
@@ -347,6 +347,14 @@ Control {
 
             signal setItemDropIndicatorVisible(int index, bool visible)
 
+            onDeselectAll: {
+                root.model.deselectAll()
+            }
+
+            onShowContextMenu: {
+                contextMenu.popup(-1, globalPos)
+            }
+
             Connections {
                 target: listView.model
 
@@ -391,23 +399,6 @@ Control {
                     dropIndicator.visible = Qt.binding(function() { return (visible || dropArea.containsDrag); })
                 }
 
-                MouseArea {
-                    anchors.fill: parent
-                    acceptedButtons: Qt.RightButton | Qt.LeftButton
-
-                    onClicked: {
-                        listView.forceActiveFocus()
-
-                        if ( mouse.button === Qt.LeftButton || mouse.button === Qt.RightButton ) {
-                            root.model.deselectAll()
-                        }
-
-                        if ( mouse.button === Qt.RightButton ) {
-                            contextMenu.popup(-1, this.mapToGlobal(mouse.x, mouse.y))
-                        }
-                    }
-                }
-
                 Rectangle {
                     id: dropIndicator
 


=====================================
modules/gui/qt/util/mouse_event_filter.cpp
=====================================
@@ -0,0 +1,124 @@
+/*****************************************************************************
+ * Copyright (C) 2021 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 "mouse_event_filter.hpp"
+
+#include <QMouseEvent>
+#include <QQuickItem>
+
+MouseEventFilter::MouseEventFilter(QObject *parent)
+    : QObject(parent)
+{
+
+}
+
+MouseEventFilter::~MouseEventFilter()
+{
+    detach();
+}
+
+QObject *MouseEventFilter::target() const
+{
+    return m_target;
+}
+
+void MouseEventFilter::setTarget(QObject *newTarget)
+{
+    if (m_target == newTarget)
+        return;
+
+    detach();
+    m_target = newTarget;
+    attach();
+
+    emit targetChanged();
+}
+
+bool MouseEventFilter::eventFilter(QObject *watched, QEvent *event)
+{
+    assert(watched == m_target);
+
+    const auto mouse = dynamic_cast<QMouseEvent*>(event);
+    if (!mouse)
+        return false;
+
+    if (!m_filterEventsSynthesizedByQt &&
+        mouse->source() == Qt::MouseEventSource::MouseEventSynthesizedByQt)
+        return false;
+
+    switch (event->type())
+    {
+    case QEvent::MouseButtonDblClick:
+        emit mouseButtonDblClick(mouse->localPos(),
+                                 mouse->globalPos(),
+                                 mouse->buttons(),
+                                 mouse->modifiers(),
+                                 mouse->source(),
+                                 mouse->flags()); break;
+    case QEvent::MouseButtonPress:
+        emit mouseButtonPress(mouse->localPos(),
+                              mouse->globalPos(),
+                              mouse->buttons(),
+                              mouse->modifiers(),
+                              mouse->source(),
+                              mouse->flags()); break;
+    case QEvent::MouseButtonRelease:
+        emit mouseButtonRelease(mouse->localPos(),
+                                mouse->globalPos(),
+                                mouse->button(),
+                                mouse->modifiers(),
+                                mouse->source(),
+                                mouse->flags()); break;
+    case QEvent::MouseMove:
+        emit mouseMove(mouse->localPos(),
+                       mouse->globalPos(),
+                       mouse->buttons(),
+                       mouse->modifiers(),
+                       mouse->source(),
+                       mouse->flags()); break;
+
+    default:
+        return false;
+    }
+
+    return true;
+}
+
+void MouseEventFilter::attach()
+{
+    if (m_target)
+    {
+        m_target->installEventFilter(this);
+        const auto item = qobject_cast<QQuickItem*>(m_target);
+        if (item)
+        {
+            m_targetItemInitialAcceptedMouseButtons = item->acceptedMouseButtons();
+            item->setAcceptedMouseButtons(Qt::AllButtons);
+        }
+    }
+}
+
+void MouseEventFilter::detach()
+{
+    if (m_target)
+    {
+        m_target->removeEventFilter(this);
+        const auto item = qobject_cast<QQuickItem*>(m_target);
+        if (item)
+            item->setAcceptedMouseButtons(m_targetItemInitialAcceptedMouseButtons);
+    }
+}


=====================================
modules/gui/qt/util/mouse_event_filter.hpp
=====================================
@@ -0,0 +1,59 @@
+/*****************************************************************************
+ * Copyright (C) 2021 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 MOUSEEVENTFILTER_HPP
+#define MOUSEEVENTFILTER_HPP
+
+#include <QObject>
+#include <QPointF>
+
+class MouseEventFilter : public QObject
+{
+    Q_OBJECT
+
+    Q_PROPERTY(QObject* target READ target WRITE setTarget NOTIFY targetChanged FINAL)
+    Q_PROPERTY(bool filterEventsSynthesizedByQt MEMBER m_filterEventsSynthesizedByQt NOTIFY filterEventsSynthesizedByQtChanged FINAL)
+
+public:
+    explicit MouseEventFilter(QObject *parent = nullptr);
+    ~MouseEventFilter();
+
+    QObject *target() const;
+    void setTarget(QObject *newTarget);
+
+signals:
+    void targetChanged();
+    void filterEventsSynthesizedByQtChanged();
+
+    void mouseButtonDblClick(QPointF localPos, QPointF globalPos, int buttons, int modifiers, int source, int flags);
+    void mouseButtonPress(QPointF localPos, QPointF globalPos, int buttons, int modifiers, int source, int flags);
+    void mouseButtonRelease(QPointF localPos, QPointF globalPos, int button, int modifiers, int source, int flags);
+    void mouseMove(QPointF localPos, QPointF globalPos, int buttons, int modifiers, int source, int flags);
+
+private:
+    bool eventFilter(QObject *watched, QEvent *event) override;
+
+    void attach();
+    void detach();
+
+private:
+    QObject *m_target = nullptr;
+    Qt::MouseButtons m_targetItemInitialAcceptedMouseButtons = 0;
+    bool m_filterEventsSynthesizedByQt = false;
+};
+
+#endif // MOUSEEVENTFILTER_HPP


=====================================
modules/gui/qt/widgets/qml/ExpandGridView.qml
=====================================
@@ -110,6 +110,8 @@ FocusScope {
     signal selectAll()
     signal actionAtIndex(int index)
 
+    signal showContextMenu(point globalPos)
+
     // Settings
 
     Accessible.role: Accessible.Table
@@ -567,6 +569,27 @@ FocusScope {
             id: flickableScrollBar
         }
 
+        MouseEventFilter {
+            target: flickable
+
+            onMouseButtonPress: {
+                if (buttons & (Qt.LeftButton | Qt.RightButton)) {
+                    Helpers.enforceFocus(flickable, Qt.MouseFocusReason)
+
+                    if (!(modifiers & (Qt.ShiftModifier | Qt.ControlModifier))) {
+                        if (delegateModel)
+                            delegateModel.clear()
+                    }
+                }
+            }
+
+            onMouseButtonRelease: {
+                if (button & Qt.RightButton) {
+                    root.showContextMenu(globalPos)
+                }
+            }
+        }
+
         Loader {
             id: headerItemLoader
             //load the header early (when the first row is visible)


=====================================
modules/gui/qt/widgets/qml/KeyNavigableListView.qml
=====================================
@@ -104,6 +104,10 @@ FocusScope {
 
     signal actionAtIndex(int index)
 
+    signal deselectAll()
+
+    signal showContextMenu(point globalPos)
+
     // Settings
 
     Accessible.role: Accessible.List
@@ -216,6 +220,26 @@ FocusScope {
         boundsBehavior: Flickable.StopAtBounds
         boundsMovement: Flickable.StopAtBounds
 
+        MouseEventFilter {
+            target: view
+
+            onMouseButtonPress: {
+                if (buttons & (Qt.LeftButton | Qt.RightButton)) {
+                    Helpers.enforceFocus(view, Qt.MouseFocusReason)
+
+                    if (!(modifiers & (Qt.ShiftModifier | Qt.ControlModifier))) {
+                        listview_id.deselectAll()
+                    }
+                }
+            }
+
+            onMouseButtonRelease: {
+                if (button & Qt.RightButton) {
+                    listview_id.showContextMenu(globalPos)
+                }
+            }
+        }
+
         // NOTE: We always want a valid 'currentIndex' by default.
         onCountChanged: if (count && currentIndex === -1) currentIndex = 0
 


=====================================
modules/gui/qt/widgets/qml/KeyNavigableTableView.qml
=====================================
@@ -225,6 +225,17 @@ FocusScope {
 
         headerPositioning: ListView.OverlayHeader
 
+        onDeselectAll: {
+            if (selectionDelegateModel) {
+                selectionDelegateModel.clear()
+            }
+        }
+
+        onShowContextMenu: {
+            if (selectionDelegateModel.hasSelection)
+                root.rightClick(null, null, globalPos);
+        }
+
         header: Rectangle {
 
             readonly property alias contentX: row.x



View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/5e14e1ed68275d6394b2c795657503777ddaf837...53ec7adcb9a7f4cc5458782ae32ce9aef635b7f8

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/compare/5e14e1ed68275d6394b2c795657503777ddaf837...53ec7adcb9a7f4cc5458782ae32ce9aef635b7f8
You're receiving this email because of your account on code.videolan.org.




More information about the vlc-commits mailing list