Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
Laurent@814: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
Laurent@814: #based on the plcopen standard. 
Laurent@814: #
Laurent@814: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
Laurent@814: #See COPYING file for copyrights details.
Laurent@814: #
Laurent@814: #This library is free software; you can redistribute it and/or
Laurent@814: #modify it under the terms of the GNU General Public
Laurent@814: #License as published by the Free Software Foundation; either
Laurent@814: #version 2.1 of the License, or (at your option) any later version.
Laurent@814: #
Laurent@814: #This library is distributed in the hope that it will be useful,
Laurent@814: #but WITHOUT ANY WARRANTY; without even the implied warranty of
Laurent@814: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Laurent@814: #General Public License for more details.
Laurent@814: #
Laurent@814: #You should have received a copy of the GNU General Public
Laurent@814: #License along with this library; if not, write to the Free Software
Laurent@814: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Laurent@814: 
Laurent@814: import re
Laurent@814: import math
Laurent@814: import time
Laurent@814: from types import TupleType
Laurent@814: from threading import Lock
Laurent@814: 
Laurent@814: import wx
Laurent@814: 
Laurent@814: from plcopen.structures import *
Laurent@885: from PLCControler import ITEM_VAR_LOCAL, ITEM_POU, ITEM_PROGRAM, ITEM_FUNCTIONBLOCK
Laurent@814: 
Laurent@814: from dialogs import *
Laurent@814: from graphics import *
Laurent@814: from EditorPanel import EditorPanel
Laurent@814: 
Laurent@814: SCROLLBAR_UNIT = 10
Laurent@814: WINDOW_BORDER = 10
Laurent@814: SCROLL_ZONE = 10
Laurent@814: 
Laurent@814: CURSORS = None
Laurent@814: 
Laurent@814: def ResetCursors():
Laurent@814:     global CURSORS
Laurent@814:     if CURSORS == None:
Laurent@814:         CURSORS = [wx.NullCursor, 
Laurent@814:                    wx.StockCursor(wx.CURSOR_HAND),
Laurent@814:                    wx.StockCursor(wx.CURSOR_SIZENWSE),
Laurent@814:                    wx.StockCursor(wx.CURSOR_SIZENESW),
Laurent@814:                    wx.StockCursor(wx.CURSOR_SIZEWE),
Laurent@814:                    wx.StockCursor(wx.CURSOR_SIZENS)]
Laurent@814: 
Laurent@814: def AppendMenu(parent, help, id, kind, text):
Laurent@814:     if wx.VERSION >= (2, 6, 0):
Laurent@814:         parent.Append(help=help, id=id, kind=kind, text=text)
Laurent@814:     else:
Laurent@814:         parent.Append(helpString=help, id=id, kind=kind, item=text)
Laurent@814: 
Laurent@814: if wx.Platform == '__WXMSW__':
Laurent@814:     faces = { 'times': 'Times New Roman',
Laurent@814:               'mono' : 'Courier New',
Laurent@814:               'helv' : 'Arial',
Laurent@814:               'other': 'Comic Sans MS',
Laurent@814:               'size' : 10,
Laurent@814:              }
Laurent@814: else:
Laurent@814:     faces = { 'times': 'Times',
Laurent@814:               'mono' : 'Courier',
Laurent@814:               'helv' : 'Helvetica',
Laurent@814:               'other': 'new century schoolbook',
Laurent@814:               'size' : 12,
Laurent@814:              }
Laurent@814: 
Laurent@814: ZOOM_FACTORS = [math.sqrt(2) ** x for x in xrange(-6, 7)]
Laurent@814: 
Laurent@814: def GetVariableCreationFunction(variable_type):
Laurent@814:     def variableCreationFunction(viewer, id, specific_values):
Laurent@814:         return FBD_Variable(viewer, variable_type, 
Laurent@814:                                     specific_values["name"], 
Laurent@814:                                     specific_values["value_type"], 
Laurent@814:                                     id,
Laurent@814:                                     specific_values["executionOrder"])
Laurent@814:     return variableCreationFunction
Laurent@814: 
Laurent@814: def GetConnectorCreationFunction(connector_type):
Laurent@814:     def connectorCreationFunction(viewer, id, specific_values):
Laurent@814:         return FBD_Connector(viewer, connector_type, 
Laurent@814:                                      specific_values["name"], id)
Laurent@814:     return connectorCreationFunction
Laurent@814: 
Laurent@814: def commentCreationFunction(viewer, id, specific_values):
Laurent@814:     return Comment(viewer, specific_values["content"], id)
Laurent@814: 
Laurent@814: def GetPowerRailCreationFunction(powerrail_type):
Laurent@814:     def powerRailCreationFunction(viewer, id, specific_values):
Laurent@814:         return LD_PowerRail(viewer, powerrail_type, id, 
Laurent@814:                                     specific_values["connectors"])
Laurent@814:     return powerRailCreationFunction
Laurent@814: 
Laurent@814: CONTACT_TYPES = {(True, "none"): CONTACT_REVERSE,
Laurent@814:                  (False, "rising"): CONTACT_RISING,
Laurent@814:                  (False, "falling"): CONTACT_FALLING}
Laurent@814: 
Laurent@814: def contactCreationFunction(viewer, id, specific_values):
Laurent@814:     contact_type = CONTACT_TYPES.get((specific_values.get("negated", False), 
Laurent@814:                                       specific_values.get("edge", "none")),
Laurent@814:                                      CONTACT_NORMAL)
Laurent@814:     return LD_Contact(viewer, contact_type, specific_values["name"], id)
Laurent@814: 
Laurent@814: COIL_TYPES = {(True, "none", "none"): COIL_REVERSE,
Laurent@814:               (False, "none", "set"): COIL_SET,
Laurent@814:               (False, "none", "reset"): COIL_RESET,
Laurent@814:               (False, "rising", "none"): COIL_RISING,
Laurent@814:               (False, "falling", "none"): COIL_FALLING}
Laurent@814: 
Laurent@814: def coilCreationFunction(viewer, id, specific_values):
Laurent@814:     coil_type = COIL_TYPES.get((specific_values.get("negated", False), 
Laurent@814:                                 specific_values.get("edge", "none"),
Laurent@814:                                 specific_values.get("storage", "none")),
Laurent@814:                                COIL_NORMAL)
Laurent@814:     return LD_Coil(viewer, coil_type, specific_values["name"], id)
Laurent@814: 
Laurent@814: def stepCreationFunction(viewer, id, specific_values):
Laurent@814:     step = SFC_Step(viewer, specific_values["name"], 
Laurent@814:                             specific_values.get("initial", False), id)
Laurent@814:     if specific_values.get("action", None):
Laurent@814:         step.AddAction()
Laurent@814:         connector = step.GetActionConnector()
Laurent@814:         connector.SetPosition(wx.Point(*specific_values["action"]["position"]))
Laurent@814:     return step
Laurent@814: 
Laurent@814: def transitionCreationFunction(viewer, id, specific_values):
Laurent@814:     transition = SFC_Transition(viewer, specific_values["condition_type"], 
Laurent@814:                                         specific_values.get("condition", None), 
Laurent@814:                                         specific_values["priority"], id)
Laurent@814:     return transition
Laurent@814: 
Laurent@814: def GetDivergenceCreationFunction(divergence_type):
Laurent@814:     def divergenceCreationFunction(viewer, id, specific_values):
Laurent@814:         return SFC_Divergence(viewer, divergence_type, 
Laurent@814:                                       specific_values["connectors"], id)
Laurent@814:     return divergenceCreationFunction
Laurent@814: 
Laurent@814: def jumpCreationFunction(viewer, id, specific_values):
Laurent@814:     return SFC_Jump(viewer, specific_values["target"], id)
Laurent@814: 
Laurent@814: def actionBlockCreationFunction(viewer, id, specific_values):
Laurent@814:     return SFC_ActionBlock(viewer, specific_values["actions"], id)
Laurent@814: 
Laurent@814: ElementCreationFunctions = {
Laurent@814:     "input": GetVariableCreationFunction(INPUT),
Laurent@814:     "output": GetVariableCreationFunction(OUTPUT),
Laurent@814:     "inout": GetVariableCreationFunction(INOUT),
Laurent@814:     "connector": GetConnectorCreationFunction(CONNECTOR),
Laurent@814:     "continuation": GetConnectorCreationFunction(CONTINUATION),
Laurent@814:     "comment": commentCreationFunction,
Laurent@814:     "leftPowerRail": GetPowerRailCreationFunction(LEFTRAIL),
Laurent@814:     "rightPowerRail": GetPowerRailCreationFunction(RIGHTRAIL),
Laurent@814:     "contact": contactCreationFunction,
Laurent@814:     "coil": coilCreationFunction,
Laurent@814:     "step": stepCreationFunction, 
Laurent@814:     "transition": transitionCreationFunction,
Laurent@814:     "selectionDivergence": GetDivergenceCreationFunction(SELECTION_DIVERGENCE), 
Laurent@814:     "selectionConvergence": GetDivergenceCreationFunction(SELECTION_CONVERGENCE), 
Laurent@814:     "simultaneousDivergence": GetDivergenceCreationFunction(SIMULTANEOUS_DIVERGENCE), 
Laurent@814:     "simultaneousConvergence": GetDivergenceCreationFunction(SIMULTANEOUS_CONVERGENCE), 
Laurent@814:     "jump": jumpCreationFunction,
Laurent@814:     "actionBlock": actionBlockCreationFunction,
Laurent@814: }
Laurent@814: 
Laurent@814: def sort_blocks(block_infos1, block_infos2):
Laurent@814:     x1, y1 = block_infos1[0].GetPosition()
Laurent@814:     x2, y2 = block_infos2[0].GetPosition()
Laurent@814:     if y1 == y2:
Laurent@814:         return cmp(x1, x2)
Laurent@814:     else:
Laurent@814:         return cmp(y1, y2)
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                       Graphic elements Viewer base class
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814: # ID Constants for alignment menu items
Laurent@814: [ID_VIEWERALIGNMENTMENUITEMS0, ID_VIEWERALIGNMENTMENUITEMS1,
Laurent@814:  ID_VIEWERALIGNMENTMENUITEMS2, ID_VIEWERALIGNMENTMENUITEMS4,
Laurent@814:  ID_VIEWERALIGNMENTMENUITEMS5, ID_VIEWERALIGNMENTMENUITEMS6,
Laurent@814: ] = [wx.NewId() for _init_coll_AlignmentMenu_Items in range(6)]
Laurent@814: 
Laurent@814: # ID Constants for contextual menu items
Laurent@814: [ID_VIEWERCONTEXTUALMENUITEMS0, ID_VIEWERCONTEXTUALMENUITEMS1,
Laurent@814:  ID_VIEWERCONTEXTUALMENUITEMS2, ID_VIEWERCONTEXTUALMENUITEMS3,
Laurent@814:  ID_VIEWERCONTEXTUALMENUITEMS5, ID_VIEWERCONTEXTUALMENUITEMS6,
Laurent@814:  ID_VIEWERCONTEXTUALMENUITEMS8, ID_VIEWERCONTEXTUALMENUITEMS9,
Laurent@814:  ID_VIEWERCONTEXTUALMENUITEMS11, ID_VIEWERCONTEXTUALMENUITEMS12,
Laurent@814:  ID_VIEWERCONTEXTUALMENUITEMS14, ID_VIEWERCONTEXTUALMENUITEMS16,
Laurent@814:  ID_VIEWERCONTEXTUALMENUITEMS17,
Laurent@814: ] = [wx.NewId() for _init_coll_ContextualMenu_Items in range(13)]
Laurent@814: 
Laurent@814: 
Laurent@814: class ViewerDropTarget(wx.TextDropTarget):
Laurent@814:     
Laurent@814:     def __init__(self, parent):
Laurent@814:         wx.TextDropTarget.__init__(self)
Laurent@814:         self.ParentWindow = parent
Laurent@814:     
Laurent@814:     def OnDropText(self, x, y, data):
Laurent@814:         self.ParentWindow.Select()
Laurent@814:         tagname = self.ParentWindow.GetTagName()
Laurent@814:         pou_name, pou_type = self.ParentWindow.Controler.GetEditedElementType(tagname, self.ParentWindow.Debug)
Laurent@814:         x, y = self.ParentWindow.CalcUnscrolledPosition(x, y)
Laurent@814:         x = int(x / self.ParentWindow.ViewScale[0])
Laurent@814:         y = int(y / self.ParentWindow.ViewScale[1])
Laurent@814:         scaling = self.ParentWindow.Scaling
Laurent@814:         message = None
Laurent@814:         try:
Laurent@814:             values = eval(data)
Laurent@814:         except:
Laurent@814:             message = _("Invalid value \"%s\" for viewer block")%data
Laurent@814:             values = None
Laurent@814:         if not isinstance(values, TupleType):
Laurent@814:             message = _("Invalid value \"%s\" for viewer block")%data
Laurent@814:             values = None
Laurent@814:         if values is not None:
Laurent@814:             if values[1] == "debug":
Laurent@814:                 pass
Laurent@814:             elif values[1] == "program":
Laurent@814:                 message = _("Programs can't be used by other POUs!")
Laurent@814:             elif values[1] in ["function", "functionBlock"]:
Laurent@814:                 words = tagname.split("::")
Laurent@814:                 if pou_name == values[0]:
Laurent@814:                     message = _("\"%s\" can't use itself!")%pou_name
Laurent@814:                 elif pou_type == "function" and values[1] != "function":
Laurent@814:                     message = _("Function Blocks can't be used in Functions!")
Laurent@814:                 elif self.ParentWindow.Controler.PouIsUsedBy(pou_name, values[0], self.ParentWindow.Debug):
Laurent@814:                     message = _("\"%s\" is already used by \"%s\"!")%(pou_name, values[0])
Laurent@814:                 else:
Laurent@814:                     blockname = values[2]
Laurent@814:                     if len(values) > 3:
Laurent@814:                         blockinputs = values[3]
Laurent@814:                     else:
Laurent@814:                         blockinputs = None
Laurent@814:                     if values[1] != "function" and blockname == "":
Laurent@814:                         blockname = self.ParentWindow.GenerateNewName(blocktype=values[0])
Laurent@814:                     if blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
Laurent@814:                         message = _("\"%s\" pou already exists!")%blockname
Laurent@814:                     elif blockname.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
Laurent@814:                         message = _("\"%s\" element for this pou already exists!")%blockname
Laurent@814:                     else:
Laurent@814:                         id = self.ParentWindow.GetNewId()
Laurent@814:                         block = FBD_Block(self.ParentWindow, values[0], blockname, id, inputs = blockinputs)
Laurent@814:                         width, height = block.GetMinSize()
Laurent@814:                         if scaling is not None:
Laurent@814:                             x = round(float(x) / float(scaling[0])) * scaling[0]
Laurent@814:                             y = round(float(y) / float(scaling[1])) * scaling[1]
Laurent@814:                             width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
Laurent@814:                             height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
Laurent@814:                         block.SetPosition(x, y)
Laurent@814:                         block.SetSize(width, height)
Laurent@814:                         self.ParentWindow.AddBlock(block)
Laurent@814:                         self.ParentWindow.Controler.AddEditedElementBlock(tagname, id, values[0], blockname)
Laurent@814:                         self.ParentWindow.RefreshBlockModel(block)
Laurent@814:                         self.ParentWindow.RefreshBuffer()
Laurent@814:                         self.ParentWindow.RefreshScrollBars()
Laurent@814:                         self.ParentWindow.RefreshVisibleElements()
Laurent@814:                         self.ParentWindow.RefreshVariablePanel()
Laurent@814:                         self.ParentWindow.Refresh(False)
Laurent@814:             elif values[1] == "location":
Laurent@814:                 if pou_type == "program":
Laurent@814:                     location = values[0]
Laurent@814:                     if not location.startswith("%"):
Laurent@814:                         dialog = wx.SingleChoiceDialog(self.ParentWindow.ParentWindow, 
Laurent@814:                               _("Select a variable class:"), _("Variable class"), 
Laurent@814:                               ["Input", "Output", "Memory"], 
Laurent@814:                               wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL)
Laurent@814:                         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:                             selected = dialog.GetSelection()
Laurent@814:                         else:
Laurent@814:                             selected = None
Laurent@814:                         dialog.Destroy()
Laurent@814:                         if selected is None:
Laurent@814:                             return
Laurent@814:                         if selected == 0:
Laurent@814:                             location = "%I" + location
Laurent@814:                         elif selected == 1:
Laurent@814:                             location = "%Q" + location
Laurent@814:                         else:
Laurent@814:                             location = "%M" + location
Laurent@814:                     var_name = values[3]
Laurent@814:                     if var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
Laurent@814:                         message = _("\"%s\" pou already exists!")%var_name
Laurent@814:                     else:
Laurent@814:                         if location[1] == "Q":
Laurent@814:                             var_class = OUTPUT
Laurent@814:                         else:
Laurent@814:                             var_class = INPUT
Laurent@814:                         if values[2] is not None:
Laurent@814:                             var_type = values[2]
Laurent@814:                         else:
Laurent@814:                             var_type = LOCATIONDATATYPES.get(location[2], ["BOOL"])[0]
Laurent@814:                         if not var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
Laurent@814:                             self.ParentWindow.Controler.AddEditedElementPouVar(tagname, var_type, var_name, location, values[4])
Laurent@814:                             self.ParentWindow.RefreshVariablePanel()
Laurent@940:                             self.ParentWindow.ParentWindow.RefreshPouInstanceVariablesPanel()
Laurent@814:                         self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, var_name, var_type)
Laurent@814:             elif values[1] == "Global":
Laurent@814:                 var_name = values[0]
Laurent@814:                 if var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
Laurent@814:                     message = _("\"%s\" pou already exists!")%var_name
Laurent@814:                 else:
Laurent@814:                     if not var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
Laurent@814:                         self.ParentWindow.Controler.AddEditedElementPouExternalVar(tagname, values[2], var_name)
Laurent@814:                         self.ParentWindow.RefreshVariablePanel()
Laurent@814:                     self.ParentWindow.AddVariableBlock(x, y, scaling, INPUT, var_name, values[2])
Laurent@814:             elif values[1] == "Constant":
Laurent@814:                 self.ParentWindow.AddVariableBlock(x, y, scaling, INPUT, values[0], None)
Laurent@814:             elif values[3] == tagname:
Laurent@814:                 if values[1] == "Output":
Laurent@814:                     var_class = OUTPUT
Laurent@814:                 elif values[1] == "InOut":
Laurent@814:                     var_class = INPUT
Laurent@814:                 else:
Laurent@814:                     var_class = INPUT
Laurent@814:                 tree = dict([(var["Name"], var["Tree"]) for var in self.ParentWindow.Controler.GetEditedElementInterfaceVars(tagname, self.ParentWindow.Debug)]).get(values[0], None)
Laurent@814:                 if tree is not None:
Laurent@814:                     if len(tree[0]) > 0:
Laurent@814:                         menu = wx.Menu(title='')
Laurent@814:                         self.GenerateTreeMenu(x, y, scaling, menu, "", var_class, [(values[0], values[2], tree)])
Laurent@814:                         self.ParentWindow.PopupMenuXY(menu)
Laurent@814:                     else:
Laurent@814:                         self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, values[0], values[2])
Laurent@814:                 else:
Laurent@814:                     message = _("Unknown variable \"%s\" for this POU!") % values[0]
Laurent@814:             else:
Laurent@814:                 message = _("Variable don't belong to this POU!")
Laurent@814:         if message is not None:
Laurent@814:             wx.CallAfter(self.ShowMessage, message)
Laurent@814: 
Laurent@814:     def GenerateTreeMenu(self, x, y, scaling, menu, base_path, var_class, tree):
Laurent@814:         for child_name, child_type, (child_tree, child_dimensions) in tree:
Laurent@814:             if base_path:
Laurent@814:                 child_path = "%s.%s" % (base_path, child_name)
Laurent@814:             else:
Laurent@814:                 child_path = child_name
Laurent@814:             if len(child_dimensions) > 0:
Laurent@814:                 child_path += "[%s]" % ",".join([str(dimension[0]) for dimension in child_dimensions])
Laurent@814:                 child_name += "[]"
Laurent@814:             new_id = wx.NewId()
Laurent@814:             AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=child_name)
Laurent@814:             self.ParentWindow.Bind(wx.EVT_MENU, self.GetAddVariableBlockFunction(x, y, scaling, var_class, child_path, child_type), id=new_id)
Laurent@814:             if len(child_tree) > 0:
Laurent@814:                 new_id = wx.NewId()
Laurent@814:                 child_menu = wx.Menu(title='')
Laurent@814:                 self.GenerateTreeMenu(x, y, scaling, child_menu, child_path, var_class, child_tree)
Laurent@814:                 menu.AppendMenu(new_id, "%s." % child_name, child_menu)
Laurent@814:             
Laurent@814:     def GetAddVariableBlockFunction(self, x, y, scaling, var_class, var_name, var_type):
Laurent@814:         def AddVariableFunction(event):
Laurent@814:             self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, var_name, var_type)
Laurent@814:         return AddVariableFunction
Laurent@814:     
Laurent@814:     def ShowMessage(self, message):
Laurent@814:         message = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK|wx.ICON_ERROR)
Laurent@814:         message.ShowModal()
Laurent@814:         message.Destroy()
Laurent@814: 
Laurent@814: """
Laurent@814: Class that implements a Viewer based on a wx.ScrolledWindow for drawing and 
Laurent@814: manipulating graphic elements
Laurent@814: """
Laurent@814: 
laurent@828: class Viewer(EditorPanel, DebugViewer, DebugDataConsumer):
Laurent@814:     
Laurent@814:     if wx.VERSION < (2, 6, 0):
Laurent@814:         def Bind(self, event, function, id = None):
Laurent@814:             if id is not None:
Laurent@814:                 event(self, id, function)
Laurent@814:             else:
Laurent@814:                 event(self, function)
Laurent@814:     
Laurent@814:     # Add list of menu items to the given menu
Laurent@814:     def AddMenuItems(self, menu, items):
Laurent@814:         for item in items:
Laurent@814:             if item is None:
Laurent@814:                 menu.AppendSeparator()
Laurent@814:             else:
Laurent@814:                 id, kind, text, help, callback = item
Laurent@814:                 AppendMenu(menu, help=help, id=id, kind=kind, text=text)
Laurent@814:                 # Link menu event to corresponding called functions
Laurent@814:                 self.Bind(wx.EVT_MENU, callback, id=id)
Laurent@814:     
Laurent@814:     # Add Block Pin Menu items to the given menu
Laurent@814:     def AddBlockPinMenuItems(self, menu, connector):
Laurent@814:         [ID_NO_MODIFIER, ID_NEGATED, ID_RISING_EDGE, 
Laurent@814:          ID_FALLING_EDGE] = [wx.NewId() for i in xrange(4)]
Laurent@814:         
Laurent@814:         # Create menu items
Laurent@814:         self.AddMenuItems(menu, [
Laurent@814:             (ID_NO_MODIFIER, wx.ITEM_RADIO, _(u'No Modifier'), '', self.OnNoModifierMenu),
Laurent@814:             (ID_NEGATED, wx.ITEM_RADIO, _(u'Negated'), '', self.OnNegatedMenu),
Laurent@814:             (ID_RISING_EDGE, wx.ITEM_RADIO, _(u'Rising Edge'), '', self.OnRisingEdgeMenu),
Laurent@814:             (ID_FALLING_EDGE, wx.ITEM_RADIO, _(u'Falling Edge'), '', self.OnFallingEdgeMenu)])
Laurent@814:         
Laurent@814:         type = self.Controler.GetEditedElementType(self.TagName, self.Debug)
Laurent@814:         menu.Enable(ID_RISING_EDGE, type != "function")
Laurent@814:         menu.Enable(ID_FALLING_EDGE, type != "function")
Laurent@814:         
Laurent@814:         if connector.IsNegated():
Laurent@814:             menu.Check(ID_NEGATED, True)
Laurent@814:         elif connector.GetEdge() == "rising":
Laurent@814:             menu.Check(ID_RISING_EDGE, True)
Laurent@814:         elif connector.GetEdge() == "falling":
Laurent@814:             menu.Check(ID_FALLING_EDGE, True)
Laurent@814:         else:
Laurent@814:             menu.Check(ID_NO_MODIFIER, True)
Laurent@814:         
Laurent@814:     # Add Alignment Menu items to the given menu
Laurent@814:     def AddAlignmentMenuItems(self, menu):
Laurent@814:         [ID_ALIGN_LEFT, ID_ALIGN_CENTER, ID_ALIGN_RIGHT,
Laurent@814:          ID_ALIGN_TOP, ID_ALIGN_MIDDLE, ID_ALIGN_BOTTOM,
Laurent@814:         ] = [wx.NewId() for i in xrange(6)]
Laurent@814:         
Laurent@814:         # Create menu items
Laurent@814:         self.AddMenuItems(menu, [
Laurent@814:             (ID_ALIGN_LEFT, wx.ITEM_NORMAL, _(u'Left'), '', self.OnAlignLeftMenu),
Laurent@814:             (ID_ALIGN_CENTER, wx.ITEM_NORMAL, _(u'Center'), '', self.OnAlignCenterMenu), 
Laurent@814:             (ID_ALIGN_RIGHT, wx.ITEM_NORMAL, _(u'Right'), '', self.OnAlignRightMenu),
Laurent@814:             None,
Laurent@814:             (ID_ALIGN_TOP, wx.ITEM_NORMAL, _(u'Top'), '', self.OnAlignTopMenu), 
Laurent@814:             (ID_ALIGN_MIDDLE, wx.ITEM_NORMAL, _(u'Middle'), '', self.OnAlignMiddleMenu),
Laurent@814:             (ID_ALIGN_BOTTOM, wx.ITEM_NORMAL, _(u'Bottom'), '', self.OnAlignBottomMenu)])
Laurent@814:     
Laurent@814:     # Add Wire Menu items to the given menu
Laurent@814:     def AddWireMenuItems(self, menu, delete=False):
Laurent@814:         [ID_ADD_SEGMENT, ID_DELETE_SEGMENT] = [wx.NewId() for i in xrange(2)]
Laurent@814:         
Laurent@814:         # Create menu items
Laurent@814:         self.AddMenuItems(menu, [
Laurent@814:             (ID_ADD_SEGMENT, wx.ITEM_NORMAL, _(u'Add Wire Segment'), '', self.OnAddSegmentMenu),
Laurent@814:             (ID_DELETE_SEGMENT, wx.ITEM_NORMAL, _(u'Delete Wire Segment'), '', self.OnDeleteSegmentMenu)])
Laurent@814:     
Laurent@814:         menu.Enable(ID_DELETE_SEGMENT, delete)
Laurent@814:     
Laurent@814:     # Add Divergence Menu items to the given menu
Laurent@814:     def AddDivergenceMenuItems(self, menu, delete=False):
Laurent@814:         [ID_ADD_BRANCH, ID_DELETE_BRANCH] = [wx.NewId() for i in xrange(2)]
Laurent@814:         
Laurent@814:         # Create menu items
Laurent@814:         self.AddMenuItems(menu, [
Laurent@814:             (ID_ADD_BRANCH, wx.ITEM_NORMAL, _(u'Add Divergence Branch'), '', self.OnAddBranchMenu),
Laurent@814:             (ID_DELETE_BRANCH, wx.ITEM_NORMAL, _(u'Delete Divergence Branch'), '', self.OnDeleteBranchMenu)])
Laurent@814:         
Laurent@814:         menu.Enable(ID_DELETE_BRANCH, delete)
Laurent@814:     
Laurent@814:     # Add Add Menu items to the given menu
Laurent@814:     def AddAddMenuItems(self, menu):
Laurent@814:         [ID_ADD_BLOCK, ID_ADD_VARIABLE, ID_ADD_CONNECTION,
Laurent@814:          ID_ADD_COMMENT] = [wx.NewId() for i in xrange(4)]
Laurent@814:         
Laurent@814:         # Create menu items
Laurent@814:         self.AddMenuItems(menu, [
Laurent@814:             (ID_ADD_BLOCK, wx.ITEM_NORMAL, _(u'Block'), '', self.GetAddMenuCallBack(self.AddNewBlock)),
Laurent@814:             (ID_ADD_VARIABLE, wx.ITEM_NORMAL, _(u'Variable'), '', self.GetAddMenuCallBack(self.AddNewVariable)),
Laurent@814:             (ID_ADD_CONNECTION, wx.ITEM_NORMAL, _(u'Connection'), '', self.GetAddMenuCallBack(self.AddNewConnection)),
Laurent@814:             None])
Laurent@814:         
Laurent@814:         if self.CurrentLanguage != "FBD":
Laurent@814:             [ID_ADD_POWER_RAIL, ID_ADD_CONTACT, ID_ADD_COIL,
Laurent@814:             ] = [wx.NewId() for i in xrange(3)]
Laurent@814:             
Laurent@814:             # Create menu items
Laurent@814:             self.AddMenuItems(menu, [
Laurent@814:                 (ID_ADD_POWER_RAIL, wx.ITEM_NORMAL, _(u'Power Rail'), '', self.GetAddMenuCallBack(self.AddNewPowerRail)),
Laurent@814:                 (ID_ADD_CONTACT, wx.ITEM_NORMAL, _(u'Contact'), '', self.GetAddMenuCallBack(self.AddNewContact))])
Laurent@814:                 
Laurent@814:             if self.CurrentLanguage != "SFC":
Laurent@814:                 self.AddMenuItems(menu, [
Laurent@814:                      (ID_ADD_COIL, wx.ITEM_NORMAL, _(u'Coil'), '', self.GetAddMenuCallBack(self.AddNewCoil))])
Laurent@814:             
Laurent@814:             menu.AppendSeparator()
Laurent@814:         
Laurent@814:         if self.CurrentLanguage == "SFC":
Laurent@814:             [ID_ADD_INITIAL_STEP, ID_ADD_STEP, ID_ADD_TRANSITION, 
Laurent@814:              ID_ADD_ACTION_BLOCK, ID_ADD_DIVERGENCE, ID_ADD_JUMP,
Laurent@814:             ] = [wx.NewId() for i in xrange(6)]
Laurent@814:             
Laurent@814:             # Create menu items
Laurent@814:             self.AddMenuItems(menu, [
Laurent@814:                 (ID_ADD_INITIAL_STEP, wx.ITEM_NORMAL, _(u'Initial Step'), '', self.GetAddMenuCallBack(self.AddNewStep, True)),
Laurent@814:                 (ID_ADD_STEP, wx.ITEM_NORMAL, _(u'Step'), '', self.GetAddMenuCallBack(self.AddNewStep)),
Laurent@814:                 (ID_ADD_TRANSITION, wx.ITEM_NORMAL, _(u'Transition'), '', self.GetAddMenuCallBack(self.AddNewTransition)),
Laurent@814:                 (ID_ADD_ACTION_BLOCK, wx.ITEM_NORMAL, _(u'Action Block'), '', self.GetAddMenuCallBack(self.AddNewActionBlock)),
Laurent@814:                 (ID_ADD_DIVERGENCE, wx.ITEM_NORMAL, _(u'Divergence'), '', self.GetAddMenuCallBack(self.AddNewDivergence)),
Laurent@814:                 (ID_ADD_JUMP, wx.ITEM_NORMAL, _(u'Jump'), '', self.GetAddMenuCallBack(self.AddNewJump)),
Laurent@814:                 None])
Laurent@814:         
Laurent@814:         self.AddMenuItems(menu, [
Laurent@814:              (ID_ADD_COMMENT, wx.ITEM_NORMAL, _(u'Comment'), '', self.GetAddMenuCallBack(self.AddNewComment))])
Laurent@814:         
Laurent@814:     # Add Default Menu items to the given menu
Laurent@814:     def AddDefaultMenuItems(self, menu, edit=False, block=False):
Laurent@814:         if block:
Laurent@852:             [ID_EDIT_BLOCK, ID_DELETE, ID_ADJUST_BLOCK_SIZE] = [wx.NewId() for i in xrange(3)]
Laurent@814:         
Laurent@814:             # Create menu items
Laurent@814:             self.AddMenuItems(menu, [
Laurent@814:                  (ID_EDIT_BLOCK, wx.ITEM_NORMAL, _(u'Edit Block'), '', self.OnEditBlockMenu),
Laurent@852:                  (ID_ADJUST_BLOCK_SIZE, wx.ITEM_NORMAL, _(u'Adjust Block Size'), '', self.OnAdjustBlockSizeMenu),
Laurent@814:                  (ID_DELETE, wx.ITEM_NORMAL, _(u'Delete'), '', self.OnDeleteMenu)])
Laurent@814:         
Laurent@814:             menu.Enable(ID_EDIT_BLOCK, edit)
Laurent@814:         
Laurent@814:         else:
Laurent@814:             [ID_CLEAR_EXEC_ORDER, ID_RESET_EXEC_ORDER] = [wx.NewId() for i in xrange(2)]
Laurent@814:         
Laurent@814:             # Create menu items
Laurent@814:             self.AddMenuItems(menu, [
Laurent@814:                  (ID_CLEAR_EXEC_ORDER, wx.ITEM_NORMAL, _(u'Clear Execution Order'), '', self.OnClearExecutionOrderMenu),
Laurent@814:                  (ID_RESET_EXEC_ORDER, wx.ITEM_NORMAL, _(u'Reset Execution Order'), '', self.OnResetExecutionOrderMenu)])
Laurent@814:             
Laurent@814:             menu.AppendSeparator()
Laurent@814:             
Laurent@814:             add_menu = wx.Menu(title='')
Laurent@814:             self.AddAddMenuItems(add_menu)
Laurent@814:             menu.AppendMenu(-1, _(u'Add'), add_menu)
Laurent@814:         
Laurent@814:         menu.AppendSeparator()
Laurent@814:         
Laurent@814:         [ID_CUT, ID_COPY, ID_PASTE] = [wx.NewId() for i in xrange(3)]
Laurent@814:         
Laurent@814:         # Create menu items
Laurent@814:         self.AddMenuItems(menu, [
Laurent@814:              (ID_CUT, wx.ITEM_NORMAL, _(u'Cut'), '', self.GetClipboardCallBack(self.Cut)),
Laurent@814:              (ID_COPY, wx.ITEM_NORMAL, _(u'Copy'), '', self.GetClipboardCallBack(self.Copy)),
Laurent@814:              (ID_PASTE, wx.ITEM_NORMAL, _(u'Paste'), '', self.GetAddMenuCallBack(self.Paste))])
Laurent@814:         
Laurent@814:         menu.Enable(ID_CUT, block)
Laurent@814:         menu.Enable(ID_COPY, block)
Laurent@814:         menu.Enable(ID_PASTE, self.ParentWindow.GetCopyBuffer() is not None)
Laurent@814:         
Laurent@814:     def _init_Editor(self, prnt):
Laurent@814:         self.Editor = wx.ScrolledWindow(prnt, name="Viewer", 
Laurent@814:             pos=wx.Point(0, 0), size=wx.Size(0, 0), 
Laurent@814:             style=wx.HSCROLL | wx.VSCROLL | wx.ALWAYS_SHOW_SB)
Laurent@814:         self.Editor.ParentWindow = self
Laurent@814:     
Laurent@814:     # Create a new Viewer
Laurent@814:     def __init__(self, parent, tagname, window, controler, debug = False, instancepath = ""):
Laurent@814:         self.VARIABLE_PANEL_TYPE = controler.GetPouType(tagname.split("::")[1])
Laurent@814:         
Laurent@814:         EditorPanel.__init__(self, parent, tagname, window, controler, debug)
Laurent@814:         DebugViewer.__init__(self, controler, debug)
laurent@828:         DebugDataConsumer.__init__(self)
Laurent@814:         
Laurent@814:         # Adding a rubberband to Viewer
Laurent@814:         self.rubberBand = RubberBand(viewer=self)
Laurent@814:         self.Editor.SetBackgroundColour(wx.Colour(255,255,255))
Laurent@814:         self.Editor.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
Laurent@814:         self.ResetView()
Laurent@814:         self.Scaling = None
Laurent@814:         self.DrawGrid = True
Laurent@814:         self.GridBrush = wx.TRANSPARENT_BRUSH
Laurent@814:         self.PageSize = None
Laurent@814:         self.PagePen = wx.TRANSPARENT_PEN
Laurent@814:         self.DrawingWire = False
Laurent@814:         self.current_id = 0
Laurent@814:         self.TagName = tagname
Laurent@814:         self.Highlights = []
Laurent@814:         self.SearchParams = None
Laurent@814:         self.SearchResults = None
Laurent@814:         self.CurrentFindHighlight = None
Laurent@814:         self.InstancePath = instancepath
Laurent@814:         self.StartMousePos = None
Laurent@814:         self.StartScreenPos = None
Laurent@814:         self.Buffering = False
Laurent@814:         
Laurent@814:         # Initialize Cursors
Laurent@814:         ResetCursors()
Laurent@814:         self.CurrentCursor = 0
Laurent@814:         
Laurent@814:         # Initialize Block, Wire and Comment numbers
Laurent@814:         self.wire_id = 0
Laurent@814:         
Laurent@814:         # Initialize Viewer mode to Selection mode
Laurent@814:         self.Mode = MODE_SELECTION
Laurent@814:         self.SavedMode = False
Laurent@814:         self.CurrentLanguage = "FBD"
Laurent@814:         
Laurent@814:         if not self.Debug:
Laurent@814:             self.Editor.SetDropTarget(ViewerDropTarget(self))
Laurent@814:         
Laurent@814:         self.ElementRefreshList = []
Laurent@814:         self.ElementRefreshList_lock = Lock()
Laurent@814:         
Laurent@814:         dc = wx.ClientDC(self.Editor)
Laurent@814:         font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"])
Laurent@814:         dc.SetFont(font)
Laurent@814:         width, height = dc.GetTextExtent("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Laurent@814:         while width > 260:
Laurent@814:             faces["size"] -= 1
Laurent@814:             font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["mono"])
Laurent@814:             dc.SetFont(font)
Laurent@814:             width, height = dc.GetTextExtent("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Laurent@814:         self.SetFont(font)
Laurent@814:         self.MiniTextDC = wx.MemoryDC()
Laurent@814:         self.MiniTextDC.SetFont(wx.Font(faces["size"] * 0.75, wx.SWISS, wx.NORMAL, wx.NORMAL, faceName = faces["helv"]))
Laurent@814:         
Laurent@814:         self.CurrentScale = None
Laurent@814:         self.SetScale(len(ZOOM_FACTORS) / 2, False)
Laurent@814:         
Laurent@814:         self.RefreshHighlightsTimer = wx.Timer(self, -1)
Laurent@814:         self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer)
Laurent@814:         
Laurent@814:         self.ResetView()
Laurent@814:         
Laurent@814:         # Link Viewer event to corresponding methods
Laurent@814:         self.Editor.Bind(wx.EVT_PAINT, self.OnPaint)
Laurent@814:         self.Editor.Bind(wx.EVT_LEFT_DOWN, self.OnViewerLeftDown)
Laurent@814:         self.Editor.Bind(wx.EVT_LEFT_UP, self.OnViewerLeftUp)
Laurent@814:         self.Editor.Bind(wx.EVT_LEFT_DCLICK, self.OnViewerLeftDClick)
Laurent@814:         self.Editor.Bind(wx.EVT_RIGHT_DOWN, self.OnViewerRightDown)
Laurent@814:         self.Editor.Bind(wx.EVT_RIGHT_UP, self.OnViewerRightUp)
Laurent@814:         self.Editor.Bind(wx.EVT_MIDDLE_DOWN, self.OnViewerMiddleDown)
Laurent@814:         self.Editor.Bind(wx.EVT_MIDDLE_UP, self.OnViewerMiddleUp)
Laurent@814:         self.Editor.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveViewer)
Laurent@814:         self.Editor.Bind(wx.EVT_MOTION, self.OnViewerMotion)
Laurent@814:         self.Editor.Bind(wx.EVT_CHAR, self.OnChar)
Laurent@814:         self.Editor.Bind(wx.EVT_SCROLLWIN, self.OnScrollWindow)
Laurent@814:         self.Editor.Bind(wx.EVT_SCROLLWIN_THUMBRELEASE, self.OnScrollStop)
Laurent@814:         self.Editor.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseWheelWindow)
Laurent@814:         self.Editor.Bind(wx.EVT_SIZE, self.OnMoveWindow)
Laurent@814:         self.Editor.Bind(wx.EVT_MOUSE_EVENTS, self.OnViewerMouseEvent)
Laurent@814:     
Laurent@814:     # Destructor
Laurent@814:     def __del__(self):
Laurent@814:         DebugViewer.__del__(self)
Laurent@814:         self.Flush()
Laurent@814:         self.ResetView()
Laurent@814:         self.RefreshHighlightsTimer.Stop()
Laurent@814:     
Laurent@814:     def SetCurrentCursor(self, cursor):
Laurent@814:         if self.Mode != MODE_MOTION:
Laurent@814:             global CURSORS
Laurent@814:             if self.CurrentCursor != cursor:
Laurent@814:                 self.CurrentCursor = cursor
Laurent@814:                 self.Editor.SetCursor(CURSORS[cursor])
Laurent@814:     
Laurent@814:     def GetScrolledRect(self, rect):
Laurent@814:         rect.x, rect.y = self.Editor.CalcScrolledPosition(int(rect.x * self.ViewScale[0]), 
Laurent@814:                                                    int(rect.y * self.ViewScale[1]))
Laurent@814:         rect.width = int(rect.width * self.ViewScale[0]) + 2
Laurent@814:         rect.height = int(rect.height * self.ViewScale[1]) + 2
Laurent@814:         return rect
Laurent@814:     
Laurent@814:     def GetTitle(self):
Laurent@814:         if self.Debug:
Laurent@814:             if len(self.InstancePath) > 15:
Laurent@814:                 return "..." + self.InstancePath[-12:]
Laurent@814:             return self.InstancePath
Laurent@814:         return EditorPanel.GetTitle(self)
Laurent@814:     
Laurent@814:     def GetScaling(self):
Laurent@814:         return self.Scaling
Laurent@814:     
laurent@828:     def GetInstancePath(self, variable_base=False):
laurent@828:         if variable_base:
laurent@828:             words = self.TagName.split("::")
laurent@828:             if words[0] in ["A", "T"]:
laurent@828:                 return ".".join(self.InstancePath.split(".")[:-1])
Laurent@814:         return self.InstancePath
Laurent@814:     
Laurent@814:     def IsViewing(self, tagname):
Laurent@814:         if self.Debug:
Laurent@814:             return self.InstancePath == tagname
Laurent@814:         return EditorPanel.IsViewing(self, tagname)
Laurent@814:     
Laurent@814:     # Returns a new id
Laurent@814:     def GetNewId(self):
Laurent@814:         self.current_id += 1
Laurent@814:         return self.current_id
Laurent@814:         
Laurent@814:     def SetScale(self, scale_number, refresh=True, mouse_event=None):
Laurent@814:         new_scale = max(0, min(scale_number, len(ZOOM_FACTORS) - 1))
Laurent@814:         if self.CurrentScale != new_scale:
Laurent@814:             if refresh:
Laurent@814:                 dc = self.GetLogicalDC()
Laurent@814:             self.CurrentScale = new_scale
Laurent@814:             self.ViewScale = (ZOOM_FACTORS[self.CurrentScale], ZOOM_FACTORS[self.CurrentScale])
Laurent@814:             if refresh:
Laurent@814:                 self.Editor.Freeze()
Laurent@814:                 if mouse_event is None:
Laurent@814:                     client_size = self.Editor.GetClientSize()
Laurent@814:                     mouse_pos = wx.Point(client_size[0] / 2, client_size[1] / 2)
Laurent@814:                     mouse_event = wx.MouseEvent(wx.EVT_MOUSEWHEEL.typeId)
Laurent@814:                     mouse_event.m_x = mouse_pos.x
Laurent@814:                     mouse_event.m_y = mouse_pos.y
Laurent@814:                 else:
Laurent@814:                     mouse_pos = mouse_event.GetPosition()
Laurent@814:                 pos = mouse_event.GetLogicalPosition(dc)
Laurent@814:                 xmax = self.GetScrollRange(wx.HORIZONTAL) - self.GetScrollThumb(wx.HORIZONTAL)
Laurent@814:                 ymax = self.GetScrollRange(wx.VERTICAL) - self.GetScrollThumb(wx.VERTICAL)
Laurent@814:                 scrollx = max(0, round(pos.x * self.ViewScale[0] - mouse_pos.x) / SCROLLBAR_UNIT)
Laurent@814:                 scrolly = max(0, round(pos.y * self.ViewScale[1] - mouse_pos.y) / SCROLLBAR_UNIT)
Laurent@814:                 if scrollx > xmax or scrolly > ymax:
Laurent@814:                     self.RefreshScrollBars(max(0, scrollx - xmax), max(0, scrolly - ymax))
Laurent@814:                     self.Scroll(scrollx, scrolly)
Laurent@814:                 else:
Laurent@814:                     self.Scroll(scrollx, scrolly)
Laurent@814:                     self.RefreshScrollBars()
Laurent@814:                 self.RefreshScaling(refresh)
Laurent@814:                 self.Editor.Thaw()
Laurent@814:     
Laurent@814:     def GetScale(self):
Laurent@814:         return self.CurrentScale
Laurent@814: 
Laurent@814:     def GetViewScale(self):
Laurent@814:         return self.ViewScale
Laurent@814: 
Laurent@814:     def GetLogicalDC(self, buffered=False):
Laurent@814:         if buffered:
Laurent@814:             bitmap = wx.EmptyBitmap(*self.Editor.GetClientSize())
Laurent@814:             dc = wx.MemoryDC(bitmap)
Laurent@814:         else:
Laurent@814:             dc = wx.ClientDC(self.Editor)
Laurent@814:         dc.SetFont(self.GetFont())
Laurent@814:         if wx.VERSION >= (2, 6, 0):
Laurent@814:             self.Editor.DoPrepareDC(dc)
Laurent@814:         else:
Laurent@814:             self.Editor.PrepareDC(dc)
Laurent@814:         dc.SetUserScale(self.ViewScale[0], self.ViewScale[1])
Laurent@814:         return dc
Laurent@814:     
Laurent@814:     def RefreshRect(self, rect, eraseBackground=True):
Laurent@814:         self.Editor.RefreshRect(rect, eraseBackground)
Laurent@814:     
Laurent@814:     def Scroll(self, x, y):
Laurent@814:         self.Editor.Scroll(x, y)
Laurent@814:     
Laurent@814:     def GetScrollPos(self, orientation):
Laurent@814:         return self.Editor.GetScrollPos(orientation)
Laurent@814:     
Laurent@814:     def GetScrollRange(self, orientation):
Laurent@814:         return self.Editor.GetScrollRange(orientation)
Laurent@814:     
Laurent@814:     def GetScrollThumb(self, orientation):
Laurent@814:         return self.Editor.GetScrollThumb(orientation)
Laurent@814:     
Laurent@814:     def CalcUnscrolledPosition(self, x, y):
Laurent@814:         return self.Editor.CalcUnscrolledPosition(x, y)
Laurent@814:     
Laurent@814:     def GetViewStart(self):
Laurent@814:         return self.Editor.GetViewStart()
Laurent@814:     
Laurent@814:     def GetTextExtent(self, text):
Laurent@814:         return self.Editor.GetTextExtent(text)
Laurent@814:     
Laurent@814:     def GetFont(self):
Laurent@814:         return self.Editor.GetFont()
Laurent@814:     
Laurent@814:     def GetMiniTextExtent(self, text):
Laurent@814:         return self.MiniTextDC.GetTextExtent(text)
Laurent@814:     
Laurent@814:     def GetMiniFont(self):
Laurent@814:         return self.MiniTextDC.GetFont()
Laurent@814:     
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                         Element management functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def AddBlock(self, block):
Laurent@814:         self.Blocks[block.GetId()] = block
Laurent@814:         
Laurent@814:     def AddWire(self, wire):
Laurent@814:         self.wire_id += 1
Laurent@814:         self.Wires[wire] = self.wire_id
Laurent@814:         
Laurent@814:     def AddComment(self, comment):
Laurent@814:         self.Comments[comment.GetId()] = comment
Laurent@814: 
Laurent@814:     def IsBlock(self, block):
Laurent@852:         if block is not None:
Laurent@852:             return self.Blocks.get(block.GetId(), False)
Laurent@852:         return False
Laurent@814:         
Laurent@814:     def IsWire(self, wire):
Laurent@814:         return self.Wires.get(wire, False)
Laurent@814:         
Laurent@814:     def IsComment(self, comment):
Laurent@852:         if comment is not None:
Laurent@852:             return self.Comments.get(comment.GetId(), False)
Laurent@852:         return False
Laurent@814: 
Laurent@814:     def RemoveBlock(self, block):
Laurent@814:         self.Blocks.pop(block.GetId())
Laurent@814:         
Laurent@814:     def RemoveWire(self, wire):
Laurent@814:         self.Wires.pop(wire)
Laurent@814:         
Laurent@814:     def RemoveComment(self, comment):
Laurent@814:         self.Comments.pop(comment.GetId())
Laurent@814: 
Laurent@814:     def GetElements(self, sort_blocks=False, sort_wires=False, sort_comments=False):
Laurent@814:         blocks = self.Blocks.values()
Laurent@814:         wires = self.Wires.keys()
Laurent@814:         comments = self.Comments.values()
Laurent@814:         if sort_blocks:
Laurent@814:             blocks.sort(lambda x, y: cmp(x.GetId(), y.GetId()))
Laurent@814:         if sort_wires:
Laurent@814:             wires.sort(lambda x, y: cmp(self.Wires[x], self.Wires[y]))
Laurent@814:         if sort_comments:
Laurent@814:             comments.sort(lambda x, y: cmp(x.GetId(), y.GetId()))
Laurent@814:         return blocks + wires + comments
Laurent@814: 
Laurent@814:     def GetConnectorByName(self, name):
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             if isinstance(block, FBD_Connector) and\
Laurent@814:                block.GetType() == CONNECTOR and\
Laurent@814:                block.GetName() == name:
Laurent@814:                 return block
Laurent@814:         return None
Laurent@814:     
Laurent@814:     def RefreshVisibleElements(self, xp = None, yp = None):
Laurent@814:         x, y = self.Editor.CalcUnscrolledPosition(0, 0)
Laurent@814:         if xp is not None:
Laurent@814:             x = xp * self.Editor.GetScrollPixelsPerUnit()[0]
Laurent@814:         if yp is not None:
Laurent@814:             y = yp * self.Editor.GetScrollPixelsPerUnit()[1]
Laurent@814:         width, height = self.Editor.GetClientSize()
Laurent@814:         screen = wx.Rect(int(x / self.ViewScale[0]), int(y / self.ViewScale[1]),
Laurent@814:                          int(width / self.ViewScale[0]), int(height / self.ViewScale[1]))
Laurent@814:         for comment in self.Comments.itervalues():
Laurent@814:             comment.TestVisible(screen)
Laurent@814:         for wire in self.Wires.iterkeys():
Laurent@814:             wire.TestVisible(screen)
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             block.TestVisible(screen)
Laurent@814:     
Laurent@814:     def GetElementIECPath(self, element):
Laurent@814:         iec_path = None
laurent@828:         instance_path = self.GetInstancePath(True)
Laurent@814:         if isinstance(element, Wire) and element.EndConnected is not None:
Laurent@814:             block = element.EndConnected.GetParentBlock()
Laurent@814:             if isinstance(block, FBD_Block):
Laurent@814:                 blockname = block.GetName()
Laurent@814:                 connectorname = element.EndConnected.GetName()
Laurent@814:                 if blockname != "":
laurent@828:                     iec_path = "%s.%s.%s"%(instance_path, blockname, connectorname)
Laurent@814:                 else:
Laurent@814:                     if connectorname == "":
laurent@828:                         iec_path = "%s.%s%d"%(instance_path, block.GetType(), block.GetId())
Laurent@814:                     else:
laurent@828:                         iec_path = "%s.%s%d_%s"%(instance_path, block.GetType(), block.GetId(), connectorname)
Laurent@814:             elif isinstance(block, FBD_Variable):
laurent@828:                 iec_path = "%s.%s"%(instance_path, block.GetName())
Laurent@814:             elif isinstance(block, FBD_Connector):
Laurent@814:                 connection = self.GetConnectorByName(block.GetName())
Laurent@814:                 if connection is not None:
Laurent@814:                     connector = connection.GetConnector()
Laurent@814:                     if len(connector.Wires) == 1:
Laurent@814:                         iec_path = self.GetElementIECPath(connector.Wires[0][0])
Laurent@814:         elif isinstance(element, LD_Contact):
laurent@828:             iec_path = "%s.%s"%(instance_path, element.GetName())
Laurent@814:         elif isinstance(element, SFC_Step):
laurent@828:             iec_path = "%s.%s.X"%(instance_path, element.GetName())
Laurent@814:         elif isinstance(element, SFC_Transition):
Laurent@814:             connectors = element.GetConnectors()
Laurent@814:             previous_steps = self.GetPreviousSteps(connectors["inputs"])
Laurent@814:             next_steps = self.GetNextSteps(connectors["outputs"])
laurent@828:             iec_path = "%s.%s->%s"%(instance_path, ",".join(previous_steps), ",".join(next_steps))
Laurent@814:         return iec_path
Laurent@814:        
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                              Reset functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     # Resets Viewer lists
Laurent@814:     def ResetView(self):
Laurent@814:         self.Blocks = {}
Laurent@814:         self.Wires = {}
Laurent@814:         self.Comments = {}
Laurent@814:         self.Subscribed = {}
Laurent@814:         self.SelectedElement = None
Laurent@814:         self.HighlightedElement = None
Laurent@814:         self.ToolTipElement = None
Laurent@814:     
Laurent@814:     def Flush(self):
Laurent@814:         self.DeleteDataConsumers()
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             block.Flush()
Laurent@814:     
Laurent@814:     # Remove all elements
Laurent@814:     def CleanView(self):
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             block.Clean()
Laurent@814:         self.ResetView()
Laurent@814:     
Laurent@814:     # Changes Viewer mode
Laurent@814:     def SetMode(self, mode):
Laurent@814:         if self.Mode != mode or mode == MODE_SELECTION:    
Laurent@814:             if self.Mode == MODE_MOTION:
Laurent@814:                 wx.CallAfter(self.Editor.SetCursor, wx.NullCursor)
Laurent@814:             self.Mode = mode
Laurent@814:             self.SavedMode = False
Laurent@814:         else:
Laurent@814:             self.SavedMode = True
Laurent@814:         # Reset selection
Laurent@814:         if self.Mode != MODE_SELECTION and self.SelectedElement:
Laurent@814:             self.SelectedElement.SetSelected(False)
Laurent@814:             self.SelectedElement = None
Laurent@814:         if self.Mode == MODE_MOTION:
Laurent@814:             wx.CallAfter(self.Editor.SetCursor, wx.StockCursor(wx.CURSOR_HAND))
Laurent@814:             self.SavedMode = True
Laurent@814:         
Laurent@814:     # Return current drawing mode
Laurent@814:     def GetDrawingMode(self):
Laurent@814:         return self.ParentWindow.GetDrawingMode()
Laurent@814:     
Laurent@814:     # Buffer the last model state
Laurent@814:     def RefreshBuffer(self):
Laurent@814:         self.Controler.BufferProject()
Laurent@814:         if self.ParentWindow:
Laurent@814:             self.ParentWindow.RefreshTitle()
Laurent@814:             self.ParentWindow.RefreshFileMenu()
Laurent@814:             self.ParentWindow.RefreshEditMenu()
Laurent@814:     
Laurent@814:     def StartBuffering(self):
Laurent@814:         if not self.Buffering:
Laurent@814:             self.Buffering = True
Laurent@814:             self.Controler.StartBuffering()
Laurent@814:             if self.ParentWindow:
Laurent@814:                 self.ParentWindow.RefreshTitle()
Laurent@814:                 self.ParentWindow.RefreshFileMenu()
Laurent@814:                 self.ParentWindow.RefreshEditMenu()
Laurent@814:     
Laurent@814:     def ResetBuffer(self):
Laurent@814:         if self.Buffering:
Laurent@814:             self.Controler.EndBuffering()
Laurent@814:             self.Buffering = False
Laurent@814:     
Laurent@814:     def GetBufferState(self):
Laurent@814:         if not self.Debug:
Laurent@814:             return self.Controler.GetBufferState()
Laurent@814:         return False, False
Laurent@814:     
Laurent@814:     def Undo(self):
Laurent@814:         if not self.Debug:
Laurent@814:             self.Controler.LoadPrevious()
Laurent@814:             self.ParentWindow.CloseTabsWithoutModel()
Laurent@814:             
Laurent@814:     def Redo(self):
Laurent@814:         if not self.Debug:
Laurent@814:             self.Controler.LoadNext()
Laurent@814:             self.ParentWindow.CloseTabsWithoutModel()
Laurent@814:         
Laurent@814:     def HasNoModel(self):
Laurent@814:         if not self.Debug:
Laurent@814:             return self.Controler.GetEditedElement(self.TagName) is None
Laurent@814:         return False
Laurent@814:     
Laurent@814:     # Refresh the current scaling
Laurent@814:     def RefreshScaling(self, refresh=True):
Laurent@814:         properties = self.Controler.GetProjectProperties(self.Debug)
Laurent@814:         scaling = properties["scaling"][self.CurrentLanguage]
Laurent@814:         if scaling[0] != 0 and scaling[1] != 0:
Laurent@814:             self.Scaling = scaling
Laurent@814:             if self.DrawGrid:
Laurent@814:                 width = max(2, int(scaling[0] * self.ViewScale[0]))
Laurent@814:                 height = max(2, int(scaling[1] * self.ViewScale[1]))
Laurent@814:                 bitmap = wx.EmptyBitmap(width, height)
Laurent@814:                 dc = wx.MemoryDC(bitmap)
Laurent@814:                 dc.SetBackground(wx.Brush(self.Editor.GetBackgroundColour()))
Laurent@814:                 dc.Clear()
Laurent@814:                 dc.SetPen(MiterPen(wx.Colour(180, 180, 180)))
Laurent@814:                 dc.DrawPoint(0, 0)
Laurent@814:                 self.GridBrush = wx.BrushFromBitmap(bitmap)
Laurent@814:             else:
Laurent@814:                 self.GridBrush = wx.TRANSPARENT_BRUSH
Laurent@814:         else:
Laurent@814:             self.Scaling = None
Laurent@814:             self.GridBrush = wx.TRANSPARENT_BRUSH
Laurent@814:         page_size = properties["pageSize"]
Laurent@814:         if page_size != (0, 0):
Laurent@814:             self.PageSize = map(int, page_size)
Laurent@814:             self.PagePen = MiterPen(wx.Colour(180, 180, 180))
Laurent@814:         else:
Laurent@814:             self.PageSize = None
Laurent@814:             self.PagePen = wx.TRANSPARENT_PEN
Laurent@814:         if refresh:
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             self.Refresh(False)
Laurent@814:         
Laurent@814:         
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                          Refresh functions
Laurent@814: #-------------------------------------------------------------------------------
laurent@828:     
laurent@828:     VALUE_TRANSLATION = {True: _("Active"), False: _("Inactive")}
laurent@828: 
laurent@828:     def SetValue(self, value):
laurent@828:         if self.Value != value:
laurent@828:             self.Value = value
laurent@828:             
laurent@828:             xstart, ystart = self.GetViewStart()
laurent@828:             window_size = self.Editor.GetClientSize()
laurent@828:             refresh_rect = self.GetRedrawRect()
laurent@828:             if (xstart * SCROLLBAR_UNIT <= refresh_rect.x + refresh_rect.width and 
laurent@828:                 xstart * SCROLLBAR_UNIT + window_size[0] >= refresh_rect.x and
laurent@828:                 ystart * SCROLLBAR_UNIT <= refresh_rect.y + refresh_rect.height and 
laurent@828:                 ystart * SCROLLBAR_UNIT + window_size[1] >= refresh_rect.y):
laurent@828:                 self.ElementNeedRefresh(self)
laurent@828:     
laurent@828:     def GetRedrawRect(self):
laurent@828:         dc = self.GetLogicalDC()
laurent@828:         ipw, iph = dc.GetTextExtent(_("Debug: %s") % self.InstancePath)
laurent@828:         vw, vh = 0, 0
laurent@828:         for value in self.VALUE_TRANSLATION.itervalues():
laurent@828:             w, h = dc.GetTextExtent("(%s)" % value)
laurent@828:             vw = max(vw, w)
laurent@828:             vh = max(vh, h)
laurent@828:         return wx.Rect(ipw + 4, 2, vw, vh)
laurent@828:     
Laurent@814:     def ElementNeedRefresh(self, element):
Laurent@814:         self.ElementRefreshList_lock.acquire()
Laurent@814:         self.ElementRefreshList.append(element)
Laurent@814:         self.ElementRefreshList_lock.release()
Laurent@814:         
Laurent@814:     def RefreshNewData(self):
Laurent@814:         refresh_rect = None
Laurent@814:         self.ElementRefreshList_lock.acquire()
Laurent@814:         for element in self.ElementRefreshList:
Laurent@814:             if refresh_rect is None:
Laurent@814:                 refresh_rect = element.GetRedrawRect()
Laurent@814:             else:
Laurent@814:                 refresh_rect.Union(element.GetRedrawRect())
Laurent@814:         self.ElementRefreshList = []
Laurent@814:         self.ElementRefreshList_lock.release()
Laurent@814:         
Laurent@814:         if refresh_rect is not None:
Laurent@814:             self.RefreshRect(self.GetScrolledRect(refresh_rect), False)
Laurent@814:         else:
Laurent@814:             DebugViewer.RefreshNewData(self)
Laurent@814:         
Laurent@814:     # Refresh Viewer elements
Laurent@814:     def RefreshView(self, variablepanel=True, selection=None):
Laurent@814:         EditorPanel.RefreshView(self, variablepanel)
Laurent@814:         
laurent@828:         if self.TagName.split("::")[0] == "A":
laurent@828:             self.AddDataConsumer("%s.Q" % self.InstancePath.upper(), self)
laurent@828:         
Laurent@814:         if self.ToolTipElement is not None:
Laurent@814:             self.ToolTipElement.ClearToolTip()
Laurent@814:             self.ToolTipElement = None
Laurent@814:         
Laurent@814:         self.Inhibit(True)
Laurent@814:         self.current_id = 0
Laurent@814:         # Start by reseting Viewer
Laurent@814:         self.Flush()
Laurent@814:         self.ResetView()
Laurent@814:         self.ResetBuffer()
Laurent@814:         instance = {}
Laurent@814:         # List of ids of already loaded blocks
Laurent@814:         ids = []
Laurent@814:         # Load Blocks until they are all loaded
Laurent@814:         while instance is not None:
Laurent@814:             instance = self.Controler.GetEditedElementInstanceInfos(self.TagName, exclude = ids, debug = self.Debug)
Laurent@814:             if instance is not None:
Laurent@814:                 self.loadInstance(instance, ids, selection)
Laurent@814:         self.RefreshScrollBars()
Laurent@814:         
Laurent@814:         for wire in self.Wires:
Laurent@814:             if not wire.IsConnectedCompatible():
Laurent@814:                 wire.SetValid(False)
Laurent@814:             if self.Debug:
Laurent@814:                 iec_path = self.GetElementIECPath(wire)
Laurent@814:                 if iec_path is None:
Laurent@814:                     block = wire.EndConnected.GetParentBlock()
Laurent@814:                     if isinstance(block, LD_PowerRail):
Laurent@814:                         wire.SetValue(True)
Laurent@814:                 elif self.AddDataConsumer(iec_path.upper(), wire) is None:
Laurent@814:                     wire.SetValue("undefined")
Laurent@814: 
Laurent@814:         if self.Debug:
Laurent@814:             for block in self.Blocks.itervalues():
Laurent@814:                 block.SpreadCurrent()
Laurent@814:                 iec_path = self.GetElementIECPath(block)
Laurent@814:                 if iec_path is not None:
Laurent@814:                     self.AddDataConsumer(iec_path.upper(), block)
Laurent@814: 
Laurent@814:         self.Inhibit(False)
Laurent@814:         self.RefreshVisibleElements()
Laurent@814:         self.ShowHighlights()
Laurent@814:         self.Refresh(False)
Laurent@814:     
Laurent@814:     def GetPreviousSteps(self, connectors):
Laurent@814:         steps = []
Laurent@814:         for connector in connectors:
Laurent@814:             for wire, handle in connector.GetWires():
Laurent@814:                 previous = wire.GetOtherConnected(connector).GetParentBlock()
Laurent@814:                 if isinstance(previous, SFC_Step):
Laurent@814:                     steps.append(previous.GetName())
Laurent@814:                 elif isinstance(previous, SFC_Divergence) and previous.GetType() in [SIMULTANEOUS_CONVERGENCE, SELECTION_DIVERGENCE]:
Laurent@814:                     connectors = previous.GetConnectors()
Laurent@814:                     steps.extend(self.GetPreviousSteps(connectors["inputs"]))
Laurent@814:         return steps
Laurent@814:     
Laurent@814:     def GetNextSteps(self, connectors):
Laurent@814:         steps = []
Laurent@814:         for connector in connectors:
Laurent@814:             for wire, handle in connector.GetWires():
Laurent@814:                 next = wire.GetOtherConnected(connector).GetParentBlock()
Laurent@814:                 if isinstance(next, SFC_Step):
Laurent@814:                     steps.append(next.GetName())
Laurent@814:                 elif isinstance(next, SFC_Jump):
Laurent@814:                     steps.append(next.GetTarget())
Laurent@814:                 elif isinstance(next, SFC_Divergence) and next.GetType() in [SIMULTANEOUS_DIVERGENCE, SELECTION_CONVERGENCE]:
Laurent@814:                     connectors = next.GetConnectors()
Laurent@814:                     steps.extend(self.GetNextSteps(connectors["outputs"]))
Laurent@814:         return steps
Laurent@814:     
Laurent@814:     def GetMaxSize(self):
Laurent@814:         maxx = maxy = 0
Laurent@814:         for element in self.GetElements():
Laurent@814:             bbox = element.GetBoundingBox()
Laurent@814:             maxx = max(maxx, bbox.x + bbox.width)
Laurent@814:             maxy = max(maxy, bbox.y + bbox.height)
Laurent@814:         return maxx, maxy
Laurent@814:     
Laurent@814:     def RefreshScrollBars(self, width_incr=0, height_incr=0):
Laurent@814:         xstart, ystart = self.GetViewStart()
Laurent@814:         window_size = self.Editor.GetClientSize()
Laurent@814:         maxx, maxy = self.GetMaxSize()
Laurent@814:         maxx = max(maxx + WINDOW_BORDER, (xstart * SCROLLBAR_UNIT + window_size[0]) / self.ViewScale[0])
Laurent@814:         maxy = max(maxy + WINDOW_BORDER, (ystart * SCROLLBAR_UNIT + window_size[1]) / self.ViewScale[1])
Laurent@814:         if self.rubberBand.IsShown():
Laurent@814:             extent = self.rubberBand.GetCurrentExtent()
Laurent@814:             maxx = max(maxx, extent.x + extent.width)
Laurent@814:             maxy = max(maxy, extent.y + extent.height)
Laurent@814:         maxx = int(maxx * self.ViewScale[0])
Laurent@814:         maxy = int(maxy * self.ViewScale[1])
Laurent@814:         self.Editor.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, 
Laurent@814:             round(maxx / SCROLLBAR_UNIT) + width_incr, round(maxy / SCROLLBAR_UNIT) + height_incr, 
Laurent@814:             xstart, ystart, True)
Laurent@814:     
Laurent@814:     def EnsureVisible(self, block):
Laurent@814:         xstart, ystart = self.GetViewStart()
Laurent@814:         window_size = self.Editor.GetClientSize()
Laurent@814:         block_bbx = block.GetBoundingBox()
Laurent@814:         
Laurent@814:         screen_minx, screen_miny = xstart * SCROLLBAR_UNIT, ystart * SCROLLBAR_UNIT
Laurent@814:         screen_maxx, screen_maxy = screen_minx + window_size[0], screen_miny + window_size[1]
Laurent@814:         block_minx = int(block_bbx.x * self.ViewScale[0]) 
Laurent@814:         block_miny = int(block_bbx.y * self.ViewScale[1])
Laurent@814:         block_maxx = int(round((block_bbx.x + block_bbx.width) * self.ViewScale[0]))
Laurent@814:         block_maxy = int(round((block_bbx.y + block_bbx.height) * self.ViewScale[1]))
Laurent@814:         
Laurent@814:         xpos, ypos = xstart, ystart
Laurent@814:         if block_minx < screen_minx and block_maxx < screen_maxx:
Laurent@814:             xpos -= (screen_minx - block_minx) / SCROLLBAR_UNIT + 1
Laurent@814:         elif block_maxx > screen_maxx and block_minx > screen_minx:
Laurent@814:             xpos += (block_maxx - screen_maxx) / SCROLLBAR_UNIT + 1
Laurent@814:         if block_miny < screen_miny and block_maxy < screen_maxy:
Laurent@814:             ypos -= (screen_miny - block_miny) / SCROLLBAR_UNIT + 1
Laurent@814:         elif block_maxy > screen_maxy and block_miny > screen_miny:
Laurent@814:             ypos += (block_maxy - screen_maxy) / SCROLLBAR_UNIT + 1
Laurent@814:         self.Scroll(xpos, ypos)
Laurent@814:     
Laurent@814:     def SelectInGroup(self, element):
Laurent@814:         element.SetSelected(True)
Laurent@814:         if self.SelectedElement is None:
Laurent@814:             self.SelectedElement = element
Laurent@814:         elif isinstance(self.SelectedElement, Graphic_Group):
Laurent@814:             self.SelectedElement.SelectElement(element)
Laurent@814:         else:
Laurent@814:             group = Graphic_Group(self)
Laurent@814:             group.SelectElement(self.SelectedElement)
Laurent@814:             group.SelectElement(element)
Laurent@814:             self.SelectedElement = group
Laurent@814:         
Laurent@814:     # Load instance from given informations
Laurent@814:     def loadInstance(self, instance, ids, selection):
Laurent@814:         ids.append(instance["id"])
Laurent@814:         self.current_id = max(self.current_id, instance["id"])
Laurent@814:         creation_function = ElementCreationFunctions.get(instance["type"], None)
Laurent@814:         connectors = {"inputs" : [], "outputs" : []}
Laurent@814:         specific_values = instance["specific_values"]
Laurent@814:         if creation_function is not None:
Laurent@814:             element = creation_function(self, instance["id"], specific_values)
Laurent@814:             if isinstance(element, SFC_Step):
Laurent@814:                 if len(instance["inputs"]) > 0:
Laurent@814:                     element.AddInput()
Laurent@814:                 else:
Laurent@814:                     element.RemoveInput()
Laurent@814:                 if len(instance["outputs"]) > 0:
Laurent@814:                     element.AddOutput()
Laurent@814:             if isinstance(element, SFC_Transition) and specific_values["condition_type"] == "connection":
Laurent@814:                 connector = element.GetConditionConnector()
Laurent@896:                 self.CreateWires(connector, instance["id"], specific_values["connection"]["links"], ids, selection)
Laurent@814:         else:
Laurent@814:             executionControl = False
Laurent@814:             for input in instance["inputs"]:
Laurent@814:                 if input["negated"]:
Laurent@814:                     connectors["inputs"].append((input["name"], None, "negated"))
Laurent@814:                 elif input["edge"]:
Laurent@814:                     connectors["inputs"].append((input["name"], None, input["edge"]))
Laurent@814:                 else:
Laurent@814:                     connectors["inputs"].append((input["name"], None, "none"))
Laurent@814:             for output in instance["outputs"]:
Laurent@814:                 if output["negated"]:
Laurent@814:                     connectors["outputs"].append((output["name"], None, "negated"))
Laurent@814:                 elif output["edge"]:
Laurent@814:                     connectors["outputs"].append((output["name"], None, output["edge"]))
Laurent@814:                 else:
Laurent@814:                     connectors["outputs"].append((output["name"], None, "none"))
Laurent@814:             if len(connectors["inputs"]) > 0 and connectors["inputs"][0][0] == "EN":
Laurent@814:                 connectors["inputs"].pop(0)
Laurent@814:                 executionControl = True
Laurent@814:             if len(connectors["outputs"]) > 0 and connectors["outputs"][0][0] == "ENO":
Laurent@814:                 connectors["outputs"].pop(0)
Laurent@814:                 executionControl = True
Laurent@814:             if specific_values["name"] is None:
Laurent@814:                 specific_values["name"] = ""
Laurent@814:             element = FBD_Block(self, instance["type"], specific_values["name"], 
Laurent@814:                       instance["id"], len(connectors["inputs"]), 
Laurent@814:                       connectors=connectors, executionControl=executionControl, 
Laurent@814:                       executionOrder=specific_values["executionOrder"])
Laurent@814:         if isinstance(element, Comment):
Laurent@814:             self.AddComment(element)
Laurent@814:         else:
Laurent@814:             self.AddBlock(element)
Laurent@814:             connectors = element.GetConnectors()
laurent@832:         element.SetPosition(instance["x"], instance["y"])
Laurent@852:         element.SetSize(instance["width"], instance["height"])
laurent@832:         for i, output_connector in enumerate(instance["outputs"]):
laurent@832:             if i < len(connectors["outputs"]):
laurent@832:                 connector = connectors["outputs"][i]
laurent@832:                 if output_connector.get("negated", False):
laurent@832:                     connector.SetNegated(True)
laurent@832:                 if output_connector.get("edge", "none") != "none":
laurent@832:                     connector.SetEdge(output_connector["edge"])
laurent@832:                 connector.SetPosition(wx.Point(*output_connector["position"]))
Laurent@814:         for i, input_connector in enumerate(instance["inputs"]):
Laurent@814:             if i < len(connectors["inputs"]):
Laurent@814:                 connector = connectors["inputs"][i]
Laurent@814:                 connector.SetPosition(wx.Point(*input_connector["position"]))
Laurent@814:                 if input_connector.get("negated", False):
Laurent@814:                     connector.SetNegated(True)
Laurent@814:                 if input_connector.get("edge", "none") != "none":
Laurent@814:                     connector.SetEdge(input_connector["edge"])
Laurent@814:                 self.CreateWires(connector, instance["id"], input_connector["links"], ids, selection)
Laurent@814:         if selection is not None and selection[0].get(instance["id"], False):
Laurent@814:             self.SelectInGroup(element)
Laurent@814: 
Laurent@814:     def CreateWires(self, start_connector, id, links, ids, selection=None):
Laurent@814:         for link in links:
Laurent@814:             refLocalId = link["refLocalId"]
Laurent@814:             if refLocalId is not None:
Laurent@814:                 if refLocalId not in ids:
Laurent@814:                     new_instance = self.Controler.GetEditedElementInstanceInfos(self.TagName, refLocalId, debug = self.Debug)
Laurent@814:                     if new_instance is not None:
Laurent@814:                         self.loadInstance(new_instance, ids, selection)
Laurent@814:                 connected = self.FindElementById(refLocalId)
Laurent@814:                 if connected is not None:
Laurent@814:                     points = link["points"]
Laurent@814:                     end_connector = connected.GetConnector(wx.Point(points[-1][0], points[-1][1]), link["formalParameter"])
Laurent@814:                     if end_connector is not None:
Laurent@814:                         wire = Wire(self)
Laurent@814:                         wire.SetPoints(points)
Laurent@814:                         start_connector.Connect((wire, 0), False)
Laurent@814:                         end_connector.Connect((wire, -1), False)
Laurent@814:                         wire.ConnectStartPoint(None, start_connector)
Laurent@814:                         wire.ConnectEndPoint(None, end_connector)
Laurent@814:                         self.AddWire(wire)
Laurent@814:                         if selection is not None and (\
Laurent@814:                            selection[1].get((id, refLocalId), False) or \
Laurent@814:                            selection[1].get((refLocalId, id), False)):
Laurent@814:                             self.SelectInGroup(wire)
Laurent@814: 
Laurent@814:     def IsOfType(self, type, reference):
Laurent@814:         return self.Controler.IsOfType(type, reference, self.Debug)
Laurent@814:     
Laurent@814:     def IsEndType(self, type):
Laurent@814:         return self.Controler.IsEndType(type)
Laurent@814: 
Laurent@814:     def GetBlockType(self, type, inputs = None):
Laurent@814:         return self.Controler.GetBlockType(type, inputs, self.Debug)
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                          Search Element functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def FindBlock(self, event):
Laurent@814:         dc = self.GetLogicalDC()
Laurent@814:         pos = event.GetLogicalPosition(dc)
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             if block.HitTest(pos) or block.TestHandle(event) != (0, 0):
Laurent@814:                 return block
Laurent@814:         return None
Laurent@814:     
Laurent@814:     def FindWire(self, event):
Laurent@814:         dc = self.GetLogicalDC()
Laurent@814:         pos = event.GetLogicalPosition(dc)
Laurent@814:         for wire in self.Wires:
Laurent@814:             if wire.HitTest(pos) or wire.TestHandle(event) != (0, 0):
Laurent@814:                 return wire
Laurent@814:         return None
Laurent@814:     
Laurent@814:     def FindElement(self, event, exclude_group = False, connectors = True):
Laurent@814:         dc = self.GetLogicalDC()
Laurent@814:         pos = event.GetLogicalPosition(dc)
Laurent@814:         if self.SelectedElement and not (exclude_group and isinstance(self.SelectedElement, Graphic_Group)):
Laurent@814:             if self.SelectedElement.HitTest(pos, connectors) or self.SelectedElement.TestHandle(event) != (0, 0):
Laurent@814:                 return self.SelectedElement
Laurent@814:         for element in self.GetElements():
Laurent@814:             if element.HitTest(pos, connectors) or element.TestHandle(event) != (0, 0):
Laurent@814:                 return element
Laurent@814:         return None
Laurent@814:     
Laurent@814:     def FindBlockConnector(self, pos, direction = None, exclude = None):
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             result = block.TestConnector(pos, direction, exclude)
Laurent@814:             if result:
Laurent@814:                 return result
Laurent@814:         return None
Laurent@814:     
Laurent@814:     def FindElementById(self, id):
Laurent@814:         block = self.Blocks.get(id, None)
Laurent@814:         if block is not None:
Laurent@814:             return block
Laurent@814:         comment = self.Comments.get(id, None)
Laurent@814:         if comment is not None:
Laurent@814:             return comment
Laurent@814:         return None
Laurent@814:     
Laurent@814:     def SearchElements(self, bbox):
Laurent@814:         elements = []
Laurent@814:         for element in self.GetElements():
Laurent@814:             if element.IsInSelection(bbox):
Laurent@814:                 elements.append(element)
Laurent@814:         return elements
Laurent@814: 
Laurent@814:     def SelectAll(self):
Laurent@814:         if self.SelectedElement is not None:
Laurent@814:             self.SelectedElement.SetSelected(False)
Laurent@814:         self.SelectedElement = Graphic_Group(self)
Laurent@814:         for element in self.GetElements():
Laurent@814:             self.SelectedElement.SelectElement(element)
Laurent@814:         self.SelectedElement.SetSelected(True)
Laurent@814:     
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                           Popup menu functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def GetForceVariableMenuFunction(self, iec_path, element):
Laurent@814:         iec_type = self.GetDataType(iec_path)
Laurent@814:         def ForceVariableFunction(event):
Laurent@814:             if iec_type is not None:
Laurent@814:                 dialog = ForceVariableDialog(self.ParentWindow, iec_type, str(element.GetValue()))
Laurent@814:                 if dialog.ShowModal() == wx.ID_OK:
Laurent@814:                     self.ParentWindow.AddDebugVariable(iec_path)
Laurent@814:                     self.ForceDataValue(iec_path, dialog.GetValue())
Laurent@814:         return ForceVariableFunction
Laurent@814: 
Laurent@814:     def GetReleaseVariableMenuFunction(self, iec_path):
Laurent@814:         def ReleaseVariableFunction(event):
Laurent@814:             self.ReleaseDataValue(iec_path)
Laurent@814:         return ReleaseVariableFunction
Laurent@814: 
Laurent@857:     def GetChangeVariableTypeMenuFunction(self, type):
Laurent@857:         def ChangeVariableTypeMenu(event):
Laurent@857:             self.ChangeVariableType(self.SelectedElement, type)
Laurent@857:         return ChangeVariableTypeMenu
Laurent@857: 
Laurent@857:     def GetChangeConnectionTypeMenuFunction(self, type):
Laurent@857:         def ChangeConnectionTypeMenu(event):
Laurent@857:             self.ChangeConnectionType(self.SelectedElement, type)
Laurent@857:         return ChangeConnectionTypeMenu
Laurent@857: 
Laurent@814:     def PopupForceMenu(self):
Laurent@814:         iec_path = self.GetElementIECPath(self.SelectedElement)
Laurent@814:         if iec_path is not None:
Laurent@814:             menu = wx.Menu(title='')
Laurent@814:             new_id = wx.NewId()
Laurent@814:             AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Force value"))
Laurent@814:             self.Bind(wx.EVT_MENU, self.GetForceVariableMenuFunction(iec_path.upper(), self.SelectedElement), id=new_id)
Laurent@814:             new_id = wx.NewId()
Laurent@814:             AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Release value"))
Laurent@814:             self.Bind(wx.EVT_MENU, self.GetReleaseVariableMenuFunction(iec_path.upper()), id=new_id)
Laurent@814:             if self.SelectedElement.IsForced():
Laurent@814:                 menu.Enable(new_id, True)
Laurent@814:             else:
Laurent@814:                 menu.Enable(new_id, False)
Laurent@814:             self.Editor.PopupMenu(menu)
Laurent@814:             menu.Destroy()
Laurent@814: 
Laurent@814:     def PopupBlockMenu(self, connector = None):
Laurent@814:         menu = wx.Menu(title='')
Laurent@814:         if connector is not None and connector.IsCompatible("BOOL"):
Laurent@814:             self.AddBlockPinMenuItems(menu, connector)
Laurent@814:         else:
Laurent@814:             edit = self.SelectedElement.GetType() in self.Controler.GetProjectPouNames(self.Debug)
Laurent@814:             self.AddDefaultMenuItems(menu, block=True, edit=edit)
Laurent@814:         self.Editor.PopupMenu(menu)
Laurent@814:         menu.Destroy()
Laurent@814:     
Laurent@857:     def PopupVariableMenu(self):
Laurent@857:         menu = wx.Menu(title='')
Laurent@857:         variable_type = self.SelectedElement.GetType()
Laurent@857:         for type_label, type in [(_("Input"), INPUT),
Laurent@857:                                  (_("Output"), OUTPUT),
Laurent@857:                                  (_("InOut"), INOUT)]:
Laurent@857:             new_id = wx.NewId()
Laurent@857:             AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_RADIO, text=type_label)
Laurent@857:             self.Bind(wx.EVT_MENU, self.GetChangeVariableTypeMenuFunction(type), id=new_id)
Laurent@857:             if type == variable_type:
Laurent@857:                 menu.Check(new_id, True)
Laurent@857:         menu.AppendSeparator()
Laurent@857:         self.AddDefaultMenuItems(menu, block=True)
Laurent@857:         self.Editor.PopupMenu(menu)
Laurent@857:         menu.Destroy()
Laurent@857:     
Laurent@857:     def PopupConnectionMenu(self):
Laurent@857:         menu = wx.Menu(title='')
Laurent@857:         connection_type = self.SelectedElement.GetType()
Laurent@857:         for type_label, type in [(_("Connector"), CONNECTOR),
Laurent@857:                                  (_("Continuation"), CONTINUATION)]:
Laurent@857:             new_id = wx.NewId()
Laurent@857:             AppendMenu(menu, help='', id=new_id, kind=wx.ITEM_RADIO, text=type_label)
Laurent@857:             self.Bind(wx.EVT_MENU, self.GetChangeConnectionTypeMenuFunction(type), id=new_id)
Laurent@857:             if type == connection_type:
Laurent@857:                 menu.Check(new_id, True)
Laurent@857:         menu.AppendSeparator()
Laurent@857:         self.AddDefaultMenuItems(menu, block=True)
Laurent@857:         self.Editor.PopupMenu(menu)
Laurent@857:         menu.Destroy()
Laurent@857:     
Laurent@814:     def PopupWireMenu(self, delete=True):
Laurent@814:         menu = wx.Menu(title='')
Laurent@814:         self.AddWireMenuItems(menu, delete)
Laurent@814:         menu.AppendSeparator()
Laurent@814:         self.AddDefaultMenuItems(menu, block=True)
Laurent@814:         self.Editor.PopupMenu(menu)
Laurent@814:         menu.Destroy()
Laurent@814:         
Laurent@814:     def PopupDivergenceMenu(self, connector):
Laurent@814:         menu = wx.Menu(title='')
Laurent@814:         self.AddDivergenceMenuItems(menu, connector)
Laurent@814:         menu.AppendSeparator()
Laurent@814:         self.AddDefaultMenuItems(menu, block=True)
Laurent@814:         self.Editor.PopupMenu(menu)
Laurent@814:         menu.Destroy()
Laurent@814:     
Laurent@814:     def PopupGroupMenu(self):
Laurent@814:         menu = wx.Menu(title='')
Laurent@814:         align_menu = wx.Menu(title='')
Laurent@814:         self.AddAlignmentMenuItems(align_menu)
Laurent@814:         menu.AppendMenu(-1, _(u'Alignment'), align_menu)
Laurent@814:         menu.AppendSeparator()
Laurent@814:         self.AddDefaultMenuItems(menu, block=True)
Laurent@814:         self.Editor.PopupMenu(menu)
Laurent@814:         menu.Destroy()
Laurent@814:         
Laurent@814:     def PopupDefaultMenu(self, block=True):
Laurent@814:         menu = wx.Menu(title='')
Laurent@814:         self.AddDefaultMenuItems(menu, block=block)
Laurent@814:         self.Editor.PopupMenu(menu)
Laurent@814:         menu.Destroy()
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                            Menu items functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def OnAlignLeftMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
Laurent@814:             self.SelectedElement.AlignElements(ALIGN_LEFT, None)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.Refresh(False)
Laurent@814:     
Laurent@814:     def OnAlignCenterMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
Laurent@814:             self.SelectedElement.AlignElements(ALIGN_CENTER, None)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.Refresh(False)
Laurent@814:     
Laurent@814:     def OnAlignRightMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
Laurent@814:             self.SelectedElement.AlignElements(ALIGN_RIGHT, None)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.Refresh(False)
Laurent@814:     
Laurent@814:     def OnAlignTopMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
Laurent@814:             self.SelectedElement.AlignElements(None, ALIGN_TOP)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.Refresh(False)
Laurent@814:     
Laurent@814:     def OnAlignMiddleMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
Laurent@814:             self.SelectedElement.AlignElements(None, ALIGN_MIDDLE)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.Refresh(False)
Laurent@814:     
Laurent@814:     def OnAlignBottomMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and isinstance(self.SelectedElement, Graphic_Group):
Laurent@814:             self.SelectedElement.AlignElements(None, ALIGN_BOTTOM)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.Refresh(False)
Laurent@814:         
Laurent@814:     def OnNoModifierMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
Laurent@814:             self.SelectedElement.SetConnectorNegated(False)
Laurent@814:             self.SelectedElement.Refresh()
Laurent@814:             self.RefreshBuffer()
Laurent@814:     
Laurent@814:     def OnNegatedMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
Laurent@814:             self.SelectedElement.SetConnectorNegated(True)
Laurent@814:             self.SelectedElement.Refresh()
Laurent@814:             self.RefreshBuffer()
Laurent@814: 
Laurent@814:     def OnRisingEdgeMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
Laurent@814:             self.SelectedElement.SetConnectorEdge("rising")
Laurent@814:             self.SelectedElement.Refresh()
Laurent@814:             self.RefreshBuffer()
Laurent@814: 
Laurent@814:     def OnFallingEdgeMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
Laurent@814:             self.SelectedElement.SetConnectorEdge("falling")
Laurent@814:             self.SelectedElement.Refresh()
Laurent@814:             self.RefreshBuffer()
Laurent@814: 
Laurent@814:     def OnAddSegmentMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and self.IsWire(self.SelectedElement):
Laurent@814:             self.SelectedElement.AddSegment()
Laurent@814:             self.SelectedElement.Refresh()
Laurent@814: 
Laurent@814:     def OnDeleteSegmentMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and self.IsWire(self.SelectedElement):
Laurent@814:             self.SelectedElement.DeleteSegment()
Laurent@814:             self.SelectedElement.Refresh()
Laurent@814: 
Laurent@814:     def OnAddBranchMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
Laurent@814:             self.AddDivergenceBranch(self.SelectedElement)
Laurent@814: 
Laurent@814:     def OnDeleteBranchMenu(self, event):
Laurent@814:         if self.SelectedElement is not None and self.IsBlock(self.SelectedElement):
Laurent@814:             self.RemoveDivergenceBranch(self.SelectedElement)
Laurent@814: 
Laurent@814:     def OnEditBlockMenu(self, event):
Laurent@814:         if self.SelectedElement is not None:
Laurent@814:             self.ParentWindow.EditProjectElement(ITEM_POU, "P::%s"%self.SelectedElement.GetType())
Laurent@814: 
Laurent@852:     def OnAdjustBlockSizeMenu(self, event):
Laurent@852:         if self.SelectedElement is not None:
Laurent@852:             movex, movey = self.SelectedElement.SetBestSize(self.Scaling)
Laurent@872:             self.SelectedElement.RefreshModel(True)
Laurent@872:             self.RefreshBuffer()
Laurent@872:             self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
Laurent@852: 
Laurent@814:     def OnDeleteMenu(self, event):
Laurent@814:         if self.SelectedElement is not None:
Laurent@814:             self.SelectedElement.Delete()
Laurent@814:             self.SelectedElement = None
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.Refresh(False)
Laurent@814: 
Laurent@814:     def OnClearExecutionOrderMenu(self, event):
Laurent@814:         self.Controler.ClearEditedElementExecutionOrder(self.TagName)
Laurent@814:         self.RefreshBuffer()
Laurent@814:         self.RefreshView()
Laurent@814:         
Laurent@814:     def OnResetExecutionOrderMenu(self, event):
Laurent@814:         self.Controler.ResetEditedElementExecutionOrder(self.TagName)
Laurent@814:         self.RefreshBuffer()
Laurent@814:         self.RefreshView()
Laurent@814: 
Laurent@814:     def GetAddMenuCallBack(self, func, *args):
Laurent@814:         def AddMenuCallBack(event):
Laurent@814:             wx.CallAfter(func, self.rubberBand.GetCurrentExtent(), *args)
Laurent@814:         return AddMenuCallBack
Laurent@814: 
Laurent@814:     def GetClipboardCallBack(self, func):
Laurent@814:         def ClipboardCallback(event):
Laurent@814:             wx.CallAfter(func)
Laurent@814:         return ClipboardCallback
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                          Mouse event functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def OnViewerMouseEvent(self, event):
Laurent@814:         if not event.Entering():
Laurent@814:             self.ResetBuffer()
Laurent@814:             element = None
Laurent@814:             if not event.Leaving() and not event.LeftUp() and not event.LeftDClick():
Laurent@814:                 element = self.FindElement(event, True, False)
Laurent@814:             if self.ToolTipElement is not None:
Laurent@814:                 self.ToolTipElement.ClearToolTip()
Laurent@814:             self.ToolTipElement = element
Laurent@814:             if self.ToolTipElement is not None:
Laurent@814:                 tooltip_pos = self.Editor.ClientToScreen(event.GetPosition())
Laurent@814:                 tooltip_pos.x += 10
Laurent@814:                 tooltip_pos.y += 10
Laurent@814:                 self.ToolTipElement.CreateToolTip(tooltip_pos)
Laurent@814:         event.Skip()
Laurent@814:     
Laurent@814:     def OnViewerLeftDown(self, event):
Laurent@814:         self.Editor.CaptureMouse()
Laurent@814:         if self.Mode == MODE_SELECTION:
Laurent@814:             dc = self.GetLogicalDC()
Laurent@814:             pos = event.GetLogicalPosition(dc)
Laurent@814:             if event.ShiftDown() and not event.ControlDown() and self.SelectedElement is not None:
Laurent@814:                 element = self.FindElement(event, True)
Laurent@814:                 if element is not None:
Laurent@814:                     if isinstance(self.SelectedElement, Graphic_Group):
Laurent@814:                         self.SelectedElement.SetSelected(False)
Laurent@814:                         self.SelectedElement.SelectElement(element)
Laurent@814:                     elif self.SelectedElement is not None:
Laurent@814:                         group = Graphic_Group(self)
Laurent@814:                         group.SelectElement(self.SelectedElement)
Laurent@814:                         group.SelectElement(element)
Laurent@814:                         self.SelectedElement = group
Laurent@814:                     elements = self.SelectedElement.GetElements()
Laurent@814:                     if len(elements) == 0:
Laurent@814:                         self.SelectedElement = element
Laurent@814:                     elif len(elements) == 1:
Laurent@814:                         self.SelectedElement = elements[0]
Laurent@814:                     self.SelectedElement.SetSelected(True)
Laurent@814:                 else:
Laurent@814:                     self.rubberBand.Reset()
Laurent@814:                     self.rubberBand.OnLeftDown(event, dc, self.Scaling)
Laurent@814:             else:
Laurent@814:                 element = self.FindElement(event)
Laurent@814:                 if not self.Debug and (element is None or element.TestHandle(event) == (0, 0)):
Laurent@814:                     connector = self.FindBlockConnector(pos)
Laurent@814:                 else:
Laurent@814:                     connector = None
Laurent@814:                 if not self.Debug and self.DrawingWire:
Laurent@814:                     self.DrawingWire = False
Laurent@814:                     if self.SelectedElement is not None:
Laurent@814:                         if element is None or element.TestHandle(event) == (0, 0):
Laurent@814:                             connector = self.FindBlockConnector(pos, self.SelectedElement.GetConnectionDirection())
Laurent@814:                         if connector is not None:
Laurent@814:                             event.Dragging = lambda : True
Laurent@814:                             self.SelectedElement.OnMotion(event, dc, self.Scaling)
Laurent@814:                         if self.SelectedElement.EndConnected is not None:
Laurent@814:                             self.SelectedElement.ResetPoints()
Laurent@814:                             self.SelectedElement.GeneratePoints()
Laurent@814:                             self.SelectedElement.RefreshModel()
Laurent@814:                             self.SelectedElement.SetSelected(True)
Laurent@814:                             element = self.SelectedElement
Laurent@814:                             self.RefreshBuffer()
Laurent@814:                         else:
Laurent@814:                             rect = self.SelectedElement.GetRedrawRect()
Laurent@814:                             self.SelectedElement.Delete()
Laurent@814:                             self.SelectedElement = None
Laurent@814:                             element = None
Laurent@814:                             self.RefreshRect(self.GetScrolledRect(rect), False)
Laurent@814:                 elif not self.Debug and connector is not None and not event.ControlDown():
Laurent@814:                     self.DrawingWire = True
Laurent@814:                     scaled_pos = GetScaledEventPosition(event, dc, self.Scaling)
Laurent@814:                     if (connector.GetDirection() == EAST):
Laurent@814:                         wire = Wire(self, [wx.Point(pos.x, pos.y), EAST], [wx.Point(scaled_pos.x, scaled_pos.y), WEST])
Laurent@814:                     else:
Laurent@814:                         wire = Wire(self, [wx.Point(pos.x, pos.y), WEST], [wx.Point(scaled_pos.x, scaled_pos.y), EAST])
Laurent@814:                     wire.oldPos = scaled_pos
Laurent@814:                     wire.Handle = (HANDLE_POINT, 0)
Laurent@814:                     wire.ProcessDragging(0, 0, event, None)
Laurent@814:                     wire.Handle = (HANDLE_POINT, 1)
Laurent@814:                     self.AddWire(wire)
Laurent@814:                     if self.SelectedElement is not None:
Laurent@814:                         self.SelectedElement.SetSelected(False)
Laurent@814:                     self.SelectedElement = wire
Laurent@814:                     if self.HighlightedElement is not None:
Laurent@814:                         self.HighlightedElement.SetHighlighted(False)
Laurent@814:                     self.HighlightedElement = wire
Laurent@814:                     self.RefreshVisibleElements()
Laurent@814:                     self.SelectedElement.SetHighlighted(True)
Laurent@814:                 else:
Laurent@814:                     if self.SelectedElement is not None and self.SelectedElement != element:
Laurent@814:                         self.SelectedElement.SetSelected(False)
Laurent@814:                         self.SelectedElement = None
Laurent@814:                     if element is not None:
Laurent@814:                         self.SelectedElement = element
Laurent@814:                         if self.Debug:
Laurent@814:                             self.StartMousePos = event.GetPosition()
Laurent@814:                             Graphic_Element.OnLeftDown(self.SelectedElement, event, dc, self.Scaling)
Laurent@814:                         else:
Laurent@814:                             self.SelectedElement.OnLeftDown(event, dc, self.Scaling)
Laurent@814:                         self.SelectedElement.Refresh()
Laurent@814:                     else:
Laurent@814:                         self.rubberBand.Reset()
Laurent@814:                         self.rubberBand.OnLeftDown(event, dc, self.Scaling)
Laurent@814:         elif self.Mode in [MODE_BLOCK, MODE_VARIABLE, MODE_CONNECTION, MODE_COMMENT, 
Laurent@814:                            MODE_CONTACT, MODE_COIL, MODE_POWERRAIL, MODE_INITIALSTEP, 
Laurent@814:                            MODE_STEP, MODE_TRANSITION, MODE_DIVERGENCE, MODE_JUMP, MODE_ACTION]:
Laurent@814:             self.rubberBand.Reset()
Laurent@814:             self.rubberBand.OnLeftDown(event, self.GetLogicalDC(), self.Scaling)
Laurent@814:         elif self.Mode == MODE_MOTION:
Laurent@814:             self.StartMousePos = event.GetPosition()
Laurent@814:             self.StartScreenPos = self.GetScrollPos(wx.HORIZONTAL), self.GetScrollPos(wx.VERTICAL)
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def OnViewerLeftUp(self, event):
Laurent@814:         self.StartMousePos = None
Laurent@814:         if self.rubberBand.IsShown():
Laurent@814:             if self.Mode == MODE_SELECTION:
Laurent@814:                 new_elements = self.SearchElements(self.rubberBand.GetCurrentExtent())
Laurent@814:                 self.rubberBand.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)
Laurent@814:                 if event.ShiftDown() and self.SelectedElement is not None:
Laurent@814:                     if isinstance(self.SelectedElement, Graphic_Group):
Laurent@814:                         elements = self.SelectedElement.GetElements()
Laurent@814:                     else:
Laurent@814:                         elements = [self.SelectedElement]
Laurent@814:                     for element in elements:
Laurent@814:                         if element not in new_elements:
Laurent@814:                             new_elements.append(element)
Laurent@814:                 if len(new_elements) == 1:
Laurent@814:                     self.SelectedElement = new_elements[0]
Laurent@814:                     self.SelectedElement.SetSelected(True)
Laurent@814:                 elif len(new_elements) > 1:
Laurent@814:                     self.SelectedElement = Graphic_Group(self)
Laurent@814:                     self.SelectedElement.SetElements(new_elements)
Laurent@814:                     self.SelectedElement.SetSelected(True)
Laurent@814:             else:
Laurent@814:                 bbox = self.rubberBand.GetCurrentExtent()
Laurent@814:                 self.rubberBand.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)                
Laurent@814:                 if self.Mode == MODE_BLOCK:
Laurent@814:                     wx.CallAfter(self.AddNewBlock, bbox)
Laurent@814:                 elif self.Mode == MODE_VARIABLE:
Laurent@814:                     wx.CallAfter(self.AddNewVariable, bbox)
Laurent@814:                 elif self.Mode == MODE_CONNECTION:
Laurent@814:                     wx.CallAfter(self.AddNewConnection, bbox)
Laurent@814:                 elif self.Mode == MODE_COMMENT:
Laurent@814:                     wx.CallAfter(self.AddNewComment, bbox)
Laurent@814:                 elif self.Mode == MODE_CONTACT:
Laurent@814:                     wx.CallAfter(self.AddNewContact, bbox)
Laurent@814:                 elif self.Mode == MODE_COIL:
Laurent@814:                     wx.CallAfter(self.AddNewCoil, bbox)
Laurent@814:                 elif self.Mode == MODE_POWERRAIL:
Laurent@814:                     wx.CallAfter(self.AddNewPowerRail, bbox)
Laurent@814:                 elif self.Mode == MODE_INITIALSTEP:
Laurent@814:                     wx.CallAfter(self.AddNewStep, bbox, True)
Laurent@814:                 elif self.Mode == MODE_STEP:
Laurent@814:                     wx.CallAfter(self.AddNewStep, bbox, False)
Laurent@814:                 elif self.Mode == MODE_TRANSITION:
Laurent@814:                     wx.CallAfter(self.AddNewTransition, bbox)
Laurent@814:                 elif self.Mode == MODE_DIVERGENCE:
Laurent@814:                     wx.CallAfter(self.AddNewDivergence, bbox)
Laurent@814:                 elif self.Mode == MODE_JUMP:
Laurent@814:                     wx.CallAfter(self.AddNewJump, bbox)
Laurent@814:                 elif self.Mode == MODE_ACTION:
Laurent@814:                     wx.CallAfter(self.AddNewActionBlock, bbox)
Laurent@814:         elif self.Mode == MODE_SELECTION and self.SelectedElement is not None:
Laurent@814:             dc = self.GetLogicalDC()
Laurent@814:             if not self.Debug and self.DrawingWire:
Laurent@814:                 pos = event.GetLogicalPosition(dc)
Laurent@814:                 connector = self.FindBlockConnector(pos, self.SelectedElement.GetConnectionDirection())
Laurent@814:                 if self.SelectedElement.EndConnected is not None:
Laurent@814:                     self.DrawingWire = False
Laurent@814:                     self.SelectedElement.StartConnected.HighlightParentBlock(False)
Laurent@814:                     self.SelectedElement.EndConnected.HighlightParentBlock(False)
Laurent@814:                     self.SelectedElement.ResetPoints()
Laurent@814:                     self.SelectedElement.OnMotion(event, dc, self.Scaling)
Laurent@814:                     self.SelectedElement.GeneratePoints()
Laurent@814:                     self.SelectedElement.RefreshModel()
Laurent@814:                     self.SelectedElement.SetSelected(True)
Laurent@814:                     self.SelectedElement.HighlightPoint(pos)
Laurent@814:                     self.RefreshBuffer()
Laurent@814:                 elif connector is None or self.SelectedElement.GetDragging():
Laurent@814:                     self.DrawingWire = False
Laurent@814:                     rect = self.SelectedElement.GetRedrawRect()
Laurent@814:                     wire = self.SelectedElement
Laurent@814:                     self.SelectedElement = self.SelectedElement.StartConnected.GetParentBlock()
Laurent@814:                     self.SelectedElement.SetSelected(True)
Laurent@814:                     rect.Union(self.SelectedElement.GetRedrawRect())
Laurent@814:                     wire.Delete()
Laurent@814:                     self.RefreshRect(self.GetScrolledRect(rect), False)
Laurent@814:             else:
Laurent@814:                 if self.Debug:
Laurent@814:                     Graphic_Element.OnLeftUp(self.SelectedElement, event, dc, self.Scaling)
Laurent@814:                 else:
Laurent@814:                     self.SelectedElement.OnLeftUp(event, dc, self.Scaling)
Laurent@814:                 wx.CallAfter(self.SetCurrentCursor, 0)
Laurent@814:         elif self.Mode == MODE_MOTION:
Laurent@814:             self.StartMousePos = None
Laurent@814:             self.StartScreenPos = None
Laurent@814:         if self.Mode != MODE_SELECTION and not self.SavedMode:
Laurent@814:             wx.CallAfter(self.ParentWindow.ResetCurrentMode)
Laurent@814:         if self.Editor.HasCapture():
Laurent@814:             self.Editor.ReleaseMouse()
Laurent@814:         event.Skip()
Laurent@814:     
Laurent@814:     def OnViewerMiddleDown(self, event):
Laurent@814:         self.Editor.CaptureMouse()
Laurent@814:         self.StartMousePos = event.GetPosition()
Laurent@814:         self.StartScreenPos = self.GetScrollPos(wx.HORIZONTAL), self.GetScrollPos(wx.VERTICAL)
Laurent@814:         event.Skip()
Laurent@814:         
Laurent@814:     def OnViewerMiddleUp(self, event):
Laurent@814:         self.StartMousePos = None
Laurent@814:         self.StartScreenPos = None
Laurent@814:         if self.Editor.HasCapture():
Laurent@814:             self.Editor.ReleaseMouse()
Laurent@814:         event.Skip()
Laurent@814:     
Laurent@814:     def OnViewerRightDown(self, event):
Laurent@814:         self.Editor.CaptureMouse()
Laurent@814:         if self.Mode == MODE_SELECTION:
Laurent@814:             element = self.FindElement(event)
Laurent@814:             if self.SelectedElement is not None and self.SelectedElement != element:
Laurent@814:                 self.SelectedElement.SetSelected(False)
Laurent@814:                 self.SelectedElement = None
Laurent@814:             if element is not None:
Laurent@814:                 self.SelectedElement = element
Laurent@814:                 if self.Debug:
Laurent@814:                     Graphic_Element.OnRightDown(self.SelectedElement, event, self.GetLogicalDC(), self.Scaling)
Laurent@814:                 else:
Laurent@814:                     self.SelectedElement.OnRightDown(event, self.GetLogicalDC(), self.Scaling)
Laurent@814:                 self.SelectedElement.Refresh()
Laurent@814:         event.Skip()
Laurent@814:     
Laurent@814:     def OnViewerRightUp(self, event):
Laurent@814:         dc = self.GetLogicalDC()
Laurent@814:         self.rubberBand.Reset()
Laurent@814:         self.rubberBand.OnLeftDown(event, dc, self.Scaling)
Laurent@814:         self.rubberBand.OnLeftUp(event, dc, self.Scaling)
Laurent@814:         if self.SelectedElement is not None:
Laurent@814:             if self.Debug:
Laurent@814:                 Graphic_Element.OnRightUp(self.SelectedElement, event, self.GetLogicalDC(), self.Scaling)
Laurent@814:             else:
Laurent@814:                 self.SelectedElement.OnRightUp(event, self.GetLogicalDC(), self.Scaling)
Laurent@814:             wx.CallAfter(self.SetCurrentCursor, 0)
Laurent@814:         elif not self.Debug:
Laurent@814:             self.PopupDefaultMenu(False)
Laurent@814:         if self.Editor.HasCapture():
Laurent@814:             self.Editor.ReleaseMouse()
Laurent@814:         event.Skip()
Laurent@814:     
Laurent@814:     def OnViewerLeftDClick(self, event):
Laurent@814:         element = self.FindElement(event, connectors=False)
Laurent@814:         if self.Mode == MODE_SELECTION and element is not None:
Laurent@814:             if self.SelectedElement is not None and self.SelectedElement != element:
Laurent@814:                 self.SelectedElement.SetSelected(False)
Laurent@814:             if self.HighlightedElement is not None and self.HighlightedElement != element:
Laurent@814:                 self.HighlightedElement.SetHighlighted(False)
Laurent@814:                     
Laurent@814:             self.SelectedElement = element
Laurent@814:             self.HighlightedElement = element
Laurent@814:             self.SelectedElement.SetHighlighted(True)
Laurent@814:             
Laurent@814:             if self.Debug:
laurent@820:                 if isinstance(self.SelectedElement, FBD_Block):
Laurent@814:                     instance_type = self.SelectedElement.GetType()
Laurent@814:                     pou_type = {
Laurent@814:                         "program": ITEM_PROGRAM,
Laurent@814:                         "functionBlock": ITEM_FUNCTIONBLOCK,
Laurent@814:                     }.get(self.Controler.GetPouType(instance_type))
Laurent@814:                     if pou_type is not None and instance_type in self.Controler.GetProjectPouNames(self.Debug):
Laurent@814:                         self.ParentWindow.OpenDebugViewer(pou_type, 
laurent@828:                             "%s.%s"%(self.GetInstancePath(True), self.SelectedElement.GetName()),
Laurent@814:                             self.Controler.ComputePouName(instance_type))
Laurent@814:                 else:
Laurent@814:                     iec_path = self.GetElementIECPath(self.SelectedElement)
Laurent@814:                     if iec_path is not None:
Laurent@814:                         if isinstance(self.SelectedElement, Wire):
Laurent@814:                             if self.SelectedElement.EndConnected is not None:
Laurent@885:                                 self.ParentWindow.OpenDebugViewer(ITEM_VAR_LOCAL, iec_path, 
Laurent@885:                                         self.SelectedElement.EndConnected.GetType())
Laurent@814:                         else:
Laurent@885:                             self.ParentWindow.OpenDebugViewer(ITEM_VAR_LOCAL, iec_path, "BOOL")
Laurent@814:             elif event.ControlDown() and not event.ShiftDown():
Laurent@857:                 if not isinstance(self.SelectedElement, Graphic_Group):
Laurent@882:                     if isinstance(self.SelectedElement, FBD_Block):
Laurent@882:                         instance_type = self.SelectedElement.GetType()
Laurent@882:                     else:
Laurent@882:                         instance_type = None
Laurent@882:                     if instance_type in self.Controler.GetProjectPouNames(self.Debug):
Laurent@852:                         self.ParentWindow.EditProjectElement(ITEM_POU, 
Laurent@882:                                 self.Controler.ComputePouName(instance_type))
Laurent@852:                     else:
Laurent@852:                         self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
Laurent@814:             elif event.ControlDown() and event.ShiftDown():
Laurent@852:                 movex, movey = self.SelectedElement.SetBestSize(self.Scaling)
Laurent@872:                 self.SelectedElement.RefreshModel()
Laurent@872:                 self.RefreshBuffer()
Laurent@872:                 self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
Laurent@814:             else:
Laurent@814:                 self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
Laurent@814:         event.Skip()
Laurent@814:     
Laurent@814:     def OnViewerMotion(self, event):
Laurent@814:         if self.Editor.HasCapture() and not event.Dragging():
Laurent@814:             return
Laurent@814:         refresh = False
Laurent@814:         dc = self.GetLogicalDC()
Laurent@814:         pos = GetScaledEventPosition(event, dc, self.Scaling)
Laurent@814:         if event.MiddleIsDown() or self.Mode == MODE_MOTION:
Laurent@814:             if self.StartMousePos is not None and self.StartScreenPos is not None:
Laurent@814:                 new_pos = event.GetPosition()
Laurent@814:                 xmax = self.GetScrollRange(wx.HORIZONTAL) - self.GetScrollThumb(wx.HORIZONTAL)
Laurent@814:                 ymax = self.GetScrollRange(wx.VERTICAL) - self.GetScrollThumb(wx.VERTICAL)
Laurent@814:                 scrollx = max(0, self.StartScreenPos[0] - (new_pos[0] - self.StartMousePos[0]) / SCROLLBAR_UNIT)
Laurent@814:                 scrolly = max(0, self.StartScreenPos[1] - (new_pos[1] - self.StartMousePos[1]) / SCROLLBAR_UNIT)
Laurent@814:                 if scrollx > xmax or scrolly > ymax:
Laurent@814:                     self.RefreshScrollBars(max(0, scrollx - xmax), max(0, scrolly - ymax))
Laurent@814:                     self.Scroll(scrollx, scrolly)
Laurent@814:                 else:
Laurent@814:                     self.Scroll(scrollx, scrolly)
Laurent@814:                     self.RefreshScrollBars()
Laurent@814:                 self.RefreshVisibleElements()
Laurent@814:         else:
Laurent@814:             if not event.Dragging():
Laurent@814:                 highlighted = self.FindElement(event, connectors=False) 
Laurent@814:                 if self.HighlightedElement is not None and self.HighlightedElement != highlighted:
Laurent@814:                     self.HighlightedElement.SetHighlighted(False)
Laurent@814:                     self.HighlightedElement = None
Laurent@814:                 if highlighted is not None:
Laurent@814:                     if isinstance(highlighted, (Wire, Graphic_Group)):
Laurent@814:                         highlighted.HighlightPoint(pos)
Laurent@814:                     if self.HighlightedElement != highlighted:
Laurent@814:                         highlighted.SetHighlighted(True)
Laurent@814:                 self.HighlightedElement = highlighted
Laurent@814:             if self.rubberBand.IsShown():
Laurent@814:                 self.rubberBand.OnMotion(event, dc, self.Scaling)
Laurent@814:             elif not self.Debug and self.Mode == MODE_SELECTION and self.SelectedElement is not None:
Laurent@814:                 if self.DrawingWire:
Laurent@814:                     connector = self.FindBlockConnector(pos, self.SelectedElement.GetConnectionDirection(), self.SelectedElement.EndConnected)
Laurent@814:                     if not connector or self.SelectedElement.EndConnected == None:
Laurent@814:                         self.SelectedElement.ResetPoints()
Laurent@814:                         movex, movey = self.SelectedElement.OnMotion(event, dc, self.Scaling)
Laurent@814:                         self.SelectedElement.GeneratePoints()
Laurent@814:                         if movex != 0 or movey != 0:
Laurent@814:                             self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
Laurent@814:                     else:
Laurent@814:                         self.SelectedElement.HighlightPoint(pos)
Laurent@814:                 else:
Laurent@814:                     movex, movey = self.SelectedElement.OnMotion(event, dc, self.Scaling)
Laurent@814:                     if movex != 0 or movey != 0:
Laurent@814:                         self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
Laurent@814:             elif self.Debug and self.StartMousePos is not None and event.Dragging():
Laurent@814:                 pos = event.GetPosition()
Laurent@814:                 if abs(self.StartMousePos.x - pos.x) > 5 or abs(self.StartMousePos.y - pos.y) > 5:
Laurent@814:                     iec_path = self.GetElementIECPath(self.SelectedElement)
Laurent@814:                     if iec_path is not None:
Laurent@814:                         self.StartMousePos = None
Laurent@814:                         if self.HighlightedElement is not None:
Laurent@814:                             self.HighlightedElement.SetHighlighted(False)
Laurent@814:                             self.HighlightedElement = None
Laurent@814:                         data = wx.TextDataObject(str((iec_path, "debug")))
Laurent@814:                         dragSource = wx.DropSource(self.Editor)
Laurent@814:                         dragSource.SetData(data)
Laurent@814:                         dragSource.DoDragDrop()
Laurent@908:                         if self.Editor.HasCapture():
Laurent@908:                             self.Editor.ReleaseMouse()
Laurent@908:                         wx.CallAfter(self.SetCurrentCursor, 0)
Laurent@814:             self.UpdateScrollPos(event)
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def OnLeaveViewer(self, event):
Laurent@814:         if self.StartScreenPos is None:
Laurent@814:             self.StartMousePos = None
Laurent@814:         if self.SelectedElement is not None and self.SelectedElement.GetDragging():
Laurent@814:             event.Skip()
Laurent@814:         elif self.HighlightedElement is not None:
Laurent@814:             self.HighlightedElement.SetHighlighted(False)
Laurent@814:             self.HighlightedElement = None
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def UpdateScrollPos(self, event):
Laurent@814:         if (event.Dragging() and self.SelectedElement is not None) or self.rubberBand.IsShown():
Laurent@814:             position = event.GetPosition()
Laurent@814:             move_window = wx.Point()
Laurent@814:             window_size = self.Editor.GetClientSize()
Laurent@814:             xstart, ystart = self.GetViewStart()
Laurent@814:             if position.x < SCROLL_ZONE and xstart > 0:
Laurent@814:                 move_window.x = -1
Laurent@814:             elif position.x > window_size[0] - SCROLL_ZONE:
Laurent@814:                 move_window.x = 1
Laurent@814:             if position.y < SCROLL_ZONE and ystart > 0:
Laurent@814:                 move_window.y = -1
Laurent@814:             elif position.y > window_size[1] - SCROLL_ZONE:
Laurent@814:                 move_window.y = 1
Laurent@814:             if move_window.x != 0 or move_window.y != 0:
Laurent@814:                 self.RefreshVisibleElements(xp = xstart + move_window.x, yp = ystart + move_window.y)
Laurent@814:                 self.Scroll(xstart + move_window.x, ystart + move_window.y)
Laurent@814:                 self.RefreshScrollBars(move_window.x, move_window.y)
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                          Keyboard event functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     ARROW_KEY_MOVE = {
Laurent@814:         wx.WXK_LEFT: (-1, 0),
Laurent@814:         wx.WXK_RIGHT: (1, 0),
Laurent@814:         wx.WXK_UP: (0, -1),
Laurent@814:         wx.WXK_DOWN: (0, 1),
Laurent@814:     }
Laurent@814: 
Laurent@814:     def OnChar(self, event):
Laurent@814:         xpos, ypos = self.GetScrollPos(wx.HORIZONTAL), self.GetScrollPos(wx.VERTICAL)
Laurent@814:         xmax = self.GetScrollRange(wx.HORIZONTAL) - self.GetScrollThumb(wx.HORIZONTAL)
Laurent@814:         ymax = self.GetScrollRange(wx.VERTICAL) - self.GetScrollThumb(wx.VERTICAL)
Laurent@814:         keycode = event.GetKeyCode()
Laurent@814:         if self.Scaling is not None:
Laurent@814:             scaling = self.Scaling
Laurent@814:         else:
Laurent@814:             scaling = (8, 8)
Laurent@814:         if not self.Debug and keycode == wx.WXK_DELETE and self.SelectedElement is not None:
Laurent@814:             rect = self.SelectedElement.GetRedrawRect(1, 1)
Laurent@814:             self.SelectedElement.Delete()
Laurent@814:             self.SelectedElement = None
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             wx.CallAfter(self.SetCurrentCursor, 0)
Laurent@814:             self.RefreshRect(self.GetScrolledRect(rect), False)
Laurent@814:         elif not self.Debug and keycode == wx.WXK_RETURN and self.SelectedElement is not None:
Laurent@814:             self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
Laurent@814:         elif self.ARROW_KEY_MOVE.has_key(keycode):
Laurent@814:             move = self.ARROW_KEY_MOVE[keycode]
Laurent@814:             if event.ControlDown() and event.ShiftDown():
Laurent@814:                 self.Scroll({-1: 0, 0: xpos, 1: xmax}[move[0]],
Laurent@814:                             {-1: 0, 0: ypos, 1: ymax}[move[1]])
Laurent@814:                 self.RefreshVisibleElements()
Laurent@814:             elif event.ControlDown():
Laurent@814:                 self.Scroll(xpos + move[0], ypos + move[1])
Laurent@814:                 self.RefreshScrollBars()
Laurent@814:                 self.RefreshVisibleElements()
Laurent@814:             elif not self.Debug and self.SelectedElement is not None:
Laurent@814:                 movex, movey = move
Laurent@814:                 if not event.AltDown() or event.ShiftDown():
Laurent@814:                     movex *= scaling[0]
Laurent@814:                     movey *= scaling[1] 
Laurent@814:                     if event.ShiftDown() and not event.AltDown():
Laurent@814:                         movex *= 10
Laurent@814:                         movey *= 10
Laurent@814:                 self.SelectedElement.Move(movex, movey)
Laurent@814:                 self.StartBuffering()
Laurent@814:                 self.SelectedElement.RefreshModel()
Laurent@814:                 self.RefreshScrollBars()
Laurent@814:                 self.RefreshRect(self.GetScrolledRect(self.SelectedElement.GetRedrawRect(movex, movey)), False)
Laurent@814:         elif not self.Debug and keycode == wx.WXK_SPACE and self.SelectedElement is not None and self.SelectedElement.Dragging:
Laurent@814:             if self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement):
Laurent@814:                 block = self.CopyBlock(self.SelectedElement, wx.Point(*self.SelectedElement.GetPosition()))
Laurent@814:                 event = wx.MouseEvent()
Laurent@814:                 event.m_x, event.m_y = self.Editor.ScreenToClient(wx.GetMousePosition())
Laurent@814:                 dc = self.GetLogicalDC()
Laurent@814:                 self.SelectedElement.OnLeftUp(event, dc, self.Scaling)
Laurent@814:                 self.SelectedElement.SetSelected(False)
Laurent@814:                 block.OnLeftDown(event, dc, self.Scaling)
Laurent@814:                 self.SelectedElement = block
Laurent@814:                 self.SelectedElement.SetSelected(True)
Laurent@814:                 self.RefreshVariablePanel()
Laurent@814:                 self.RefreshVisibleElements()
Laurent@814:             else:
Laurent@814:                 event.Skip()
Laurent@814:         elif keycode == ord("+"):
Laurent@814:             self.SetScale(self.CurrentScale + 1)
Laurent@814:             self.ParentWindow.RefreshDisplayMenu()
Laurent@814:         elif keycode == ord("-"):
Laurent@814:             self.SetScale(self.CurrentScale - 1)
Laurent@814:             self.ParentWindow.RefreshDisplayMenu()
Laurent@814:         else:
Laurent@814:             event.Skip()
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                  Model adding functions from Drop Target
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type):
Laurent@814:         id = self.GetNewId()
Laurent@814:         variable = FBD_Variable(self, var_class, var_name, var_type, id)
Laurent@814:         width, height = variable.GetMinSize()
Laurent@814:         if scaling is not None:
Laurent@814:             x = round(float(x) / float(scaling[0])) * scaling[0]
Laurent@814:             y = round(float(y) / float(scaling[1])) * scaling[1]
Laurent@814:             width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
Laurent@814:             height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
Laurent@814:         variable.SetPosition(x, y)
Laurent@814:         variable.SetSize(width, height)
Laurent@814:         self.AddBlock(variable)
Laurent@814:         self.Controler.AddEditedElementVariable(self.GetTagName(), id, var_class)
Laurent@814:         self.RefreshVariableModel(variable)
Laurent@814:         self.RefreshBuffer()
Laurent@814:         self.RefreshScrollBars()
Laurent@814:         self.RefreshVisibleElements()
Laurent@814:         self.Refresh(False)
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                          Model adding functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def GetScaledSize(self, width, height):
Laurent@814:         if self.Scaling is not None:
Laurent@814:             width = round(float(width) / float(self.Scaling[0]) + 0.4) * self.Scaling[0]
Laurent@814:             height = round(float(height) / float(self.Scaling[1]) + 0.4) * self.Scaling[1]
Laurent@814:         return width, height
Laurent@814:     
Laurent@814:     def AddNewBlock(self, bbox):
Laurent@814:         dialog = FBDBlockDialog(self.ParentWindow, self.Controler)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetBlockList(self.Controler.GetBlockTypes(self.TagName, self.Debug))
Laurent@814:         dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
Laurent@814:         dialog.SetPouElementNames(self.Controler.GetEditedElementVariables(self.TagName, self.Debug))
Laurent@814:         dialog.SetMinBlockSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             values.setdefault("name", "")
Laurent@814:             block = FBD_Block(self, values["type"], values["name"], id, 
Laurent@814:                     values["extension"], values["inputs"], 
Laurent@814:                     executionControl = values["executionControl"],
Laurent@814:                     executionOrder = values["executionOrder"])
Laurent@814:             block.SetPosition(bbox.x, bbox.y)
Laurent@814:             block.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             self.AddBlock(block)
Laurent@814:             self.Controler.AddEditedElementBlock(self.TagName, id, values["type"], values.get("name", None))
Laurent@814:             self.RefreshBlockModel(block)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             self.RefreshVariablePanel()
Laurent@814:             self.ParentWindow.RefreshPouInstanceVariablesPanel()
Laurent@814:             block.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814:     
Laurent@814:     def AddNewVariable(self, bbox):
Laurent@814:         words = self.TagName.split("::")
Laurent@814:         if words[0] == "T":
Laurent@814:             dialog = FBDVariableDialog(self.ParentWindow, self.Controler, words[2])
Laurent@814:         else:
Laurent@814:             dialog = FBDVariableDialog(self.ParentWindow, self.Controler)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetMinVariableSize((bbox.width, bbox.height))
Laurent@814:         varlist = []
Laurent@814:         vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
Laurent@814:         if vars:
Laurent@814:             for var in vars:
Laurent@814:                 if var["Edit"]:
Laurent@814:                     varlist.append((var["Name"], var["Class"], var["Type"]))
Laurent@814:         returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
Laurent@814:         if returntype:
Laurent@814:             varlist.append((self.Controler.GetEditedElementName(self.TagName), "Output", returntype))
Laurent@814:         dialog.SetVariables(varlist)
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             variable = FBD_Variable(self, values["type"], values["name"], values["value_type"], id)
Laurent@814:             variable.SetPosition(bbox.x, bbox.y)
Laurent@814:             variable.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             self.AddBlock(variable)
Laurent@814:             self.Controler.AddEditedElementVariable(self.TagName, id, values["type"])
Laurent@814:             self.RefreshVariableModel(variable)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             variable.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewConnection(self, bbox):
Laurent@814:         dialog = ConnectionDialog(self.ParentWindow, self.Controler)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
Laurent@814:         dialog.SetPouElementNames(self.Controler.GetEditedElementVariables(self.TagName, self.Debug))
Laurent@814:         dialog.SetMinConnectionSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             connection = FBD_Connector(self, values["type"], values["name"], id)
Laurent@814:             connection.SetPosition(bbox.x, bbox.y)
Laurent@814:             connection.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             self.AddBlock(connection)
Laurent@814:             self.Controler.AddEditedElementConnection(self.TagName, id, values["type"])
Laurent@814:             self.RefreshConnectionModel(connection)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             connection.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewComment(self, bbox):
Laurent@814:         if wx.VERSION >= (2, 5, 0):
Laurent@814:             dialog = wx.TextEntryDialog(self.ParentWindow, _("Edit comment"), _("Please enter comment text"), "", wx.OK|wx.CANCEL|wx.TE_MULTILINE)
Laurent@814:         else:
Laurent@814:             dialog = wx.TextEntryDialog(self.ParentWindow, _("Edit comment"), _("Please enter comment text"), "", wx.OK|wx.CANCEL)
Laurent@814:         dialog.SetClientSize(wx.Size(400, 200))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             value = dialog.GetValue()
Laurent@814:             id = self.GetNewId()
Laurent@814:             comment = Comment(self, value, id)
Laurent@814:             comment.SetPosition(bbox.x, bbox.y)
Laurent@814:             min_width, min_height = comment.GetMinSize()
Laurent@814:             comment.SetSize(*self.GetScaledSize(max(min_width,bbox.width),max(min_height,bbox.height)))
Laurent@814:             self.AddComment(comment)
Laurent@814:             self.Controler.AddEditedElementComment(self.TagName, id)
Laurent@814:             self.RefreshCommentModel(comment)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             comment.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewContact(self, bbox):
Laurent@814:         dialog = LDElementDialog(self.ParentWindow, self.Controler, "contact")
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         varlist = []
Laurent@814:         vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
Laurent@814:         if vars:
Laurent@814:             for var in vars:
Laurent@814:                 if var["Type"] == "BOOL":
Laurent@814:                     varlist.append(var["Name"])
Laurent@814:         dialog.SetVariables(varlist)
Laurent@814:         dialog.SetValues({"name":"","type":CONTACT_NORMAL})
Laurent@814:         dialog.SetElementSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             contact = LD_Contact(self, values["type"], values["name"], id)
Laurent@814:             contact.SetPosition(bbox.x, bbox.y)
Laurent@814:             contact.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             self.AddBlock(contact)
Laurent@814:             self.Controler.AddEditedElementContact(self.TagName, id)
Laurent@814:             self.RefreshContactModel(contact)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             contact.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewCoil(self, bbox):
Laurent@814:         dialog = LDElementDialog(self.ParentWindow, self.Controler, "coil")
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         varlist = []
Laurent@814:         vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
Laurent@814:         if vars:
Laurent@814:             for var in vars:
Laurent@814:                 if var["Class"] != "Input" and var["Type"] == "BOOL":
Laurent@814:                     varlist.append(var["Name"])
Laurent@814:         returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
Laurent@814:         if returntype == "BOOL":
Laurent@814:             varlist.append(self.Controler.GetEditedElementName(self.TagName))
Laurent@814:         dialog.SetVariables(varlist)
Laurent@814:         dialog.SetValues({"name":"","type":COIL_NORMAL})
Laurent@814:         dialog.SetElementSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             coil = LD_Coil(self, values["type"], values["name"], id)
Laurent@814:             coil.SetPosition(bbox.x, bbox.y)
Laurent@814:             coil.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             self.AddBlock(coil)
Laurent@814:             self.Controler.AddEditedElementCoil(self.TagName, id)
Laurent@814:             self.RefreshCoilModel(coil)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             coil.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewPowerRail(self, bbox):
Laurent@814:         dialog = LDPowerRailDialog(self.ParentWindow, self.Controler)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetMinSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             powerrail = LD_PowerRail(self, values["type"], id, values["number"])
Laurent@814:             powerrail.SetPosition(bbox.x, bbox.y)
Laurent@814:             powerrail.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             self.AddBlock(powerrail)
Laurent@814:             self.Controler.AddEditedElementPowerRail(self.TagName, id, values["type"])
Laurent@814:             self.RefreshPowerRailModel(powerrail)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             powerrail.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewStep(self, bbox, initial = False):
Laurent@814:         dialog = SFCStepDialog(self.ParentWindow, self.Controler, initial)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
Laurent@814:         dialog.SetVariables(self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug))
Laurent@814:         dialog.SetStepNames([block.GetName() for block in self.Blocks.itervalues() if isinstance(block, SFC_Step)])
Laurent@814:         dialog.SetMinStepSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             step = SFC_Step(self, values["name"], initial, id)
Laurent@814:             if values["input"]:
Laurent@814:                 step.AddInput()
Laurent@814:             else:
Laurent@814:                 step.RemoveInput()
Laurent@814:             if values["output"]:
Laurent@814:                 step.AddOutput()
Laurent@814:             else:
Laurent@814:                 step.RemoveOutput()
Laurent@814:             if values["action"]:
Laurent@814:                 step.AddAction()    
Laurent@814:             else:
Laurent@814:                 step.RemoveAction()
Laurent@814:             step.SetPosition(bbox.x, bbox.y)
Laurent@814:             min_width, min_height = step.GetMinSize()
Laurent@814:             step.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
Laurent@814:             self.AddBlock(step)
Laurent@814:             self.Controler.AddEditedElementStep(self.TagName, id)
Laurent@814:             self.RefreshStepModel(step)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             step.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewTransition(self, bbox):
Laurent@814:         dialog = SFCTransitionDialog(self.ParentWindow, self.Controler, self.GetDrawingMode() == FREEDRAWING_MODE)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetTransitions(self.Controler.GetEditedElementTransitions(self.TagName, self.Debug))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             transition = SFC_Transition(self, values["type"], values["value"], values["priority"], id)
Laurent@814:             transition.SetPosition(bbox.x, bbox.y)
Laurent@814:             min_width, min_height = transition.GetMinSize()
Laurent@814:             transition.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
Laurent@814:             self.AddBlock(transition)
Laurent@814:             self.Controler.AddEditedElementTransition(self.TagName, id)
Laurent@814:             self.RefreshTransitionModel(transition)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             transition.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewDivergence(self, bbox):
Laurent@814:         dialog = SFCDivergenceDialog(self.ParentWindow, self.Controler)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetMinSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             divergence = SFC_Divergence(self, values["type"], values["number"], id)
Laurent@814:             divergence.SetPosition(bbox.x, bbox.y)
Laurent@814:             min_width, min_height = divergence.GetMinSize(True)
Laurent@814:             divergence.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
Laurent@814:             self.AddBlock(divergence)
Laurent@814:             self.Controler.AddEditedElementDivergence(self.TagName, id, values["type"])
Laurent@814:             self.RefreshDivergenceModel(divergence)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             divergence.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewJump(self, bbox):
Laurent@814:         choices = []
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             if isinstance(block, SFC_Step):
Laurent@814:                 choices.append(block.GetName())
Laurent@814:         dialog = wx.SingleChoiceDialog(self.ParentWindow, 
Laurent@814:               _("Add a new jump"), _("Please choose a target"), 
Laurent@814:               choices, wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL)
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             value = dialog.GetStringSelection()
Laurent@814:             jump = SFC_Jump(self, value, id)
Laurent@814:             jump.SetPosition(bbox.x, bbox.y)
Laurent@814:             min_width, min_height = jump.GetMinSize()
Laurent@814:             jump.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
Laurent@814:             self.AddBlock(jump)
Laurent@814:             self.Controler.AddEditedElementJump(self.TagName, id)
Laurent@814:             self.RefreshJumpModel(jump)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             jump.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def AddNewActionBlock(self, bbox):
Laurent@814:         dialog = ActionBlockDialog(self.ParentWindow)
Laurent@814:         dialog.SetQualifierList(self.Controler.GetQualifierTypes())
Laurent@814:         dialog.SetActionList(self.Controler.GetEditedElementActions(self.TagName, self.Debug))
Laurent@814:         dialog.SetVariableList(self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             actions = dialog.GetValues()
Laurent@814:             id = self.GetNewId()
Laurent@814:             actionblock = SFC_ActionBlock(self, actions, id)
Laurent@814:             actionblock.SetPosition(bbox.x, bbox.y)
Laurent@814:             min_width, min_height = actionblock.GetMinSize()
Laurent@814:             actionblock.SetSize(*self.GetScaledSize(max(bbox.width, min_width), max(bbox.height, min_height)))
Laurent@814:             self.AddBlock(actionblock)
Laurent@814:             self.Controler.AddEditedElementActionBlock(self.TagName, id)
Laurent@814:             self.RefreshActionBlockModel(actionblock)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             actionblock.Refresh()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                          Edit element content functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def EditBlockContent(self, block):
Laurent@814:         dialog = FBDBlockDialog(self.ParentWindow, self.Controler)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetBlockList(self.Controler.GetBlockTypes(self.TagName, self.Debug))
Laurent@814:         dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
Laurent@814:         variable_names = self.Controler.GetEditedElementVariables(self.TagName, self.Debug)
Laurent@814:         if block.GetName() != "":
Laurent@814:             variable_names.remove(block.GetName())
Laurent@814:         dialog.SetPouElementNames(variable_names)
Laurent@814:         dialog.SetMinBlockSize(block.GetSize())
Laurent@814:         old_values = {"name" : block.GetName(), 
Laurent@814:                       "type" : block.GetType(), 
Laurent@814:                       "extension" : block.GetExtension(), 
Laurent@814:                       "inputs" : block.GetInputTypes(), 
Laurent@814:                       "executionControl" : block.GetExecutionControl(), 
Laurent@814:                       "executionOrder" : block.GetExecutionOrder()}
Laurent@814:         dialog.SetValues(old_values)
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             new_values = dialog.GetValues()
Laurent@814:             rect = block.GetRedrawRect(1, 1)
Laurent@814:             if "name" in new_values:
Laurent@814:                 block.SetName(new_values["name"])
Laurent@814:             else:
Laurent@814:                 block.SetName("")
Laurent@814:             block.SetSize(*self.GetScaledSize(new_values["width"], new_values["height"]))
Laurent@814:             block.SetType(new_values["type"], new_values["extension"], executionControl = new_values["executionControl"])
Laurent@814:             block.SetExecutionOrder(new_values["executionOrder"])
Laurent@814:             rect = rect.Union(block.GetRedrawRect())
Laurent@814:             self.RefreshBlockModel(block)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             if old_values["executionOrder"] != new_values["executionOrder"]:
Laurent@814:                 self.RefreshView(selection=({block.GetId(): True}, {}))
Laurent@814:             else:
Laurent@814:                 self.RefreshScrollBars()
Laurent@814:                 self.RefreshVisibleElements()
Laurent@814:                 block.Refresh(rect)
Laurent@814:             self.RefreshVariablePanel()
Laurent@814:             self.ParentWindow.RefreshPouInstanceVariablesPanel()
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def EditVariableContent(self, variable):
Laurent@814:         words = self.TagName.split("::")
Laurent@814:         if words[0] == "T":
Laurent@814:             dialog = FBDVariableDialog(self.ParentWindow, self.Controler, words[2])
Laurent@814:         else:
Laurent@814:             dialog = FBDVariableDialog(self.ParentWindow, self.Controler)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetMinVariableSize(variable.GetSize())
Laurent@814:         varlist = []
Laurent@814:         vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
Laurent@814:         if vars:
Laurent@814:             for var in vars:
Laurent@814:                 if var["Edit"]:
Laurent@814:                     varlist.append((var["Name"], var["Class"], var["Type"]))
Laurent@814:         returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
Laurent@814:         if returntype:
Laurent@814:             varlist.append((self.Controler.GetEditedElementName(self.TagName), "Output", returntype))
Laurent@814:         dialog.SetVariables(varlist)
Laurent@814:         old_values = {"name" : variable.GetName(), "type" : variable.GetType(), 
Laurent@814:             "executionOrder" : variable.GetExecutionOrder()}
Laurent@814:         dialog.SetValues(old_values)
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             new_values = dialog.GetValues()
Laurent@814:             rect = variable.GetRedrawRect(1, 1)
Laurent@814:             variable.SetName(new_values["name"])
Laurent@814:             variable.SetType(new_values["type"], new_values["value_type"])
Laurent@814:             variable.SetSize(*self.GetScaledSize(new_values["width"], new_values["height"]))
Laurent@814:             variable.SetExecutionOrder(new_values["executionOrder"])
Laurent@814:             rect = rect.Union(variable.GetRedrawRect())
Laurent@814:             if old_values["type"] != new_values["type"]:
Laurent@814:                 id = variable.GetId()
Laurent@814:                 self.Controler.RemoveEditedElementInstance(self.TagName, id)
Laurent@814:                 self.Controler.AddEditedElementVariable(self.TagName, id, new_values["type"])
Laurent@814:             self.RefreshVariableModel(variable)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             if old_values["executionOrder"] != new_values["executionOrder"]:
Laurent@814:                 self.RefreshView(selection=({variable.GetId(): True}, {}))
Laurent@814:             else:
Laurent@814:                 self.RefreshVisibleElements()
Laurent@814:                 self.RefreshScrollBars()
Laurent@814:                 variable.Refresh(rect)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def EditConnectionContent(self, connection):
Laurent@856:         dialog = ConnectionDialog(self.ParentWindow, self.Controler, True)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
Laurent@814:         dialog.SetPouElementNames(self.Controler.GetEditedElementVariables(self.TagName, self.Debug))
Laurent@814:         dialog.SetMinConnectionSize(connection.GetSize())
Laurent@814:         values = {"name" : connection.GetName(), "type" : connection.GetType()}
Laurent@814:         dialog.SetValues(values)
Laurent@856:         result = dialog.ShowModal()
Laurent@856:         dialog.Destroy()
Laurent@856:         if result in [wx.ID_OK, wx.ID_YESTOALL]:
Laurent@814:             old_type = connection.GetType()
Laurent@814:             old_name = connection.GetName()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = connection.GetRedrawRect(1, 1)
Laurent@814:             connection.SetName(values["name"])
Laurent@814:             connection.SetType(values["type"])
Laurent@814:             connection.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             rect = rect.Union(connection.GetRedrawRect())
Laurent@814:             if old_type != values["type"]:
Laurent@814:                 id = connection.GetId()
Laurent@814:                 self.Controler.RemoveEditedElementInstance(self.TagName, id)
Laurent@814:                 self.Controler.AddEditedElementConnection(self.TagName, id, values["type"])
Laurent@814:             self.RefreshConnectionModel(connection)
Laurent@856:             if old_name != values["name"] and result == wx.ID_YESTOALL:
Laurent@814:                 self.Controler.UpdateEditedElementUsedVariable(self.TagName, old_name, values["name"])
Laurent@852:                 self.RefreshBuffer()
Laurent@814:                 self.RefreshView(selection=({connection.GetId(): True}, {}))
Laurent@814:             else:
Laurent@856:                 self.RefreshBuffer()
Laurent@814:                 self.RefreshScrollBars()
Laurent@814:                 self.RefreshVisibleElements()
Laurent@814:                 connection.Refresh(rect)
Laurent@856:         
Laurent@814:     def EditContactContent(self, contact):
Laurent@814:         dialog = LDElementDialog(self.ParentWindow, self.Controler, "contact")
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         varlist = []
Laurent@814:         vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
Laurent@814:         if vars:
Laurent@814:             for var in vars:
Laurent@814:                 if var["Type"] == "BOOL":
Laurent@814:                     varlist.append(var["Name"])
Laurent@814:         dialog.SetVariables(varlist)
Laurent@814:         values = {"name" : contact.GetName(), "type" : contact.GetType()}
Laurent@814:         dialog.SetValues(values)
Laurent@814:         dialog.SetElementSize(contact.GetSize())
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = contact.GetRedrawRect(1, 1)
Laurent@814:             contact.SetName(values["name"])
Laurent@814:             contact.SetType(values["type"])
Laurent@814:             contact.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             rect = rect.Union(contact.GetRedrawRect())
Laurent@814:             self.RefreshContactModel(contact)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             contact.Refresh(rect)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def EditCoilContent(self, coil):
Laurent@814:         dialog = LDElementDialog(self.ParentWindow, self.Controler, "coil")
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         varlist = []
Laurent@814:         vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
Laurent@814:         if vars:
Laurent@814:             for var in vars:
Laurent@814:                 if var["Class"] != "Input" and var["Type"] == "BOOL":
Laurent@814:                     varlist.append(var["Name"])
Laurent@814:         returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
Laurent@814:         if returntype == "BOOL":
Laurent@814:             varlist.append(self.Controler.GetEditedElementName(self.TagName))
Laurent@814:         dialog.SetVariables(varlist)
Laurent@814:         values = {"name" : coil.GetName(), "type" : coil.GetType()}
Laurent@814:         dialog.SetValues(values)
Laurent@814:         dialog.SetElementSize(coil.GetSize())
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = coil.GetRedrawRect(1, 1)
Laurent@814:             coil.SetName(values["name"])
Laurent@814:             coil.SetType(values["type"])
Laurent@814:             coil.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             rect = rect.Union(coil.GetRedrawRect())
Laurent@814:             self.RefreshCoilModel(coil)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             coil.Refresh(rect)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def EditPowerRailContent(self, powerrail):
Laurent@814:         connectors = powerrail.GetConnectors()
Laurent@814:         type = powerrail.GetType()
Laurent@814:         if type == LEFTRAIL:
Laurent@814:             pin_number = len(connectors["outputs"])
Laurent@814:         else:
Laurent@814:             pin_number = len(connectors["inputs"])
Laurent@814:         dialog = LDPowerRailDialog(self.ParentWindow, self.Controler, type, pin_number)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetMinSize(powerrail.GetSize())
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             old_type = powerrail.GetType()
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = powerrail.GetRedrawRect(1, 1)
Laurent@814:             powerrail.SetType(values["type"], values["number"])
Laurent@814:             powerrail.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             rect = rect.Union(powerrail.GetRedrawRect())
Laurent@814:             if old_type != values["type"]:
Laurent@814:                 id = powerrail.GetId()
Laurent@814:                 self.Controler.RemoveEditedElementInstance(self.TagName, id)
Laurent@814:                 self.Controler.AddEditedElementPowerRail(self.TagName, id, values["type"])
Laurent@814:             self.RefreshPowerRailModel(powerrail)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             powerrail.Refresh(rect)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def EditStepContent(self, step):
Laurent@814:         dialog = SFCStepDialog(self.ParentWindow, self.Controler, step.GetInitial())
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetPouNames(self.Controler.GetProjectPouNames(self.Debug))
Laurent@814:         dialog.SetVariables(self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug))
Laurent@814:         dialog.SetStepNames([block.GetName() for block in self.Blocks.itervalues() if isinstance(block, SFC_Step) and block.GetName() != step.GetName()])
Laurent@814:         dialog.SetMinStepSize(step.GetSize())
Laurent@814:         values = {"name" : step.GetName()}
Laurent@814:         connectors = step.GetConnectors()
Laurent@814:         values["input"] = len(connectors["inputs"]) > 0
Laurent@814:         values["output"] = len(connectors["outputs"]) > 0
Laurent@814:         values["action"] = step.GetActionConnector() != None
Laurent@814:         dialog.SetValues(values)
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = step.GetRedrawRect(1, 1)
Laurent@814:             step.SetName(values["name"])
Laurent@814:             if values["input"]:
Laurent@814:                 step.AddInput()
Laurent@814:             else:
Laurent@814:                 step.RemoveInput()
Laurent@814:             if values["output"]:
Laurent@814:                 step.AddOutput()
Laurent@814:             else:
Laurent@814:                 step.RemoveOutput()
Laurent@814:             if values["action"]:
Laurent@814:                 step.AddAction()    
Laurent@814:             else:
Laurent@814:                 step.RemoveAction()
Laurent@814:             step.UpdateSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             rect = rect.Union(step.GetRedrawRect())
Laurent@814:             self.RefreshStepModel(step)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             step.Refresh(rect)
Laurent@814:         
Laurent@814:     def EditTransitionContent(self, transition):
Laurent@814:         dialog = SFCTransitionDialog(self.ParentWindow, self.Controler, self.GetDrawingMode() == FREEDRAWING_MODE)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@814:         dialog.SetTransitions(self.Controler.GetEditedElementTransitions(self.TagName, self.Debug))
Laurent@814:         dialog.SetValues({"type":transition.GetType(),"value":transition.GetCondition(), "priority":transition.GetPriority()})
Laurent@814:         dialog.SetElementSize(transition.GetSize())
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = transition.GetRedrawRect(1, 1)
Laurent@814:             transition.SetType(values["type"],values["value"])
Laurent@814:             transition.SetPriority(values["priority"])
Laurent@814:             rect = rect.Union(transition.GetRedrawRect())
Laurent@814:             self.RefreshTransitionModel(transition)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             transition.Refresh(rect)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def EditJumpContent(self, jump):
Laurent@814:         choices = []
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             if isinstance(block, SFC_Step):
Laurent@814:                 choices.append(block.GetName())
Laurent@814:         dialog = wx.SingleChoiceDialog(self.ParentWindow, 
Laurent@814:               _("Edit jump target"), _("Please choose a target"), 
Laurent@814:               choices, wx.DEFAULT_DIALOG_STYLE|wx.OK|wx.CANCEL)
Laurent@814:         dialog.SetSelection(choices.index(jump.GetTarget()))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             value = dialog.GetStringSelection()
Laurent@814:             rect = jump.GetRedrawRect(1, 1)
Laurent@814:             jump.SetTarget(value)
Laurent@814:             rect = rect.Union(jump.GetRedrawRect())
Laurent@814:             self.RefreshJumpModel(jump)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             jump.Refresh(rect)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def EditActionBlockContent(self, actionblock):
Laurent@814:         dialog = ActionBlockDialog(self.ParentWindow)
Laurent@814:         dialog.SetQualifierList(self.Controler.GetQualifierTypes())
Laurent@814:         dialog.SetActionList(self.Controler.GetEditedElementActions(self.TagName, self.Debug))
Laurent@814:         dialog.SetVariableList(self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug))
Laurent@814:         dialog.SetValues(actionblock.GetActions())
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             actions = dialog.GetValues()
Laurent@814:             rect = actionblock.GetRedrawRect(1, 1)
Laurent@814:             actionblock.SetActions(actions)
Laurent@814:             actionblock.SetSize(*self.GetScaledSize(*actionblock.GetSize()))
Laurent@814:             rect = rect.Union(actionblock.GetRedrawRect())
Laurent@814:             self.RefreshActionBlockModel(actionblock)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             actionblock.Refresh(rect)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814:     def EditCommentContent(self, comment):
Laurent@814:         if wx.VERSION >= (2, 5, 0):
Laurent@814:             dialog = wx.TextEntryDialog(self.ParentWindow, _("Edit comment"), _("Please enter comment text"), comment.GetContent(), wx.OK|wx.CANCEL|wx.TE_MULTILINE)
Laurent@814:         else:
Laurent@814:             dialog = wx.TextEntryDialog(self.ParentWindow, _("Edit comment"), _("Please enter comment text"), comment.GetContent(), wx.OK|wx.CANCEL)
Laurent@814:         dialog.SetClientSize(wx.Size(400, 200))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             value = dialog.GetValue()
Laurent@814:             rect = comment.GetRedrawRect(1, 1)
Laurent@814:             comment.SetContent(value)
Laurent@814:             comment.SetSize(*self.GetScaledSize(*comment.GetSize()))
Laurent@814:             rect = rect.Union(comment.GetRedrawRect())
Laurent@814:             self.RefreshCommentModel(comment)
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVisibleElements()
Laurent@814:             comment.Refresh(rect)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                          Model update functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def RefreshBlockModel(self, block):
Laurent@814:         blockid = block.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["type"] = block.GetType()
Laurent@814:         infos["name"] = block.GetName()
Laurent@814:         if self.CurrentLanguage == "FBD":
Laurent@814:             infos["executionOrder"] = block.GetExecutionOrder()
Laurent@814:         infos["x"], infos["y"] = block.GetPosition()
Laurent@814:         infos["width"], infos["height"] = block.GetSize()
Laurent@814:         infos["connectors"] = block.GetConnectors()
Laurent@814:         self.Controler.SetEditedElementBlockInfos(self.TagName, blockid, infos)
Laurent@814:     
Laurent@857:     def ChangeVariableType(self, variable, new_type):
Laurent@857:         old_type = variable.GetType()
Laurent@857:         rect = variable.GetRedrawRect(1, 1)
Laurent@857:         if old_type != new_type:
Laurent@857:             variable.SetType(new_type, variable.GetValueType())
Laurent@857:             id = variable.GetId()
Laurent@857:             self.Controler.RemoveEditedElementInstance(self.TagName, id)
Laurent@857:             self.Controler.AddEditedElementVariable(self.TagName, id, new_type)
Laurent@857:             self.RefreshVariableModel(variable)
Laurent@857:             self.RefreshBuffer()
Laurent@857:             self.RefreshVisibleElements()
Laurent@857:             self.RefreshScrollBars()
Laurent@857:             variable.Refresh(rect.Union(variable.GetRedrawRect()))
Laurent@857:     
Laurent@814:     def RefreshVariableModel(self, variable):
Laurent@814:         variableid = variable.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["name"] = variable.GetName()
Laurent@814:         if self.CurrentLanguage == "FBD":
Laurent@814:             infos["executionOrder"] = variable.GetExecutionOrder()
Laurent@814:         infos["x"], infos["y"] = variable.GetPosition()
Laurent@814:         infos["width"], infos["height"] = variable.GetSize()
Laurent@814:         infos["connectors"] = variable.GetConnectors()
Laurent@814:         self.Controler.SetEditedElementVariableInfos(self.TagName, variableid, infos)
Laurent@814: 
Laurent@857:     def ChangeConnectionType(self, connection, new_type):
Laurent@857:         old_type = connection.GetType()
Laurent@857:         rect = connection.GetRedrawRect(1, 1)
Laurent@857:         if old_type != new_type:
Laurent@857:             connection.SetType(new_type)
Laurent@857:             id = connection.GetId()
Laurent@857:             self.Controler.RemoveEditedElementInstance(self.TagName, id)
Laurent@857:             self.Controler.AddEditedElementConnection(self.TagName, id, new_type)
Laurent@857:             self.RefreshConnectionModel(connection)
Laurent@857:             self.RefreshBuffer()
Laurent@857:             self.RefreshScrollBars()
Laurent@857:             self.RefreshVisibleElements()
Laurent@857:             connection.Refresh(rect.Union(connection.GetRedrawRect()))
Laurent@857: 
Laurent@814:     def RefreshConnectionModel(self, connection):
Laurent@814:         connectionid = connection.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["name"] = connection.GetName()
Laurent@814:         infos["x"], infos["y"] = connection.GetPosition()
Laurent@814:         infos["width"], infos["height"] = connection.GetSize()
Laurent@814:         infos["connector"] = connection.GetConnector()
Laurent@814:         self.Controler.SetEditedElementConnectionInfos(self.TagName, connectionid, infos)
Laurent@814: 
Laurent@814:     def RefreshCommentModel(self, comment):
Laurent@814:         commentid = comment.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["content"] = comment.GetContent()
Laurent@814:         infos["x"], infos["y"] = comment.GetPosition()
Laurent@814:         infos["width"], infos["height"] = comment.GetSize()
Laurent@814:         self.Controler.SetEditedElementCommentInfos(self.TagName, commentid, infos)
Laurent@814: 
Laurent@814:     def RefreshPowerRailModel(self, powerrail):
Laurent@814:         powerrailid = powerrail.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["x"], infos["y"] = powerrail.GetPosition()
Laurent@814:         infos["width"], infos["height"] = powerrail.GetSize()
Laurent@814:         infos["connectors"] = powerrail.GetConnectors()
Laurent@814:         self.Controler.SetEditedElementPowerRailInfos(self.TagName, powerrailid, infos)
Laurent@814: 
Laurent@814:     def RefreshContactModel(self, contact):
Laurent@814:         contactid = contact.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["name"] = contact.GetName()
Laurent@814:         infos["type"] = contact.GetType()
Laurent@814:         infos["x"], infos["y"] = contact.GetPosition()
Laurent@814:         infos["width"], infos["height"] = contact.GetSize()
Laurent@814:         infos["connectors"] = contact.GetConnectors()
Laurent@814:         self.Controler.SetEditedElementContactInfos(self.TagName, contactid, infos)
Laurent@814: 
Laurent@814:     def RefreshCoilModel(self, coil):
Laurent@814:         coilid = coil.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["name"] = coil.GetName()
Laurent@814:         infos["type"] = coil.GetType()
Laurent@814:         infos["x"], infos["y"] = coil.GetPosition()
Laurent@814:         infos["width"], infos["height"] = coil.GetSize()
Laurent@814:         infos["connectors"] = coil.GetConnectors()
Laurent@814:         self.Controler.SetEditedElementCoilInfos(self.TagName, coilid, infos)
Laurent@814: 
Laurent@814:     def RefreshStepModel(self, step):
Laurent@814:         stepid = step.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["name"] = step.GetName()
Laurent@814:         infos["initial"] = step.GetInitial()
Laurent@814:         infos["x"], infos["y"] = step.GetPosition()
Laurent@814:         infos["width"], infos["height"] = step.GetSize()
Laurent@814:         infos["connectors"] = step.GetConnectors()
Laurent@814:         infos["action"] = step.GetActionConnector()
Laurent@814:         self.Controler.SetEditedElementStepInfos(self.TagName, stepid, infos)
Laurent@814: 
Laurent@814:     def RefreshTransitionModel(self, transition):
Laurent@814:         transitionid = transition.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["type"] = transition.GetType()
Laurent@814:         infos["priority"] = transition.GetPriority()
Laurent@814:         infos["condition"] = transition.GetCondition()
Laurent@814:         infos["x"], infos["y"] = transition.GetPosition()
Laurent@814:         infos["width"], infos["height"] = transition.GetSize()
Laurent@814:         infos["connectors"] = transition.GetConnectors()
Laurent@814:         infos["connection"] = transition.GetConditionConnector()
Laurent@814:         self.Controler.SetEditedElementTransitionInfos(self.TagName, transitionid, infos)
Laurent@814: 
Laurent@814:     def RefreshDivergenceModel(self, divergence):
Laurent@814:         divergenceid = divergence.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["x"], infos["y"] = divergence.GetPosition()
Laurent@814:         infos["width"], infos["height"] = divergence.GetSize()
Laurent@814:         infos["connectors"] = divergence.GetConnectors()
Laurent@814:         self.Controler.SetEditedElementDivergenceInfos(self.TagName, divergenceid, infos)
Laurent@814: 
Laurent@814:     def RefreshJumpModel(self, jump):
Laurent@814:         jumpid = jump.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["target"] = jump.GetTarget()
Laurent@814:         infos["x"], infos["y"] = jump.GetPosition()
Laurent@814:         infos["width"], infos["height"] = jump.GetSize()
Laurent@814:         infos["connector"] = jump.GetConnector()
Laurent@814:         self.Controler.SetEditedElementJumpInfos(self.TagName, jumpid, infos)
Laurent@814: 
Laurent@814:     def RefreshActionBlockModel(self, actionblock):
Laurent@814:         actionblockid = actionblock.GetId()
Laurent@814:         infos = {}
Laurent@814:         infos["actions"] = actionblock.GetActions()
Laurent@814:         infos["x"], infos["y"] = actionblock.GetPosition()
Laurent@814:         infos["width"], infos["height"] = actionblock.GetSize()
Laurent@814:         infos["connector"] = actionblock.GetConnector()
Laurent@814:         self.Controler.SetEditedElementActionBlockInfos(self.TagName, actionblockid, infos)
Laurent@814: 
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                          Model delete functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814: 
Laurent@814:     def DeleteBlock(self, block):
Laurent@814:         elements = []
Laurent@814:         for output in block.GetConnectors()["outputs"]:
Laurent@814:             for element in output.GetConnectedBlocks():
Laurent@814:                 if element not in elements:
Laurent@814:                     elements.append(element)
Laurent@814:         block.Clean()
Laurent@814:         self.RemoveBlock(block)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, block.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Laurent@814:         wx.CallAfter(self.RefreshVariablePanel)
Laurent@814:         wx.CallAfter(self.ParentWindow.RefreshPouInstanceVariablesPanel)
Laurent@814:         
Laurent@814:     def DeleteVariable(self, variable):
Laurent@814:         connectors = variable.GetConnectors()
Laurent@814:         if len(connectors["outputs"]) > 0:
Laurent@814:             elements = connectors["outputs"][0].GetConnectedBlocks()
Laurent@814:         else:
Laurent@814:             elements = []
Laurent@814:         variable.Clean()
Laurent@814:         self.RemoveBlock(variable)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, variable.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Laurent@814: 
Laurent@814:     def DeleteConnection(self, connection):
Laurent@814:         if connection.GetType() == CONTINUATION:
Laurent@814:             elements = connection.GetConnector().GetConnectedBlocks()
Laurent@814:         else:
Laurent@814:             elements = []
Laurent@814:         connection.Clean()
Laurent@814:         self.RemoveBlock(connection)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, connection.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Laurent@814: 
Laurent@814:     def DeleteComment(self, comment):
Laurent@814:         self.RemoveComment(comment)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, comment.GetId())
Laurent@814: 
Laurent@814:     def DeleteWire(self, wire):
Laurent@814:         if wire in self.Wires:
Laurent@814:             connected = wire.GetConnected()
Laurent@814:             wire.Clean()
Laurent@814:             self.RemoveWire(wire)
Laurent@814:             for connector in connected:
Laurent@814:                 connector.RefreshParentBlock()
Laurent@814: 
Laurent@814:     def DeleteContact(self, contact):
Laurent@814:         connectors = contact.GetConnectors()
Laurent@814:         elements = connectors["outputs"][0].GetConnectedBlocks()
Laurent@814:         contact.Clean()
Laurent@814:         self.RemoveBlock(contact)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, contact.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Laurent@814: 
Laurent@814:     def DeleteCoil(self, coil):
Laurent@814:         connectors = coil.GetConnectors()
Laurent@814:         elements = connectors["outputs"][0].GetConnectedBlocks()
Laurent@814:         coil.Clean()
Laurent@814:         self.RemoveBlock(coil)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, coil.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Laurent@814: 
Laurent@814:     def DeletePowerRail(self, powerrail):
Laurent@814:         elements = []
Laurent@814:         if powerrail.GetType() == LEFTRAIL:
Laurent@814:             connectors = powerrail.GetConnectors()
Laurent@814:             for connector in connectors["outputs"]:
Laurent@814:                 for element in connector.GetConnectedBlocks():
Laurent@814:                     if element not in elements:
Laurent@814:                         elements.append(element)
Laurent@814:         powerrail.Clean()
Laurent@814:         self.RemoveBlock(powerrail)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, powerrail.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Laurent@814: 
Laurent@814:     def DeleteStep(self, step):
Laurent@814:         elements = []
Laurent@814:         connectors = step.GetConnectors()
Laurent@814:         action_connector = step.GetActionConnector()
Laurent@814:         if len(connectors["outputs"]) > 0:
Laurent@814:             for element in connectors["outputs"][0].GetConnectedBlocks():
Laurent@814:                 if element not in elements:
Laurent@814:                     elements.append(element)
Laurent@814:         if action_connector is not None:
Laurent@814:             for element in action_connector.GetConnectedBlocks():
Laurent@814:                 if element not in elements:
Laurent@814:                     elements.append(element)
Laurent@814:         step.Clean()
Laurent@814:         self.RemoveBlock(step)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, step.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Laurent@814:             
Laurent@814:     def DeleteTransition(self, transition):
Laurent@814:         elements = []
Laurent@814:         connectors = transition.GetConnectors()
Laurent@814:         for element in connectors["outputs"][0].GetConnectedBlocks():
Laurent@814:             if element not in elements:
Laurent@814:                 elements.append(element)
Laurent@814:         transition.Clean()
Laurent@814:         self.RemoveBlock(transition)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, transition.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Laurent@814: 
Laurent@814:     def DeleteDivergence(self, divergence):
Laurent@814:         elements = []
Laurent@814:         connectors = divergence.GetConnectors()
Laurent@814:         for output in connectors["outputs"]:
Laurent@814:             for element in output.GetConnectedBlocks():
Laurent@814:                 if element not in elements:
Laurent@814:                     elements.append(element)
Laurent@814:         divergence.Clean()
Laurent@814:         self.RemoveBlock(divergence)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, divergence.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Laurent@814:     
Laurent@814:     def DeleteJump(self, jump):
Laurent@814:         jump.Clean()
Laurent@814:         self.RemoveBlock(jump)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, jump.GetId())
Laurent@814:     
Laurent@814:     def DeleteActionBlock(self, actionblock):
Laurent@814:         actionblock.Clean()
Laurent@814:         self.RemoveBlock(actionblock)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, actionblock.GetId())
Laurent@814: 
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                            Editing functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814:     
Laurent@814:     def Cut(self):
Laurent@814:         if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)):
Laurent@814:             blocks, wires = self.SelectedElement.GetDefinition()
Laurent@814:             text = self.Controler.GetEditedElementInstancesCopy(self.TagName, blocks, wires, self.Debug)
Laurent@814:             self.ParentWindow.SetCopyBuffer(text)
Laurent@814:             rect = self.SelectedElement.GetRedrawRect(1, 1)
Laurent@814:             self.SelectedElement.Delete()
Laurent@814:             self.SelectedElement = None
Laurent@814:             self.RefreshBuffer()
Laurent@814:             self.RefreshScrollBars()
Laurent@814:             self.RefreshVariablePanel()
Laurent@814:             self.ParentWindow.RefreshPouInstanceVariablesPanel()
Laurent@814:             self.RefreshRect(self.GetScrolledRect(rect), False)
Laurent@814:         
Laurent@814:     def Copy(self):
Laurent@814:         if not self.Debug and (self.IsBlock(self.SelectedElement) or self.IsComment(self.SelectedElement) or isinstance(self.SelectedElement, Graphic_Group)):
Laurent@814:             blocks, wires = self.SelectedElement.GetDefinition()
Laurent@814:             text = self.Controler.GetEditedElementInstancesCopy(self.TagName, blocks, wires, self.Debug)
Laurent@814:             self.ParentWindow.SetCopyBuffer(text)
Laurent@814:             
Laurent@814:     def Paste(self, bbx=None):
Laurent@814:         if not self.Debug:
Laurent@814:             element = self.ParentWindow.GetCopyBuffer()
Laurent@814:             if bbx is None:
Laurent@814:                 mouse_pos = self.Editor.ScreenToClient(wx.GetMousePosition())
Laurent@814:                 middle = wx.Rect(0, 0, *self.Editor.GetClientSize()).InsideXY(mouse_pos.x, mouse_pos.y)
Laurent@814:                 if middle:
Laurent@814:                     x, y = self.CalcUnscrolledPosition(mouse_pos.x, mouse_pos.y)
Laurent@814:                 else:
Laurent@814:                     x, y = self.CalcUnscrolledPosition(0, 0)
Laurent@814:                 new_pos = [int(x / self.ViewScale[0]), int(y / self.ViewScale[1])]
Laurent@814:             else:
Laurent@814:                 middle = True
Laurent@814:                 new_pos = [bbx.x, bbx.y]
Laurent@814:             result = self.Controler.PasteEditedElementInstances(self.TagName, element, new_pos, middle, self.Debug)
Laurent@814:             if not isinstance(result, (StringType, UnicodeType)):
Laurent@814:                 self.RefreshBuffer()
Laurent@814:                 self.RefreshView(selection=result)
Laurent@814:                 self.RefreshVariablePanel()
Laurent@814:                 self.ParentWindow.RefreshPouInstanceVariablesPanel()
Laurent@814:             else:
Laurent@814:                 message = wx.MessageDialog(self.Editor, result, "Error", wx.OK|wx.ICON_ERROR)
Laurent@814:                 message.ShowModal()
Laurent@814:                 message.Destroy()
Laurent@814: 
Laurent@814:     def CanAddElement(self, block):
Laurent@814:         if isinstance(block, Graphic_Group):
Laurent@814:             return block.CanAddBlocks(self)
Laurent@814:         elif self.CurrentLanguage == "SFC":
Laurent@814:             return True
Laurent@814:         elif self.CurrentLanguage == "LD" and not isinstance(block, (SFC_Step, SFC_Transition, SFC_Divergence, SFC_Jump, SFC_ActionBlock)):
Laurent@814:             return True
Laurent@814:         elif self.CurrentLanguage == "FBD" and isinstance(block, (FBD_Block, FBD_Variable, FBD_Connector, Comment)):
Laurent@814:             return True
Laurent@814:         return False
Laurent@814: 
Laurent@814:     def GenerateNewName(self, element=None, blocktype=None, exclude={}):
Laurent@814:         if element is not None and isinstance(element, SFC_Step):
Laurent@814:             format = "Step%d"
Laurent@814:         else:
Laurent@814:             if element is not None:
Laurent@814:                 blocktype = element.GetType()
Laurent@814:             if blocktype is None:
Laurent@814:                 blocktype = "Block"
Laurent@814:             format = "%s%%d" % blocktype
Laurent@814:         return self.Controler.GenerateNewName(self.TagName, None, format, exclude, self.Debug)
Laurent@814: 
Laurent@814:     def IsNamedElement(self, element):
Laurent@814:         return isinstance(element, FBD_Block) and element.GetName() != "" or isinstance(element, SFC_Step)
Laurent@814: 
Laurent@814:     def CopyBlock(self, element, pos):
Laurent@814:         if isinstance(element, Graphic_Group):
Laurent@814:             block = element.Clone(self, pos=pos)
Laurent@814:         else:
Laurent@891:             new_id = self.GetNewId()
Laurent@814:             if self.IsNamedElement(element):
Laurent@814:                 name = self.GenerateNewName(element)
Laurent@891:                 block = element.Clone(self, new_id, name, pos)
Laurent@814:             else:
Laurent@814:                 name = None
Laurent@891:                 block = element.Clone(self, new_id, pos=pos)
Laurent@814:             self.AddBlockInModel(block)
Laurent@814:         return block
Laurent@814:     
Laurent@814:     def AddBlockInModel(self, block):
Laurent@814:         if isinstance(block, Comment):
Laurent@814:             self.AddComment(block)
Laurent@814:             self.Controler.AddEditedElementComment(self.TagName, block.GetId())
Laurent@814:             self.RefreshCommentModel(block)
Laurent@814:         else:
Laurent@814:             self.AddBlock(block)
Laurent@814:             if isinstance(block, FBD_Block):
Laurent@814:                 self.Controler.AddEditedElementBlock(self.TagName, block.GetId(), block.GetType(), block.GetName())
Laurent@814:                 self.RefreshBlockModel(block)
Laurent@814:             elif isinstance(block, FBD_Variable):
Laurent@814:                 self.Controler.AddEditedElementVariable(self.TagName, block.GetId(), block.GetType())
Laurent@814:                 self.RefreshVariableModel(block)
Laurent@814:             elif isinstance(block, FBD_Connector):
Laurent@814:                 self.Controler.AddEditedElementConnection(self.TagName, block.GetId(), block.GetType())
Laurent@814:                 self.RefreshConnectionModel(block)
Laurent@814:             elif isinstance(block, LD_Contact):
Laurent@814:                 self.Controler.AddEditedElementContact(self.TagName, block.GetId())
Laurent@814:                 self.RefreshContactModel(block)
Laurent@814:             elif isinstance(block, LD_Coil):
Laurent@814:                 self.Controler.AddEditedElementCoil(self.TagName, block.GetId())
Laurent@814:                 self.RefreshCoilModel(block)
Laurent@814:             elif isinstance(block, LD_PowerRail):
Laurent@814:                 self.Controler.AddEditedElementPowerRail(self.TagName, block.GetId(), block.GetType())
Laurent@814:                 self.RefreshPowerRailModel(block)
Laurent@814:             elif isinstance(block, SFC_Step):
Laurent@814:                 self.Controler.AddEditedElementStep(self.TagName, block.GetId())
Laurent@814:                 self.RefreshStepModel(block)    
Laurent@814:             elif isinstance(block, SFC_Transition):
Laurent@814:                 self.Controler.AddEditedElementTransition(self.TagName, block.GetId())
Laurent@814:                 self.RefreshTransitionModel(block)       
Laurent@814:             elif isinstance(block, SFC_Divergence):
Laurent@814:                 self.Controler.AddEditedElementDivergence(self.TagName, block.GetId(), block.GetType())
Laurent@814:                 self.RefreshDivergenceModel(block)
Laurent@814:             elif isinstance(block, SFC_Jump):
Laurent@814:                 self.Controler.AddEditedElementJump(self.TagName, block.GetId())
Laurent@814:                 self.RefreshJumpModel(block)       
Laurent@814:             elif isinstance(block, SFC_ActionBlock):
Laurent@814:                 self.Controler.AddEditedElementActionBlock(self.TagName, block.GetId())
Laurent@814:                 self.RefreshActionBlockModel(block)
Laurent@814: 
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                         Find and Replace functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def Find(self, direction, search_params):
Laurent@814:         if self.SearchParams != search_params:
Laurent@814:             self.ClearHighlights(SEARCH_RESULT_HIGHLIGHT)
Laurent@814:             
Laurent@814:             self.SearchParams = search_params
Laurent@814:             criteria = {
Laurent@814:                 "raw_pattern": search_params["find_pattern"], 
Laurent@814:                 "pattern": re.compile(search_params["find_pattern"]),
Laurent@814:                 "case_sensitive": search_params["case_sensitive"],
Laurent@814:                 "regular_expression": search_params["regular_expression"],
Laurent@814:                 "filter": "all"}
Laurent@814:             
Laurent@814:             self.SearchResults = []
Laurent@814:             blocks = []
Laurent@814:             for infos, start, end, text in self.Controler.SearchInPou(self.TagName, criteria, self.Debug):
Laurent@814:                 if infos[1] in ["var_local", "var_input", "var_output", "var_inout"]:
Laurent@814:                     self.SearchResults.append((infos[1:], start, end, SEARCH_RESULT_HIGHLIGHT))
Laurent@814:                 else:
Laurent@814:                     block = self.Blocks.get(infos[2])
Laurent@814:                     if block is not None:
Laurent@814:                         blocks.append((block, (infos[1:], start, end, SEARCH_RESULT_HIGHLIGHT)))
Laurent@814:             blocks.sort(sort_blocks)
Laurent@814:             self.SearchResults.extend([infos for block, infos in blocks])
Laurent@814:         
Laurent@814:         if len(self.SearchResults) > 0:
Laurent@814:             if self.CurrentFindHighlight is not None:
Laurent@814:                 old_idx = self.SearchResults.index(self.CurrentFindHighlight)
Laurent@814:                 if self.SearchParams["wrap"]:
Laurent@814:                     idx = (old_idx + direction) % len(self.SearchResults)
Laurent@814:                 else:
Laurent@814:                     idx = max(0, min(old_idx + direction, len(self.SearchResults) - 1))
Laurent@814:                 if idx != old_idx:
Laurent@814:                     self.RemoveHighlight(*self.CurrentFindHighlight)
Laurent@814:                     self.CurrentFindHighlight = self.SearchResults[idx]
Laurent@814:                     self.AddHighlight(*self.CurrentFindHighlight)
Laurent@814:             else:
Laurent@814:                 self.CurrentFindHighlight = self.SearchResults[0]
Laurent@814:                 self.AddHighlight(*self.CurrentFindHighlight)
Laurent@814:             
Laurent@814:         else:
Laurent@814:             if self.CurrentFindHighlight is not None:
Laurent@814:                 self.RemoveHighlight(*self.CurrentFindHighlight)
Laurent@814:             self.CurrentFindHighlight = None
Laurent@814:         
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                        Highlights showing functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def OnRefreshHighlightsTimer(self, event):
Laurent@814:         self.RefreshView()
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def ClearHighlights(self, highlight_type=None):
Laurent@814:         EditorPanel.ClearHighlights(self, highlight_type)
Laurent@814:         
Laurent@814:         if highlight_type is None:
Laurent@814:             self.Highlights = []
Laurent@814:         else:
Laurent@814:             self.Highlights = [(infos, start, end, highlight) for (infos, start, end, highlight) in self.Highlights if highlight != highlight_type]
Laurent@814:         self.RefreshView()
Laurent@814: 
Laurent@814:     def AddHighlight(self, infos, start, end, highlight_type):
Laurent@814:         EditorPanel.AddHighlight(self, infos, start, end, highlight_type)
Laurent@814:         
Laurent@814:         self.Highlights.append((infos, start, end, highlight_type))
Laurent@814:         if infos[0] not in ["var_local", "var_input", "var_output", "var_inout"]:
Laurent@814:             block = self.Blocks.get(infos[1])
Laurent@814:             if block is not None:
Laurent@814:                 self.EnsureVisible(block)
Laurent@814:             self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True)
Laurent@814:     
Laurent@814:     def RemoveHighlight(self, infos, start, end, highlight_type):
Laurent@814:         EditorPanel.RemoveHighlight(self, infos, start, end, highlight_type)
Laurent@814:         
Laurent@814:         if (infos, start, end, highlight_type) in self.Highlights:
Laurent@814:             self.Highlights.remove((infos, start, end, highlight_type))
Laurent@814:             self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True)
Laurent@814:     
Laurent@814:     def ShowHighlights(self):
Laurent@814:         for infos, start, end, highlight_type in self.Highlights:
Laurent@814:             if infos[0] in ["comment", "io_variable", "block", "connector", "coil", "contact", "step", "transition", "jump", "action_block"]:
Laurent@814:                 block = self.FindElementById(infos[1])
Laurent@814:                 if block is not None:
Laurent@814:                     block.AddHighlight(infos[2:], start, end, highlight_type)
Laurent@814:         
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: #                            Drawing functions
Laurent@814: #-------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def OnScrollWindow(self, event):
Laurent@814:         if self.Editor.HasCapture() and self.StartMousePos:
Laurent@814:             return
Laurent@814:         if wx.Platform == '__WXMSW__':
Laurent@814:             wx.CallAfter(self.RefreshVisibleElements)
Laurent@814:         elif event.GetOrientation() == wx.HORIZONTAL:
Laurent@814:             self.RefreshVisibleElements(xp = event.GetPosition())
Laurent@814:         else:
Laurent@814:             self.RefreshVisibleElements(yp = event.GetPosition())
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def OnScrollStop(self, event):
Laurent@814:         self.RefreshScrollBars()
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def OnMouseWheelWindow(self, event):
Laurent@814:         if self.StartMousePos is None or self.StartScreenPos is None:
Laurent@814:             rotation = event.GetWheelRotation() / event.GetWheelDelta()
Laurent@814:             if event.ShiftDown():
Laurent@814:                 x, y = self.GetViewStart()
Laurent@814:                 xp = max(0, min(x - rotation * 3, self.Editor.GetVirtualSize()[0] / self.Editor.GetScrollPixelsPerUnit()[0]))
Laurent@814:                 self.RefreshVisibleElements(xp = xp)
Laurent@814:                 self.Scroll(xp, y)
Laurent@814:             elif event.ControlDown():
Laurent@814:                 dc = self.GetLogicalDC()
Laurent@814:                 self.SetScale(self.CurrentScale + rotation, mouse_event = event)
Laurent@814:                 self.ParentWindow.RefreshDisplayMenu()
Laurent@814:             else:
Laurent@814:                 x, y = self.GetViewStart()
Laurent@814:                 yp = max(0, min(y - rotation * 3, self.Editor.GetVirtualSize()[1] / self.Editor.GetScrollPixelsPerUnit()[1]))
Laurent@814:                 self.RefreshVisibleElements(yp = yp)
Laurent@814:                 self.Scroll(x, yp)
Laurent@814:         
Laurent@814:     def OnMoveWindow(self, event):
Laurent@814:         self.RefreshScrollBars()
Laurent@814:         self.RefreshVisibleElements()
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814:     def DoDrawing(self, dc, printing = False):
Laurent@814:         if printing:
Laurent@814:             if getattr(dc, "printing", False):
Laurent@814:                 font = wx.Font(self.GetFont().GetPointSize(), wx.MODERN, wx.NORMAL, wx.NORMAL)
Laurent@814:                 dc.SetFont(font)
Laurent@814:             else:
Laurent@814:                 dc.SetFont(self.GetFont())
Laurent@814:         else:
Laurent@814:             dc.SetBackground(wx.Brush(self.Editor.GetBackgroundColour()))
Laurent@814:             dc.Clear()
Laurent@814:             dc.BeginDrawing()
Laurent@814:         if self.Scaling is not None and self.DrawGrid and not printing:
Laurent@814:             dc.SetPen(wx.TRANSPARENT_PEN)
Laurent@814:             dc.SetBrush(self.GridBrush)
Laurent@814:             xstart, ystart = self.GetViewStart()
Laurent@814:             window_size = self.Editor.GetClientSize()
Laurent@814:             width, height = self.Editor.GetVirtualSize()
Laurent@814:             width = int(max(width, xstart * SCROLLBAR_UNIT + window_size[0]) / self.ViewScale[0])
Laurent@814:             height = int(max(height, ystart * SCROLLBAR_UNIT + window_size[1]) / self.ViewScale[1])
Laurent@814:             dc.DrawRectangle(1, 1, width, height)
Laurent@814:         if self.PageSize is not None and not printing:
Laurent@814:             dc.SetPen(self.PagePen)
Laurent@814:             xstart, ystart = self.GetViewStart()
Laurent@814:             window_size = self.Editor.GetClientSize()
Laurent@814:             for x in xrange(self.PageSize[0] - (xstart * SCROLLBAR_UNIT) % self.PageSize[0], int(window_size[0] / self.ViewScale[0]), self.PageSize[0]):
Laurent@814:                 dc.DrawLine(xstart * SCROLLBAR_UNIT + x + 1, int(ystart * SCROLLBAR_UNIT / self.ViewScale[0]), 
Laurent@814:                             xstart * SCROLLBAR_UNIT + x + 1, int((ystart * SCROLLBAR_UNIT + window_size[1]) / self.ViewScale[0]))
Laurent@814:             for y in xrange(self.PageSize[1] - (ystart * SCROLLBAR_UNIT) % self.PageSize[1], int(window_size[1] / self.ViewScale[1]), self.PageSize[1]):
Laurent@814:                 dc.DrawLine(int(xstart * SCROLLBAR_UNIT / self.ViewScale[0]), ystart * SCROLLBAR_UNIT + y + 1, 
Laurent@814:                             int((xstart * SCROLLBAR_UNIT + window_size[0]) / self.ViewScale[1]), ystart * SCROLLBAR_UNIT + y + 1)
Laurent@814:         
Laurent@814:         # Draw all elements
Laurent@814:         for comment in self.Comments.itervalues():
Laurent@814:             if comment != self.SelectedElement and (comment.IsVisible() or printing):
Laurent@814:                 comment.Draw(dc)
Laurent@814:         for wire in self.Wires.iterkeys():
Laurent@814:             if wire != self.SelectedElement and (wire.IsVisible() or printing):
Laurent@814:                  if not self.Debug or wire.GetValue() != True:
Laurent@814:                     wire.Draw(dc)
Laurent@814:         if self.Debug:
Laurent@814:             for wire in self.Wires.iterkeys():
Laurent@814:                 if wire != self.SelectedElement and (wire.IsVisible() or printing) and wire.GetValue() == True:
Laurent@814:                     wire.Draw(dc)
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             if block != self.SelectedElement and (block.IsVisible() or printing):
Laurent@814:                 block.Draw(dc)
Laurent@814:         
Laurent@814:         if self.SelectedElement is not None and (self.SelectedElement.IsVisible() or printing):
Laurent@814:             self.SelectedElement.Draw(dc)
Laurent@814:         
Laurent@814:         if not printing:
Laurent@814:             if self.Debug:
laurent@828:                 is_action = self.TagName.split("::")[0] == "A"
laurent@828:                 text = _("Debug: %s") % self.InstancePath
laurent@828:                 if is_action and self.Value is not None:
laurent@828:                     text += " ("
laurent@828:                 dc.DrawText(text, 2, 2)
laurent@828:                 if is_action and self.Value is not None:
laurent@828:                     value_text = self.VALUE_TRANSLATION[self.Value]
laurent@828:                     tw, th = dc.GetTextExtent(text)
laurent@828:                     if self.Value:
laurent@828:                         dc.SetTextForeground(wx.GREEN)
laurent@828:                     dc.DrawText(value_text, tw + 2, 2)
laurent@828:                     if self.Value:
laurent@828:                         dc.SetTextForeground(wx.BLACK)
laurent@828:                     vw, vh = dc.GetTextExtent(value_text)
laurent@828:                     dc.DrawText(")", tw + vw + 4, 2)
Laurent@814:             if self.rubberBand.IsShown():
Laurent@814:                 self.rubberBand.Draw(dc)
Laurent@814:             dc.EndDrawing()
Laurent@814: 
Laurent@814:     def OnPaint(self, event):
Laurent@814:         dc = self.GetLogicalDC(True)
Laurent@814:         self.DoDrawing(dc)
Laurent@814:         wx.BufferedPaintDC(self.Editor, dc.GetAsBitmap())
Laurent@814:         if self.Debug:
Laurent@814:             DebugViewer.RefreshNewData(self)
Laurent@814:         event.Skip()
Laurent@814: 
Laurent@814: