Handle tap gestures

This commit is contained in:
Kovid Goyal 2017-07-31 14:39:32 +05:30
parent 0a62b1f98d
commit f62ed87b79
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -33,6 +33,7 @@ class TouchPoint(object):
self.start_screen_position = self.current_screen_position = self.previous_screen_position = QPointF(tp.screenPos()) self.start_screen_position = self.current_screen_position = self.previous_screen_position = QPointF(tp.screenPos())
self.time_since_last_update = -1 self.time_since_last_update = -1
self.total_movement = 0 self.total_movement = 0
self.start_position = tp.pos()
def update(self, tp): def update(self, tp):
now = monotonic() now = monotonic()
@ -107,6 +108,7 @@ class State(QObject):
self.check_for_holds() self.check_for_holds()
if {Flick} & self.possible_gestures: if {Flick} & self.possible_gestures:
tp = next(self.touch_points.itervalues()) tp = next(self.touch_points.itervalues())
if tp.flick_type is not None:
self.flicking.emit(*tp.flick_live) self.flicking.emit(*tp.flick_live)
def check_for_holds(self): def check_for_holds(self):
@ -167,6 +169,7 @@ class GestureManager(QObject):
def __init__(self, view): def __init__(self, view):
QObject.__init__(self, view) QObject.__init__(self, view)
view.viewport().setAttribute(Qt.WA_AcceptTouchEvents)
self.state = State() self.state = State()
self.state.flicked.connect(self.handle_flick) self.state.flicked.connect(self.handle_flick)
self.state.tapped.connect(self.handle_tap) self.state.tapped.connect(self.handle_tap)
@ -175,6 +178,7 @@ class GestureManager(QObject):
self.state.tap_hold_updated.connect(partial(self.handle_tap_hold, 'update')) self.state.tap_hold_updated.connect(partial(self.handle_tap_hold, 'update'))
self.state.tap_hold_finished.connect(partial(self.handle_tap_hold, 'end')) self.state.tap_hold_finished.connect(partial(self.handle_tap_hold, 'end'))
self.evmap = {QEvent.TouchBegin: 'start', QEvent.TouchUpdate: 'update', QEvent.TouchEnd: 'end'} self.evmap = {QEvent.TouchBegin: 'start', QEvent.TouchUpdate: 'update', QEvent.TouchEnd: 'end'}
self.last_tap_at = 0
def handle_event(self, ev): def handle_event(self, ev):
if not touch_supported: if not touch_supported:
@ -202,16 +206,18 @@ class GestureManager(QObject):
def handle_flick(self, direction): def handle_flick(self, direction):
if self.close_open_menu(): if self.close_open_menu():
return return
raise NotImplementedError('TODO: Implement')
def handle_flicking(self, x, y): def handle_flicking(self, x, y):
raise NotImplementedError('TODO: Implement') raise NotImplementedError('TODO: Implement')
def handle_tap(self, tp): def handle_tap(self, tp):
last_tap_at, self.last_tap_at = self.last_tap_at, monotonic()
if self.close_open_menu(): if self.close_open_menu():
return return
raise NotImplementedError('TODO: Implement') interval = QApplication.instance().doubleClickInterval() / 1000
double_tap = self.last_tap_at - last_tap_at < interval
send_click(self.parent(), tp.start_position, double_click=double_tap)
def handle_tap_hold(self, action, tp): def handle_tap_hold(self, action, tp):
if action == 'end': if action == 'end':
raise NotImplementedError('TODO: Implement') send_click(self.parent(), tp.start_position, button=Qt.RightButton)