Gaphas extensions for RAFCON: rafcon.gui.mygaphas

Gaphas is a Python library for state-machine editors using Canvas: https://github.com/gaphor/gaphas

It is used here as library for the graphical editor as replacement for OpenGL. This modules contains all extensions necessary for RAFCON.

Views

Views are derived from gaphas.item and are the visual representations for core elements/models.

Perpendicular Line

The base class for the rafcon.gui.mygaphas.items.connection.ConnectionView.

class rafcon.gui.mygaphas.items.line.PerpLine(hierarchy_level)

Bases: gaphas.item.Line

add_perp_waypoint(pos=(0, 0), begin=True)
add_waypoint(pos)
draw(context)

Draw the line itself. See Item.draw(context).

draw_head(context, port)

Default head drawer: move cursor to the first handle.

draw_tail(context, port)

Default tail drawer: draw line to the last handle.

end_handles(include_waypoints=False)
from_handle()
property from_port
get_parent_state_v()
static is_in_port(port)
static is_out_port(port)
property name
property parent
point(pos)
>>> a = Line()
>>> a.handles()[1].pos = 25, 5
>>> a._handles.append(a._create_handle((30, 30)))
>>> a.point((-1, 0))
1.0
>>> f"{a.point((5, 4)):.3f}
'2.942'
>>> f"{a.point((29, 29)):.3f}
'0.784'
remove()
remove_all_waypoints()
reset_from_port()
reset_to_port()
to_handle()
property to_port
property view
property waypoints

ConnectionViews

class rafcon.gui.mygaphas.items.connection.ConnectionPlaceholderView(hierarchy_level)

Bases: rafcon.gui.mygaphas.items.connection.ConnectionView

class rafcon.gui.mygaphas.items.connection.ConnectionView(hierarchy_level)

Bases: rafcon.gui.mygaphas.items.line.PerpLine

apply_meta_data()
remove()
remove_connection_from_ports()
reset_port_for_handle(handle)
set_port_for_handle(port, handle)
class rafcon.gui.mygaphas.items.connection.DataFlowPlaceholderView(hierarchy_level)

Bases: rafcon.gui.mygaphas.items.connection.ConnectionPlaceholderView

class rafcon.gui.mygaphas.items.connection.DataFlowView(data_flow_m, hierarchy_level)

Bases: rafcon.gui.mygaphas.items.connection.ConnectionView

draw(context)

Draw the line itself. See Item.draw(context).

property model
property show_connection
class rafcon.gui.mygaphas.items.connection.TransitionPlaceholderView(hierarchy_level)

Bases: rafcon.gui.mygaphas.items.connection.ConnectionPlaceholderView

class rafcon.gui.mygaphas.items.connection.TransitionView(transition_m, hierarchy_level)

Bases: rafcon.gui.mygaphas.items.connection.ConnectionView

draw(context)

Draw the line itself. See Item.draw(context).

property model
property show_connection

PortViews

Each port (income, outcome, data ports) are represented as a view element.

class rafcon.gui.mygaphas.items.ports.DataPortView(in_port, parent, port_m, side)

Bases: rafcon.gui.mygaphas.items.ports.PortView

draw(context, state)
has_label()
property model
property name
property port_id
class rafcon.gui.mygaphas.items.ports.IncomeView(income_m, parent)

Bases: rafcon.gui.mygaphas.items.ports.LogicPortView

draw(context, state, highlight=False)
property model
class rafcon.gui.mygaphas.items.ports.InputPortView(parent, port_m)

Bases: rafcon.gui.mygaphas.items.ports.DataPortView

draw(context, state)
class rafcon.gui.mygaphas.items.ports.LogicPortView(in_port, name=None, parent=None, side=SnappedSide.RIGHT)

Bases: rafcon.gui.mygaphas.items.ports.PortView

Base class for ports connecting transitions

A logic port is either a income our an outcome.

draw(context, state, highlight)
class rafcon.gui.mygaphas.items.ports.OutcomeView(outcome_m, parent)

Bases: rafcon.gui.mygaphas.items.ports.LogicPortView

draw(context, state, highlight=False)
has_label()
property model
property name
property outcome_id
class rafcon.gui.mygaphas.items.ports.OutputPortView(parent, port_m)

Bases: rafcon.gui.mygaphas.items.ports.DataPortView

draw(context, state)
class rafcon.gui.mygaphas.items.ports.PortView(in_port, name=None, parent=None, side=SnappedSide.RIGHT)

Bases: object

add_connected_handle(handle, connection_view, moving=False)
property connected
property connected_connections
property connected_incoming
property connected_outgoing
draw(context, state)
draw_name(context, transparency, value)
draw_port(context, fill_color, transparency, value=None)
get_port_area(view)

Calculates the drawing area affected by the (hovered) port

property handle_pos
handles()
has_label()
has_outgoing_connection()
is_selected()
property name
property parent
property port_side_size
property port_size
property pos
remove_connected_handle(handle)
property side
tmp_connect(handle, connection_view)
tmp_disconnect()
property view
class rafcon.gui.mygaphas.items.ports.ScopedVariablePortView(parent, scoped_variable_m)

Bases: rafcon.gui.mygaphas.items.ports.PortView

draw(context, state)
draw_name(context, transparency, only_calculate_size=False)

Draws the name of the port

Offers the option to only calculate the size of the name.

Parameters
  • context – The context to draw on

  • transparency – The transparency of the text

  • only_calculate_size – Whether to only calculate the size

Returns

Size of the name

Return type

float, float

property model
property name
property port_id
property port_size

StateView and NameView

Each rafcon.gui.mygaphas.items.state.StateView holds a child item rafcon.gui.mygaphas.items.state.NameView, as the name of a state can be resized and repositioned.

class rafcon.gui.mygaphas.items.state.NameView(name, size)

Bases: gaphas.item.Element

apply_meta_data()
draw(context)

Render the item to a canvas view. Context contains the following attributes:

  • cairo: the Cairo Context use this one to draw

  • view: the view that is to be rendered to

  • selected, focused, hovered, dropzone: view state of items (True/False)

  • draw_all: a request to draw everything, for bounding box calculations

property model
property name
property parent
property position
remove()
property transparency
update_minimum_size()
property view
class rafcon.gui.mygaphas.items.state.StateView(state_m, size, background_color, hierarchy_level)

Bases: gaphas.item.Element

A State has 4 handles (for a start): NW +—+ NE SW +—+ SE

add_income(income_m)
add_input_port(port_m)
static add_keep_rect_within_constraint(canvas, parent, child)
add_outcome(outcome_m)
add_output_port(port_m)
add_rect_constraint_for_port(port)
add_scoped_variable(scoped_variable_m)
apply_meta_data(recursive=False)
property border_width
child_state_views()
connect_connection_to_port(connection_v, port, as_target=True)
connect_to_income(connection_v, handle)
connect_to_input_port(port_id, connection_v, handle)
connect_to_outcome(outcome_id, connection_v, handle)
connect_to_output_port(port_id, connection_v, handle)
connect_to_scoped_variable_port(scoped_variable_id, connection_v, handle)
property corner_handles
draw(context)

Render the item to a canvas view. Context contains the following attributes:

  • cairo: the Cairo Context use this one to draw

  • view: the view that is to be rendered to

  • selected, focused, hovered, dropzone: view state of items (True/False)

  • draw_all: a request to draw everything, for bounding box calculations

get_all_ports()
get_logic_ports()
get_port_for_handle(handle)
static get_state_drawing_area(state)
get_transitions()
property hovered
property income
input_port(port_id)
property inputs
property model
property moving
property name_view
outcome_port(outcome_id)
property outcomes
output_port(port_id)
property outputs
property parent
property position
remove()

Remove recursively all children and then the StateView itself

remove_income()
remove_input_port(input_port_v)
remove_keep_rect_within_constraint_from_parent()
remove_outcome(outcome_v)
remove_output_port(output_port_v)
remove_scoped_variable(scoped_variable_port_v)
resize_all_children(old_size, paste=False)
scoped_variable(scoped_variable_id)
property scoped_variables
property selected
set_enable_flag_keep_rect_within_constraints(enable)

Enable/disables the KeepRectangleWithinConstraint for child states

setup_canvas()

Called when the canvas is set for the item. This method can be used to create constraints.

show_content(with_content=False)

Checks if the state is a library with the show_content flag set

Parameters

with_content – If this parameter is True, the method return only True if the library represents a ContainerState

Returns

Whether the content of a library state is shown

property show_data_port_label
property transparency

Calculates the transparency for the state

Returns

State transparency

Return type

float

update_minimum_size()
update_minimum_size_of_children()
property view

Utility functions

Enumerations

rafcon.gui.mygaphas.utils.enums.Direction

alias of rafcon.gui.mygaphas.utils.enums.DIRECTION

class rafcon.gui.mygaphas.utils.enums.SnappedSide(value)

Bases: enum.Enum

An enumeration.

BOTTOM = 4
LEFT = 1
RIGHT = 3
TOP = 2
next()
opposite()
prev()

Helper methods for drawing operations

rafcon.gui.mygaphas.utils.gap_draw_helper.draw_label_path(context, width, height, arrow_height, distance_to_port, port_offset)

Draws the path for an upright label

Parameters
  • context – The Cairo context

  • width (float) – Width of the label

  • height (float) – Height of the label

  • distance_to_port (float) – Distance to the port related to the label

  • port_offset (float) – Distance from the port center to its border

rafcon.gui.mygaphas.utils.gap_draw_helper.draw_port_label(context, port, transparency, fill, label_position, show_additional_value=False, additional_value=None, only_extent_calculations=False)

Draws a normal label indicating the port name.

Parameters
  • context – Draw Context

  • port – The PortView

  • transparency – Transparency of the text

  • fill – Whether the label should be filled or not

  • label_position – Side on which the label should be drawn

  • show_additional_value – Whether to show an additional value (for data ports)

  • additional_value – The additional value to be shown

  • only_extent_calculations – Calculate only the extends and do not actually draw

rafcon.gui.mygaphas.utils.gap_draw_helper.get_col_rgba(color, transparency=None, opacity=None)

This class converts a Gdk.Color into its r, g, b parts and adds an alpha according to needs

If both transparency and opacity is None, alpha is set to 1 => opaque

Parameters
  • color (Gdk.Color) – Color to extract r, g and b from

  • transparency (float | None) – Value between 0 (opaque) and 1 (transparent) or None if opacity is to be used

  • opacity (float | None) – Value between 0 (transparent) and 1 (opaque) or None if transparency is to be used

Returns

Red, Green, Blue and Alpha value (all between 0.0 - 1.0)

rafcon.gui.mygaphas.utils.gap_draw_helper.get_side_length_of_resize_handle(view, item)

Calculate the side length of a resize handle

Parameters
Returns

side length

Return type

float

rafcon.gui.mygaphas.utils.gap_draw_helper.get_text_layout(cairo_context, text, size)
rafcon.gui.mygaphas.utils.gap_draw_helper.limit_value_string_length(value)

This method limits the string representation of the value to MAX_VALUE_LABEL_TEXT_LENGTH + 3 characters.

Parameters

value – Value to limit string representation

Returns

String holding the value with a maximum length of MAX_VALUE_LABEL_TEXT_LENGTH + 3

General helper methods

rafcon.gui.mygaphas.utils.gap_helper.add_data_flow_to_state(from_port_m, to_port_m, add_data_port=False)

Interface method between Gaphas and RAFCON core for adding data flows

The method checks the types of the given ports and their relation. From this the necessary parameters for the add_dat_flow method of the RAFCON core are determined. Also the parent state is derived from the ports.

Parameters
  • from_port_m – Port model from which the data flow starts

  • to_port_m – Port model to which the data flow goes to

  • add_data_port – Boolean add the data port automatically

Returns

True if a data flow was added, False if an error occurred

rafcon.gui.mygaphas.utils.gap_helper.add_transition_to_state(from_port_m, to_port_m)

Interface method between Gaphas and RAFCON core for adding transitions

The method checks the types of the given ports (IncomeView or OutcomeView) and from this determines the necessary parameters for the add_transition method of the RAFCON core. Also the parent state is derived from the ports.

Parameters
  • from_port_m – Port model from which the transition starts

  • to_port_m – Port model to which the transition goes to

Returns

True if a transition was added, False if an error occurred

rafcon.gui.mygaphas.utils.gap_helper.calc_rel_pos_to_parent(canvas, item, handle)

This method calculates the relative position of the given item’s handle to its parent

Parameters
  • canvas – Canvas to find relative position in

  • item – Item to find relative position to parent

  • handle – Handle of item to find relative position to

Returns

Relative position (x, y)

rafcon.gui.mygaphas.utils.gap_helper.create_new_connection(from_port_m, to_port_m)

Checks the type of connection and tries to create it

If bot port are logical port,s a transition is created. If both ports are data ports (including scoped variable), then a data flow is added. An error log is created, when the types are not compatible.

Parameters
  • from_port_m – The starting port model of the connection

  • to_port_m – The end port model of the connection

Returns

True if a new connection was added

rafcon.gui.mygaphas.utils.gap_helper.extend_extents(extents, factor=1.1)

Extend a given bounding box

The bounding box (x1, y1, x2, y2) is centrally stretched by the given factor.

Parameters
  • extents – The bound box extents

  • factor – The factor for stretching

Returns

(x1, y1, x2, y2) of the extended bounding box

rafcon.gui.mygaphas.utils.gap_helper.get_port_for_handle(handle, state)

Looks for and returns the PortView to the given handle in the provided state

Parameters
  • handle – Handle to look for port

  • state – State containing handle and port

Returns

PortView for handle

rafcon.gui.mygaphas.utils.gap_helper.get_relative_positions_of_waypoints(transition_v)

This method takes the waypoints of a connection and returns all relative positions of these waypoints.

Parameters

transition_v – Transition view to extract all relative waypoint positions

Returns

List with all relative positions of the given transition

rafcon.gui.mygaphas.utils.gap_helper.update_meta_data_for_connection_waypoints(graphical_editor_view, connection_v, last_waypoint_list, publish=True)

This method updates the relative position metadata of the connections waypoints if they changed

Parameters
  • graphical_editor_view – Graphical Editor the change occurred in

  • connection_v – Transition/Data Flow that changed

  • last_waypoint_list – List of waypoints before change

  • publish (bool) – Whether to publish the changes using the meta signal

rafcon.gui.mygaphas.utils.gap_helper.update_meta_data_for_name_view(graphical_editor_view, name_v, publish=True)

This method updates the metadata of a name view.

Parameters
  • graphical_editor_view – Graphical Editor view the change occurred in

  • name_v – The name view which has been changed/moved

  • publish – Whether to publish the changes of the meta data

rafcon.gui.mygaphas.utils.gap_helper.update_meta_data_for_port(graphical_editor_view, item, handle)

This method updates the metadata of the states ports if they changed.

Parameters
  • graphical_editor_view – Graphical Editor the change occurred in

  • item – State the port was moved in

  • handle – Handle of moved port or None if all ports are to be updated

rafcon.gui.mygaphas.utils.gap_helper.update_meta_data_for_state_view(graphical_editor_view, state_v, affects_children=False, publish=True)

This method updates the metadata of a state view

Parameters
  • graphical_editor_view – Graphical Editor view the change occurred in

  • state_v – The state view which has been changed/moved

  • affects_children – Whether the children of the state view have been resized or not

  • publish – Whether to publish the changes of the metadata

aspect

canvas

class rafcon.gui.mygaphas.canvas.ItemProjection(point, item_point, item_target)

Bases: object

Project a point of item A into the coordinate system of item B.

The class os based on the implementation of gaphas.canvas.CanvasProjection.

property pos
class rafcon.gui.mygaphas.canvas.MyCanvas

Bases: gaphas.canvas.Canvas

add(item, parent=None, index=None)

Add an item to the canvas.

>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> len(c._tree.nodes)
1
>>> i._canvas is c
True
add_port(port_v)
exchange_model(old_model, new_model)
get_first_view()

Return first registered view object

get_parent(item)

See tree.Tree.get_parent().

>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> ii = item.Item()
>>> c.add(ii, i)
>>> c.get_parent(i)
>>> c.get_parent(ii) 
<gaphas.item.Item ...>
get_view_for_core_element(core_element, parent_item=None)

Searches and returns the View for the given core element

Parameters
  • core_element – The core element of the searched view

  • parent_item (gaphas.item.Item) – Restrict the search to this parent item

Returns

The view for the given core element or None if not found

get_view_for_model(model)

Searches and return the View for the given model

Parameters

model (gtkmvc3.ModelMT) – The model of the searched view

Returns

The view for the given model or None if not found

remove(item)

Remove item from the canvas.

>>> c = Canvas()
>>> from gaphas import item
>>> i = item.Item()
>>> c.add(i)
>>> c.remove(i)
>>> c._tree.nodes
[]
>>> i._canvas
remove_port(port_v)
resolve_constraint(constraints)
resolve_item_constraints(item)
update_root_items()
wait_for_update(trigger_update=False)

Update canvas and handle all events in the gtk queue

Parameters

trigger_update (bool) – Whether to call update_now() or not

connector

class rafcon.gui.mygaphas.connector.RectanglePointPort(point, port_v=None)

Bases: gaphas.connector.PointPort

glue(pos)

Calculates the distance between the given position and the port

Parameters

pos ((float, float)) – Distance to this position is calculated

Returns

Distance to port

Return type

float

property height
property width

constraint

class rafcon.gui.mygaphas.constraint.BorderWidthConstraint(north_west, south_east, border_width, factor)

Bases: gaphas.constraint.Constraint

solve_for(var=None)

Solve the constraint for a given variable. The variable itself is updated.

class rafcon.gui.mygaphas.constraint.KeepPointWithinConstraint(parent_nw, parent_se, child, margin_method=None)

Bases: gaphas.constraint.Constraint

Ensure that the point is within its parent

Attributes:
  • parent_nw: NW coordinates of parent

  • parent_se: SE coordinates of parent

  • child: coordinates of child

solve_for(var=None)

Ensure that the children is within its parent

class rafcon.gui.mygaphas.constraint.KeepPortDistanceConstraint(anchor, point, port, distance_func, incoming)

Bases: gaphas.constraint.Constraint

solve_for(var)

Solve the constraint for a given variable. The variable itself is updated.

class rafcon.gui.mygaphas.constraint.KeepRectangleWithinConstraint(parent_nw, parent_se, child_nw, child_se, child=None, margin_method=None)

Bases: gaphas.constraint.Constraint

Ensure that the children is within its parent

Attributes:
  • parent_nw: NW coordinates of parent

  • parent_se: SE coordinates of parent

  • child_nw: NW coordinates of child

  • child_se: SE coordinates of child

solve_for(var=None)

Ensure that the children is within its parent

class rafcon.gui.mygaphas.constraint.KeepRelativePositionConstraint(anchor, point)

Bases: gaphas.constraint.Constraint

solve_for(var)

Solve the constraint for a given variable. The variable itself is updated.

class rafcon.gui.mygaphas.constraint.PortRectConstraint(rect, point, port)

Bases: gaphas.constraint.Constraint

Keeps ports on rectangular path along containing state :param rect: Rect (NWpos, SEpos) specifying the path to bind port to :param point: Port position :param port: Port to bind

get_adjusted_border_positions()

Calculates the positions to limit the port movement to :return: Adjusted positions nw_x, nw_y, se_x, se_y

static limit_pos(p, se_pos, nw_pos)

Limits position p to stay inside containing state :param p: Position to limit :param se_pos: Bottom/Right boundary :param nw_pos: Top/Left boundary :return:

set_nearest_border()

Snaps the port to the correct side upon state size change

solve_for(var=None)

Solve the constraint for a given variable. The variable itself is updated.

update_distance_to_border()
update_port_side()

Updates the initial position of the port

The port side is ignored but calculated from the port position. Then the port position is limited to the four side lines of the state.

update_position(p)

guide

class rafcon.gui.mygaphas.guide.GuidedNameInMotion(item, view)

Bases: gaphas.guide.GuidedItemInMotion

move(pos)

Move the item. x and y are in view coordinates.

class rafcon.gui.mygaphas.guide.GuidedStateHandleInMotion(item, handle, view)

Bases: rafcon.gui.mygaphas.guide.GuidedStateMixin, gaphas.guide.GuidedItemHandleInMotion

glue(pos, distance=None)

Glue to an item near a specific point.

Returns a ConnectionSink or None.

move(pos)
class rafcon.gui.mygaphas.guide.GuidedStateInMotion(item, view)

Bases: rafcon.gui.mygaphas.guide.GuidedStateMixin, gaphas.guide.GuidedItemInMotion

move(pos)

Move the item. x and y are in view coordinates.

start_move(pos)
stop_move()
class rafcon.gui.mygaphas.guide.GuidedStateMixin

Bases: gaphas.guide.GuideMixin

MARGIN = 5
find_horizontal_guides(item_hedges, pdy, width, excluded_items)
find_vertical_guides(item_vedges, pdx, height, excluded_items)
get_excluded_items()

Get a set of items excluded from guide calculation.

painter

segment

class rafcon.gui.mygaphas.segment.TransitionSegment(item, view)

Bases: gaphas.segment.LineSegment

This class is used to redefine the behavior of transitions and how new waypoints may be added. It checks if the waypoint that should be created is not between the perpendicular connectors to the ports.

property item
split(pos)
split_segment(segment, count=2)

Split one item segment into count equal pieces.

Two lists are returned

  • list of created handles

  • list of created ports

Parameters
segment

Segment number to split (starting from zero).

count

Amount of new segments to be created (minimum 2).

property view

tools

view

class rafcon.gui.mygaphas.view.ExtendedGtkView(graphical_editor_v, state_machine_m, *args)

Bases: gaphas.view.GtkView, rafcon.design_patterns.observer.observer.Observer

do_configure_event(event)

configure_event(self, event:Gdk.EventConfigure) -> bool

property focused_item

The item with focus (receives key events a.o.)

get_items_at_point(pos, selected=True, distance=0)

Return the items located at pos (x, y).

Parameters
  • selected (bool) – if False returns first non-selected item

  • distance (float) – Maximum distance to be considered as “at point” (in viewport pixel)

get_port_at_point(vpos, distance=10, exclude=None, exclude_port_fun=None)

Find item with port closest to specified position.

List of items to be ignored can be specified with exclude parameter.

Tuple is returned

  • found item

  • closest, connectable port

  • closest point on found port (in view coordinates)

Parameters
vpos

Position specified in view coordinates.

distance

Max distance from point to a port (default 10)

exclude

Set of items to ignore.

get_state_at_point(vpos, distance=10)
get_zoom_factor()

Returns the current zoom factor of the view

The zoom factor can be read out from the view’s matrix. _matrix[0] should be equal _matrix[3]. Index 0 is for the zoom in x direction, index 3 for the y direction :return: Current zoom factor

property graphical_editor
handle_new_selection(items)

Determines the selection

The selection is based on the previous selection, the currently pressed keys and the passes newly selected items

Parameters

items – The newly selected item(s)

hovered_handle = None
prepare_destruction()

Get rid of circular references

queue_draw_item(*items)

Extends the base class method to allow Ports to be passed as item

Parameters

items – Items that are to be redrawn

select_item(items)

Select an items. This adds items to the set of selected items.

property selected_items

Items selected by the view

unselect_all()

Clearing the selected_item also clears the focused_item.

unselect_item(item)

Unselect an item.