SFCViewer.py
author etisserant
Wed, 31 Jan 2007 16:31:39 +0100
changeset 0 b622defdfd98
child 1 e9d01d824086
permissions -rw-r--r--
PLCOpenEditor initial commit. 31/01/07.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard.
#
#Copyright (C): Edouard TISSERANT and Laurent BESSARD
#
#See COPYING file for copyrights details.
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

from wxPython.wx import *
from wxPython.grid import *
import wx
from types import *

from plcopen.structures import *
from graphics.GraphicCommons import *
from graphics.SFC_Objects import *
from Viewer import *

class SFC_Viewer(Viewer):
    
    def __init__(self, parent, window, controler):
        Viewer.__init__(self, parent, window, controler)
    
    def ConnectConnectors(self, start, end):
        startpoint = [start.GetPosition(False), start.GetDirection()]
        endpoint = [end.GetPosition(False), end.GetDirection()]
        wire = Wire(self, startpoint, endpoint)
        self.Wires.append(wire)
        self.Elements.append(wire)
        start.Connect((wire, 0), False)
        end.Connect((wire, -1), False)
        wire.ConnectStartPoint(None, start)
        wire.ConnectEndPoint(None, end)
        return wire
    
    def CreateTransition(self, connector, next = None):
        previous = connector.GetParentBlock()
        id = self.GetNewId()
        transition = SFC_Transition(self, "reference", "", id)
        pos = connector.GetPosition(False)
        transition.SetPosition(pos.x, pos.y + SFC_WIRE_MIN_SIZE)
        transition_connectors = transition.GetConnectors()
        wire = self.ConnectConnectors(transition_connectors["input"], connector)
        if isinstance(previous, SFC_Divergence):
            previous.RefreshConnectedPosition(connector)
        else:
            previous.RefreshOutputPosition()
        wire.SetPoints([wxPoint(pos.x, pos.y + GetWireSize(previous)), wxPoint(pos.x, pos.y)])
        self.Blocks.append(transition)
        self.Elements.append(transition)
        self.Controler.AddCurrentElementEditingTransition(id)
        self.RefreshTransitionModel(transition)
        if next:
            wire = self.ConnectConnectors(next, transition_connectors["output"])
            pos = transition_connectors["output"].GetPosition(False)
            next_block = next.GetParentBlock()
            next_pos = next.GetPosition(False)
            transition.RefreshOutputPosition((0, pos.y + SFC_WIRE_MIN_SIZE - next_pos.y))
            wire.SetPoints([wxPoint(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wxPoint(pos.x, pos.y)])
            if isinstance(next_block, SFC_Divergence):
                next_block.RefreshPosition()
            next_block.RefreshModel()
        return transition
    
    def RemoveTransition(self, transition):
        connectors = transition.GetConnectors()
        input_wires = connectors["input"].GetWires()
        if len(input_wires) != 1:
            return
        input_wire = input_wires[0][0]
        previous = input_wire.EndConnected
        input_wire.Clean()
        self.Wires.remove(input_wire)
        self.Elements.remove(input_wire)
        output_wires = connectors["output"].GetWires()
        if len(output_wires) != 1:
            return
        output_wire = output_wires[0][0]
        next = output_wire.StartConnected
        output_wire.Clean()
        self.Wires.remove(output_wire)
        self.Elements.remove(output_wire)
        transition.Clean()
        self.Blocks.remove(transition)
        self.Elements.remove(transition)
        self.Controler.RemoveCurrentElementEditingInstance(transition.GetId())
        wire = self.ConnectConnectors(next, previous)
        return wire
    
    def CreateStep(self, name, connector, next = None):
        previous = connector.GetParentBlock()
        id = self.GetNewId()
        step = SFC_Step(self, name, False, id)
        if next:
            step.AddOutput()
        min_width, min_height = step.GetMinSize()
        pos = connector.GetPosition(False)
        step.SetPosition(pos.x, pos.y + SFC_WIRE_MIN_SIZE)
        step.SetSize(min_width, min_height)
        step_connectors = step.GetConnectors()
        wire = self.ConnectConnectors(step_connectors["input"], connector)
        if isinstance(previous, SFC_Divergence):
            previous.RefreshConnectedPosition(connector)
        else:
            previous.RefreshOutputPosition()
        wire.SetPoints([wxPoint(pos.x, pos.y + GetWireSize(previous)), wxPoint(pos.x, pos.y)])
        self.Blocks.append(step)
        self.Elements.append(step)
        self.Controler.AddCurrentElementEditingStep(id)
        self.RefreshStepModel(step)
        if next:
            wire = self.ConnectConnectors(next, step_connectors["output"])
            pos = step_connectors["output"].GetPosition(False)
            next_block = next.GetParentBlock()
            next_pos = next.GetPosition(False)
            step.RefreshOutputPosition((0, pos.y + SFC_WIRE_MIN_SIZE - next_pos.y))
            wire.SetPoints([wxPoint(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wxPoint(pos.x, pos.y)])
            if isinstance(next_block, SFC_Divergence):
                next_block.RefreshPosition()
            next_block.RefreshModel()
        return step

    def RemoveStep(self, step):
        connectors = step.GetConnectors()
        if connectors["input"]:
            input_wires = connectors["input"].GetWires()
            if len(input_wires) != 1:
                return
            input_wire = input_wires[0][0]
            previous = input_wire.EndConnected
            input_wire.Clean()
            self.Wires.remove(input_wire)
            self.Elements.remove(input_wire)
        else:
            previous = None
        if connectors["output"]:
            output_wires = connectors["output"].GetWires()
            if len(output_wires) != 1:
                return
            output_wire = output_wires[0][0]
            next = output_wire.StartConnected
            output_wire.Clean()
            self.Wires.remove(output_wire)
            self.Elements.remove(output_wire)
        else:
            next = None
        action = step.GetActionConnector()
        if action:
            self.DeleteActionBlock(action.GetParentBlock())
        step.Clean()
        self.Blocks.remove(step)
        self.Elements.remove(step)
        self.Controler.RemoveCurrentElementEditingInstance(step.GetId())
        if next and previous:
            wire = self.ConnectConnectors(next, previous)
            return wire
        else:
            return None

#-------------------------------------------------------------------------------
#                          Mouse event functions
#-------------------------------------------------------------------------------

    def OnViewerLeftDown(self, event):
        if self.Mode == MODE_SELECTION:
            pos = event.GetPosition()
            if event.ControlDown():
                element = self.FindElement(pos, True)
                if element and element not in self.Wires:
                    if isinstance(self.SelectedElement, Graphic_Group):
                        self.SelectedElement.SelectElement(element)
                    else:
                        group = Graphic_Group(self)
                        self.SelectedElement.SetSelected(False)
                        group.SelectElement(self.SelectedElement)
                        group.SelectElement(element)
                        self.SelectedElement = group
                    elements = self.SelectedElement.GetElements()
                    if len(elements) == 0:
                        self.SelectedElement = element
                    elif len(elements) == 1:
                        self.SelectedElement = elements[0]
            else:
                element = self.FindElement(pos)
                if self.SelectedElement and self.SelectedElement != element:
                    if self.SelectedElement in self.Wires:
                        self.SelectedElement.SetSelectedSegment(None)
                    else:
                        self.SelectedElement.SetSelected(False)
                    self.SelectedElement = None
                    self.Refresh()
                if element:
                    self.SelectedElement = element
                    self.SelectedElement.OnLeftDown(event, self.Scaling)
                    self.Refresh()
                else:
                    self.rubberBand.Reset()
                    self.rubberBand.OnLeftDown(event, self.Scaling)
        elif self.Mode == MODE_COMMENT:
            self.rubberBand.Reset()
            self.rubberBand.OnLeftDown(event, self.Scaling)
        elif self.Mode == MODE_WIRE:
            pos = GetScaledEventPosition(event, self.Scaling)
            wire = Wire(self, [wxPoint(pos.x, pos.y), SOUTH], [wxPoint(pos.x, pos.y), NORTH])
            wire.oldPos = pos
            wire.Handle = (HANDLE_POINT, 0)
            wire.ProcessDragging(0, 0)
            wire.Handle = (HANDLE_POINT, 1)
            self.Wires.append(wire)
            self.Elements.append(wire)
            if self.SelectedElement:
                self.SelectedElement.SetSelected(False)
            self.SelectedElement = wire
            self.Refresh()
        event.Skip()

    def OnViewerLeftUp(self, event):
        if self.rubberBand.IsShown():
            if self.Mode == MODE_SELECTION:
                elements = self.SearchElements(self.rubberBand.GetCurrentExtent())
                self.rubberBand.OnLeftUp(event, self.Scaling)
                if len(elements) > 0:
                    self.SelectedElement = Graphic_Group(self)
                    self.SelectedElement.SetElements(elements)
                    self.SelectedElement.SetSelected(True)
                    self.Refresh()
            elif self.Mode == MODE_COMMENT:
                bbox = self.rubberBand.GetCurrentExtent()
                self.rubberBand.OnLeftUp(event, self.Scaling)
                wxCallAfter(self.AddComment, bbox)
        elif self.Mode == MODE_INITIAL_STEP:
            wxCallAfter(self.AddInitialStep, GetScaledEventPosition(event, self.Scaling))
        elif self.Mode == MODE_SELECTION and self.SelectedElement:
            if self.SelectedElement in self.Wires:
                self.SelectedElement.SetSelectedSegment(0)
            else:
                self.SelectedElement.OnLeftUp(event, self.Scaling)
            wxCallAfter(self.SetCursor, wxNullCursor)
            self.ReleaseMouse()
            self.Refresh()
        elif self.Mode == MODE_WIRE and self.SelectedElement:
            self.SelectedElement.ResetPoints()
            self.SelectedElement.OnMotion(event, self.Scaling)
            self.SelectedElement.GeneratePoints()
            self.SelectedElement.RefreshModel()
            self.SelectedElement.SetSelected(True)
            self.Refresh()
        event.Skip()
    
    def OnViewerRightUp(self, event):
        pos = event.GetPosition()
        element = self.FindElement(pos)
        if element:
            if self.SelectedElement and self.SelectedElement != element:
                self.SelectedElement.SetSelected(False)
            self.SelectedElement = element
            if self.SelectedElement in self.Wires:
                self.SelectedElement.SetSelectedSegment(0)
            else:
                self.SelectedElement.SetSelected(True)
                self.SelectedElement.OnRightUp(event, self.Scaling)
            wxCallAfter(self.SetCursor, wxNullCursor)
            self.ReleaseMouse()
            self.Refresh()
        event.Skip()
    
    def OnViewerLeftDClick(self, event):
        if self.Mode == MODE_SELECTION and self.SelectedElement:
            self.SelectedElement.OnLeftDClick(event, self.Scaling)
            self.Refresh()
        event.Skip()
    
    def OnViewerMotion(self, event):
        if self.rubberBand.IsShown():
            self.rubberBand.OnMotion(event, self.Scaling)
        elif self.Mode == MODE_SELECTION and self.SelectedElement:
            if self.SelectedElement not in self.Wires:
                self.SelectedElement.OnMotion(event, self.Scaling)
            self.Refresh()
        elif self.Mode == MODE_WIRE and self.SelectedElement:
            self.SelectedElement.ResetPoints()
            self.SelectedElement.OnMotion(event, self.Scaling)
            self.SelectedElement.GeneratePoints()
            self.Refresh()
        event.Skip()

#-------------------------------------------------------------------------------
#                          Keyboard event functions
#-------------------------------------------------------------------------------

    def OnChar(self, event):
        keycode = event.GetKeyCode()
        if self.Scaling:
            scaling = self.Scaling
        else:
            scaling = (8, 8)
        if keycode == WXK_DELETE and self.SelectedElement:
            self.SelectedElement.Delete()
            self.SelectedElement = None
        elif keycode == WXK_LEFT and self.SelectedElement:
            self.SelectedElement.Move(-scaling[0], 0)
        elif keycode == WXK_RIGHT and self.SelectedElement:
            self.SelectedElement.Move(scaling[0], 0)
        elif keycode == WXK_UP and self.SelectedElement:
            self.SelectedElement.Move(0, -scaling[1])
        elif keycode == WXK_DOWN and self.SelectedElement:
            self.SelectedElement.Move(0, scaling[1])
        self.Refresh()
        event.Skip()
        
#-------------------------------------------------------------------------------
#                          Adding element functions
#-------------------------------------------------------------------------------

    def AddInitialStep(self, pos):
        dialog = wxTextEntryDialog(self.Parent, "Add a new initial step", "Please enter step name", "", wxOK|wxCANCEL)
        if dialog.ShowModal() == wxID_OK:
            id = self.GetNewId()
            name = dialog.GetValue()
            step = SFC_Step(self, name, True, id)
            min_width, min_height = step.GetMinSize()
            step.SetPosition(pos.x, pos.y)
            width, height = step.GetSize()
            step.SetSize(max(min_width, width), max(min_height, height))
            self.Blocks.append(step)
            self.Elements.append(step)
            self.Controler.AddCurrentElementEditingStep(id)
            self.RefreshStepModel(step)
            self.Parent.RefreshProjectTree()
            self.Refresh()
        dialog.Destroy()

    def AddStep(self):
        if self.SelectedElement in self.Wires or isinstance(self.SelectedElement, SFC_Step):
            dialog = wxTextEntryDialog(self.Parent, "Add a new step", "Please enter step name", "", wxOK|wxCANCEL)
            if dialog.ShowModal() == wxID_OK:
                name = dialog.GetValue()
                if self.SelectedElement in self.Wires:
                    self.SelectedElement.SetSelectedSegment(None)
                    previous = self.SelectedElement.EndConnected
                    next = self.SelectedElement.StartConnected
                    self.SelectedElement.Clean()
                    self.Wires.remove(self.SelectedElement)
                    self.Elements.remove(self.SelectedElement)
                else:
                    connectors = self.SelectedElement.GetConnectors()
                    if connectors["output"]:
                        previous = connectors["output"]
                        wires = previous.GetWires()
                        if len(wires) != 1:
                            return
                        wire = wires[0][0]
                        next = wire.StartConnected
                        wire.Clean()
                        self.Wires.remove(wire)
                        self.Elements.remove(wire)
                    else:
                        self.SelectedElement.AddOutput()
                        connectors = self.SelectedElement.GetConnectors()
                        self.RefreshStepModel(self.SelectedElement)
                        previous = connectors["output"]
                        next = None
                previous_block = previous.GetParentBlock()
                if isinstance(previous_block, SFC_Step) or isinstance(previous_block, SFC_Divergence) and previous_block.GetType() in [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE]:
                    transition = self.CreateTransition(previous)
                    transition_connectors = transition.GetConnectors()
                    step = self.CreateStep(name, transition_connectors["output"], next)
                else:
                    step = self.CreateStep(name, previous)
                    step.AddOutput()
                    step.RefreshModel()
                    step_connectors = step.GetConnectors()
                    transition = self.CreateTransition(step_connectors["output"], next)
                if self.SelectedElement in self.Wires:
                    self.SelectedElement = wire
                    self.SelectedElement.SetSelectedSegment(0)
                else:
                    self.SelectedElement.SetSelected(False)
                    self.SelectedElement = step
                    self.SelectedElement.SetSelected(True)
                self.Parent.RefreshProjectTree()
                self.Refresh()
            dialog.Destroy()
    
    def AddStepAction(self):
        if isinstance(self.SelectedElement, SFC_Step):
            connectors = self.SelectedElement.GetConnectors()
            if not connectors["action"]:
                dialog = ActionBlockDialog(self.Parent)
                dialog.SetQualifierList(self.Controler.GetQualifierTypes())
                dialog.SetActionList(self.Controler.GetCurrentElementEditingActions())
                dialog.SetVariableList(self.Controler.GetCurrentElementEditingInterfaceVars())
                if dialog.ShowModal() == wxID_OK:
                    actions = dialog.GetValues()
                    self.SelectedElement.AddAction()
                    self.RefreshStepModel(self.SelectedElement)
                    connectors = self.SelectedElement.GetConnectors()
                    pos = connectors["action"].GetPosition(False)
                    id = self.GetNewId()
                    actionblock = SFC_ActionBlock(self, [], id)
                    actionblock.SetPosition(pos.x + SFC_WIRE_MIN_SIZE, pos.y - SFC_STEP_DEFAULT_SIZE[1] / 2)
                    actionblock_connector = actionblock.GetConnector()
                    wire = self.ConnectConnectors(actionblock_connector, connectors["action"])
                    wire.SetPoints([wxPoint(pos.x + SFC_WIRE_MIN_SIZE, pos.y), wxPoint(pos.x, pos.y)])
                    actionblock.SetActions(actions)
                    self.Blocks.append(actionblock)
                    self.Elements.append(actionblock)
                    self.Controler.AddCurrentElementEditingActionBlock(id)
                    self.RefreshActionBlockModel(actionblock)
                    self.Refresh()
                dialog.Destroy()
    
    def AddDivergence(self):
        if self.SelectedElement in self.Wires or isinstance(self.SelectedElement, Graphic_Group) or isinstance(self.SelectedElement, SFC_Step):        
            dialog = DivergenceCreateDialog(self.Parent)
            if dialog.ShowModal() == wxID_OK:
                value = dialog.GetValues()
                if value["type"] == SELECTION_DIVERGENCE:
                    if self.SelectedElement in self.Wires and isinstance(self.SelectedElement.EndConnected.GetParentBlock(), SFC_Step):
                        self.SelectedElement.SetSelectedSegment(None)
                        previous = self.SelectedElement.EndConnected
                        next = self.SelectedElement.StartConnected
                        self.SelectedElement.Clean()
                        self.Wires.remove(self.SelectedElement)
                        self.Elements.remove(self.SelectedElement)
                        self.SelectedElement = None
                    elif isinstance(self.SelectedElement, SFC_Step):
                        connectors = self.SelectedElement.GetConnectors()
                        if connectors["output"]:
                            previous = connectors["output"]
                            wires = previous.GetWires()
                            if len(wires) != 1:
                                return
                            wire = wires[0][0]
                            next = wire.StartConnected
                            wire.Clean()
                            self.Wires.remove(wire)
                            self.Elements.remove(wire)
                        else:
                            self.SelectedElement.AddOutput()
                            connectors = self.SelectedElement.GetConnectors()
                            self.RefreshStepModel(self.SelectedElement)
                            previous = connectors["output"]
                            next = None
                    else:
                        return
                    id = self.GetNewId()
                    divergence = SFC_Divergence(self, SELECTION_DIVERGENCE, value["number"], id)
                    pos = previous.GetPosition(False)
                    previous_block = previous.GetParentBlock()
                    wire_size = GetWireSize(previous_block)
                    divergence.SetPosition(pos.x, pos.y + wire_size)
                    divergence_connectors = divergence.GetConnectors()
                    wire = self.ConnectConnectors(divergence_connectors["inputs"][0], previous)
                    previous_block.RefreshOutputPosition()
                    wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
                    self.Blocks.append(divergence)
                    self.Elements.append(divergence)
                    self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
                    self.RefreshDivergenceModel(divergence)
                    for index, connector in enumerate(divergence_connectors["outputs"]):
                        if next:
                            wire = self.ConnectConnectors(next, connector)
                            pos = connector.GetPosition(False)
                            next_pos = next.GetPosition(False)
                            next_block = next.GetParentBlock()
                            divergence.RefreshOutputPosition((0, pos.y + SFC_WIRE_MIN_SIZE - next_pos.y))
                            divergence.RefreshConnectedPosition(connector)
                            wire.SetPoints([wxPoint(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wxPoint(pos.x, pos.y)])
                            next_block.RefreshModel()
                            next = None
                        else:
                            transition = self.CreateTransition(connector)
                            transition_connectors = transition.GetConnectors()
                            step = self.CreateStep("Step", transition_connectors["output"])
                elif value["type"] == SIMULTANEOUS_DIVERGENCE:
                    if self.SelectedElement in self.Wires and isinstance(self.SelectedElement.EndConnected.GetParentBlock(), SFC_Transition):
                        self.SelectedElement.SetSelectedSegment(None)
                        previous = self.SelectedElement.EndConnected
                        next = self.SelectedElement.StartConnected
                        self.SelectedElement.Clean()
                        self.Wires.remove(self.SelectedElement)
                        self.Elements.remove(self.SelectedElement)
                        self.SelectedElement = None
                    elif isinstance(self.SelectedElement, SFC_Step):
                        connectors = self.SelectedElement.GetConnectors()
                        if connectors["output"]:
                            previous = connectors["output"]
                            wires = previous.GetWires()
                            if len(wires) != 1:
                                return
                            wire = wires[0][0]
                            next = wire.StartConnected
                            wire.Clean()
                            self.Wires.remove(wire)
                            self.Elements.remove(wire)
                        else:
                            self.SelectedElement.AddOutput()
                            connectors = self.SelectedElement.GetConnectors()
                            self.RefreshStepModel(self.SelectedElement)
                            previous = connectors["output"]
                            next = None
                        transition = self.CreateTransition(previous)
                        transition_connectors = transition.GetConnectors()
                        previous = transition_connectors["output"]
                    else:
                        return
                    id = self.GetNewId()
                    divergence = SFC_Divergence(self, SIMULTANEOUS_DIVERGENCE, value["number"], id)
                    pos = previous.GetPosition(False)
                    previous_block = previous.GetParentBlock()
                    wire_size = GetWireSize(previous_block)
                    divergence.SetPosition(pos.x, pos.y + wire_size)
                    divergence_connectors = divergence.GetConnectors()
                    wire = self.ConnectConnectors(divergence_connectors["inputs"][0], previous)
                    previous_block.RefreshOutputPosition()
                    wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
                    self.Blocks.append(divergence)
                    self.Elements.append(divergence)
                    self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
                    self.RefreshDivergenceModel(divergence)
                    for index, connector in enumerate(divergence_connectors["outputs"]):
                        if next:
                            wire = self.ConnectConnectors(next, connector)
                            pos = connector.GetPosition(False)
                            next_pos = next.GetPosition(False)
                            next_block = next.GetParentBlock()
                            divergence.RefreshOutputPosition((0, pos.y + SFC_WIRE_MIN_SIZE - next_pos.y))
                            divergence.RefreshConnectedPosition(connector)
                            wire.SetPoints([wxPoint(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wxPoint(pos.x, pos.y)])
                            next_block.RefreshModel()
                            next = None
                        else:
                            step = self.CreateStep("Step", connector)
                elif isinstance(self.SelectedElement, Graphic_Group) and len(self.SelectedElement.GetElements()) > 1:
                    next = None
                    for element in self.SelectedElement.GetElements():
                        connectors = element.GetConnectors()
                        if not isinstance(element, SFC_Step) or connectors["output"] and next:
                            return
                        elif connectors["output"] and not next:
                            wires = connectors["output"].GetWires()
                            if len(wires) != 1:
                                return
                            if value["type"] == SELECTION_CONVERGENCE:
                                transition = wires[0][0].StartConnected.GetParentBlock()
                                transition_connectors = transition.GetConnectors()
                                wires = transition_connectors["output"].GetWires()
                                if len(wires) != 1:
                                    return
                            wire = wires[0][0]
                            next = wire.StartConnected
                            wire.Clean()
                            self.Wires.remove(wire)
                            self.Elements.remove(wire)
                    inputs = []
                    for input in self.SelectedElement.GetElements():
                        input_connectors = input.GetConnectors()
                        if not input_connectors["output"]:
                            input.AddOutput()
                            input.RefreshModel()
                            input_connectors = input.GetConnectors()
                            if value["type"] == SELECTION_CONVERGENCE:
                                transition = self.CreateTransition(input_connectors["output"])
                                transition_connectors = transition.GetConnectors()
                                inputs.append(transition_connectors["output"])
                            else:
                                inputs.append(input_connectors["output"])
                        elif value["type"] == SELECTION_CONVERGENCE:
                            wires = input_connectors["output"].GetWires()
                            transition = wires[0][0].StartConnected.GetParentBlock()
                            transition_connectors = transition.GetConnectors()
                            inputs.append(transition_connectors["output"])
                        else:
                            inputs.append(input_connectors["output"])
                    id = self.GetNewId()
                    divergence = SFC_Divergence(self, value["type"], len(inputs), id)
                    pos = inputs[0].GetPosition(False)
                    divergence.SetPosition(pos.x, pos.y + SFC_WIRE_MIN_SIZE)
                    divergence_connectors = divergence.GetConnectors()
                    for i, input in enumerate(inputs):
                        pos = input.GetPosition(False)
                        wire = self.ConnectConnectors(divergence_connectors["inputs"][i], input)
                        wire_size = GetWireSize(input)
                        wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
                        input_block = input.GetParentBlock()
                        input_block.RefreshOutputPosition()
                    divergence.RefreshPosition()
                    pos = divergence_connectors["outputs"][0].GetRelPosition()
                    divergence.MoveConnector(divergence_connectors["outputs"][0], - pos.x)
                    self.Blocks.append(divergence)
                    self.Elements.append(divergence)
                    self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
                    self.RefreshDivergenceModel(divergence)
                    if next:
                        wire = self.ConnectConnectors(next, divergence_connectors["outputs"][0])
                        pos = divergence_connectors["outputs"][0].GetPosition(False)
                        next_pos = next.GetPosition(False)
                        next_block = next.GetParentBlock()
                        divergence.RefreshOutputPosition((0, pos.y + SFC_WIRE_MIN_SIZE - next_pos.y))
                        divergence.RefreshConnectedPosition(divergence_connectors["outputs"][0])
                        wire.SetPoints([wxPoint(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wxPoint(pos.x, pos.y)])
                        next_block.RefreshModel()
                    else:
                        if value["type"] == SELECTION_CONVERGENCE:
                            previous = divergence_connectors["outputs"][0]
                        else:
                            transition = self.CreateTransition(divergence_connectors["outputs"][0])
                            transition_connectors = transition.GetConnectors()
                            previous = transition_connectors["output"]
                        self.CreateStep("Step", previous)
                self.Refresh()
            dialog.Destroy()
    
    def AddDivergenceBranch(self, divergence):
        if isinstance(divergence, SFC_Divergence):
            type = divergence.GetType()
            if type in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
                divergence.AddBranch()
                divergence_connectors = divergence.GetConnectors()
                if type == SELECTION_DIVERGENCE:
                    transition = self.CreateTransition(divergence_connectors["outputs"][-1])
                    transition_connectors = transition.GetConnectors()
                    previous = transition_connectors["output"]
                else:
                    previous = divergence_connectors["outputs"][-1]
                step = self.CreateStep("Step", previous)
                self.Refresh()
    
    def AddJump(self):
        if isinstance(self.SelectedElement, SFC_Step) and not self.SelectedElement.Output:
            choices = []
            for block in self.Blocks:
                if isinstance(block, SFC_Step):
                    choices.append(block.GetName())
            dialog = wxSingleChoiceDialog(self.Parent, "Add a new jump", "Please choose a target", choices, wxOK|wxCANCEL)
            if dialog.ShowModal() == wxID_OK:
                value = dialog.GetStringSelection()
                self.SelectedElement.AddOutput()
                self.RefreshStepModel(self.SelectedElement)
                step_connectors = self.SelectedElement.GetConnectors()
                transition = self.CreateTransition(step_connectors["output"])
                transition_connectors = transition.GetConnectors()
                id = self.GetNewId()
                jump = SFC_Jump(self, value, id)
                pos = transition_connectors["output"].GetPosition(False)
                jump.SetPosition(pos.x, pos.y + SFC_WIRE_MIN_SIZE)
                self.Blocks.append(jump)
                self.Elements.append(jump)
                self.Controler.AddCurrentElementEditingJump(id)
                jump_connector = jump.GetConnector()
                wire = self.ConnectConnectors(jump_connector, transition_connectors["output"])
                transition.RefreshOutputPosition()
                wire.SetPoints([wxPoint(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wxPoint(pos.x, pos.y)])
                self.RefreshJumpModel(jump)
                self.Refresh()
            dialog.Destroy()
    
    def AddComment(self, bbox):
        dialog = wxTextEntryDialog(self.Parent, "Add a new comment", "Please enter comment text", "", wxOK|wxCANCEL|wxTE_MULTILINE)
        if dialog.ShowModal() == wxID_OK:
            value = dialog.GetValue()
            id = self.GetNewId()
            comment = Comment(self, value, id)
            comment.SetPosition(bbox.x, bbox.y)
            min_width, min_height = comment.GetMinSize()
            comment.SetSize(max(min_width,bbox.width),max(min_height,bbox.height))
            self.Elements.append(comment)
            self.Controler.AddCurrentElementEditingComment(id)
            self.RefreshCommentModel(comment)
            self.Refresh()
        dialog.Destroy()

    def EditStepContent(self, step):
        dialog = wxTextEntryDialog(self.Parent, "Edit step name", "Please enter step name", step.GetName(), wxOK|wxCANCEL)
        if dialog.ShowModal() == wxID_OK:
            value = dialog.GetValue()
            step.SetName(value)
            min_size = step.GetMinSize()
            size = step.GetSize()
            step.UpdateSize(max(min_size[0], size[0]), max(min_size[1], size[1]))
            step.RefreshModel()
            self.Refresh()
        dialog.Destroy()

    def EditTransitionContent(self, transition):
        dialog = TransitionContentDialog(self.Parent)
        dialog.SetTransitions(self.Controler.GetCurrentElementEditingTransitions())
        dialog.SetValues({"type":transition.GetType(),"value":transition.GetCondition()})
        if dialog.ShowModal() == wxID_OK:
            values = dialog.GetValues()
            transition.SetType(values["type"])
            transition.SetCondition(values["value"])
            transition.RefreshModel()
            self.Refresh()
        dialog.Destroy()

    def EditJumpContent(self, jump):
        choices = []
        for block in self.Blocks:
            if isinstance(block, SFC_Step):
                choices.append(block.GetName())
        dialog = wxSingleChoiceDialog(self.Parent, "Edit jump target", "Please choose a target", choices, wxOK|wxCANCEL)
        dialog.SetSelection(choices.index(jump.GetTarget()))
        if dialog.ShowModal() == wxID_OK:
            value = dialog.GetStringSelection()
            jump.SetTarget(value)
            jump.RefreshModel()
            self.Refresh()
        dialog.Destroy()

    def EditActionBlockContent(self, actionblock):
        dialog = ActionBlockDialog(self.Parent)
        dialog.SetQualifierList(self.Controler.GetQualifierTypes())
        dialog.SetActionList(self.Controler.GetCurrentElementEditingActions())
        dialog.SetVariableList(self.Controler.GetCurrentElementEditingInterfaceVars())
        dialog.SetValues(actionblock.GetActions())
        if dialog.ShowModal() == wxID_OK:
            actions = dialog.GetValues()
            actionblock.SetActions(actions)
            actionblock.RefreshModel()
            self.Refresh()
        dialog.Destroy()
        

#-------------------------------------------------------------------------------
#                          Delete element functions
#-------------------------------------------------------------------------------

    def DeleteStep(self, step):
        step_connectors = step.GetConnectors()
        if not step.GetInitial() or not step_connectors["output"]:
            previous = step.GetPreviousConnector()
            if previous:
                previous_block = previous.GetParentBlock()
            else:
                previous_block = None
            next = step.GetNextConnector()
            if next:
                next_block = next.GetParentBlock()
            else:
                next_block = None
            if isinstance(next_block, SFC_Transition):
                self.RemoveTransition(next_block)
                next = step.GetNextConnector()
                if next:
                    next_block = next.GetParentBlock()
                else:
                    next_block = None
            elif isinstance(previous_block, SFC_Transition):
                self.RemoveTransition(previous_block)
                previous = step.GetPreviousConnector()
                if previous:
                    previous_block = previous.GetParentBlock()
                else:
                    previous_block = None
            wire = self.RemoveStep(step)
            self.SelectedElement = None
            if next_block:
                if isinstance(next_block, SFC_Divergence) and next_block.GetType() == SIMULTANEOUS_CONVERGENCE and isinstance(previous_block, SFC_Divergence) and previous_block.GetType() == SIMULTANEOUS_DIVERGENCE:
                    wire.Clean()
                    self.Wires.remove(wire)
                    self.Elements.remove(wire)
                    next_block.RemoveBranch(next)
                    if next_block.GetBranchNumber() < 2:
                        self.DeleteDivergence(next_block)
                    else:
                        next_block.RefreshModel()
                    previous_block.RemoveBranch(previous)
                    if previous_block.GetBranchNumber() < 2:
                        self.DeleteDivergence(previous_block)
                    else:
                        previous_block.RefreshModel()
                else:
                    pos = previous.GetPosition(False)
                    next_pos = next.GetPosition(False)
                    wire_size = GetWireSize(previous_block)
                    previous_block.RefreshOutputPosition((0, pos.y + wire_size - next_pos.y))
                    wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
                    if isinstance(next_block, SFC_Divergence):
                        next_block.RefreshPosition()
                    next_block.RefreshModel()
            else:
                if isinstance(previous_block, SFC_Step):
                    previous_block.RemoveOutput()
                    self.RefreshStepModel(previous_block)
                elif isinstance(previous_block, SFC_Divergence):
                    if previous_block.GetType() in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
                        self.DeleteDivergence(previous_block)
                    else:
                        previous_block.RemoveBranch(previous)
                        if previous_block.GetBranchNumber() < 2:
                            self.DeleteDivergence(previous_block)
                        else:
                            self.RefreshDivergenceModel(previous_block)
        
    def DeleteTransition(self, transition):
        previous = transition.GetPreviousConnector()
        previous_block = previous.GetParentBlock()
        next = transition.GetNextConnector()
        next_block = next.GetParentBlock()
        if isinstance(previous_block, SFC_Divergence) and previous_block.GetType() == SELECTION_DIVERGENCE and isinstance(next_block, SFC_Divergence) and next_block.GetType() == SELECTION_CONVERGENCE:
            wires = previous.GetWires()
            if len(wires) != 1:
                return
            wire = wires[0][0]
            wire.Clean()
            self.Wires.remove(wire)
            self.Elements.remove(wire)
            wires = next.GetWires()
            if len(wires) != 1:
                return
            wire = wires[0][0]
            wire.Clean()
            self.Wires.remove(wire)
            self.Elements.remove(wire)
            transition.Clean()
            self.Blocks.remove(transition)
            self.Elements.remove(transition)
            self.Controler.RemoveCurrentElementEditingInstance(transition.GetId())
            previous_block.RemoveBranch(previous)
            if previous_block.GetBranchNumber() < 2:
                self.DeleteDivergence(previous_block)
            else:
                self.RefreshDivergenceModel(previous_block)
            next_block.RemoveBranch(next)
            if next_block.GetBranchNumber() < 2:
                self.DeleteDivergence(next_block)
            else:
                self.RefreshDivergenceModel(next_block)
        self.Parent.RefreshProjectTree()

    def DeleteDivergence(self, divergence):
        connectors = divergence.GetConnectors()
        type = divergence.GetType()
        if type in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
            wires = connectors["outputs"][0].GetWires()
            if len(wires) > 1:
                return
            elif len(wires) == 1:
                next = wires[0][0].StartConnected
                next_block = next.GetParentBlock()
                wire = wires[0][0]
                wire.Clean()
                self.Wires.remove(wire)
                self.Elements.remove(wire)
            else:
                next = None
                next_block = None
            for index, connector in enumerate(connectors["inputs"]):
                if next and index == 0:
                    wires = connector.GetWires()
                    wire = wires[0][0]
                    previous = wires[0][0].EndConnected
                    wire.Clean()
                    self.Wires.remove(wire)
                    self.Elements.remove(wire)
                else:
                    if type == SELECTION_CONVERGENCE:
                        wires = connector.GetWires()
                        previous_block = wires[0][0].EndConnected.GetParentBlock()
                        self.RemoveTransition(previous_block)
                    wires = connector.GetWires()
                    wire = wires[0][0]
                    previous_connector = wire.EndConnected
                    previous_block = previous_connector.GetParentBlock()
                    wire.Clean()
                    self.Wires.remove(wire)
                    self.Elements.remove(wire)
                    if isinstance(previous_block, SFC_Step):
                        previous_block.RemoveOutput()
                        self.RefreshStepModel(previous_block)
                    elif isinstance(previous_block, SFC_Divergence):
                        if previous_block.GetType() in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
                            previous_block.RemoveBranch(previous_connector)
                            if previous_block.GetBranchNumber() < 2:
                                self.DeleteDivergence(previous_block)
                            else:
                                self.RefreshDivergenceModel(previous_block)
                        else:
                            self.DeleteDivergence(previous_block)
            divergence.Clean()
            self.Blocks.remove(divergence)
            self.Elements.remove(divergence)
            self.Controler.RemoveCurrentElementEditingInstance(divergence.GetId())
            if next:
                wire = self.ConnectConnectors(next, previous)
                previous_block = previous.GetParentBlock()
                pos = previous.GetPosition(False)
                next_pos = next.GetPosition(False)
                wire_size = GetWireSize(previous_block)
                previous_block.RefreshOutputPosition((0, previous_pos.y + wire_size - next_pos.y))
                wire.SetPoints([wxPoint(previous_pos.x, previous_pos.y + wire_size), 
                    wxPoint(previous_pos.x, previous_pos.y)])
                if isinstance(next_block, SFC_Divergence):
                    next_block.RefreshPosition()
                next_block.RefreshModel()
        elif divergence.GetBranchNumber() == 1:
            wires = connectors["inputs"][0].GetWires()
            if len(wires) != 1:
                return
            wire = wires[0][0]
            previous = wire.EndConnected
            previous_block = previous.GetParentBlock()
            wire.Clean()
            self.Wires.remove(wire)
            self.Elements.remove(wire)
            wires = connectors["outputs"][0].GetWires()
            if len(wires) != 1:
                return
            wire = wires[0][0]
            next = wire.StartConnected
            next_block = next.GetParentBlock()
            wire.Clean()
            self.Wires.remove(wire)
            self.Elements.remove(wire)
            divergence.Clean()
            self.Blocks.remove(divergence)
            self.Elements.remove(divergence)
            self.Controler.RemoveCurrentElementEditingInstance(divergence.GetId())
            wire = self.ConnectConnectors(next, previous)
            previous_pos = previous.GetPosition(False)
            next_pos = next.GetPosition(False)
            wire_size = GetWireSize(previous_block)
            previous_block.RefreshOutputPosition((previous_pos.x - next_pos.x, previous_pos.y + wire_size - next_pos.y))
            wire.SetPoints([wxPoint(previous_pos.x, previous_pos.y + wire_size), 
                wxPoint(previous_pos.x, previous_pos.y)])
            if isinstance(next_block, SFC_Divergence):
                next_block.RefreshPosition()
            next_block.RefreshModel()
        self.Parent.RefreshProjectTree()

    def DeleteJump(self, jump):
        previous = jump.GetPreviousConnector()
        previous_block = previous.GetParentBlock()
        if isinstance(previous_block, SFC_Transition):
            self.RemoveTransition(previous_block)
            previous = jump.GetPreviousConnector()
            if previous:
                previous_block = previous.GetParentBlock()
            else:
                previous_block = None
        wires = previous.GetWires()
        if len(wires) != 1:
            return
        wire = wires[0][0]
        wire.Clean()
        self.Wires.remove(wire)
        self.Elements.remove(wire)
        jump.Clean()
        self.Blocks.remove(jump)
        self.Elements.remove(jump)
        self.Controler.RemoveCurrentElementEditingInstance(jump.GetId())
        if isinstance(previous_block, SFC_Step):
            previous_block.RemoveOutput()
            self.RefreshStepModel(previous_block)
        elif isinstance(previous_block, SFC_Divergence):
            if previous_block.GetType() in [SELECTION_CONVERGENCE, SIMULTANEOUS_CONVERGENCE]:
                self.DeleteDivergence(previous_block)
            else:
                previous_block.RemoveBranch(previous)
                if previous_block.GetBranchNumber() < 2:
                    self.DeleteDivergence(previous_block)
                else:
                    previous_block.RefreshModel()
        self.Parent.RefreshProjectTree()

    def DeleteActionBlock(self, actionblock):
        connector = actionblock.GetConnector()
        wires = connector.GetWires()
        if len(wires) != 1:
            return
        wire = wires[0][0]
        step = wire.EndConnected.GetParentBlock()
        wire.Clean()
        self.Wires.remove(wire)
        self.Elements.remove(wire)
        actionblock.Clean()
        self.Blocks.remove(actionblock)
        self.Elements.remove(actionblock)
        self.Controler.RemoveCurrentElementEditingInstance(actionblock.GetId())
        step.RemoveAction()
        self.RefreshStepModel(step)
        step.RefreshOutputPosition()
        step.RefreshOutputModel(True)
        self.Parent.RefreshProjectTree()

    def DeleteComment(self, comment):
        self.Elements.remove(self.SelectedElement)
        self.Controler.RemoveCurrentElementEditingInstance(comment.GetId())
        
    def DeleteWire(self, wire):
        pass


#-------------------------------------------------------------------------------
#                          Edit Transition Content Dialog
#-------------------------------------------------------------------------------

[wxID_TRANSITIONCONTENTDIALOG, wxID_TRANSITIONCONTENTDIALOGMAINPANEL, 
 wxID_TRANSITIONCONTENTDIALOGREFERENCE, wxID_TRANSITIONCONTENTDIALOGINLINE, 
 wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON1, wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON2, 
] = [wx.NewId() for _init_ctrls in range(6)]

class TransitionContentDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_TRANSITIONCONTENTDIALOG,
              name='ProjectDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(300, 200), style=wx.DEFAULT_DIALOG_STYLE,
              title='Edit transition')
        self.SetClientSize(wx.Size(300, 200))

        self.MainPanel = wx.Panel(id=wxID_TRANSITIONCONTENTDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(300, 200), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.radioButton1 = wx.RadioButton(id=wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON1,
              label='Reference', name='radioButton1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(114, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON1, self.OnTypeChanged)
        self.radioButton1.SetValue(True)

        self.Reference = wx.Choice(id=wxID_TRANSITIONCONTENTDIALOGREFERENCE,
              name='Reference', parent=self.MainPanel, pos=wx.Point(48, 48), 
              size=wx.Size(200, 24), style=0)

        self.radioButton2 = wx.RadioButton(id=wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON2,
              label='Inline', name='radioButton2', parent=self.MainPanel,
              pos=wx.Point(24, 72), size=wx.Size(114, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_TRANSITIONCONTENTDIALOGRADIOBUTTON2, self.OnTypeChanged)
        self.radioButton2.SetValue(False)

        self.Inline = wx.TextCtrl(id=wxID_TRANSITIONCONTENTDIALOGINLINE,
              name='Inline', parent=self.MainPanel, pos=wx.Point(48, 96),
              size=wx.Size(200, 24), style=0)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
        
        EVT_BUTTON(self, self.ButtonSizer.GetAffirmativeButton().GetId(), self.OnOK)
    
    def OnOK(self, event):
        error = []
        if self.radioButton1.GetValue() and self.Reference.GetStringSelection() == "":
            error.append("Reference")
        if self.radioButton2.GetValue() and self.Inline.GetValue() == "":
            error.append("Inline")
        if len(error) > 0:
            text = ""
            for i, item in enumerate(error):
                if i == 0:
                    text += item
                elif i == len(error) - 1:
                    text += " and %s"%item
                else:
                    text += ", %s"%item 
            message = wxMessageDialog(self, "Form isn't complete. %s must be filled!"%text, "Error", wxOK|wxICON_ERROR)
            message.ShowModal()
            message.Destroy()
        else:
            self.EndModal(wxID_OK)

    def OnTypeChanged(self, event):
        if self.radioButton1.GetValue():
            self.Reference.Enable(True)
            self.Inline.Enable(False)
        else:
            self.Reference.Enable(False)
            self.Inline.Enable(True)
        event.Skip()

    def SetTransitions(self, transitions):
        for transition in transitions:
            self.Reference.Append(transition)

    def SetValues(self, values):
        if values["type"] == "reference":
            self.radioButton1.SetValue(True)
            self.radioButton2.SetValue(False)
            self.Reference.Enable(True)
            self.Inline.Enable(False)
            self.Reference.SetStringSelection(values["value"])
        elif values["type"] == "inline":
            self.radioButton1.SetValue(False)
            self.radioButton2.SetValue(True)
            self.Reference.Enable(False)
            self.Inline.Enable(True)
            self.Inline.SetValue(values["value"])
                
    def GetValues(self):
        values = {}
        if self.radioButton1.GetValue():
            values["type"] = "reference"
            values["value"] = self.Reference.GetStringSelection()
        else:
            values["type"] = "inline"
            values["value"] = self.Inline.GetValue()
        return values

#-------------------------------------------------------------------------------
#                         Create New Divergence Dialog
#-------------------------------------------------------------------------------

[wxID_DIVERGENCECREATEDIALOG, wxID_DIVERGENCECREATEDIALOGMAINPANEL, 
 wxID_DIVERGENCECREATEDIALOGRADIOBUTTON1, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON2,
 wxID_DIVERGENCECREATEDIALOGRADIOBUTTON3, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON4, 
 wxID_DIVERGENCECREATEDIALOGSEQUENCES, wxID_DIVERGENCECREATEDIALOGPREVIEW, 
 wxID_DIVERGENCECREATEDIALOGSTATICTEXT1, wxID_DIVERGENCECREATEDIALOGSTATICTEXT2, 
 wxID_DIVERGENCECREATEDIALOGSTATICTEXT3,  
] = [wx.NewId() for _init_ctrls in range(11)]

class DivergenceCreateDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DIVERGENCECREATEDIALOG,
              name='DivergencePropertiesDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(500, 300), style=wx.DEFAULT_DIALOG_STYLE,
              title='Create a new divergence or convergence')
        self.SetClientSize(wx.Size(500, 260))

        self.MainPanel = wx.Panel(id=wxID_DIVERGENCECREATEDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(600, 220), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.staticText1 = wx.StaticText(id=wxID_DIVERGENCECREATEDIALOGSTATICTEXT1,
              label='Type:', name='staticText1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(200, 17), style=0)

        self.radioButton1 = wx.RadioButton(id=wxID_DIVERGENCECREATEDIALOGRADIOBUTTON1,
              label='Selection Divergence', name='radioButton1', parent=self.MainPanel,
              pos=wx.Point(24, 48), size=wx.Size(200, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON1, self.OnTypeChanged)
        self.radioButton1.SetValue(True)

        self.radioButton2 = wx.RadioButton(id=wxID_DIVERGENCECREATEDIALOGRADIOBUTTON2,
              label='Selection Convergence', name='radioButton2', parent=self.MainPanel, 
              pos=wx.Point(24, 72), size=wx.Size(200, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON2, self.OnTypeChanged)
        self.radioButton2.SetValue(False)

        self.radioButton3 = wx.RadioButton(id=wxID_DIVERGENCECREATEDIALOGRADIOBUTTON3,
              label='Simultaneous Divergence', name='radioButton3', parent=self.MainPanel,
              pos=wx.Point(24, 96), size=wx.Size(200, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON3, self.OnTypeChanged)
        self.radioButton3.SetValue(False)

        self.radioButton4 = wx.RadioButton(id=wxID_DIVERGENCECREATEDIALOGRADIOBUTTON4,
              label='Simultaneous Convergence', name='radioButton4', parent=self.MainPanel, 
              pos=wx.Point(24, 120), size=wx.Size(200, 24), style=0)
        EVT_RADIOBUTTON(self, wxID_DIVERGENCECREATEDIALOGRADIOBUTTON4, self.OnTypeChanged)
        self.radioButton4.SetValue(False)

        self.staticText2 = wx.StaticText(id=wxID_DIVERGENCECREATEDIALOGSTATICTEXT2,
              label='Number of sequences:', name='staticText2', parent=self.MainPanel,
              pos=wx.Point(24, 150), size=wx.Size(200, 17), style=0)

        self.Sequences = wx.SpinCtrl(id=wxID_DIVERGENCECREATEDIALOGSEQUENCES,
              name='Sequences', parent=self.MainPanel, pos=wx.Point(24, 174),
              size=wx.Size(200, 24), style=0, min=2, max=20)
        EVT_SPINCTRL(self, wxID_DIVERGENCECREATEDIALOGSEQUENCES, self.OnSequencesChanged)

        self.staticText3 = wx.StaticText(id=wxID_DIVERGENCECREATEDIALOGSTATICTEXT3,
              label='Preview:', name='staticText3', parent=self.MainPanel,
              pos=wx.Point(250, 24), size=wx.Size(100, 17), style=0)

        self.Preview = wx.Panel(id=wxID_DIVERGENCECREATEDIALOGPREVIEW,
              name='Preview', parent=self.MainPanel, pos=wx.Point(250, 48),
              size=wx.Size(225, 150), style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
        self.Preview.SetBackgroundColour(wxColour(255,255,255))

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
        
        self.Divergence = None
        
        EVT_PAINT(self, self.OnPaint)

    def GetValues(self):
        values = {}
        if self.radioButton1.GetValue():
            values["type"] = SELECTION_DIVERGENCE
        elif self.radioButton2.GetValue():
            values["type"] = SELECTION_CONVERGENCE
        elif self.radioButton3.GetValue():
            values["type"] = SIMULTANEOUS_DIVERGENCE
        else:
            values["type"] = SIMULTANEOUS_CONVERGENCE
        values["number"] = self.Sequences.GetValue()
        return values

    def OnTypeChanged(self, event):
        self.RefreshPreview()
        event.Skip()

    def OnSequencesChanged(self, event):
        self.RefreshPreview()
        event.Skip()
        
    def RefreshPreview(self):
        dc = wxClientDC(self.Preview)
        dc.Clear()
        if self.radioButton1.GetValue():
            self.Divergence = SFC_Divergence(self.Preview, SELECTION_DIVERGENCE, self.Sequences.GetValue())
        elif self.radioButton2.GetValue():
            self.Divergence = SFC_Divergence(self.Preview, SELECTION_CONVERGENCE, self.Sequences.GetValue())
        elif self.radioButton3.GetValue():
            self.Divergence = SFC_Divergence(self.Preview, SIMULTANEOUS_DIVERGENCE, self.Sequences.GetValue())
        else:
            self.Divergence = SFC_Divergence(self.Preview, SIMULTANEOUS_CONVERGENCE, self.Sequences.GetValue())
        width, height = self.Divergence.GetSize()
        clientsize = self.Preview.GetClientSize()
        x = (clientsize.width - width) / 2
        y = (clientsize.height - height) / 2
        self.Divergence.SetPosition(x, y)
        self.Divergence.Draw(dc)

    def OnPaint(self, event):
        self.RefreshPreview()


#-------------------------------------------------------------------------------
#                            Action Block Dialog
#-------------------------------------------------------------------------------

class ActionTable(wxPyGridTableBase):
    
    """
    A custom wxGrid Table using user supplied data
    """
    def __init__(self, parent, data, colnames):
        # The base class must be initialized *first*
        wxPyGridTableBase.__init__(self)
        self.data = data
        self.colnames = colnames
        self.Parent = parent
        # XXX
        # we need to store the row length and collength to
        # see if the table has changed size
        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()
    
    def GetNumberCols(self):
        return len(self.colnames)
        
    def GetNumberRows(self):
        return len(self.data)

    def GetColLabelValue(self, col):
        if col < len(self.colnames):
            return self.colnames[col]

    def GetRowLabelValues(self, row):
        return row

    def GetValue(self, row, col):
        if row < self.GetNumberRows():
            name = str(self.data[row].get(self.GetColLabelValue(col), ""))
            return name
    
    def GetValueByName(self, row, colname):
        return self.data[row].get(colname)

    def SetValue(self, row, col, value):
        if col < len(self.colnames):
            self.data[row][self.GetColLabelValue(col)] = value
        
    def ResetView(self, grid):
        """
        (wxGrid) -> Reset the grid view.   Call this to
        update the grid if rows and columns have been added or deleted
        """
        grid.BeginBatch()
        for current, new, delmsg, addmsg in [
            (self._rows, self.GetNumberRows(), wxGRIDTABLE_NOTIFY_ROWS_DELETED, wxGRIDTABLE_NOTIFY_ROWS_APPENDED),
            (self._cols, self.GetNumberCols(), wxGRIDTABLE_NOTIFY_COLS_DELETED, wxGRIDTABLE_NOTIFY_COLS_APPENDED),
        ]:
            if new < current:
                msg = wxGridTableMessage(self,delmsg,new,current-new)
                grid.ProcessTableMessage(msg)
            elif new > current:
                msg = wxGridTableMessage(self,addmsg,new-current)
                grid.ProcessTableMessage(msg)
                self.UpdateValues(grid)
        grid.EndBatch()

        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()
        # update the column rendering scheme
        self._updateColAttrs(grid)

        # update the scrollbars and the displayed part of the grid
        grid.AdjustScrollbars()
        grid.ForceRefresh()

    def UpdateValues(self, grid):
        """Update all displayed values"""
        # This sends an event to the grid table to update all of the values
        msg = wxGridTableMessage(self, wxGRIDTABLE_REQUEST_VIEW_GET_VALUES)
        grid.ProcessTableMessage(msg)

    def _updateColAttrs(self, grid):
        """
        wxGrid -> update the column attributes to add the
        appropriate renderer given the column name.

        Otherwise default to the default renderer.
        """
        
        for col in range(self.GetNumberCols()):
            attr = wxGridCellAttr()
            attr.SetAlignment(self.Parent.ColAlignements[col], wxALIGN_CENTRE)
            grid.SetColAttr(col, attr)
            grid.SetColSize(col, self.Parent.ColSizes[col])
        
        typelist = None
        accesslist = None
        for row in range(self.GetNumberRows()):
            for col in range(self.GetNumberCols()):
                editor = None
                renderer = None
                readonly = False
                colname = self.GetColLabelValue(col)
                if colname == "Qualifier":
                    editor = wxGridCellChoiceEditor()
                    editor.SetParameters(self.Parent.QualifierList)
                if colname == "Duration":
                    editor = wxGridCellTextEditor()
                    renderer = wxGridCellStringRenderer()
                    if self.Parent.DurationList[self.data[row]["Qualifier"]]:
                        readonly = False
                    else:
                        readonly = True
                        self.data[row]["Duration"] = ""
                elif colname == "Type":
                    editor = wxGridCellChoiceEditor()
                    editor.SetParameters(self.Parent.TypeList)
                elif colname == "Value":
                    type = self.data[row]["Type"]
                    if type == "Action":
                        editor = wxGridCellChoiceEditor()
                        editor.SetParameters(self.Parent.ActionList)
                    elif type == "Variable":
                        editor = wxGridCellChoiceEditor()
                        editor.SetParameters(self.Parent.VariableList)
                    elif type == "Inline":
                        editor = wxGridCellTextEditor()
                        renderer = wxGridCellStringRenderer()
                elif colname == "Indicator":
                    editor = wxGridCellChoiceEditor()
                    editor.SetParameters(self.Parent.VariableList)
                    
                grid.SetCellEditor(row, col, editor)
                grid.SetCellRenderer(row, col, renderer)
                grid.SetReadOnly(row, col, readonly)
                
                grid.SetCellBackgroundColour(row, col, wxWHITE)
    
    def SetData(self, data):
        self.data = data
    
    def GetData(self):
        return self.data
    
    def GetCurrentIndex(self):
        return self.CurrentIndex
    
    def SetCurrentIndex(self, index):
        self.CurrentIndex = index
    
    def AppendRow(self, row_content):
        self.data.append(row_content)

    def RemoveRow(self, row_index):
        self.data.pop(row_index)
        
    def MoveRow(self, row_index, move, grid):
        new_index = max(0, min(row_index + move, len(self.data) - 1))
        if new_index != row_index:
            self.data.insert(new_index, self.data.pop(row_index))
            grid.SetGridCursor(new_index, grid.GetGridCursorCol())

    def Empty(self):
        self.data = []
        self.editors = []

[wxID_ACTIONBLOCKDIALOG, wxID_ACTIONBLOCKDIALOGMAINPANEL, 
 wxID_ACTIONBLOCKDIALOGVARIABLESGRID, wxID_ACTIONBLOCKDIALOGSTATICTEXT1, 
 wxID_ACTIONBLOCKDIALOGADDBUTTON,wxID_ACTIONBLOCKDIALOGDELETEBUTTON, 
 wxID_ACTIONBLOCKDIALOGUPBUTTON, wxID_ACTIONBLOCKDIALOGDOWNBUTTON, 
] = [wx.NewId() for _init_ctrls in range(8)]

class ActionBlockDialog(wx.Dialog):
    def _init_coll_flexGridSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.MainPanel, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.flexGridSizer1 = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)

        self._init_coll_flexGridSizer1_Items(self.flexGridSizer1)

        self.SetSizer(self.flexGridSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_ACTIONBLOCKDIALOG,
              name='ActionBlockDialog', parent=prnt, pos=wx.Point(376, 223),
              size=wx.Size(500, 300), style=wx.DEFAULT_DIALOG_STYLE,
              title='Edit action block properties')
        self.SetClientSize(wx.Size(500, 300))

        self.MainPanel = wx.Panel(id=wxID_ACTIONBLOCKDIALOGMAINPANEL,
              name='MainPanel', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(500, 300), style=wx.TAB_TRAVERSAL)
        self.MainPanel.SetAutoLayout(True)

        self.staticText1 = wx.StaticText(id=wxID_ACTIONBLOCKDIALOGSTATICTEXT1,
              label='Actions:', name='staticText1', parent=self.MainPanel,
              pos=wx.Point(24, 24), size=wx.Size(95, 17), style=0)

        self.ActionsGrid = wx.grid.Grid(id=wxID_ACTIONBLOCKDIALOGVARIABLESGRID,
              name='ActionsGrid', parent=self.MainPanel, pos=wx.Point(24, 44), 
              size=wx.Size(450, 150), style=wxVSCROLL)
        self.ActionsGrid.SetFont(wx.Font(12, 77, wx.NORMAL, wx.NORMAL, False,
              'Sans'))
        self.ActionsGrid.SetLabelFont(wx.Font(10, 77, wx.NORMAL, wx.NORMAL,
              False, 'Sans'))
        self.ActionsGrid.DisableDragGridSize()
        self.ActionsGrid.EnableScrolling(False, True)
        EVT_GRID_CELL_CHANGE(self.ActionsGrid, self.OnActionsGridCellChange)

        self.AddButton = wx.Button(id=wxID_ACTIONBLOCKDIALOGADDBUTTON, label='Add',
              name='AddButton', parent=self.MainPanel, pos=wx.Point(245, 204),
              size=wx.Size(72, 32), style=0)
        EVT_BUTTON(self, wxID_ACTIONBLOCKDIALOGADDBUTTON, self.OnAddButton)

        self.DeleteButton = wx.Button(id=wxID_ACTIONBLOCKDIALOGDELETEBUTTON, label='Delete',
              name='DeleteButton', parent=self.MainPanel, pos=wx.Point(325, 204),
              size=wx.Size(72, 32), style=0)
        EVT_BUTTON(self, wxID_ACTIONBLOCKDIALOGDELETEBUTTON, self.OnDeleteButton)

        self.UpButton = wx.Button(id=wxID_ACTIONBLOCKDIALOGUPBUTTON, label='^',
              name='UpButton', parent=self.MainPanel, pos=wx.Point(405, 204),
              size=wx.Size(32, 32), style=0)
        EVT_BUTTON(self, wxID_ACTIONBLOCKDIALOGUPBUTTON, self.OnUpButton)

        self.DownButton = wx.Button(id=wxID_ACTIONBLOCKDIALOGDOWNBUTTON, label='v',
              name='DownButton', parent=self.MainPanel, pos=wx.Point(445, 204),
              size=wx.Size(32, 32), style=0)
        EVT_BUTTON(self, wxID_ACTIONBLOCKDIALOGDOWNBUTTON, self.OnDownButton)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.ButtonSizer = self.CreateButtonSizer(wxOK|wxCANCEL)
        self.flexGridSizer1.Add(self.ButtonSizer, 1, wxALIGN_RIGHT)
        
        self.DefaultValue = {"Qualifier" : "N", "Duration" : "", "Type" : "Action", "Value" : "", "Indicator" : ""}
        self.Table = ActionTable(self, [], ["Qualifier","Duration","Type","Value","Indicator"])
        self.TypeList = "Action,Variable,Inline"
        self.ColSizes = [60, 90, 80, 110, 80]
        self.ColAlignements = [wxALIGN_LEFT, wxALIGN_LEFT, wxALIGN_LEFT, wxALIGN_LEFT, wxALIGN_LEFT]
        
        self.ActionsGrid.SetTable(self.Table)
        self.ActionsGrid.SetRowLabelSize(0)
        
        self.Table.ResetView(self.ActionsGrid)

    def OnAddButton(self, event):
        self.Table.AppendRow(self.DefaultValue.copy())
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def OnDeleteButton(self, event):
        row = self.ActionsGrid.GetGridCursorRow()
        self.Table.RemoveRow(row)
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def OnUpButton(self, event):
        row = self.ActionsGrid.GetGridCursorRow()
        self.Table.MoveRow(row, -1, self.ActionsGrid)
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def OnDownButton(self, event):
        row = self.ActionsGrid.GetGridCursorRow()
        self.Table.MoveRow(row, 1, self.ActionsGrid)
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def OnActionsGridCellChange(self, event):
        self.Table.ResetView(self.ActionsGrid)
        event.Skip()

    def SetQualifierList(self, list):
        self.QualifierList = ""
        sep = ""
        for qualifier in list.keys():
            self.QualifierList += "%s%s"%(sep, qualifier)
            sep = ","
        self.DurationList = list

    def SetVariableList(self, list):
        self.VariableList = ""
        sep = ""
        for variable in list:
            self.VariableList += "%s%s"%(sep, variable["Name"])
            sep = ","

    def SetActionList(self, list):
        self.ActionList = ""
        sep = ""
        for action in list:
            self.ActionList += "%s%s"%(sep, action)
            sep = ","

    def SetValues(self, actions):
        for action in actions:
            row = {"Qualifier" : action["qualifier"], "Value" : action["value"],
                "Indicator" : action["indicator"]}
            if action["type"] == "reference":
                if action["value"] in self.ActionList:
                    row["Type"] = "Action"
                elif action["value"] in self.VariableList:
                    row["Type"] = "Variable"
                else:
                    row["Type"] = "Inline"
            else:
                row["Type"] = "Inline"
            if "duration" in action:
                row["Duration"] = action["duration"]
            self.Table.AppendRow(row)
        self.Table.ResetView(self.ActionsGrid)
    
    def GetValues(self):
        values = []
        for data in self.Table.GetData():
            action = {"qualifier" : data["Qualifier"], "value" : data["Value"],
                "indicator" : data["Indicator"]}
            if data["Type"] in ["Action", "Variable"]:
                action["type"] = "reference"
            else:
                action["type"] = "inline"
            if data["Duration"] != "":
                action["duration"] = data["Duration"]
            values.append(action)
        return values