Use Qt to detect touchscreen support

This commit is contained in:
Kovid Goyal 2022-03-08 15:02:38 +05:30
parent 3aed3daaa2
commit 5277bad302
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -2,22 +2,26 @@
# License: GPLv3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>
import sys, os
import os
from functools import lru_cache
from qt.core import (
QApplication, QEvent, QMouseEvent, QObject, QPointF, QScroller, Qt, QTouchDevice,
pyqtSignal
)
from calibre.constants import iswindows
from calibre.utils.monotonic import monotonic
from polyglot.builtins import itervalues
touch_supported = False
if iswindows and sys.getwindowsversion()[:2] >= (6, 2): # At least windows 7
touch_supported = True
if 'CALIBRE_NO_TOUCH' in os.environ:
touch_supported = False
@lru_cache(maxsize=2)
def touch_supported():
if 'CALIBRE_NO_TOUCH' in os.environ:
return False
for dev in QTouchDevice.devices():
if dev.type() == QTouchDevice.DeviceType.TouchScreen:
return True
return False
HOLD_THRESHOLD = 1.0 # seconds
TAP_THRESHOLD = 50 # manhattan pixels
@ -150,7 +154,7 @@ class GestureManager(QObject):
def __init__(self, view):
QObject.__init__(self, view)
if touch_supported:
if touch_supported():
view.viewport().setAttribute(Qt.WidgetAttribute.WA_AcceptTouchEvents)
self.state = State()
self.state.tapped.connect(
@ -162,11 +166,11 @@ class GestureManager(QObject):
connect_lambda(self.state.tap_hold_finished, self, lambda self, tp: self.handle_tap_hold('end', tp))
self.evmap = {QEvent.Type.TouchBegin: 'start', QEvent.Type.TouchUpdate: 'update', QEvent.Type.TouchEnd: 'end'}
self.last_tap_at = 0
if touch_supported:
if touch_supported():
self.scroller = QScroller.scroller(view.viewport())
def handle_event(self, ev):
if not touch_supported:
if not touch_supported():
return
etype = ev.type()
if etype in (QEvent.Type.MouseButtonPress, QEvent.Type.MouseMove, QEvent.Type.MouseButtonRelease, QEvent.Type.MouseButtonDblClick):