Fix synthesized mouse event ignoring code not working on QT 5. Use Qt 5's new API to ignore such events instead.

This commit is contained in:
Kovid Goyal 2014-07-05 13:37:28 +05:30
parent ccaf8c5507
commit e3b9f76569

View File

@ -6,7 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
import time, ctypes, sys import time, sys
from functools import partial from functools import partial
from PyQt5.Qt import ( from PyQt5.Qt import (
QObject, QPointF, pyqtSignal, QEvent, QApplication, QMouseEvent, Qt, QObject, QPointF, pyqtSignal, QEvent, QApplication, QMouseEvent, Qt,
@ -16,7 +16,6 @@ from calibre.constants import iswindows
touch_supported = False touch_supported = False
if iswindows and sys.getwindowsversion()[:2] >= (6, 2): # At least windows 7 if iswindows and sys.getwindowsversion()[:2] >= (6, 2): # At least windows 7
from ctypes import wintypes
touch_supported = True touch_supported = True
SWIPE_HOLD_INTERVAL = 0.5 # seconds SWIPE_HOLD_INTERVAL = 0.5 # seconds
@ -263,34 +262,14 @@ class GestureHandler(QObject):
self.state.pinched.connect(self.handle_pinch) self.state.pinched.connect(self.handle_pinch)
self.evmap = {QEvent.TouchBegin: 'start', QEvent.TouchUpdate: 'update', QEvent.TouchEnd: 'end'} self.evmap = {QEvent.TouchBegin: 'start', QEvent.TouchUpdate: 'update', QEvent.TouchEnd: 'end'}
# Ignore fake mouse events generated by the window system from touch
# events. At least on windows, we know how to identify these fake
# events. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms703320(v=vs.85).aspx
self.is_fake_mouse_event = lambda : False
if touch_supported and iswindows:
MI_WP_SIGNATURE = 0xFF515700
SIGNATURE_MASK = 0xFFFFFF00
try:
f = ctypes.windll.user32.GetMessageExtraInfo
f.restype = wintypes.LPARAM
def is_fake_mouse_event():
val = f()
ans = (val & SIGNATURE_MASK) == MI_WP_SIGNATURE
return ans
self.is_fake_mouse_event = is_fake_mouse_event
except Exception:
import traceback
traceback.print_exc()
def __call__(self, ev): def __call__(self, ev):
if not touch_supported: if not touch_supported:
return False return False
etype = ev.type() etype = ev.type()
if etype in ( if etype in (
QEvent.MouseMove, QEvent.MouseButtonPress, QEvent.MouseMove, QEvent.MouseButtonPress,
QEvent.MouseButtonRelease, QEvent.MouseButtonDblClick, QEvent.MouseButtonRelease, QEvent.MouseButtonDblClick) and ev.source() != Qt.MouseEventNotSynthesized:
QEvent.ContextMenu) and self.is_fake_mouse_event(): # swallow fake mouse events generated from touch events
# swallow fake mouse events that the windowing system generates from touch events
ev.accept() ev.accept()
return True return True
boundary = self.evmap.get(etype, None) boundary = self.evmap.get(etype, None)