Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Change mouse cursor to indicate option to move connection
elif isinstance(view.hovered_item, ConnectionView):
# If a handle is connection handle is hovered, show the move cursor
item, handle = HandleFinder(view.hovered_item, view).get_handle_at_point(pos, split=False)
if handle:
self.view.get_window().set_cursor(Gdk.Cursor.new(constants.MOVE_CURSOR))
# If no handle is hovered, indicate the option for selection with the selection cursor
else:
self.view.get_window().set_cursor(Gdk.Cursor.new(constants.SELECT_CURSOR))
if isinstance(self.view.hovered_item, StateView):
self._prev_hovered_item = self.view.hovered_item
class MultiSelectionTool(gaphas.tool.RubberbandTool):
def on_button_press(self, event):
if event.get_state()[1] & constants.RUBBERBAND_MODIFIER:
return super(MultiSelectionTool, self).on_button_press(event)
return False
def on_motion_notify(self, event):
if event.get_state()[1] & Gdk.EventMask.BUTTON_PRESS_MASK and event.get_state()[1] & \
constants.RUBBERBAND_MODIFIER:
view = self.view
self.queue_draw(view)
self.x1, self.y1 = event.x, event.y
self.queue_draw(view)
return True
def on_button_release(self, event):
"""Select or deselect rubber banded groups of items
rt = super(ToolChain, self).handle(event)
if suppressed_grabbed_tool:
self._grabbed_tool = suppressed_grabbed_tool
return rt
def ungrab(self, tool):
"""Fixes parental ungrab method for the case `tool` is None"""
if tool is None:
return
super(ToolChain, self).ungrab(tool)
class PanTool(gaphas.tool.PanTool):
def __init__(self, view=None):
super(PanTool, self).__init__(view)
self.zoom_with_control = global_gui_config.get_config_value("ZOOM_WITH_CTRL", False)
def on_scroll(self, event):
ctrl_key_pressed = bool(event.get_state()[1] & Gdk.ModifierType.CONTROL_MASK)
if (self.zoom_with_control and ctrl_key_pressed) or (not self.zoom_with_control and not ctrl_key_pressed):
return False
return super(PanTool, self).on_scroll(event)
class ZoomTool(gaphas.tool.ZoomTool):
def __init__(self, view=None):
super(ZoomTool, self).__init__(view)
self.zoom_with_control = global_gui_config.get_config_value("ZOOM_WITH_CTRL", False)
rt = super(ToolChain, self).handle(event)
if suppressed_grabbed_tool:
self._grabbed_tool = suppressed_grabbed_tool
return rt
def ungrab(self, tool):
"""Fixes parental ungrab method for the case `tool` is None"""
if tool is None:
return
super(ToolChain, self).ungrab(tool)
class PanTool(gaphas.tool.PanTool):
def __init__(self, view=None):
super(PanTool, self).__init__(view)
self.zoom_with_control = global_gui_config.get_config_value("ZOOM_WITH_CTRL", False)
def on_scroll(self, event):
ctrl_key_pressed = bool(event.get_state()[1] & Gdk.ModifierType.CONTROL_MASK)
if (self.zoom_with_control and ctrl_key_pressed) or (not self.zoom_with_control and not ctrl_key_pressed):
return False
return super(PanTool, self).on_scroll(event)
class ZoomTool(gaphas.tool.ZoomTool):
def __init__(self, view=None):
super(ZoomTool, self).__init__(view)
self.zoom_with_control = global_gui_config.get_config_value("ZOOM_WITH_CTRL", False)
connection_core_element.modify_origin(port_state_id, port_id)
except ValueError as e:
self._reset_connection()
logger.error(e)
self.view.canvas.update_now()
super(ConnectionModificationTool, self).on_button_release(event)
self._end_handle = None
def _reset_connection(self):
modify_target = self._end_handle is self._connection_v.to_handle()
self._start_port_v.parent.connect_connection_to_port(self._connection_v, self._start_port_v,
as_target=modify_target)
self._redraw_port(self._start_port_v)
class RightClickTool(gaphas.tool.ItemTool):
def __init__(self, view=None, buttons=(3,)):
super(RightClickTool, self).__init__(view, buttons)
# TODO correct destruction of the StateRightClickMenu-Controller
self.sm_right_click_menu = StateRightClickMenuGaphas()
def on_button_press(self, event):
self.sm_right_click_menu.mouse_click(None, event)
def on_button_release(self, event):
"""Select or deselect rubber banded groups of items
The selection of elements is prior and never items are selected or deselected at the same time.
"""
self.queue_draw(self.view)
x0, y0, x1, y1 = self.x0, self.y0, self.x1, self.y1
rectangle = (min(x0, x1), min(y0, y1), abs(x1 - x0), abs(y1 - y0))
selected_items = self.view.get_items_in_rectangle(rectangle, intersect=False)
self.view.handle_new_selection(selected_items)
return True
class MoveHandleTool(gaphas.tool.HandleTool):
"""Tool to move handles around
Handles can be moved using click'n'drag. This is already implemented in the base class `HandleTool`. This class
extends the behaviour by requiring a modifier key to be pressed when moving ports. It also allows to change the
modifier key, which are defined in `rafcon.gui.utils.constants`.
"""
def on_button_press(self, event):
"""Handle button press events.
If the (mouse) button is pressed on top of a Handle (item.Handle), that handle is grabbed and can be
dragged around.
"""
if not event.get_button()[1] == 1: # left mouse button
return False
view = self.view
from rafcon.gui.mygaphas.items.connection import ConnectionView, TransitionPlaceholderView, DataFlowPlaceholderView, \
TransitionView
from rafcon.gui.mygaphas.items.ports import InputPortView, PortView
from rafcon.gui.mygaphas.items.state import StateView, NameView
from rafcon.gui.mygaphas.utils import gap_helper
from rafcon.gui.utils import constants
from rafcon.utils import log
from rafcon.utils.decorators import avoid_parallel_execution
from rafcon.gui.config import global_gui_config
logger = log.get_logger(__name__)
PortMoved = Enum('PORT', 'FROM TO')
class ToolChain(gaphas.tool.ToolChain):
def handle(self, event):
"""
Handle the event by calling each tool until the event is handled
or grabbed.
If a tool is returning True on a button press event, the motion and
button release events are also passed to this
"""
# Allow to handle a subset of events while having a grabbed tool (between a button press & release event)
suppressed_grabbed_tool = None
if event.type in (Gdk.EventType.SCROLL, Gdk.EventType.KEY_PRESS, Gdk.EventType.KEY_RELEASE):
suppressed_grabbed_tool = self._grabbed_tool
self._grabbed_tool = None
rt = super(ToolChain, self).handle(event)
def on_button_release(self, event):
"""Select or deselect rubber banded groups of items
The selection of elements is prior and never items are selected or deselected at the same time.
"""
self.queue_draw(self.view)
x0, y0, x1, y1 = self.x0, self.y0, self.x1, self.y1
rectangle = (min(x0, x1), min(y0, y1), abs(x1 - x0), abs(y1 - y0))
selected_items = self.view.get_items_in_rectangle(rectangle, intersect=False)
self.view.handle_new_selection(selected_items)
return True
class MoveHandleTool(gaphas.tool.HandleTool):
"""Tool to move handles around
Handles can be moved using click'n'drag. This is already implemented in the base class `HandleTool`. This class
extends the behaviour by requiring a modifier key to be pressed when moving ports. It also allows to change the
modifier key, which are defined in `rafcon.gui.utils.constants`.
"""
def on_button_press(self, event):
"""Handle button press events.
If the (mouse) button is pressed on top of a Handle (item.Handle), that handle is grabbed and can be
dragged around.
"""
if not event.get_button()[1] == 1: # left mouse button
return False
view = self.view
sx = view._matrix[0]
sy = view._matrix[3]
ox = (view._matrix[4] - event_coords[0]) / sx
oy = (view._matrix[5] - event_coords[1]) / sy
factor = 0.9
if event.get_scroll_direction()[1] == Gdk.ScrollDirection.UP:
factor = 1. / factor
view._matrix.translate(-ox, -oy)
view._matrix.scale(factor, factor)
view._matrix.translate(+ox, +oy)
# Make sure everything's updated
view.request_update((), view._canvas.get_all_items())
return True
class MoveItemTool(gaphas.tool.ItemTool):
"""This class is responsible for moving states, names, connections, etc.
"""
def __init__(self, view=None, buttons=(1,)):
super(MoveItemTool, self).__init__(view, buttons)
self._item = None
self._move_name_v = False
self._old_selection = None
def movable_items(self):
"""Filter selection
Filter items of selection that cannot be moved (i.e. are not instances of `Item`) and return the rest.
"""
view = self.view
gap_helper.update_meta_data_for_port(graphical_editor, item, self.grabbed_handle)
else:
gap_helper.update_meta_data_for_state_view(graphical_editor, item, affects_children=True,
publish=True)
# The handle was not moved. Check if the handle is to be selected.
else:
# Only handles belonging to a state (i.e. port handles) can be selected
if isinstance(item, StateView):
corresponding_ports = [port for port in item.get_all_ports() if port.handle is self.grabbed_handle]
if corresponding_ports: # should be exactly one
self.view.handle_new_selection(corresponding_ports[0])
super(MoveHandleTool, self).on_button_release(event)
class ConnectionTool(gaphas.tool.ConnectHandleTool):
def __init__(self):
super(ConnectionTool, self).__init__()
self._connection_v = None
self._start_port_v = None
self._parent_state_v = None
self._is_transition = False
self._current_sink = None
def on_button_release(self, event):
self._is_transition = False
self._connection_v = None
self._start_port_v = None
self._parent_state_v = None
self._current_sink = None
self.grabbed_item = None
# selected, there are two cases to be considered:
# 1. extend-selection-modifier is clicked: we need to remove the state from the selection
# 2. extend-selection-modifier is not clicked: we need to remove all other states from the selection
from rafcon.gui.models.selection import extend_selection
if self._item in self._old_selection:
if extend_selection():
self.view.unselect_item(self._item)
else:
map(self.view.unselect_item, [view for view in self._old_selection if view is not self._item])
self._move_name_v = False
self._old_selection = None
return super(MoveItemTool, self).on_button_release(event)
class HoverItemTool(gaphas.tool.HoverTool):
def __init__(self, view=None):
super(HoverItemTool, self).__init__(view)
self._prev_hovered_item = None
@staticmethod
def dismiss_upper_items(items, item):
try:
return items[items.index(item):]
except ValueError:
return []
def _filter_library_state(self, items):
"""Filters out child elements of library state when they cannot be hovered
Checks if hovered item is within a LibraryState
* if not, the list is returned unfiltered