Combine all event filters in one module, and apply the status tip filter to the sidebar as well

This commit is contained in:
Veronica Berglyd Olsen
2023-11-29 16:05:58 +01:00
parent b526315084
commit 1118f01c91
6 changed files with 27 additions and 24 deletions
@@ -1,9 +1,10 @@
"""
novelWriter Custom Object: Wheel Event Filter
===============================================
novelWriter Custom Objects: Event Filters
===========================================
File History:
Created: 2023-08-31 [2.1rc1]
Created: 2023-08-31 [2.1rc1] WheelEventFilter
Created: 2023-11-28 [2.2] StatusTipFilter
This file is a part of novelWriter
Copyright 20182023, Veronica Berglyd Olsen
@@ -23,7 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from __future__ import annotations
from PyQt5.QtGui import QWheelEvent
from PyQt5.QtGui import QStatusTipEvent, QWheelEvent
from PyQt5.QtCore import QEvent, QObject
from PyQt5.QtWidgets import QWidget
@@ -63,3 +64,12 @@ class WheelEventFilter(QObject):
return False
# END Class WheelEventFilter
class StatusTipFilter(QObject):
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
"""Filter out status tip events on menus."""
return True if isinstance(event, QStatusTipEvent) else super().eventFilter(obj, event)
# END Class StatusTipFilter
+1 -1
View File
@@ -62,7 +62,7 @@ from novelwriter.tools.lipsum import GuiLipsum
from novelwriter.core.document import NWDocument
from novelwriter.gui.dochighlight import GuiDocHighlighter
from novelwriter.gui.editordocument import GuiTextDocument
from novelwriter.extensions.wheeleventfilter import WheelEventFilter
from novelwriter.extensions.eventfilters import WheelEventFilter
if TYPE_CHECKING: # pragma: no cover
from novelwriter.guimain import GuiMain
+1 -1
View File
@@ -46,7 +46,7 @@ from novelwriter.enum import nwItemType, nwDocAction, nwDocMode
from novelwriter.error import logException
from novelwriter.constants import nwUnicode
from novelwriter.core.tohtml import ToHtml
from novelwriter.extensions.wheeleventfilter import WheelEventFilter
from novelwriter.extensions.eventfilters import WheelEventFilter
if TYPE_CHECKING: # pragma: no cover
from novelwriter.guimain import GuiMain
+4 -13
View File
@@ -3,8 +3,7 @@ novelWriter GUI Main Menu
===========================
File History:
Created: 2019-04-27 [0.0.1] GuiMainMenu
Created: 2023-11-28 [2.2] StatusTipFilter
Created: 2019-04-27 [0.0.1]
This file is a part of novelWriter
Copyright 20182023, Veronica Berglyd Olsen
@@ -29,14 +28,15 @@ import logging
from typing import TYPE_CHECKING
from pathlib import Path
from PyQt5.QtGui import QDesktopServices, QStatusTipEvent
from PyQt5.QtCore import QEvent, QObject, QUrl, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtCore import QUrl, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QMenuBar, QAction
from novelwriter import CONFIG, SHARED
from novelwriter.enum import nwDocAction, nwDocInsert, nwWidget
from novelwriter.common import openExternalPath
from novelwriter.constants import nwConst, trConst, nwKeyWords, nwLabels, nwUnicode
from novelwriter.extensions.eventfilters import StatusTipFilter
if TYPE_CHECKING: # pragma: no cover
from novelwriter.guimain import GuiMain
@@ -971,12 +971,3 @@ class GuiMainMenu(QMenuBar):
return
# END Class GuiMainMenu
class StatusTipFilter(QObject):
def eventFilter(self, obj: QObject, event: QEvent) -> bool:
"""Filter out status tip events."""
return True if isinstance(event, QStatusTipEvent) else super().eventFilter(obj, event)
# END Class StatusTipFilter
+4 -2
View File
@@ -27,12 +27,13 @@ import logging
from typing import TYPE_CHECKING
from PyQt5.QtCore import QEvent, QPoint, Qt, QSize, pyqtSignal
from PyQt5.QtGui import QPalette
from PyQt5.QtCore import QEvent, QPoint, Qt, QSize, pyqtSignal
from PyQt5.QtWidgets import QMenu, QToolButton, QVBoxLayout, QWidget
from novelwriter import CONFIG, SHARED
from novelwriter.enum import nwView
from novelwriter.extensions.eventfilters import StatusTipFilter
if TYPE_CHECKING: # pragma: no cover
from novelwriter.guimain import GuiMain
@@ -54,6 +55,7 @@ class GuiSideBar(QWidget):
iPx = CONFIG.pxInt(24)
iconSize = QSize(iPx, iPx)
self.setContentsMargins(0, 0, 0, 0)
self.installEventFilter(StatusTipFilter(mainGui))
# Buttons
self.tbProject = QToolButton(self)
@@ -162,7 +164,7 @@ class GuiSideBar(QWidget):
class _PopRightMenu(QMenu):
def event(self, event: QEvent):
def event(self, event: QEvent) -> bool:
"""Overload the show event and move the menu popup location."""
if event.type() == QEvent.Show:
parent = self.parent()
@@ -26,7 +26,7 @@ from PyQt5.QtGui import QKeyEvent, QWheelEvent
from PyQt5.QtCore import QEvent, QObject, QPoint, Qt
from PyQt5.QtWidgets import QWidget
from novelwriter.extensions.wheeleventfilter import WheelEventFilter
from novelwriter.extensions.eventfilters import WheelEventFilter
class MockWidget(QWidget):
@@ -42,7 +42,7 @@ class MockWidget(QWidget):
@pytest.mark.gui
def testExtWheelEventFilter_Main():
def testExtEventFilters_WheelEventFilter():
"""Test the WheelEventFilter class."""
obj = QObject()
widget = MockWidget()
@@ -65,4 +65,4 @@ def testExtWheelEventFilter_Main():
eFilter.eventFilter(obj, event)
assert widget.count == 1
# END Test testExtWheelEventFilter_Main
# END Test testExtEventFilters_WheelEventFilter