Ejemplo n.º 1
0
    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

        item, handle = HandleFinder(view.hovered_item,
                                    view).get_handle_at_point(
                                        (event.x, event.y))

        # Handle must be the end handle of a connection
        if not handle or not isinstance(
                item, ConnectionView) or handle not in item.end_handles():
            return False

        if handle is item.from_handle():
            self._start_port_v = item.from_port
        else:
            self._start_port_v = item.to_port

        self._parent_state_v = item.parent
        self._end_handle = handle
        if isinstance(item, TransitionView):
            self._is_transition = True
        self._connection_v = item

        return True
Ejemplo n.º 2
0
    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

        item, handle = HandleFinder(view.hovered_item, view).get_handle_at_point((event.x, event.y))

        if not handle:  # Require a handle
            return False

        # Connection handle must belong to a port and the MOVE_PORT_MODIFIER must not be pressed
        if not isinstance(item, StateView) or handle not in [port.handle for port in item.get_all_ports()] or (
                    event.get_state()[1] & constants.MOVE_PORT_MODIFIER):
            return False

        for port in item.get_all_ports():
            if port.handle is handle:
                self._start_port_v = port
                if port in item.get_logic_ports():
                    self._is_transition = True
                if port is item.income or isinstance(port, InputPortView) or port in item.scoped_variables:
                    self._parent_state_v = port.parent
                elif port.parent.parent:
                    self._parent_state_v = port.parent.parent
                else:  # Outgoing port of the root state was clicked on, no connection can be drawn here
                    self._parent_state_v = None

        return True
Ejemplo n.º 3
0
    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.
        """
        view = self.view

        item, handle = HandleFinder(view.hovered_item,
                                    view).get_handle_at_point(
                                        event.get_coords()[1:])

        if handle:
            # Deselect all items unless CTRL or SHIFT is pressed
            # or the item is already selected.
            ### TODO: duplicate from ItemTool
            if not (
                    event.get_state()[1]
                    &
                (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK)
                    or view.hovered_item in view.selected_items):
                del view.selected_items
            ###/
            view.hovered_item = item
            view.focused_item = item

            self.motion_handle = None

            self.grab_handle(item, handle)

            return True
Ejemplo n.º 4
0
    def on_button_press(self, event):
        """
		Returns a True in case of a click on a Line Handle. This allows the
		on_button_release of this tool to be triggered. This provides a
		mechanism to rejoin a stray line handle previously disconnected.
		"""

        self.h_finder = HandleFinder(None, self.view)
        try:
            handle = self.h_finder.get_handle_at_point((event.x, event.y))
            if type(handle[0]) == BlockLine and handle[1]:
                line = handle[0]
                ports = line.get_connected_ports()
                connectedport = ports[0] or ports[1]
                self.toggle_highlight_ports(connectedport)
                return True
        except Exception as e:
            print 'Connection Failed, Disconnect/Connect the last Connection again: /n', e
        finally:
            return super(BlockConnectTool, self).on_button_press(event)
Ejemplo n.º 5
0
	def on_button_press(self,event):
		"""
		Returns a True in case of a click on a Line Handle. This allows the
		on_button_release of this tool to be triggered. This provides a
		mechanism to rejoin a stray line handle previously disconnected.
		"""

		self.h_finder = HandleFinder(None,self.view)
		try:
			handle = self.h_finder.get_handle_at_point((event.x,event.y))
			if type(handle[0])==BlockLine and handle[1]:
				line = handle[0]
				ports = line.get_connected_ports()
				connectedport = ports[0] or ports[1]
				self.toggle_highlight_ports(connectedport)
				return True
		except Exception as e:
			print 'Connection Failed, Disconnect/Connect the last Connection again: /n',e
		finally:
			return super(BlockConnectTool,self).on_button_press(event)
Ejemplo n.º 6
0
    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

        if isinstance(view.hovered_item, StateView):
            distance = view.hovered_item.border_width / 2.
            item, handle = HandleFinder(view.hovered_item,
                                        view).get_handle_at_point(
                                            (event.x, event.y), distance)
        else:
            item, handle = HandleFinder(view.hovered_item,
                                        view).get_handle_at_point(
                                            (event.x, event.y))

        if not handle:
            return False

        # Only move ports when the MOVE_PORT_MODIFIER key is pressed
        if isinstance(item, (StateView, PortView)) and \
            handle in [port.handle for port in item.get_all_ports()] and \
                not (event.get_state()[1] & constants.MOVE_PORT_MODIFIER):
            return False

        # Do not move from/to handles of connections (only their waypoints)
        if isinstance(item, ConnectionView) and handle in item.end_handles(
                include_waypoints=True):
            return False

        if handle:
            view.hovered_item = item

            self.motion_handle = None

            self.grab_handle(item, handle)

            return True
Ejemplo n.º 7
0
    def on_motion_notify(self, event):
        from gaphas.tool import HandleFinder
        from gaphas.view import DEFAULT_CURSOR
        from gaphas.aspect import ElementHandleSelection

        view = self.view
        hovered_items = view.get_items_at_point((event.x, event.y), distance=3)
        hovered_items = self._filter_hovered_items(hovered_items, event)

        view.hovered_item = hovered_items[0] if hovered_items else None

        if view.hovered_handle:
            handle = view.hovered_handle
            view.hovered_handle = None
            port_v = self._prev_hovered_item.get_port_for_handle(handle)
            view.queue_draw_area(*port_v.get_port_area(view))
        pos = event.x, event.y

        # Reset cursor
        self.view.get_window().set_cursor(Gdk.Cursor.new(DEFAULT_CURSOR))

        def handle_hover_of_port(hovered_handle):
            view.hovered_handle = hovered_handle
            port_v = state_v.get_port_for_handle(hovered_handle)
            view.queue_draw_area(*port_v.get_port_area(view))
            if event.get_state()[1] & constants.MOVE_PORT_MODIFIER:
                self.view.get_window().set_cursor(
                    Gdk.Cursor.new(constants.MOVE_CURSOR))
            else:
                self.view.get_window().set_cursor(
                    Gdk.Cursor.new(constants.CREATION_CURSOR))

        if isinstance(view.hovered_item, PortView):
            if event.get_state()[1] & constants.MOVE_PORT_MODIFIER:
                self.view.get_window().set_cursor(
                    Gdk.Cursor.new(constants.MOVE_CURSOR))
            else:
                self.view.get_window().set_cursor(
                    Gdk.Cursor.new(constants.CREATION_CURSOR))

        elif isinstance(view.hovered_item, StateView):
            distance = view.hovered_item.border_width / 2.
            state_v, hovered_handle = HandleFinder(view.hovered_item,
                                                   view).get_handle_at_point(
                                                       pos, distance)

            # Hover over port => show hover state of port and different cursor
            if hovered_handle and hovered_handle not in state_v.corner_handles:
                handle_hover_of_port(hovered_handle)

            # Hover over corner/resize handles => show with cursor
            elif hovered_handle and hovered_handle in state_v.corner_handles:
                cursors = ElementHandleSelection.CURSORS
                index = state_v.handles().index(hovered_handle)
                display = self.view.get_display()
                self.view.get_window().set_cursor(
                    Gdk.Cursor.new_from_name(display, cursors[index]))

        # NameView should only be hovered, if its state is selected
        elif isinstance(view.hovered_item, NameView):
            state_v = self.view.canvas.get_parent(view.hovered_item)
            distance = state_v.border_width / 2.
            _, hovered_handle = HandleFinder(state_v,
                                             view).get_handle_at_point(
                                                 pos, distance)

            # Hover over port => show hover state of port and different cursor
            if hovered_handle and hovered_handle not in state_v.corner_handles:
                view.hovered_item = state_v
                handle_hover_of_port(hovered_handle)
            else:
                name_v, hovered_handle = HandleFinder(
                    view.hovered_item, view).get_handle_at_point(pos)
                # Hover over corner/resize handles => show with cursor
                if name_v:
                    cursors = ElementHandleSelection.CURSORS
                    index = name_v.handles().index(hovered_handle)
                    display = self.view.get_display()
                    self.view.get_window().set_cursor(
                        Gdk.Cursor.new_from_name(display, cursors[index]))

        # 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
Ejemplo n.º 8
0
class BlockConnectTool(ConnectHandleTool):
	"""
	This class modifies the ConnectHandleTool for Drag and Connect support.
	The `ConnectorTool` will also be making use of this class.
	"""
	def on_button_press(self,event):
		"""
		Returns a True in case of a click on a Line Handle. This allows the
		on_button_release of this tool to be triggered. This provides a
		mechanism to rejoin a stray line handle previously disconnected.
		"""

		self.h_finder = HandleFinder(None,self.view)
		try:
			handle = self.h_finder.get_handle_at_point((event.x,event.y))
			if type(handle[0])==BlockLine and handle[1]:
				line = handle[0]
				ports = line.get_connected_ports()
				connectedport = ports[0] or ports[1]
				self.toggle_highlight_ports(connectedport)
				return True
		except Exception as e:
			print 'Connection Failed, Disconnect/Connect the last Connection again: /n',e
		finally:
			return super(BlockConnectTool,self).on_button_press(event)


	def on_button_release(self,event):
		"""
		This handles and 'drag' part of any connection, after the
		on_button_press  of this tool returns a True (i.e by clicking on an
		Line Handle) the on_button_release is triggered on mouse button release.
		Note that this handles both the new connections and modified
		connections.
		Hint:
		ConnectorTool is a child of this class.
		Uncommenting all the print statements should give a fair idea how this
		tool works.
		"""
		line = self.grabbed_item
		handle = self.grabbed_handle
		glueitem,glueport,gluepos = self.view.get_port_at_point((event.x,event.y),distance = 10,exclude = [line])
		line = self.grabbed_item
		handle = self.grabbed_handle
		try:
			if glueport and hasattr(glueport,"point"):
				#print 'READY_CONNECTION_CHECK_PASSED_1'
				conn = Connector(line,handle)
				sink = ConnectionSink(glueitem,glueport)
				conn.connect_port(sink)
				self.ungrab_handle()
				#print 'READY_CONNECTION_CHECK_PASSED_2'
		except Exception as e:
			print 'Connection Failed, Disconnect/Connect the last Connection again: /n',e
		finally:
			self.toggle_highlight_ports()
			return super(BlockConnectTool, self).on_button_release(event)

	def toggle_highlight_ports(self,portinstance=None):
		global SET_CONNECTION_FLAG
		if SET_CONNECTION_FLAG[0]:
			SET_CONNECTION_FLAG[0] = False
			SET_CONNECTION_FLAG[1] = None
		elif portinstance:
			SET_CONNECTION_FLAG[0] = True
			SET_CONNECTION_FLAG[1] = portinstance
		#request for an update to draw the ports again, coloured
		self.view.queue_draw_refresh()
Ejemplo n.º 9
0
class BlockConnectTool(ConnectHandleTool):
    """
	This class modifies the ConnectHandleTool for Drag and Connect support.
	The `ConnectorTool` will also be making use of this class.
	"""
    def on_button_press(self, event):
        """
		Returns a True in case of a click on a Line Handle. This allows the
		on_button_release of this tool to be triggered. This provides a
		mechanism to rejoin a stray line handle previously disconnected.
		"""

        self.h_finder = HandleFinder(None, self.view)
        try:
            handle = self.h_finder.get_handle_at_point((event.x, event.y))
            if type(handle[0]) == BlockLine and handle[1]:
                line = handle[0]
                ports = line.get_connected_ports()
                connectedport = ports[0] or ports[1]
                self.toggle_highlight_ports(connectedport)
                return True
        except Exception as e:
            print 'Connection Failed, Disconnect/Connect the last Connection again: /n', e
        finally:
            return super(BlockConnectTool, self).on_button_press(event)

    def on_button_release(self, event):
        """
		This handles and 'drag' part of any connection, after the
		on_button_press  of this tool returns a True (i.e by clicking on an
		Line Handle) the on_button_release is triggered on mouse button release.
		Note that this handles both the new connections and modified
		connections.
		Hint:
		ConnectorTool is a child of this class.
		Uncommenting all the print statements should give a fair idea how this
		tool works.
		"""
        line = self.grabbed_item
        handle = self.grabbed_handle
        glueitem, glueport, gluepos = self.view.get_port_at_point(
            (event.x, event.y), distance=10, exclude=[line])
        line = self.grabbed_item
        handle = self.grabbed_handle
        try:
            if glueport and hasattr(glueport, "point"):
                #print 'READY_CONNECTION_CHECK_PASSED_1'
                conn = Connector(line, handle)
                sink = ConnectionSink(glueitem, glueport)
                conn.connect_port(sink)
                self.ungrab_handle()
                #print 'READY_CONNECTION_CHECK_PASSED_2'
        except Exception as e:
            print 'Connection Failed, Disconnect/Connect the last Connection again: /n', e
        finally:
            self.toggle_highlight_ports()
            return super(BlockConnectTool, self).on_button_release(event)

    def toggle_highlight_ports(self, portinstance=None):
        global SET_CONNECTION_FLAG
        if SET_CONNECTION_FLAG[0]:
            SET_CONNECTION_FLAG[0] = False
            SET_CONNECTION_FLAG[1] = None
        elif portinstance:
            SET_CONNECTION_FLAG[0] = True
            SET_CONNECTION_FLAG[1] = portinstance
        #request for an update to draw the ports again, coloured
        self.view.queue_draw_refresh()