/usr/share/pyshared/guiqwt/events.py is in python-guiqwt 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | # -*- coding: utf-8 -*-
#
# Copyright © 2009-2010 CEA
# Pierre Raybaut
# Licensed under the terms of the CECILL License
# (see guiqwt/__init__.py for details)
# pylint: disable-msg=W0622,R0903
# W0622: complains about filter being a builtin
# R0903: complains about too few public methods which is the purpose here
"""
guiqwt.events
-------------
The `event` module handles event management (states, event filter, ...).
"""
from __future__ import print_function
import weakref
from guidata.qt.QtCore import QEvent, Qt, QObject, QPoint
from guidata.qt.QtGui import QKeySequence
CursorShape = type(Qt.ArrowCursor)
from guiqwt.config import CONF
from guiqwt.debug import evt_type_to_str, buttons_to_str
from guiqwt.signals import (SIG_CLICK_EVENT, SIG_START_TRACKING, SIG_END_RECT,
SIG_STOP_NOT_MOVING, SIG_STOP_MOVING, SIG_MOVE)
# Sélection d'événements ---------
class EventMatch(object):
"""A callable returning true if it matches an event"""
def __call__(self, event):
raise NotImplementedError
def get_event_types(self):
"""Returns a set of event types handled by this
EventMatch.
This is used to quickly optimize events not handled
by any event matchers
"""
return frozenset()
class KeyEventMatch(EventMatch):
"""
A callable returning True if it matches a key event
keys: list of keys or couples (key, modifier)
"""
def __init__(self, keys):
super(KeyEventMatch, self).__init__()
key_list, mod_list = [], []
for item in keys:
if isinstance(item, (tuple, list)):
k, m = item
else:
k = item
# Avoid bad arguments: modifier instead of key
assert k not in (Qt.ControlModifier, Qt.ShiftModifier,
Qt.AltModifier, Qt.NoModifier)
m = Qt.NoModifier
key_list.append(k)
mod_list.append(m)
self.keys = key_list
self.mods = mod_list
def get_event_types(self):
return frozenset((QEvent.KeyPress,))
def __call__(self, event):
if event.type() == QEvent.KeyPress:
my_key = event.key()
my_mod = event.modifiers()
if my_key in self.keys:
mod = self.mods[self.keys.index(my_key)]
if mod == Qt.NoModifier or my_mod & mod:
return True
return False
class StandardKeyMatch(EventMatch):
"""
A callable returning True if it matches a key event
keysequence: QKeySequence.StandardKey integer
"""
def __init__(self, keysequence):
super(StandardKeyMatch, self).__init__()
assert isinstance(keysequence, int)
self.keyseq = keysequence
def get_event_types(self):
return frozenset((QEvent.KeyPress,))
def __call__(self, event):
return event.type() == QEvent.KeyPress and event.matches(self.keyseq)
class MouseEventMatch(EventMatch):
"""Base class for matching mouse events"""
def __init__(self, evt_type, btn, modifiers = Qt.NoModifier):
super(MouseEventMatch, self).__init__()
assert isinstance(modifiers, (int, Qt.KeyboardModifiers))
self.evt_type = evt_type
self.button = btn
self.modifiers = modifiers
def get_event_types(self):
return frozenset((self.evt_type,))
def __call__(self, event):
if event.type() == self.evt_type:
if event.button() == self.button:
if self.modifiers != Qt.NoModifier:
if (event.modifiers() & self.modifiers) == self.modifiers:
return True
elif event.modifiers()==Qt.NoModifier:
return True
return False
def __repr__(self):
return "<MouseMatch: %s/ %08x:%s>" % (evt_type_to_str(self.evt_type),
self.modifiers,
buttons_to_str(self.button))
class MouseMoveMatch(MouseEventMatch):
def __call__(self, event):
if event.type() == self.evt_type:
if ((self.button != Qt.NoButton and
(event.buttons() & self.button == self.button)) or
event.buttons() == Qt.NoButton):
if self.modifiers != Qt.NoModifier:
if (event.modifiers() & self.modifiers) == self.modifiers:
return True
elif event.modifiers() == Qt.NoModifier:
return True
return False
# Machine d'état ----------
class StatefulEventFilter(QObject):
"""Gestion d'une machine d'état pour les événements
d'un canvas
"""
def __init__(self, parent):
super(StatefulEventFilter, self).__init__()
self.states = { 0 : {} } # 0 : cursor 1: panning, 2: zooming
self.cursors = {}
self.state = 0
self.max_state = 0
self.events = {}
self.plot = parent
self.all_event_types = frozenset()
def eventFilter(self, _obj, event):
"""Le callback 'eventfilter' pour Qt"""
if not hasattr(self, "all_event_types"):
print(repr(self), self)
if event.type() not in self.all_event_types:
return False
state = self.states[self.state]
# from pprint import pprint
# print self.state
# pprint(state.keys())
for match, (call_list, next_state) in list(state.items()):
if match(event):
self.set_state(next_state, event)
for call in call_list:
call(self, event) # might change state
return False
def set_state(self, state, event):
"""Change l'état courant.
Peut être appelé par les handlers pour annuler un
changement d'état"""
assert state in self.states
if state == self.state:
return
self.state = state
cursor = self.get_cursor(event)
if cursor is not None:
self.plot.canvas().setCursor(cursor)
def new_state(self):
"""Création (réservation) d'un nouveau numéro d'état"""
self.max_state += 1
self.states[self.max_state] = {}
return self.max_state
def add_event(self, state, match, call, next_state=None):
"""Ajoute une transition sur la machine d'état
si next_state est fourni, il doit correspondre à un état existant
sinon un nouvel état d'arrivée est créé
"""
assert isinstance(state, int)
assert isinstance(match, EventMatch)
self.all_event_types = self.all_event_types.union(match.get_event_types())
entry = self.states[state].setdefault(match, [ [], None ])
entry[0].append(call)
if entry[1] is None:
if next_state is None:
next_state = self.new_state()
else:
pass
else:
if next_state is not None:
assert next_state == entry[1]
else:
next_state = entry[1]
entry[1] = next_state
return next_state
# gestion du curseur
def set_cursor(self, cursor, *states):
"""Associe un curseur à un ou plusieurs états"""
assert isinstance(cursor, CursorShape)
for s in states:
self.cursors[s] = cursor
def get_cursor(self, _event):
"""Récupère le curseur associé à un état / événement donné"""
# on passe event pour eventuellement pouvoir choisir
# le curseur en fonction des touches de modification
cursor = self.cursors.get(self.state, None)
if cursor is None:
# no cursor specified : should keep previous one
return None
return cursor
# Fonction utilitaires
def mouse_press(self, btn, modifiers=Qt.NoModifier):
"""Création d'un filtre pour l'événement MousePress"""
return self.events.setdefault(("mousepress", btn, modifiers),
MouseEventMatch(QEvent.MouseButtonPress,
btn, modifiers))
def mouse_move(self, btn, modifiers=Qt.NoModifier):
"""Création d'un filtre pour l'événement MouseMove"""
return self.events.setdefault(("mousemove", btn, modifiers),
MouseMoveMatch(QEvent.MouseMove,
btn, modifiers))
def mouse_release(self, btn, modifiers=Qt.NoModifier):
"""Création d'un filtre pour l'événement MouseRelease"""
return self.events.setdefault(("mouserelease", btn, modifiers),
MouseEventMatch(QEvent.MouseButtonRelease,
btn, modifiers))
def nothing(self, filter, event):
"""A nothing filter, provided to help removing duplicate handlers"""
pass
class DragHandler(QObject):
"""Classe de base pour les gestionnaires d'événements du type
click - drag - release
"""
cursor = None
def __init__(self, filter, btn, mods=Qt.NoModifier, start_state=0):
super(DragHandler, self).__init__()
self.state0 = filter.add_event(start_state,
filter.mouse_press(btn, mods),
self.start_tracking)
self.state1 = filter.add_event(self.state0,
filter.mouse_move(btn, mods),
self.start_moving)
filter.add_event(self.state1, filter.mouse_move(btn, mods),
self.move, self.state1)
filter.add_event(self.state0, filter.mouse_release(btn, mods),
self.stop_notmoving, start_state)
filter.add_event(self.state1, filter.mouse_release(btn, mods),
self.stop_moving, start_state)
if self.cursor is not None:
filter.set_cursor(self.cursor, self.state0, self.state1)
self.start = None # first mouse position
self.last = None # mouse position seen during last event
self.parent_tracking = None
def get_move_state(self, filter, pos):
rct = filter.plot.contentsRect()
dx = (pos.x(), self.last.x(), self.start.x(), rct.width())
dy = (pos.y(), self.last.y(), self.start.y(), rct.height())
self.last = QPoint(pos)
return dx, dy
def start_tracking(self, _filter, event):
self.start = self.last = QPoint(event.pos())
def start_moving(self, filter, event):
return self.move(filter, event)
def stop_tracking(self, _filter, _event):
pass
#filter.plot.canvas().setMouseTracking(self.parent_tracking)
def stop_notmoving(self, filter, event):
self.stop_tracking(filter, event)
def stop_moving(self, filter, event):
self.stop_tracking(filter, event)
def move(self, filter, event):
raise NotImplementedError()
class ClickHandler(QObject):
"""Classe de base pour les gestionnaires d'événements du type
click - release
"""
def __init__(self, filter, btn, mods=Qt.NoModifier, start_state=0):
super(ClickHandler, self).__init__()
self.state0 = filter.add_event(start_state,
filter.mouse_press(btn, mods),
filter.nothing)
filter.add_event(self.state0, filter.mouse_release(btn, mods),
self.click, start_state)
def click(self, filter, event):
self.emit(SIG_CLICK_EVENT, filter, event)
class PanHandler(DragHandler):
cursor = Qt.ClosedHandCursor
def move(self, filter, event):
x_state, y_state = self.get_move_state(filter, event.pos())
filter.plot.do_pan_view(x_state, y_state)
class ZoomHandler(DragHandler):
cursor = Qt.SizeAllCursor
def move(self, filter, event):
x_state, y_state = self.get_move_state(filter, event.pos())
filter.plot.do_zoom_view(x_state, y_state)
class MenuHandler(ClickHandler):
def click(self, filter, event):
menu = filter.plot.get_context_menu()
if menu:
menu.popup(event.globalPos())
class QtDragHandler(DragHandler):
def start_tracking(self, filter, event):
DragHandler.start_tracking(self, filter, event)
self.emit(SIG_START_TRACKING, filter, event)
def stop_notmoving(self, filter, event):
self.emit(SIG_STOP_NOT_MOVING, filter, event)
def stop_moving(self, filter, event):
self.emit(SIG_STOP_MOVING, filter, event)
def move(self, filter, event):
self.emit(SIG_MOVE, filter, event)
class AutoZoomHandler(ClickHandler):
def click(self, filter, _event):
filter.plot.do_autoscale()
class MoveHandler(object):
def __init__(self, filter, btn=Qt.NoButton, mods=Qt.NoModifier, start_state=0):
filter.add_event(start_state, filter.mouse_move(btn, mods),
self.move, start_state)
def move(self, filter, event):
filter.plot.do_move_marker(event)
class UndoMoveObject(object):
def __init__(self, obj, pos1, pos2):
self.obj = obj
from guiqwt.baseplot import canvas_to_axes
self.coords1 = canvas_to_axes(obj, pos1)
self.coords2 = canvas_to_axes(obj, pos2)
def is_valid(self):
return self.obj.plot() is not None
def compute_positions(self):
from guiqwt.baseplot import axes_to_canvas
pos1 = QPoint(*axes_to_canvas(self.obj, *self.coords1))
pos2 = QPoint(*axes_to_canvas(self.obj, *self.coords2))
return pos1, pos2
def undo(self):
pos1, pos2 = self.compute_positions()
self.obj.plot().unselect_all()
self.obj.move_local_shape(pos1, pos2)
def redo(self):
pos1, pos2 = self.compute_positions()
self.obj.plot().unselect_all()
self.obj.move_local_shape(pos2, pos1)
class UndoMovePoint(UndoMoveObject):
def __init__(self, obj, pos1, pos2, handle, ctrl):
super(UndoMovePoint, self).__init__(obj, pos1, pos2)
self.handle = handle
self.ctrl = ctrl
def undo(self):
pos1, pos2 = self.compute_positions()
self.obj.move_local_point_to(self.handle, pos1, self.ctrl)
def redo(self):
pos1, pos2 = self.compute_positions()
self.obj.move_local_point_to(self.handle, pos2, self.ctrl)
class ObjectHandler(object):
def __init__(self, filter, btn, mods=Qt.NoModifier, start_state=0,
multiselection=False):
self.multiselection = multiselection
self.start_state = start_state
self.state0 = filter.add_event(start_state,
filter.mouse_press(btn, mods),
self.start_tracking)
filter.add_event(self.state0, filter.mouse_move(btn, mods),
self.move, self.state0)
filter.add_event(self.state0, filter.mouse_release(btn, mods),
self.stop_tracking, start_state)
filter.add_event(start_state, StandardKeyMatch(QKeySequence.Undo),
self.undo, start_state)
filter.add_event(start_state, StandardKeyMatch(QKeySequence.Redo),
self.redo, start_state)
self.handle = None # first mouse position
self.inside = False
self._active = None # mouse position seen during last event
self.last_pos = None
self.unselection_pending = None
self.undo_stack = [None]
self.undo_index = 0
self.first_pos = None
self.undo_action = None
@property
def active(self):
if self._active is not None:
return self._active()
@active.setter
def active(self, value):
if value is None:
self._active = None
else:
self._active = weakref.ref(value)
def add_undo_move_action(self, undo_action):
self.undo_stack = self.undo_stack[:self.undo_index+1]
self.undo_stack.append(undo_action)
self.undo_index = len(self.undo_stack) - 1
def undo(self, filter, event):
action = self.undo_stack[self.undo_index]
if action is not None:
if action.is_valid():
action.undo()
filter.plot.replot()
else:
self.undo_stack.remove(action)
self.undo_index -= 1
def redo(self, filter, event):
if self.undo_index < len(self.undo_stack) - 1:
action = self.undo_stack[self.undo_index + 1]
if action.is_valid():
action.redo()
filter.plot.replot()
self.undo_index += 1
else:
self.undo_stack.remove(action)
def start_tracking(self, filter, event):
plot = filter.plot
self.inside = False
self.active = None
self.handle = None
self.first_pos = pos = event.pos()
self.last_pos = QPoint(pos)
selected = plot.get_active_item()
distance = CONF.get("plot", "selection/distance", 6)
(nearest, nearest_dist, nearest_handle,
nearest_inside) = plot.get_nearest_object(pos, distance)
if nearest is not None:
# Is the nearest object the real deal?
if not nearest.can_select() or nearest_dist >= distance:
# Looking for the nearest object in z containing cursor position
(nearest, nearest_dist, nearest_handle,
nearest_inside) = plot.get_nearest_object_in_z(pos)
# This will unselect active item only if it's not moved afterwards:
self.unselection_pending = selected is nearest
if selected and not self.multiselection:
# An item is selected
self.active = selected
(dist, self.handle, self.inside,
other_object) = self.active.hit_test(pos)
if other_object is not None:
# e.g. LegendBoxItem: 'other_object' is the selected curve
plot.set_active_item(other_object)
return
if dist >= distance and not self.inside:
# The following allows to move together selected items by
# clicking inside any of them (instead of active item only)
other_selitems = [_it for _it in plot.get_selected_items()
if _it is not self.active and _it.can_move()]
for selitem in other_selitems:
dist, handle, inside, _other = selitem.hit_test(pos)
if dist < distance or inside:
self.inside = inside
break
else:
self.__unselect_objects(filter)
filter.set_state(self.start_state, event)
return
else:
# No item is selected
self.active = nearest
self.handle = nearest_handle
self.inside = nearest_inside
dist = nearest_dist
if nearest is not None:
plot.set_active_item(nearest)
if not nearest.selected:
if not self.multiselection:
plot.unselect_all()
plot.select_item(nearest)
# Eventually move or resize selected object:
self.__move_or_resize_object(dist, distance, event, filter)
plot.replot()
def __unselect_objects(self, filter):
"""Unselect selected object*s*"""
plot = filter.plot
plot.unselect_all()
plot.replot()
def __move_or_resize_object(self, dist, distance, event, filter):
if dist < distance and self.handle is not None \
and (self.active.can_resize() or self.active.can_rotate()):
# Resize / move handle
self.inside = False
return
if self.inside and self.active.can_move():
# Move object
return
# can't move, can't resize
filter.set_state(self.start_state, event)
if self.unselection_pending:
self.__unselect_objects(filter)
def move(self, filter, event):
if self.active is None:
return
self.unselection_pending = False
if self.inside:
self.active.move_local_shape(self.last_pos, event.pos())
self.undo_action = UndoMoveObject(self.active, event.pos(),
self.first_pos)
else:
ctrl = event.modifiers() & Qt.ControlModifier == Qt.ControlModifier
self.active.move_local_point_to(self.handle, event.pos(), ctrl)
self.undo_action = UndoMovePoint(self.active, self.first_pos,
event.pos(), self.handle, ctrl)
self.last_pos = QPoint(event.pos())
filter.plot.replot()
def stop_tracking(self, filter, event):
self.add_undo_move_action(self.undo_action)
if self.unselection_pending:
self.__unselect_objects(filter)
self.handle = None
self.active = None
class RectangularSelectionHandler(DragHandler):
def __init__(self, filter, btn, mods=Qt.NoModifier, start_state=0):
super(RectangularSelectionHandler, self).__init__(filter, btn, mods,
start_state)
self.avoid_null_shape = False
def set_shape(self, shape, h0, h1,
setup_shape_cb=None, avoid_null_shape=False):
self.shape = shape
self.shape_h0 = h0
self.shape_h1 = h1
self.setup_shape_cb = setup_shape_cb
self.avoid_null_shape = avoid_null_shape
def start_tracking(self, filter, event):
self.start = self.last = QPoint(event.pos())
def start_moving(self, filter, event):
self.shape.attach(filter.plot)
self.shape.setZ(filter.plot.get_max_z()+1)
if self.avoid_null_shape:
self.start -= QPoint(1, 1)
self.shape.move_local_point_to(self.shape_h0, self.start)
self.shape.move_local_point_to(self.shape_h1, event.pos())
self.start_moving_action(filter, event)
if self.setup_shape_cb is not None:
self.setup_shape_cb(self.shape)
self.shape.show()
filter.plot.replot()
def start_moving_action(self, filter, event):
"""Les classes derivees peuvent surcharger cette methode"""
pass
def move(self, filter, event):
self.shape.move_local_point_to(self.shape_h1, event.pos())
self.move_action(filter, event)
filter.plot.replot()
def move_action(self, filter, event):
"""Les classes derivees peuvent surcharger cette methode"""
pass
def stop_moving(self, filter, event):
self.shape.detach()
self.stop_moving_action(filter, event)
filter.plot.replot()
def stop_moving_action(self, filter, event):
"""Les classes derivees peuvent surcharger cette methode"""
self.emit(SIG_END_RECT, filter, self.start, event.pos())
class ZoomRectHandler(RectangularSelectionHandler):
def stop_moving_action(self, filter, event):
filter.plot.do_zoom_rect_view(self.start, event.pos())
def setup_standard_tool_filter(filter, start_state):
"""Création des filtres standard (pan/zoom) sur boutons milieu/droit"""
# Bouton du milieu
PanHandler(filter, Qt.MidButton, start_state=start_state)
AutoZoomHandler(filter, Qt.MidButton, start_state=start_state)
# Bouton droit
ZoomHandler(filter, Qt.RightButton, start_state=start_state)
MenuHandler(filter, Qt.RightButton, start_state=start_state)
# Autres (touches, move)
MoveHandler(filter, start_state=start_state)
MoveHandler(filter, start_state=start_state, mods=Qt.ShiftModifier)
MoveHandler(filter, start_state=start_state, mods=Qt.AltModifier)
return start_state
|