Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
Laurent@814: #
andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
andrej@1571: # See COPYING file for copyrights details.
Laurent@814: #
andrej@1571: # This program is free software; you can redistribute it and/or
andrej@1571: # modify it under the terms of the GNU General Public License
andrej@1571: # as published by the Free Software Foundation; either version 2
andrej@1571: # of the License, or (at your option) any later version.
Laurent@814: #
andrej@1571: # This program is distributed in the hope that it will be useful,
andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1571: # GNU General Public License for more details.
Laurent@814: #
andrej@1571: # You should have received a copy of the GNU General Public License
andrej@1571: # along with this program; if not, write to the Free Software
andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Laurent@814: 
andrej@1853: 
andrej@1853: from __future__ import absolute_import
andrej@2437: from __future__ import division
Laurent@814: import math
Laurent@1224: from time import time as gettime
Laurent@814: from threading import Lock
andrej@2457: from future.builtins import round
Laurent@814: 
Laurent@814: import wx
andrej@2432: from six.moves import xrange
Laurent@814: 
Laurent@814: from plcopen.structures import *
Edouard@1948: from plcopen.types_enums import ComputePouName
Laurent@885: from PLCControler import ITEM_VAR_LOCAL, ITEM_POU, ITEM_PROGRAM, ITEM_FUNCTIONBLOCK
Edouard@1931: 
Edouard@1931: from graphics.GraphicCommons import *
Edouard@1931: from graphics.FBD_Objects import *
Edouard@1931: from graphics.LD_Objects import *
Edouard@1931: from graphics.SFC_Objects import *
Edouard@1931: from graphics.RubberBand import RubberBand
Edouard@1931: from graphics.DebugDataConsumer import DebugDataConsumer
Edouard@1931: 
Laurent@814: from dialogs import *
Laurent@1224: from editors.DebugViewer import DebugViewer, REFRESH_PERIOD
andrej@1853: from editors.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
surkovsv93@1584: SFC_Objects = (SFC_Step, SFC_ActionBlock, SFC_Transition, SFC_Divergence, SFC_Jump)
Laurent@814: 
andrej@1736: 
Laurent@814: def ResetCursors():
Laurent@814:     global CURSORS
andrej@1743:     if CURSORS is None:
Edouard@1417:         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: 
andrej@1736: 
Laurent@814: def AppendMenu(parent, help, id, kind, text):
andrej@2177:     parent.Append(help=help, id=id, kind=kind, text=text)
Laurent@814: 
andrej@1749: 
Laurent@814: if wx.Platform == '__WXMSW__':
andrej@1747:     faces = {
andrej@1747:         'times': 'Times New Roman',
andrej@1747:         'mono':  'Courier New',
andrej@1747:         'helv':  'Arial',
andrej@1747:         'other': 'Comic Sans MS',
andrej@1747:         'size':  10,
andrej@1747:     }
Laurent@814: else:
andrej@1747:     faces = {
andrej@1747:         'times': 'Times',
edouard@2704:         'mono':  'FreeMono',
andrej@1747:         'helv':  'Helvetica',
andrej@1747:         'other': 'new century schoolbook',
andrej@2345:         'size':  10,
andrej@1747:     }
Laurent@814: 
Laurent@1123: if wx.Platform == '__WXMSW__':
Laurent@1123:     MAX_ZOOMIN = 4
Laurent@1123: else:
Laurent@1123:     MAX_ZOOMIN = 7
Laurent@1123: ZOOM_FACTORS = [math.sqrt(2) ** x for x in xrange(-6, MAX_ZOOMIN)]
Laurent@814: 
andrej@1736: 
Laurent@814: def GetVariableCreationFunction(variable_type):
Laurent@814:     def variableCreationFunction(viewer, id, specific_values):
andrej@1767:         return FBD_Variable(viewer,
andrej@1767:                             variable_type,
andrej@1767:                             specific_values.name,
andrej@1767:                             specific_values.value_type,
andrej@1767:                             id,
andrej@1767:                             specific_values.execution_order)
Laurent@814:     return variableCreationFunction
Laurent@814: 
andrej@1736: 
Laurent@814: def GetConnectorCreationFunction(connector_type):
Laurent@814:     def connectorCreationFunction(viewer, id, specific_values):
andrej@1767:         return FBD_Connector(viewer,
andrej@1767:                              connector_type,
andrej@1767:                              specific_values.name,
andrej@1767:                              id)
Laurent@814:     return connectorCreationFunction
Laurent@814: 
andrej@1736: 
Laurent@814: def commentCreationFunction(viewer, id, specific_values):
Laurent@1338:     return Comment(viewer, specific_values.content, id)
Laurent@814: 
andrej@1736: 
Laurent@814: def GetPowerRailCreationFunction(powerrail_type):
Laurent@814:     def powerRailCreationFunction(viewer, id, specific_values):
andrej@1767:         return LD_PowerRail(viewer,
andrej@1767:                             powerrail_type,
andrej@1767:                             id,
andrej@1767:                             specific_values.connectors)
Laurent@814:     return powerRailCreationFunction
Laurent@814: 
andrej@1749: 
andrej@1762: def NEGATED_VALUE(x):
andrej@1762:     return x if x is not None else False
andrej@1762: 
andrej@1762: 
andrej@1762: def MODIFIER_VALUE(x):
andrej@1762:     return x if x is not None else 'none'
andrej@1762: 
Laurent@1338: 
Laurent@814: CONTACT_TYPES = {(True, "none"): CONTACT_REVERSE,
Laurent@814:                  (False, "rising"): CONTACT_RISING,
Laurent@814:                  (False, "falling"): CONTACT_FALLING}
Laurent@814: 
andrej@1736: 
Laurent@814: def contactCreationFunction(viewer, id, specific_values):
Edouard@1417:     contact_type = CONTACT_TYPES.get((NEGATED_VALUE(specific_values.negated),
Laurent@1338:                                       MODIFIER_VALUE(specific_values.edge)),
Laurent@814:                                      CONTACT_NORMAL)
Laurent@1338:     return LD_Contact(viewer, contact_type, specific_values.name, id)
Laurent@814: 
andrej@1749: 
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: 
andrej@1736: 
Laurent@814: def coilCreationFunction(viewer, id, specific_values):
Edouard@1417:     coil_type = COIL_TYPES.get((NEGATED_VALUE(specific_values.negated),
Laurent@1338:                                 MODIFIER_VALUE(specific_values.edge),
Laurent@1338:                                 MODIFIER_VALUE(specific_values.storage)),
Laurent@814:                                COIL_NORMAL)
Laurent@1338:     return LD_Coil(viewer, coil_type, specific_values.name, id)
Laurent@814: 
andrej@1736: 
Laurent@814: def stepCreationFunction(viewer, id, specific_values):
andrej@1767:     step = SFC_Step(viewer,
andrej@1767:                     specific_values.name,
andrej@1767:                     specific_values.initial,
andrej@1767:                     id)
Laurent@1338:     if specific_values.action is not None:
Laurent@814:         step.AddAction()
Laurent@814:         connector = step.GetActionConnector()
Laurent@1338:         connector.SetPosition(wx.Point(*specific_values.action.position))
Laurent@814:     return step
Laurent@814: 
andrej@1736: 
Laurent@814: def transitionCreationFunction(viewer, id, specific_values):
andrej@1767:     transition = SFC_Transition(viewer,
andrej@1767:                                 specific_values.condition_type,
andrej@1767:                                 specific_values.condition,
andrej@1767:                                 specific_values.priority,
andrej@1767:                                 id)
Laurent@814:     return transition
Laurent@814: 
andrej@1749: 
surkovsv93@1584: divergence_types = [SELECTION_DIVERGENCE,
surkovsv93@1584:                     SELECTION_CONVERGENCE, SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE]
surkovsv93@1584: 
andrej@1736: 
Laurent@814: def GetDivergenceCreationFunction(divergence_type):
Laurent@814:     def divergenceCreationFunction(viewer, id, specific_values):
Edouard@1417:         return SFC_Divergence(viewer, divergence_type,
andrej@1767:                               specific_values.connectors, id)
Laurent@814:     return divergenceCreationFunction
Laurent@814: 
andrej@1736: 
Laurent@814: def jumpCreationFunction(viewer, id, specific_values):
Laurent@1338:     return SFC_Jump(viewer, specific_values.target, id)
Laurent@814: 
andrej@1736: 
Laurent@814: def actionBlockCreationFunction(viewer, id, specific_values):
Laurent@1338:     return SFC_ActionBlock(viewer, specific_values.actions, id)
Laurent@814: 
andrej@1749: 
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,
Edouard@1417:     "step": stepCreationFunction,
Laurent@814:     "transition": transitionCreationFunction,
Edouard@1417:     "selectionDivergence": GetDivergenceCreationFunction(SELECTION_DIVERGENCE),
Edouard@1417:     "selectionConvergence": GetDivergenceCreationFunction(SELECTION_CONVERGENCE),
Edouard@1417:     "simultaneousDivergence": GetDivergenceCreationFunction(SIMULTANEOUS_DIVERGENCE),
Edouard@1417:     "simultaneousConvergence": GetDivergenceCreationFunction(SIMULTANEOUS_CONVERGENCE),
Laurent@814:     "jump": jumpCreationFunction,
Laurent@814:     "actionBlock": actionBlockCreationFunction,
Laurent@814: }
Laurent@814: 
andrej@1736: 
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: 
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: #                       Graphic elements Viewer base class
andrej@1782: # -------------------------------------------------------------------------------
Laurent@814: 
andrej@1749: 
Laurent@814: # ID Constants for alignment menu items
andrej@1773: [
andrej@1773:     ID_VIEWERALIGNMENTMENUITEMS0, ID_VIEWERALIGNMENTMENUITEMS1,
andrej@1773:     ID_VIEWERALIGNMENTMENUITEMS2, ID_VIEWERALIGNMENTMENUITEMS4,
andrej@1773:     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
andrej@1773: [
andrej@1773:     ID_VIEWERCONTEXTUALMENUITEMS0, ID_VIEWERCONTEXTUALMENUITEMS1,
andrej@1773:     ID_VIEWERCONTEXTUALMENUITEMS2, ID_VIEWERCONTEXTUALMENUITEMS3,
andrej@1773:     ID_VIEWERCONTEXTUALMENUITEMS5, ID_VIEWERCONTEXTUALMENUITEMS6,
andrej@1773:     ID_VIEWERCONTEXTUALMENUITEMS8, ID_VIEWERCONTEXTUALMENUITEMS9,
andrej@1773:     ID_VIEWERCONTEXTUALMENUITEMS11, ID_VIEWERCONTEXTUALMENUITEMS12,
andrej@1773:     ID_VIEWERCONTEXTUALMENUITEMS14, ID_VIEWERCONTEXTUALMENUITEMS16,
andrej@1773:     ID_VIEWERCONTEXTUALMENUITEMS17,
Laurent@814: ] = [wx.NewId() for _init_coll_ContextualMenu_Items in range(13)]
Laurent@814: 
Laurent@814: 
Laurent@814: class ViewerDropTarget(wx.TextDropTarget):
Edouard@1417: 
Laurent@814:     def __init__(self, parent):
Laurent@814:         wx.TextDropTarget.__init__(self)
Laurent@814:         self.ParentWindow = parent
Edouard@1417: 
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)
andrej@1780:         except Exception:
andrej@1734:             message = _("Invalid value \"%s\" for viewer block") % data
Laurent@814:             values = None
andrej@2450:         if not isinstance(values, tuple):
andrej@1734:             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:                 if pou_name == values[0]:
andrej@1734:                     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):
andrej@1744:                     message = _("\"{a1}\" is already used by \"{a2}\"!").format(a1=pou_name, a2=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)]:
andrej@1734:                         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)]:
andrej@1734:                         message = _("\"%s\" element for this pou already exists!") % blockname
Laurent@814:                     else:
Laurent@814:                         id = self.ParentWindow.GetNewId()
andrej@1744:                         block = FBD_Block(self.ParentWindow, values[0], blockname, id, inputs=blockinputs)
Laurent@814:                         width, height = block.GetMinSize()
Laurent@814:                         if scaling is not None:
andrej@2437:                             x = round(x / scaling[0]) * scaling[0]
andrej@2437:                             y = round(y / scaling[1]) * scaling[1]
andrej@2437:                             width = round(width / scaling[0] + 0.5) * scaling[0]
andrej@2437:                             height = round(height / 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@1233:                         self.ParentWindow.ParentWindow.RefreshPouInstanceVariablesPanel()
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("%"):
andrej@1768:                         dialog = wx.SingleChoiceDialog(
andrej@1768:                             self.ParentWindow.ParentWindow,
andrej@1768:                             _("Select a variable class:"),
andrej@1768:                             _("Variable class"),
andrej@1768:                             [_("Input"), _("Output"), _("Memory")],
andrej@1768:                             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]
Edouard@1417:                     dlg = wx.TextEntryDialog(
Edouard@1417:                         self.ParentWindow.ParentWindow,
Edouard@1417:                         _("Confirm or change variable name"),
andrej@1578:                         _('Variable Drop'), var_name)
Edouard@1417:                     dlg.SetValue(var_name)
Edouard@1417:                     var_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
Edouard@1417:                     dlg.Destroy()
Edouard@1417:                     if var_name is None:
Edouard@1417:                         return
Edouard@1417:                     elif var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
andrej@1734:                         message = _("\"%s\" pou already exists!") % var_name
Edouard@1417:                     elif not var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
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]
Edouard@1417:                         self.ParentWindow.Controler.AddEditedElementPouVar(tagname, var_type, var_name, location=location, description=values[4])
Edouard@1417:                         self.ParentWindow.RefreshVariablePanel()
Edouard@1417:                         self.ParentWindow.ParentWindow.RefreshPouInstanceVariablesPanel()
Edouard@1406:                         self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, var_name, var_type)
Edouard@1417:                     else:
andrej@1734:                         message = _("\"%s\" element for this pou already exists!") % var_name
Edouard@1406:             elif values[1] == "NamedConstant":
Edouard@1406:                 if pou_type == "program":
Edouard@1406:                     initval = values[0]
Edouard@1406:                     var_name = values[3]
Edouard@1417:                     dlg = wx.TextEntryDialog(
Edouard@1417:                         self.ParentWindow.ParentWindow,
Edouard@1417:                         _("Confirm or change variable name"),
andrej@1578:                         _('Variable Drop'), var_name)
Edouard@1417:                     dlg.SetValue(var_name)
Edouard@1417:                     var_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
Edouard@1417:                     dlg.Destroy()
Edouard@1417:                     if var_name is None:
Edouard@1417:                         return
Edouard@1417:                     elif var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
andrej@1734:                         message = _("\"%s\" pou already exists!") % var_name
Edouard@1417:                     elif not var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
Edouard@1406:                         var_class = INPUT
Edouard@1406:                         var_type = values[2]
Edouard@1417:                         self.ParentWindow.Controler.AddEditedElementPouVar(tagname, var_type, var_name, description=values[4], initval=initval)
Edouard@1417:                         self.ParentWindow.RefreshVariablePanel()
Edouard@1417:                         self.ParentWindow.ParentWindow.RefreshPouInstanceVariablesPanel()
Laurent@814:                         self.ParentWindow.AddVariableBlock(x, y, scaling, var_class, var_name, var_type)
Edouard@1417:                     else:
andrej@1734:                         message = _("\"%s\" element for this pou already exists!") % var_name
Laurent@814:             elif values[1] == "Global":
Laurent@814:                 var_name = values[0]
Edouard@1417:                 dlg = wx.TextEntryDialog(
Edouard@1417:                     self.ParentWindow.ParentWindow,
Edouard@1417:                     _("Confirm or change variable name"),
andrej@1578:                     _('Variable Drop'), var_name)
Edouard@1417:                 dlg.SetValue(var_name)
Edouard@1417:                 var_name = dlg.GetValue() if dlg.ShowModal() == wx.ID_OK else None
Edouard@1417:                 dlg.Destroy()
Edouard@1417:                 if var_name is None:
Edouard@1417:                     return
Edouard@1417:                 elif var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetProjectPouNames(self.ParentWindow.Debug)]:
andrej@1734:                     message = _("\"%s\" pou already exists!") % var_name
Edouard@1417:                 elif not var_name.upper() in [name.upper() for name in self.ParentWindow.Controler.GetEditedElementVariables(tagname, self.ParentWindow.Debug)]:
Edouard@1417:                     self.ParentWindow.Controler.AddEditedElementPouExternalVar(tagname, values[2], var_name)
Edouard@1417:                     self.ParentWindow.RefreshVariablePanel()
Edouard@1417:                     self.ParentWindow.ParentWindow.RefreshPouInstanceVariablesPanel()
Edouard@1417:                     self.ParentWindow.AddVariableBlock(x, y, scaling, INPUT, var_name, values[2])
Laurent@814:                 else:
andrej@1734:                     message = _("\"%s\" element for this pou already exists!") % var_name
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@1347:                 tree = dict([(var.Name, var.Tree) for var in self.ParentWindow.Controler.GetEditedElementInterfaceVars(tagname, True, 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 += "[]"
Edouard@2555:             item = menu.Append(wx.ID_ANY, help='', kind=wx.ITEM_NORMAL, text=child_name)
Edouard@2555:             self.ParentWindow.Bind(wx.EVT_MENU, self.GetAddVariableBlockFunction(x, y, scaling, var_class, child_path, child_type), item)
Laurent@814:             if len(child_tree) > 0:
Laurent@814:                 child_menu = wx.Menu(title='')
Laurent@814:                 self.GenerateTreeMenu(x, y, scaling, child_menu, child_path, var_class, child_tree)
Edouard@2555:                 menu.AppendMenu(wx.ID_ANY, "%s." % child_name, child_menu)
Edouard@1417: 
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
Edouard@1417: 
Laurent@814:     def ShowMessage(self, message):
andrej@1745:         message = wx.MessageDialog(self.ParentWindow, message, _("Error"), wx.OK | wx.ICON_ERROR)
Laurent@814:         message.ShowModal()
Laurent@814:         message.Destroy()
Laurent@814: 
andrej@1674: 
andrej@1674: class DebugInstanceName(DebugDataConsumer):
andrej@2301:     VALUE_TRANSLATION = None
andrej@1730: 
andrej@1674:     def __init__(self, parent):
andrej@1674:         DebugDataConsumer.__init__(self)
andrej@1674:         self.Parent = parent
andrej@1674:         self.ActionLastState = None
andrej@1674:         self.ActionState = None
andrej@1674:         self.x_offset = 2
andrej@1674:         self.y_offset = 2
andrej@1730: 
andrej@2301:         if self.VALUE_TRANSLATION is None:
andrej@2301:             self.__class__.VALUE_TRANSLATION = {True: _("Active"), False: _("Inactive")}
andrej@2301: 
andrej@1674:     def SetValue(self, value):
andrej@1674:         self.ActionState = value
andrej@1674:         if self.ActionState != self.ActionLastState:
andrej@1674:             self.ActionLastState = self.ActionState
andrej@1674:             wx.CallAfter(self.Parent.ElementNeedRefresh, self)
andrej@1674: 
andrej@1674:     def GetInstanceName(self):
andrej@1674:         return _("Debug: %s") % self.Parent.InstancePath
andrej@1674: 
andrej@1674:     def GetRedrawRect(self):
andrej@1730:         x, y = self.Parent.CalcUnscrolledPosition(self.x_offset, self.y_offset)
andrej@1674:         dc = self.Parent.GetLogicalDC()
andrej@1847:         ipw, _iph = dc.GetTextExtent(self.GetInstanceName())
andrej@1674:         vw, vh = 0, 0
andrej@1674:         for value in self.VALUE_TRANSLATION.itervalues():
andrej@1674:             w, h = dc.GetTextExtent(" (%s)" % value)
andrej@1674:             vw = max(vw, w)
andrej@1674:             vh = max(vh, h)
andrej@1674:         return wx.Rect(ipw + x, y, vw, vh)
andrej@1674: 
andrej@1674:     def Draw(self, dc):
andrej@1674:         scalex, scaley = dc.GetUserScale()
andrej@1674:         dc.SetUserScale(1, 1)
andrej@1674:         x, y = self.Parent.CalcUnscrolledPosition(self.x_offset, self.y_offset)
andrej@1730: 
andrej@1674:         text = self.GetInstanceName()
andrej@1674:         if self.ActionState is not None:
andrej@1674:             text += " ("
andrej@1674: 
andrej@1674:         dc.DrawText(text, x, y)
andrej@1847:         tw, _th = dc.GetTextExtent(text)
andrej@1674:         if self.ActionState is not None:
andrej@1674: 
andrej@1674:             text = self.VALUE_TRANSLATION[self.ActionState]
andrej@1674:             if self.ActionState:
andrej@1674:                 dc.SetTextForeground(wx.GREEN)
andrej@1674:             dc.DrawText(text, x + tw, y)
andrej@1674:             if self.ActionState:
andrej@1674:                 dc.SetTextForeground(wx.BLACK)
andrej@1674:             tw = tw + dc.GetTextExtent(text)[0]
andrej@1730: 
andrej@1730:             text = ")"
andrej@1674:             dc.DrawText(text, x + tw, y)
andrej@1674:         dc.SetUserScale(scalex, scaley)
andrej@1674: 
Laurent@814: 
Laurent@1176: class Viewer(EditorPanel, DebugViewer):
andrej@1736:     """
andrej@1736:     Class that implements a Viewer based on a wx.ScrolledWindow for drawing and
andrej@1736:     manipulating graphic elements
andrej@1736:     """
Edouard@1417: 
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)
Edouard@1417: 
Edouard@2557:     def AppendItem(self, menu, text, callback, *args, **kwargs):
Edouard@2557:         item = menu.Append(wx.ID_ANY, text, *args, **kwargs)
Edouard@2557:         self.Bind(wx.EVT_MENU, callback, item)
Edouard@2557:         return item
Edouard@2557: 
Laurent@814:     # Add Block Pin Menu items to the given menu
Laurent@814:     def AddBlockPinMenuItems(self, menu, connector):
Edouard@2557:         no_modifier = self.AppendItem(menu,  _(u'No modifier'), self.OnNoModifierMenu, kind=wx.ITEM_RADIO)
Edouard@2557:         negated = self.AppendItem(menu,  _(u'Negated'), self.OnNegatedMenu, kind=wx.ITEM_RADIO)
Edouard@2557:         rising_edge = self.AppendItem(menu,  _(u'Rising Edge'), self.OnRisingEdgeMenu, kind=wx.ITEM_RADIO)
Edouard@2557:         falling_edge = self.AppendItem(menu,  _(u'Falling Edge'), self.OnFallingEdgeMenu, kind=wx.ITEM_RADIO)
Edouard@2556: 
Edouard@2556:         not_a_function = self.Controler.GetEditedElementType(
Edouard@2556:             self.TagName, self.Debug) != "function"
Edouard@2556:         rising_edge.Enable(not_a_function)
Edouard@2556:         falling_edge.Enable(not_a_function)
Edouard@1417: 
Laurent@814:         if connector.IsNegated():
Edouard@2556:             negated.Check(True)
Laurent@814:         elif connector.GetEdge() == "rising":
Edouard@2556:             rising_edge.Check(True)
Laurent@814:         elif connector.GetEdge() == "falling":
Edouard@2556:             falling_edge.Check(True)
Laurent@814:         else:
Edouard@2556:             no_modifier.Check(True)
Edouard@1417: 
Laurent@814:     # Add Alignment Menu items to the given menu
Laurent@814:     def AddAlignmentMenuItems(self, menu):
Edouard@2557:         self.AppendItem(menu, _(u'Left'), self.OnAlignLeftMenu)
Edouard@2557:         self.AppendItem(menu, _(u'Center'), self.OnAlignCenterMenu)
Edouard@2557:         self.AppendItem(menu, _(u'Right'), self.OnAlignRightMenu)
Edouard@2557:         menu.AppendSeparator()
Edouard@2557:         self.AppendItem(menu, _(u'Top'), self.OnAlignTopMenu)
Edouard@2557:         self.AppendItem(menu, _(u'Middle'), self.OnAlignMiddleMenu)
Edouard@2557:         self.AppendItem(menu, _(u'Bottom'), self.OnAlignBottomMenu)
Edouard@1417: 
Laurent@814:     # Add Wire Menu items to the given menu
Laurent@1263:     def AddWireMenuItems(self, menu, delete=False, replace=False):
Edouard@2567:         self.AppendItem(menu, _(u'Add Wire Segment'), self.OnAddSegmentMenu)
Edouard@2567:         delete_segment = self.AppendItem(menu, _(u'Delete Wire Segment'),
Edouard@2567:                                          self.OnDeleteSegmentMenu)
Edouard@2567:         replace_wire = self.AppendItem(menu, _(u'Replace Wire by connections'),
Edouard@2567:                                        self.OnReplaceWireMenu)
Edouard@2567: 
Edouard@2567:         delete_segment.Enable(delete)
Edouard@2567:         replace_wire.Enable(replace)
Edouard@1417: 
Laurent@814:     # Add Divergence Menu items to the given menu
Laurent@814:     def AddDivergenceMenuItems(self, menu, delete=False):
edouard@2579:         self.AppendItem(menu, _(u'Add Divergence Branch'),
edouard@2579:                         self.OnAddBranchMenu)
Edouard@2567:         delete_branch = self.AppendItem(menu, _(u'Delete Divergence Branch'),
Edouard@2567:                                         self.OnDeleteBranchMenu)
Edouard@2567: 
Edouard@2567:         delete_branch.Enable(delete)
Edouard@1417: 
Laurent@814:     # Add Add Menu items to the given menu
Laurent@814:     def AddAddMenuItems(self, menu):
Edouard@2567:         self.AppendItem(menu, _(u'Block'),
Edouard@2567:                         self.GetAddMenuCallBack(self.AddNewBlock))
Edouard@2567:         self.AppendItem(menu, _(u'Variable'),
Edouard@2567:                         self.GetAddMenuCallBack(self.AddNewVariable))
Edouard@2567:         self.AppendItem(menu, _(u'Connection'),
Edouard@2567:                         self.GetAddMenuCallBack(self.AddNewConnection))
Edouard@2567:         menu.AppendSeparator()
Edouard@1417: 
Laurent@814:         if self.CurrentLanguage != "FBD":
Edouard@2567:             self.AppendItem(menu, _(u'Power Rail'),
Edouard@2567:                             self.GetAddMenuCallBack(self.AddNewPowerRail))
Edouard@2567:             self.AppendItem(menu, _(u'Contact'),
Edouard@2567:                             self.GetAddMenuCallBack(self.AddNewContact))
Edouard@1417: 
Laurent@814:             if self.CurrentLanguage != "SFC":
Edouard@2567:                 self.AppendItem(menu, _(u'Coil'),
Edouard@2567:                                 self.GetAddMenuCallBack(self.AddNewCoil))
Edouard@1417: 
Laurent@814:             menu.AppendSeparator()
Edouard@1417: 
Laurent@814:         if self.CurrentLanguage == "SFC":
Edouard@2567:             self.AppendItem(menu, _(u'Initial Step'),
Edouard@2567:                             self.GetAddMenuCallBack(self.AddNewStep, True))
Edouard@2567:             self.AppendItem(menu, (u'Step'),
Edouard@2567:                             self.GetAddMenuCallBack(self.AddNewStep))
Edouard@2567:             self.AppendItem(menu, (u'Transition'),
Edouard@2567:                             self.GetAddMenuCallBack(self.AddNewTransition))
Edouard@2567:             self.AppendItem(menu, (u'Action Block'),
Edouard@2567:                             self.GetAddMenuCallBack(self.AddNewActionBlock))
Edouard@2567:             self.AppendItem(menu, (u'Divergence'),
Edouard@2567:                             self.GetAddMenuCallBack(self.AddNewDivergence))
Edouard@2567:             self.AppendItem(menu, (u'Jump'),
Edouard@2567:                             self.GetAddMenuCallBack(self.AddNewJump))
Edouard@2567:             menu.AppendSeparator()
Edouard@2567: 
Edouard@2567:         self.AppendItem(menu, _(u'Comment'),
edouard@2579:                         self.GetAddMenuCallBack(self.AddNewComment))
Edouard@1417: 
Laurent@814:     # Add Default Menu items to the given menu
Laurent@814:     def AddDefaultMenuItems(self, menu, edit=False, block=False):
Laurent@814:         if block:
Edouard@2567:             edit_block = self.AppendItem(menu, _(u'Edit Block'),
Edouard@2567:                                          self.OnEditBlockMenu)
Edouard@2567:             self.AppendItem(menu, _(u'Adjust Block Size'),
Edouard@2567:                             self.OnAdjustBlockSizeMenu)
Edouard@2567:             self.AppendItem(menu, _(u'Delete'), self.OnDeleteMenu)
Edouard@2567: 
Edouard@2567:             edit_block.Enable(edit)
Edouard@1417: 
Laurent@814:         else:
surkovsv93@1586:             if self.CurrentLanguage == 'FBD':
Edouard@2567:                 self.AppendItem(menu, _(u'Clear Execution Order'),
Edouard@2567:                                 self.OnClearExecutionOrderMenu)
Edouard@2567:                 self.AppendItem(menu, _(u'Reset Execution Order'),
Edouard@2567:                                 self.OnResetExecutionOrderMenu)
surkovsv93@1586:                 menu.AppendSeparator()
Edouard@1417: 
Laurent@814:             add_menu = wx.Menu(title='')
Laurent@814:             self.AddAddMenuItems(add_menu)
Laurent@814:             menu.AppendMenu(-1, _(u'Add'), add_menu)
Edouard@1417: 
Laurent@814:         menu.AppendSeparator()
Edouard@1417: 
edouard@2579:         cut = self.AppendItem(menu, _(u'Cut'), self.GetClipboardCallBack(self.Cut))
edouard@2579:         copy = self.AppendItem(menu, _(u'Copy'), self.GetClipboardCallBack(self.Copy))
Edouard@2567:         paste = self.AppendItem(menu, _(u'Paste'), self.GetAddMenuCallBack(self.Paste))
Edouard@2567: 
Edouard@2567:         cut.Enable(block)
Edouard@2567:         copy.Enable(block)
Edouard@2567:         paste.Enable(self.ParentWindow.GetCopyBuffer() is not None)
Edouard@1417: 
Laurent@814:     def _init_Editor(self, prnt):
Edouard@1417:         self.Editor = wx.ScrolledWindow(prnt, name="Viewer",
andrej@1768:                                         pos=wx.Point(0, 0), size=wx.Size(0, 0),
andrej@1768:                                         style=wx.HSCROLL | wx.VSCROLL)
Laurent@814:         self.Editor.ParentWindow = self
Edouard@1417: 
Laurent@814:     # Create a new Viewer
andrej@1744:     def __init__(self, parent, tagname, window, controler, debug=False, instancepath=""):
Laurent@814:         self.VARIABLE_PANEL_TYPE = controler.GetPouType(tagname.split("::")[1])
Edouard@1417: 
Laurent@814:         EditorPanel.__init__(self, parent, tagname, window, controler, debug)
Laurent@814:         DebugViewer.__init__(self, controler, debug)
Edouard@1417: 
Laurent@814:         # Adding a rubberband to Viewer
Laurent@814:         self.rubberBand = RubberBand(viewer=self)
andrej@1740:         self.Editor.SetBackgroundColour(wx.Colour(255, 255, 255))
Laurent@814:         self.Editor.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
Laurent@814:         self.ResetView()
Laurent@1342:         self.LastClientSize = None
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
andrej@1674:         self.InstanceName = DebugInstanceName(self)
Edouard@1417: 
Laurent@1224:         # Prevent search for highlighted element to be called too often
Laurent@1224:         self.LastHighlightCheckTime = gettime()
Laurent@1224:         # Prevent search for element producing tooltip to be called too often
Laurent@1224:         self.LastToolTipCheckTime = gettime()
Edouard@1417: 
Laurent@814:         self.Buffering = False
Edouard@1417: 
Laurent@814:         # Initialize Cursors
Laurent@814:         ResetCursors()
Laurent@814:         self.CurrentCursor = 0
Edouard@1417: 
Laurent@814:         # Initialize Block, Wire and Comment numbers
Laurent@814:         self.wire_id = 0
Edouard@1417: 
Laurent@814:         # Initialize Viewer mode to Selection mode
Laurent@814:         self.Mode = MODE_SELECTION
Laurent@814:         self.SavedMode = False
Laurent@814:         self.CurrentLanguage = "FBD"
Edouard@1417: 
Laurent@814:         if not self.Debug:
Laurent@814:             self.Editor.SetDropTarget(ViewerDropTarget(self))
Edouard@1417: 
Laurent@814:         self.ElementRefreshList = []
Laurent@814:         self.ElementRefreshList_lock = Lock()
Edouard@1417: 
Laurent@814:         dc = wx.ClientDC(self.Editor)
andrej@2344:         while True:
andrej@1744:             font = wx.Font(faces["size"], wx.SWISS, wx.NORMAL, wx.NORMAL, faceName=faces["mono"])
Laurent@814:             dc.SetFont(font)
andrej@1847:             width, _height = dc.GetTextExtent("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
andrej@2344:             if width < 260:
andrej@2344:                 break
andrej@2344:             faces["size"] -= 1
andrej@2344:         self.Editor.SetFont(font)
andrej@2229:         self.MiniTextDC = wx.MemoryDC(wx.EmptyBitmap(1, 1))
andrej@1744:         self.MiniTextDC.SetFont(wx.Font(faces["size"] * 0.75, wx.SWISS, wx.NORMAL, wx.NORMAL, faceName=faces["helv"]))
Edouard@1417: 
Laurent@814:         self.CurrentScale = None
Laurent@1123:         self.SetScale(ZOOM_FACTORS.index(1.0), False)
Edouard@1417: 
Laurent@814:         self.RefreshHighlightsTimer = wx.Timer(self, -1)
Laurent@814:         self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer)
Edouard@1417: 
Laurent@814:         self.ResetView()
Edouard@1417: 
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)
Edouard@1417: 
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()
Edouard@1417: 
Laurent@814:     def SetCurrentCursor(self, cursor):
Laurent@814:         if self.Mode != MODE_MOTION:
Laurent@814:             if self.CurrentCursor != cursor:
Laurent@814:                 self.CurrentCursor = cursor
Laurent@814:                 self.Editor.SetCursor(CURSORS[cursor])
Edouard@1417: 
Laurent@814:     def GetScrolledRect(self, rect):
Edouard@1417:         rect.x, rect.y = self.Editor.CalcScrolledPosition(int(rect.x * self.ViewScale[0]),
andrej@1768:                                                           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
Edouard@1417: 
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)
Edouard@1417: 
Laurent@814:     def GetScaling(self):
Laurent@814:         return self.Scaling
Edouard@1417: 
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
Edouard@1417: 
Laurent@814:     def IsViewing(self, tagname):
Laurent@814:         if self.Debug:
Laurent@814:             return self.InstancePath == tagname
Laurent@814:         return EditorPanel.IsViewing(self, tagname)
Edouard@1417: 
Laurent@814:     # Returns a new id
Laurent@814:     def GetNewId(self):
Laurent@814:         self.current_id += 1
Laurent@814:         return self.current_id
Edouard@1417: 
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()
andrej@2437:                     mouse_pos = wx.Point(client_size[0] // 2, client_size[1] // 2)
Laurent@814:                     mouse_event = wx.MouseEvent(wx.EVT_MOUSEWHEEL.typeId)
andrej@1498:                     mouse_event.x = mouse_pos.x
andrej@1498:                     mouse_event.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()
Edouard@1417: 
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())
andrej@2177:         self.Editor.DoPrepareDC(dc)
Laurent@814:         dc.SetUserScale(self.ViewScale[0], self.ViewScale[1])
Laurent@814:         return dc
Edouard@1417: 
Laurent@814:     def RefreshRect(self, rect, eraseBackground=True):
Laurent@814:         self.Editor.RefreshRect(rect, eraseBackground)
Edouard@1417: 
Laurent@814:     def Scroll(self, x, y):
Laurent@1204:         if self.Debug and wx.Platform == '__WXMSW__':
Laurent@1204:             self.Editor.Freeze()
Laurent@814:         self.Editor.Scroll(x, y)
Laurent@1204:         if self.Debug:
Laurent@1204:             if wx.Platform == '__WXMSW__':
Laurent@1208:                 self.Editor.Thaw()
Laurent@1204:             else:
Laurent@1204:                 self.Editor.Refresh()
Edouard@1417: 
Laurent@814:     def GetScrollPos(self, orientation):
Laurent@814:         return self.Editor.GetScrollPos(orientation)
Edouard@1417: 
Laurent@814:     def GetScrollRange(self, orientation):
Laurent@814:         return self.Editor.GetScrollRange(orientation)
Edouard@1417: 
Laurent@814:     def GetScrollThumb(self, orientation):
Laurent@814:         return self.Editor.GetScrollThumb(orientation)
Edouard@1417: 
Laurent@814:     def CalcUnscrolledPosition(self, x, y):
Laurent@814:         return self.Editor.CalcUnscrolledPosition(x, y)
Edouard@1417: 
Laurent@814:     def GetViewStart(self):
Laurent@814:         return self.Editor.GetViewStart()
Edouard@1417: 
Laurent@814:     def GetTextExtent(self, text):
Laurent@814:         return self.Editor.GetTextExtent(text)
Edouard@1417: 
Laurent@814:     def GetFont(self):
Laurent@814:         return self.Editor.GetFont()
Edouard@1417: 
Laurent@814:     def GetMiniTextExtent(self, text):
Laurent@814:         return self.MiniTextDC.GetTextExtent(text)
Edouard@1417: 
Laurent@814:     def GetMiniFont(self):
Laurent@814:         return self.MiniTextDC.GetFont()
Edouard@1417: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                         Element management functions
andrej@1782:     # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def AddBlock(self, block):
Laurent@814:         self.Blocks[block.GetId()] = block
Edouard@1417: 
Laurent@814:     def AddWire(self, wire):
Laurent@814:         self.wire_id += 1
Laurent@814:         self.Wires[wire] = self.wire_id
Edouard@1417: 
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
Edouard@1417: 
Laurent@814:     def IsWire(self, wire):
Laurent@814:         return self.Wires.get(wire, False)
Edouard@1417: 
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())
Edouard@1417: 
Laurent@814:     def RemoveWire(self, wire):
Laurent@814:         self.Wires.pop(wire)
Edouard@1417: 
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: 
andrej@1605:     def GetContinuationByName(self, name):
andrej@1605:         blocks = []
andrej@1605:         for block in self.Blocks.itervalues():
andrej@1605:             if isinstance(block, FBD_Connector) and\
andrej@1605:                block.GetType() == CONTINUATION and\
andrej@1605:                block.GetName() == name:
andrej@1605:                 blocks.append(block)
andrej@1605:         return blocks
andrej@1730: 
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
Edouard@1417: 
andrej@1744:     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)
Edouard@1417: 
Laurent@814:     def GetElementIECPath(self, element):
Laurent@814:         iec_path = None
laurent@828:         instance_path = self.GetInstancePath(True)
Laurent@1166:         if isinstance(element, (Wire, Connector)):
Laurent@1166:             if isinstance(element, Wire):
Laurent@1166:                 element = element.EndConnected
Laurent@1166:             block = element.GetParentBlock()
Laurent@814:             if isinstance(block, FBD_Block):
Laurent@814:                 blockname = block.GetName()
Laurent@1166:                 connectorname = element.GetName()
Laurent@814:                 if blockname != "":
andrej@1734:                     iec_path = "%s.%s.%s" % (instance_path, blockname, connectorname)
Laurent@814:                 else:
Laurent@814:                     if connectorname == "":
andrej@1734:                         iec_path = "%s.%s%d" % (instance_path, block.GetType(), block.GetId())
Laurent@814:                     else:
edouard@2639:                         iec_path = "%s._TMP_%s%d_%s" % (instance_path, block.GetType(), block.GetId(), connectorname)
Laurent@814:             elif isinstance(block, FBD_Variable):
andrej@1734:                 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):
andrej@1734:             iec_path = "%s.%s" % (instance_path, element.GetName())
Laurent@814:         elif isinstance(element, SFC_Step):
andrej@1734:             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"])
andrej@1734:             iec_path = "%s.%s->%s" % (instance_path, ",".join(previous_steps), ",".join(next_steps))
Laurent@814:         return iec_path
Edouard@1417: 
Laurent@1377:     def GetWireModifier(self, wire):
Laurent@1377:         connector = wire.EndConnected
Laurent@1377:         block = connector.GetParentBlock()
Laurent@1377:         if isinstance(block, FBD_Connector):
Laurent@1377:             connection = self.GetConnectorByName(block.GetName())
Laurent@1377:             if connection is not None:
Laurent@1377:                 connector = connection.GetConnector()
Laurent@1377:                 if len(connector.Wires) == 1:
Laurent@1377:                     return self.GetWireModifier(connector.Wires[0][0])
Laurent@1377:         else:
Laurent@1377:             if connector.IsNegated():
Laurent@1377:                 return "negated"
Laurent@1377:             else:
Laurent@1377:                 return connector.GetEdge()
Laurent@1377:         return "none"
Edouard@1417: 
surkovsv93@1644:     def CorrectElementSize(self, element, width, height):
surkovsv93@1644:         min_width, min_height = element.GetMinSize()
surkovsv93@1644:         if width < min_width:
surkovsv93@1644:             width = min_width
surkovsv93@1644:         if height < min_height:
surkovsv93@1644:             height = min_height
surkovsv93@1644:         if element.Size != (width, height):
surkovsv93@1644:             element.SetSize(width, height)
surkovsv93@1644:             element.RefreshModel()
surkovsv93@1644: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                              Reset functions
andrej@1782:     # -------------------------------------------------------------------------------
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
Edouard@1417: 
Laurent@814:     def Flush(self):
Laurent@1205:         self.UnsubscribeAllDataConsumers(tick=False)
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             block.Flush()
Edouard@1417: 
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()
Edouard@1417: 
Laurent@814:     # Changes Viewer mode
Laurent@814:     def SetMode(self, mode):
Edouard@1417:         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
Edouard@1417: 
Laurent@814:     # Return current drawing mode
Laurent@814:     def GetDrawingMode(self):
Laurent@814:         return self.ParentWindow.GetDrawingMode()
Edouard@1417: 
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()
Edouard@1417: 
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()
Edouard@1417: 
Laurent@814:     def ResetBuffer(self):
Laurent@814:         if self.Buffering:
Laurent@814:             self.Controler.EndBuffering()
Laurent@814:             self.Buffering = False
Edouard@1417: 
Laurent@814:     def GetBufferState(self):
Laurent@814:         if not self.Debug:
Laurent@814:             return self.Controler.GetBufferState()
Laurent@814:         return False, False
Edouard@1417: 
Laurent@814:     def Undo(self):
Laurent@814:         if not self.Debug:
Laurent@814:             self.Controler.LoadPrevious()
Laurent@814:             self.ParentWindow.CloseTabsWithoutModel()
Edouard@1417: 
Laurent@814:     def Redo(self):
Laurent@814:         if not self.Debug:
Laurent@814:             self.Controler.LoadNext()
Laurent@814:             self.ParentWindow.CloseTabsWithoutModel()
Edouard@1417: 
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
Edouard@1417: 
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@1204:             self.Editor.Refresh(False)
Edouard@1417: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                          Refresh functions
andrej@1782:     # -------------------------------------------------------------------------------
Edouard@1417: 
Laurent@814:     def ElementNeedRefresh(self, element):
Laurent@814:         self.ElementRefreshList_lock.acquire()
Laurent@814:         self.ElementRefreshList.append(element)
Laurent@814:         self.ElementRefreshList_lock.release()
Edouard@1417: 
Edouard@1431:     def NewDataAvailable(self, ticks):
Laurent@1366:         if self.IsShown():
Laurent@1366:             refresh_rect = None
Laurent@1366:             self.ElementRefreshList_lock.acquire()
Laurent@1366:             for element in self.ElementRefreshList:
Laurent@1366:                 if refresh_rect is None:
Laurent@1366:                     refresh_rect = element.GetRedrawRect()
Laurent@1366:                 else:
Laurent@1366:                     refresh_rect.Union(element.GetRedrawRect())
Laurent@1366:             self.ElementRefreshList = []
Laurent@1366:             self.ElementRefreshList_lock.release()
Edouard@1417: 
Laurent@1366:             if refresh_rect is not None:
Laurent@1366:                 self.RefreshRect(self.GetScrolledRect(refresh_rect), False)
Edouard@1417: 
Laurent@1176:     def SubscribeAllDataConsumers(self):
Laurent@1201:         self.RefreshView()
Laurent@1176:         DebugViewer.SubscribeAllDataConsumers(self)
Edouard@1417: 
Laurent@814:     # Refresh Viewer elements
Laurent@814:     def RefreshView(self, variablepanel=True, selection=None):
Laurent@814:         EditorPanel.RefreshView(self, variablepanel)
Edouard@1417: 
Laurent@814:         if self.ToolTipElement is not None:
Laurent@1170:             self.ToolTipElement.DestroyToolTip()
Laurent@814:             self.ToolTipElement = None
Edouard@1417: 
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()
andrej@1846: 
Laurent@814:         # List of ids of already loaded blocks
andrej@1744:         instances = self.Controler.GetEditedElementInstancesInfos(self.TagName, debug=self.Debug)
Laurent@814:         # Load Blocks until they are all loaded
Laurent@1338:         while len(instances) > 0:
Laurent@1338:             self.loadInstance(instances.popitem(0)[1], instances, selection)
Edouard@1417: 
andrej@1766:         if selection is not None and isinstance(self.SelectedElement, Graphic_Group):
Laurent@1226:             self.SelectedElement.RefreshWireExclusion()
Laurent@1226:             self.SelectedElement.RefreshBoundingBox()
Edouard@1417: 
Laurent@814:         self.RefreshScrollBars()
Edouard@1417: 
andrej@1674:         if self.TagName.split("::")[0] == "A" and self.Debug:
andrej@1674:             self.AddDataConsumer("%s.Q" % self.InstancePath.upper(), self.InstanceName)
andrej@1730: 
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@1377:                 else:
Laurent@1377:                     wire.SetModifier(self.GetWireModifier(wire))
Laurent@814: 
Laurent@814:         if self.Debug:
Laurent@814:             for block in self.Blocks.itervalues():
Laurent@814:                 block.SpreadCurrent()
Laurent@1166:                 if isinstance(block, FBD_Block):
Laurent@1166:                     for output_connector in block.GetConnectors()["outputs"]:
Laurent@1166:                         if len(output_connector.GetWires()) == 0:
Laurent@1166:                             iec_path = self.GetElementIECPath(output_connector)
Laurent@1166:                             if iec_path is not None:
Laurent@1166:                                 self.AddDataConsumer(iec_path.upper(), output_connector)
Laurent@1166:                 else:
Laurent@1166:                     iec_path = self.GetElementIECPath(block)
Laurent@1166:                     if iec_path is not None:
Laurent@1166:                         self.AddDataConsumer(iec_path.upper(), block)
Laurent@814: 
Laurent@814:         self.Inhibit(False)
Laurent@814:         self.RefreshVisibleElements()
Laurent@814:         self.ShowHighlights()
Laurent@1204:         self.Editor.Refresh(False)
Edouard@1417: 
Laurent@814:     def GetPreviousSteps(self, connectors):
Laurent@814:         steps = []
Laurent@814:         for connector in connectors:
andrej@1847:             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
Edouard@1417: 
Laurent@814:     def GetNextSteps(self, connectors):
Laurent@814:         steps = []
Laurent@814:         for connector in connectors:
andrej@1847:             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
Edouard@1417: 
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
Edouard@1417: 
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])
andrej@1768:         self.Editor.SetScrollbars(
andrej@1768:             SCROLLBAR_UNIT, SCROLLBAR_UNIT,
andrej@2437:             round(maxx / SCROLLBAR_UNIT) + width_incr,
andrej@2437:             round(maxy / SCROLLBAR_UNIT) + height_incr,
Laurent@814:             xstart, ystart, True)
Edouard@1417: 
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()
Edouard@1417: 
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]
Edouard@1417:         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]))
Edouard@1417: 
Laurent@814:         xpos, ypos = xstart, ystart
Laurent@814:         if block_minx < screen_minx and block_maxx < screen_maxx:
andrej@2437:             xpos -= (screen_minx - block_minx) // SCROLLBAR_UNIT + 1
Laurent@814:         elif block_maxx > screen_maxx and block_minx > screen_minx:
andrej@2437:             xpos += (block_maxx - screen_maxx) // SCROLLBAR_UNIT + 1
Laurent@814:         if block_miny < screen_miny and block_maxy < screen_maxy:
andrej@2437:             ypos -= (screen_miny - block_miny) // SCROLLBAR_UNIT + 1
Laurent@814:         elif block_maxy > screen_maxy and block_miny > screen_miny:
andrej@2437:             ypos += (block_maxy - screen_maxy) // SCROLLBAR_UNIT + 1
Laurent@814:         self.Scroll(xpos, ypos)
Edouard@1417: 
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@1226:             self.SelectedElement.AddElement(element)
Laurent@814:         else:
Laurent@814:             group = Graphic_Group(self)
Laurent@1226:             group.AddElement(self.SelectedElement)
Laurent@1226:             group.AddElement(element)
Laurent@814:             self.SelectedElement = group
Edouard@1417: 
Laurent@814:     # Load instance from given informations
Laurent@1338:     def loadInstance(self, instance, remaining_instances, selection):
Laurent@1338:         self.current_id = max(self.current_id, instance.id)
Laurent@1338:         creation_function = ElementCreationFunctions.get(instance.type, None)
andrej@1739:         connectors = {"inputs": [], "outputs": []}
Laurent@1338:         specific_values = instance.specific_values
Laurent@814:         if creation_function is not None:
Laurent@1338:             element = creation_function(self, instance.id, specific_values)
Laurent@814:             if isinstance(element, SFC_Step):
Laurent@1338:                 if len(instance.inputs) > 0:
Laurent@814:                     element.AddInput()
Laurent@814:                 else:
Laurent@814:                     element.RemoveInput()
Laurent@1338:                 if len(instance.outputs) > 0:
Laurent@814:                     element.AddOutput()
Laurent@1338:             if isinstance(element, SFC_Transition) and specific_values.condition_type == "connection":
Laurent@814:                 connector = element.GetConditionConnector()
Laurent@1338:                 self.CreateWires(connector, instance.id, specific_values.connection.links, remaining_instances, selection)
Laurent@814:         else:
Laurent@814:             executionControl = False
Laurent@1338:             for input in instance.inputs:
Laurent@1338:                 input_edge = MODIFIER_VALUE(input.edge)
Laurent@1338:                 if input.negated:
Laurent@1338:                     connectors["inputs"].append((input.name, None, "negated"))
Laurent@1338:                 elif input_edge:
Laurent@1338:                     connectors["inputs"].append((input.name, None, input_edge))
Laurent@814:                 else:
Laurent@1338:                     connectors["inputs"].append((input.name, None, "none"))
Laurent@1338:             for output in instance.outputs:
Laurent@1338:                 output_edge = MODIFIER_VALUE(output.edge)
Laurent@1338:                 if output.negated:
Laurent@1338:                     connectors["outputs"].append((output.name, None, "negated"))
Laurent@1338:                 elif output_edge:
Laurent@1338:                     connectors["outputs"].append((output.name, None, output_edge))
Laurent@814:                 else:
Laurent@1338:                     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@1338:             block_name = specific_values.name if specific_values.name is not None else ""
andrej@1768:             element = FBD_Block(
andrej@1768:                 self, instance.type, block_name,
andrej@1768:                 instance.id, len(connectors["inputs"]),
andrej@1768:                 connectors=connectors, executionControl=executionControl,
andrej@1768:                 executionOrder=specific_values.execution_order)
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@1338:         element.SetPosition(instance.x, instance.y)
Laurent@1338:         element.SetSize(instance.width, instance.height)
Laurent@1338:         for i, output_connector in enumerate(instance.outputs):
Laurent@1338:             connector_pos = wx.Point(*output_connector.position)
Laurent@1141:             if isinstance(element, FBD_Block):
Laurent@1338:                 connector = element.GetConnector(connector_pos,
andrej@1768:                                                  output_name=output_connector.name)
Laurent@1141:             elif i < len(connectors["outputs"]):
Laurent@1141:                 connector = connectors["outputs"][i]
Laurent@1141:             else:
Laurent@1141:                 connector = None
Laurent@1141:             if connector is not None:
Laurent@1338:                 if output_connector.negated:
Laurent@1141:                     connector.SetNegated(True)
Laurent@1338:                 if output_connector.edge is not None:
Laurent@1338:                     connector.SetEdge(output_connector.edge)
Laurent@1141:                 if connectors["outputs"].index(connector) == i:
Laurent@1338:                     connector.SetPosition(connector_pos)
Laurent@1338:         for i, input_connector in enumerate(instance.inputs):
Laurent@1338:             connector_pos = wx.Point(*input_connector.position)
Laurent@1141:             if isinstance(element, FBD_Block):
Laurent@1338:                 connector = element.GetConnector(connector_pos,
andrej@1768:                                                  input_name=input_connector.name)
Laurent@1141:             elif i < len(connectors["inputs"]):
Laurent@1141:                 connector = connectors["inputs"][i]
Laurent@1141:             else:
Laurent@1141:                 connector = None
Laurent@1141:             if connector is not None:
Laurent@1141:                 if connectors["inputs"].index(connector) == i:
Laurent@1338:                     connector.SetPosition(connector_pos)
Laurent@1338:                 if input_connector.negated:
Laurent@1141:                     connector.SetNegated(True)
Laurent@1338:                 if input_connector.edge is not None:
Laurent@1338:                     connector.SetEdge(input_connector.edge)
Laurent@1338:                 if not self.CreateWires(connector, instance.id, input_connector.links, remaining_instances, selection):
Laurent@1141:                     element.RefreshModel()
Laurent@1130:         element.RefreshConnectors()
surkovsv93@1644:         self.CorrectElementSize(element, instance.width, instance.height)
Laurent@1338:         if selection is not None and selection[0].get(instance.id, False):
Laurent@814:             self.SelectInGroup(element)
Laurent@814: 
Laurent@1338:     def CreateWires(self, start_connector, id, links, remaining_instances, selection=None):
Laurent@1135:         links_connected = True
Laurent@814:         for link in links:
Laurent@1338:             refLocalId = link.refLocalId
Laurent@1135:             if refLocalId is None:
Laurent@1135:                 links_connected = False
Laurent@1135:                 continue
Edouard@1417: 
Laurent@1338:             new_instance = remaining_instances.pop(refLocalId, None)
Laurent@1338:             if new_instance is not None:
Laurent@1338:                 self.loadInstance(new_instance, remaining_instances, selection)
Edouard@1417: 
Laurent@1135:             connected = self.FindElementById(refLocalId)
Laurent@1135:             if connected is None:
Laurent@1135:                 links_connected = False
Laurent@1135:                 continue
Edouard@1417: 
Laurent@1338:             points = link.points
Laurent@1335:             end_connector = connected.GetConnector(
Laurent@1338:                 wx.Point(points[-1].x, points[-1].y)
Edouard@1417:                 if len(points) > 0 else wx.Point(0, 0),
Laurent@1338:                 link.formalParameter)
Laurent@1135:             if end_connector is not None:
Laurent@1335:                 if len(points) > 0:
Laurent@1335:                     wire = Wire(self)
Laurent@1335:                     wire.SetPoints(points)
Laurent@1335:                 else:
andrej@1768:                     wire = Wire(
andrej@1768:                         self,
andrej@1768:                         [wx.Point(*start_connector.GetPosition()), start_connector.GetDirection()],
andrej@1768:                         [wx.Point(*end_connector.GetPosition()),   end_connector.GetDirection()])
Laurent@1342:                 start_connector.Wires.append((wire, 0))
Laurent@1342:                 end_connector.Wires.append((wire, -1))
Laurent@1342:                 wire.StartConnected = start_connector
Laurent@1342:                 wire.EndConnected = end_connector
Laurent@1135:                 connected.RefreshConnectors()
Laurent@1135:                 self.AddWire(wire)
andrej@1878:                 if selection is not None and (selection[1].get((id, refLocalId), False) or
andrej@1878:                                               selection[1].get((refLocalId, id), False)):
Laurent@1135:                     self.SelectInGroup(wire)
Laurent@1135:             else:
Laurent@1135:                 links_connected = False
Edouard@1417: 
Laurent@1135:         return links_connected
Edouard@1417: 
Laurent@814:     def IsOfType(self, type, reference):
Laurent@814:         return self.Controler.IsOfType(type, reference, self.Debug)
Edouard@1417: 
Laurent@814:     def IsEndType(self, type):
Laurent@814:         return self.Controler.IsEndType(type)
Laurent@814: 
andrej@1744:     def GetBlockType(self, type, inputs=None):
Laurent@814:         return self.Controler.GetBlockType(type, inputs, self.Debug)
Laurent@814: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                          Search Element functions
andrej@1782:     # -------------------------------------------------------------------------------
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
Edouard@1417: 
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
Edouard@1417: 
andrej@1744:     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
Edouard@1417: 
andrej@1744:     def FindBlockConnector(self, pos, direction=None, exclude=None):
andrej@1847:         result, _error = self.FindBlockConnectorWithError(pos, direction, exclude)
surkovsv93@1544:         return result
surkovsv93@1544: 
andrej@1744:     def FindBlockConnectorWithError(self, pos, direction=None, exclude=None):
andrej@1730:         error = False
surkovsv93@1567:         startblock = None
Laurent@814:         for block in self.Blocks.itervalues():
surkovsv93@1544:             connector = block.TestConnector(pos, direction, exclude)
surkovsv93@1544:             if connector:
surkovsv93@1567:                 if self.IsWire(self.SelectedElement):
surkovsv93@1567:                     startblock = self.SelectedElement.StartConnected.GetParentBlock()
surkovsv93@1544:                 avail, error = connector.ConnectionAvailable(direction, exclude)
surkovsv93@1567:                 if not avail or not self.BlockCompatibility(startblock, block, direction):
surkovsv93@1544:                     connector = None
surkovsv93@1567:                     error = True
surkovsv93@1544:                 return connector, error
surkovsv93@1544:         return None, error
andrej@1730: 
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
Edouard@1417: 
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@1226:         self.SelectedElement.SetElements(self.GetElements())
Laurent@814:         self.SelectedElement.SetSelected(True)
Edouard@1417: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                           Popup menu functions
andrej@1782:     # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def GetForceVariableMenuFunction(self, iec_path, element):
Laurent@814:         iec_type = self.GetDataType(iec_path)
andrej@1750: 
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='')
Edouard@2565:             item = menu.Append(wx.ID_ANY, help='', kind=wx.ITEM_NORMAL, text=_("Force value"))
Edouard@2555:             self.Bind(wx.EVT_MENU, self.GetForceVariableMenuFunction(iec_path.upper(), self.SelectedElement), item)
Edouard@2565:             ritem = menu.Append(wx.ID_ANY, help='', kind=wx.ITEM_NORMAL, text=_("Release value"))
Edouard@2555:             self.Bind(wx.EVT_MENU, self.GetReleaseVariableMenuFunction(iec_path.upper()), ritem)
Laurent@814:             if self.SelectedElement.IsForced():
Edouard@2555:                 ritem.Enable(True)
Laurent@814:             else:
Edouard@2555:                 ritem.Enable(False)
surkovsv93@1711:             if self.Editor.HasCapture():
surkovsv93@1711:                 self.Editor.ReleaseMouse()
Laurent@814:             self.Editor.PopupMenu(menu)
Laurent@814:             menu.Destroy()
Laurent@814: 
andrej@1744:     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()
Edouard@1417: 
Laurent@857:     def PopupVariableMenu(self):
Laurent@857:         menu = wx.Menu(title='')
Laurent@857:         variable_type = self.SelectedElement.GetType()
edouard@2579:         for type_label, vtype in [(_("Input"), INPUT),
edouard@2579:                                   (_("Output"), OUTPUT),
edouard@2579:                                   (_("InOut"), INOUT)]:
Edouard@2567:             item = self.AppendItem(menu, type_label,
edouard@2579:                                    self.GetChangeVariableTypeMenuFunction(vtype),
Edouard@2567:                                    kind=wx.ITEM_RADIO)
edouard@2579:             if vtype == variable_type:
Edouard@2567:                 item.Check(True)
Laurent@857:         menu.AppendSeparator()
Laurent@857:         self.AddDefaultMenuItems(menu, block=True)
Laurent@857:         self.Editor.PopupMenu(menu)
Laurent@857:         menu.Destroy()
Edouard@1417: 
Laurent@857:     def PopupConnectionMenu(self):
Laurent@857:         menu = wx.Menu(title='')
Laurent@857:         connection_type = self.SelectedElement.GetType()
edouard@2579:         for type_label, ctype in [(_("Connector"), CONNECTOR),
edouard@2579:                                   (_("Continuation"), CONTINUATION)]:
Edouard@2567:             item = self.AppendItem(menu, type_label,
edouard@2579:                                    self.GetChangeConnectionTypeMenuFunction(ctype),
Edouard@2567:                                    kind=wx.ITEM_RADIO)
edouard@2579:             if ctype == connection_type:
Edouard@2567:                 item.Check(True)
Laurent@857:         menu.AppendSeparator()
Laurent@857:         self.AddDefaultMenuItems(menu, block=True)
Laurent@857:         self.Editor.PopupMenu(menu)
Laurent@857:         menu.Destroy()
Edouard@1417: 
Laurent@814:     def PopupWireMenu(self, delete=True):
Laurent@814:         menu = wx.Menu(title='')
Edouard@1417: 
Laurent@1263:         # If Check that wire can be replace by connections or abort
Laurent@1263:         connected = self.SelectedElement.GetConnected()
Laurent@1263:         start_connector = (
Laurent@1263:             self.SelectedElement.GetEndConnected()
Laurent@1263:             if self.SelectedElement.GetStartConnected() in connected
Laurent@1263:             else self.SelectedElement.GetStartConnected())
Edouard@1417: 
andrej@1768:         self.AddWireMenuItems(
andrej@1768:             menu, delete,
Edouard@1417:             start_connector.GetDirection() == EAST and
Laurent@1263:             not isinstance(start_connector.GetParentBlock(), SFC_Step))
Edouard@1417: 
Laurent@814:         menu.AppendSeparator()
Laurent@814:         self.AddDefaultMenuItems(menu, block=True)
Laurent@814:         self.Editor.PopupMenu(menu)
Laurent@814:         menu.Destroy()
Edouard@1417: 
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()
Edouard@1417: 
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()
Edouard@1417: 
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: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                            Menu items functions
andrej@1782:     # -------------------------------------------------------------------------------
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@1204:             self.Editor.Refresh(False)
Edouard@1417: 
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@1204:             self.Editor.Refresh(False)
Edouard@1417: 
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@1204:             self.Editor.Refresh(False)
Edouard@1417: 
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@1204:             self.Editor.Refresh(False)
Edouard@1417: 
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@1204:             self.Editor.Refresh(False)
Edouard@1417: 
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@1204:             self.Editor.Refresh(False)
Edouard@1417: 
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()
Edouard@1417: 
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()
Edouard@1417: 
Laurent@1263:     def OnReplaceWireMenu(self, event):
Laurent@1263:         # Check that selected element is a wire before applying replace
andrej@1766:         if self.SelectedElement is not None and self.IsWire(self.SelectedElement):
Laurent@1263:             # Get wire redraw bbox to erase it from screen
Laurent@1263:             wire = self.SelectedElement
Laurent@1263:             redraw_rect = wire.GetRedrawRect()
Edouard@1417: 
Laurent@1263:             # Get connector at both ends of wire
Laurent@1263:             connected = wire.GetConnected()
Laurent@1263:             if wire.GetStartConnected() in connected:
Laurent@1263:                 start_connector = wire.GetEndConnected()
Laurent@1263:                 end_connector = wire.GetStartConnected()
Laurent@1263:                 wire.UnConnectStartPoint()
Laurent@1263:                 point_to_connect = 0
Laurent@1263:             else:
Laurent@1263:                 start_connector = wire.GetStartConnected()
Laurent@1263:                 end_connector = wire.GetEndConnected()
Laurent@1263:                 wire.UnConnectEndPoint()
Laurent@1263:                 point_to_connect = -1
Edouard@1417: 
Laurent@1263:             # Get a new default connection name
Laurent@1263:             connection_name = self.Controler.GenerateNewName(
andrej@1878:                 self.TagName, None, "Connection%d", 0)
Edouard@1417: 
Laurent@1263:             # Create a connector to connect to wire
Laurent@1263:             id = self.GetNewId()
Laurent@1263:             connection = FBD_Connector(self, CONNECTOR, connection_name, id)
Laurent@1263:             connection.SetSize(*self.GetScaledSize(*connection.GetMinSize()))
Edouard@1417: 
Edouard@1417:             # Calculate position of connector at the right of start connector
Laurent@1263:             connector = connection.GetConnectors()["inputs"][0]
Laurent@1263:             rel_pos = connector.GetRelPosition()
Laurent@1263:             start_point = start_connector.GetPosition(False)
Laurent@1263:             end_point = (start_point[0] + LD_WIRE_SIZE, start_point[1])
Edouard@1417:             connection.SetPosition(end_point[0] - rel_pos[0],
Laurent@1263:                                    end_point[1] - rel_pos[1])
Edouard@1417: 
Laurent@1263:             # Connect connector to wire
Laurent@1263:             connector.Connect((wire, point_to_connect))
Laurent@1263:             if point_to_connect == 0:
Laurent@1263:                 wire.SetPoints([end_point, start_point])
Laurent@1263:             else:
Laurent@1263:                 wire.SetPoints([start_point, end_point])
Laurent@1263:             # Update redraw bbox with new wire trace so that it will be redraw
Laurent@1263:             # on screen
Laurent@1263:             redraw_rect.Union(wire.GetRedrawRect())
Edouard@1417: 
Laurent@1263:             # Add connector to Viewer and model
Laurent@1263:             self.AddBlock(connection)
Edouard@1417:             self.Controler.AddEditedElementConnection(self.TagName, id,
Laurent@1263:                                                       CONNECTOR)
Laurent@1263:             connection.RefreshModel()
Laurent@1263:             # Update redraw bbox with new connector bbox so that it will be
Laurent@1263:             # drawn on screen
Laurent@1263:             redraw_rect.Union(connection.GetRedrawRect())
Edouard@1417: 
Laurent@1263:             # Add new continuation
Laurent@1263:             id = self.GetNewId()
Laurent@1263:             connection = FBD_Connector(self, CONTINUATION, connection_name, id)
Laurent@1263:             connection.SetSize(*self.GetScaledSize(*connection.GetMinSize()))
Edouard@1417: 
Laurent@1263:             # Calculate position of connection at the left of end connector
Laurent@1263:             connector = connection.GetConnectors()["outputs"][0]
Laurent@1263:             rel_pos = connector.GetRelPosition()
Laurent@1263:             end_point = end_connector.GetPosition(False)
Laurent@1263:             start_point = (end_point[0] - LD_WIRE_SIZE, end_point[1])
Edouard@1417:             connection.SetPosition(start_point[0] - rel_pos[0],
Laurent@1263:                                    start_point[1] - rel_pos[1])
Edouard@1417: 
Laurent@1263:             # Add Wire to Viewer and connect it to blocks
Edouard@1417:             new_wire = Wire(self,
andrej@1768:                             [wx.Point(*start_point), connector.GetDirection()],
andrej@1768:                             [wx.Point(*end_point), end_connector.GetDirection()])
Laurent@1263:             self.AddWire(new_wire)
Laurent@1263:             connector.Connect((new_wire, 0), False)
Laurent@1263:             end_connector.Connect((new_wire, -1), False)
Laurent@1263:             new_wire.ConnectStartPoint(None, connector)
Laurent@1263:             new_wire.ConnectEndPoint(None, end_connector)
Laurent@1263:             # Update redraw bbox with new wire bbox so that it will be drawn on
Laurent@1263:             # screen
Laurent@1263:             redraw_rect.Union(new_wire.GetRedrawRect())
Edouard@1417: 
Laurent@1263:             # Add connection to Viewer and model
Laurent@1263:             self.AddBlock(connection)
Edouard@1417:             self.Controler.AddEditedElementConnection(self.TagName, id,
Laurent@1263:                                                       CONTINUATION)
Laurent@1263:             connection.RefreshModel()
Laurent@1263:             # Update redraw bbox with new connection bbox so that it will be
Laurent@1263:             # drawn on screen
Laurent@1263:             redraw_rect.Union(connection.GetRedrawRect())
Edouard@1417: 
Laurent@1263:             # Refresh model for new wire
Laurent@1263:             end_connector.RefreshParentBlock()
Edouard@1417: 
Edouard@1417:             # Redraw
Laurent@1263:             self.RefreshBuffer()
Laurent@1263:             self.RefreshScrollBars()
Laurent@1263:             self.RefreshVisibleElements()
Laurent@1263:             self.RefreshRect(self.GetScrolledRect(redraw_rect), False)
Edouard@1417: 
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:
andrej@1734:             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@1204:             self.Editor.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()
Edouard@1417: 
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@1259:     def GetAddToWireMenuCallBack(self, func, *args):
Laurent@1259:         args += (self.SelectedElement,)
andrej@1750: 
Laurent@1259:         def AddToWireMenuCallBack(event):
Laurent@1259:             func(wx.Rect(0, 0, 0, 0), *args)
Laurent@1259:         return AddToWireMenuCallBack
Laurent@1259: 
Laurent@814:     def GetClipboardCallBack(self, func):
Laurent@814:         def ClipboardCallback(event):
Laurent@814:             wx.CallAfter(func)
Laurent@814:         return ClipboardCallback
Laurent@814: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                          Mouse event functions
andrej@1782:     # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def OnViewerMouseEvent(self, event):
Laurent@1224:         self.ResetBuffer()
Laurent@1228:         if event.Leaving() and self.ToolTipElement is not None:
Laurent@1224:             self.ToolTipElement.DestroyToolTip()
Laurent@1228:         elif (not event.Entering() and
andrej@1768:               gettime() - self.LastToolTipCheckTime > REFRESH_PERIOD):
Laurent@1224:             self.LastToolTipCheckTime = gettime()
Laurent@814:             element = None
Laurent@814:             if not event.Leaving() and not event.LeftUp() and not event.LeftDClick():
Laurent@1170:                 dc = self.GetLogicalDC()
Laurent@1170:                 pos = event.GetLogicalPosition(dc)
Laurent@1170:                 element = self.FindBlockConnector(pos)
Laurent@1224:                 if element is None or len(element.GetWires()) > 0:
Laurent@1170:                     element = self.FindElement(event, True, False)
Laurent@1228:             if self.ToolTipElement is not None:
Laurent@1228:                 self.ToolTipElement.DestroyToolTip()
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@1170:                 self.ToolTipElement.DisplayToolTip(tooltip_pos)
Laurent@814:         event.Skip()
Edouard@1417: 
Laurent@814:     def OnViewerLeftDown(self, event):
Laurent@814:         self.Editor.CaptureMouse()
Laurent@1273:         self.StartMousePos = event.GetPosition()
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:
andrej@1739:                             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@1259:                     directions = {
Laurent@1259:                         EAST: [EAST, WEST],
Laurent@1259:                         WEST: [WEST, EAST],
Laurent@1259:                         NORTH: [NORTH, SOUTH],
Laurent@1259:                         SOUTH: [SOUTH, NORTH]}[connector.GetDirection()]
andrej@1878:                     wire = Wire(self,
andrej@1878:                                 *map(list, zip(
andrej@1878:                                     [wx.Point(pos.x, pos.y),
andrej@1878:                                      wx.Point(scaled_pos.x, scaled_pos.y)],
andrej@1878:                                     directions)))
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@1259:                     self.SelectedElement.StartConnected.HighlightParentBlock(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:                             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)
Edouard@1417:         elif self.Mode in [MODE_BLOCK, MODE_VARIABLE, MODE_CONNECTION, MODE_COMMENT,
Edouard@1417:                            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.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()
Edouard@1417:                 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():
surkovsv93@1584:                     items = self.GetPopupMenuItems()
Laurent@1259:                     if len(items) > 0:
Laurent@1259:                         if self.Editor.HasCapture():
Laurent@1259:                             self.Editor.ReleaseMouse()
Edouard@1417: 
Laurent@1259:                         # Popup contextual menu
Laurent@1259:                         menu = wx.Menu()
edouard@2579:                         for text, callback in items:
edouard@2579:                             self.AppendItem(menu, text, callback)
Laurent@1259:                         self.PopupMenu(menu)
Edouard@1417: 
Laurent@1259:                     self.SelectedElement.StartConnected.HighlightParentBlock(False)
Laurent@1259:                     if self.DrawingWire:
Laurent@1259:                         self.DrawingWire = False
Laurent@1259:                         rect = self.SelectedElement.GetRedrawRect()
Laurent@1259:                         wire = self.SelectedElement
Laurent@1259:                         self.SelectedElement = self.SelectedElement.StartConnected.GetParentBlock()
Laurent@1259:                         self.SelectedElement.SetSelected(True)
Laurent@1259:                         rect.Union(self.SelectedElement.GetRedrawRect())
Laurent@1259:                         wire.Delete()
Laurent@1259:                         self.RefreshRect(self.GetScrolledRect(rect), False)
Laurent@1259:                     else:
Laurent@1259:                         self.SelectedElement.SetSelected(True)
Laurent@1260:                         if not self.SelectedElement.IsConnectedCompatible():
Laurent@1260:                             self.SelectedElement.SetValid(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.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()
Edouard@1417: 
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()
Edouard@1417: 
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()
Edouard@1417: 
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()
Edouard@1417: 
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()
Edouard@1417: 
Laurent@814:     def OnViewerLeftDClick(self, event):
Laurent@1170:         element = self.FindElement(event)
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)
Edouard@1417: 
Laurent@814:             self.SelectedElement = element
Laurent@814:             self.HighlightedElement = element
Laurent@814:             self.SelectedElement.SetHighlighted(True)
Edouard@1417: 
Laurent@814:             if self.Debug:
laurent@820:                 if isinstance(self.SelectedElement, FBD_Block):
Laurent@1170:                     dc = self.GetLogicalDC()
Laurent@1170:                     pos = event.GetLogicalPosition(dc)
Laurent@1170:                     connector = self.SelectedElement.TestConnector(pos, EAST)
Laurent@1170:                     if connector is not None and len(connector.GetWires()) == 0:
Laurent@1170:                         iec_path = self.GetElementIECPath(connector)
Laurent@1170:                         if iec_path is not None:
Laurent@1170:                             self.ParentWindow.OpenDebugViewer(
Laurent@1170:                                 ITEM_VAR_LOCAL, iec_path, connector.GetType())
Laurent@1170:                     else:
Laurent@1170:                         instance_type = self.SelectedElement.GetType()
Laurent@1170:                         pou_type = {
Laurent@1170:                             "program": ITEM_PROGRAM,
Laurent@1170:                             "functionBlock": ITEM_FUNCTIONBLOCK,
Laurent@1170:                         }.get(self.Controler.GetPouType(instance_type))
Laurent@1170:                         if pou_type is not None and instance_type in self.Controler.GetProjectPouNames(self.Debug):
andrej@1768:                             self.ParentWindow.OpenDebugViewer(
andrej@1768:                                 pou_type,
andrej@1734:                                 "%s.%s" % (self.GetInstancePath(True), self.SelectedElement.GetName()),
Edouard@1948:                                 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:
andrej@1768:                                 self.ParentWindow.OpenDebugViewer(
andrej@1768:                                     ITEM_VAR_LOCAL, iec_path,
andrej@1768:                                     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):
andrej@1768:                         self.ParentWindow.EditProjectElement(
andrej@1768:                             ITEM_POU,
Edouard@1948:                             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()
Edouard@1417: 
Laurent@814:     def OnViewerMotion(self, event):
Laurent@814:         if self.Editor.HasCapture() and not event.Dragging():
Laurent@814:             return
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)
andrej@2437:                 scrollx = max(0, self.StartScreenPos[0] - (new_pos[0] - self.StartMousePos[0]) // SCROLLBAR_UNIT)
andrej@2437:                 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:
andrej@1766:             if not event.Dragging() and (gettime() - self.LastHighlightCheckTime) > REFRESH_PERIOD:
Laurent@1224:                 self.LastHighlightCheckTime = gettime()
Edouard@1417:                 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@1225:                     if not self.Debug and 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:
surkovsv93@1544:                     connector, errorHighlight = self.FindBlockConnectorWithError(pos, self.SelectedElement.GetConnectionDirection(), self.SelectedElement.EndConnected)
andrej@1752:                     self.SelectedElement.ErrHighlight = errorHighlight
andrej@1743:                     if not connector or self.SelectedElement.EndConnected is 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@1225:                     elif not self.Debug:
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@1170:                     element = self.SelectedElement
Laurent@1170:                     if isinstance(self.SelectedElement, FBD_Block):
Laurent@1170:                         dc = self.GetLogicalDC()
Laurent@1170:                         connector = self.SelectedElement.TestConnector(
Edouard@1417:                             wx.Point(dc.DeviceToLogicalX(self.StartMousePos.x),
Laurent@1170:                                      dc.DeviceToLogicalY(self.StartMousePos.y)))
Laurent@1170:                         if connector is not None:
Laurent@1170:                             element = connector
Laurent@1170:                     iec_path = self.GetElementIECPath(element)
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.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:
andrej@1744:                 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: 
andrej@1744:     def BlockCompatibility(self, startblock=None, endblock=None, direction=None):
surkovsv93@1567:         return True
surkovsv93@1567: 
surkovsv93@1584:     def GetPopupMenuItems(self):
surkovsv93@1584:         start_connector = self.SelectedElement.GetStartConnected()
surkovsv93@1584:         start_direction = start_connector.GetDirection()
surkovsv93@1584:         startblock = start_connector.GetParentBlock()
surkovsv93@1584:         items = []
surkovsv93@1584:         if isinstance(startblock, SFC_Objects):
surkovsv93@1584:             startblockname = self.GetBlockName(startblock)
surkovsv93@1584:             poss_div_types = []
surkovsv93@1584: 
surkovsv93@1584:             SFC_WireMenu_Buttons = {
surkovsv93@1584:                 'SFC_Step': (_(u'Step'), self.GetAddToWireMenuCallBack(self.AddNewStep, False)),
surkovsv93@1584:                 'SFC_Jump': (_(u'Jump'), self.GetAddToWireMenuCallBack(self.AddNewJump)),
surkovsv93@1584:                 'SFC_Transition': (_(u'Transition'), self.GetAddToWireMenuCallBack(self.AddNewTransition, False)),
surkovsv93@1584:                 'SFC_ActionBlock': (_(u'Action Block'), self.GetAddToWireMenuCallBack(self.AddNewActionBlock))}
surkovsv93@1584: 
surkovsv93@1584:             for endblock in self.SFC_StandardRules.get(startblockname):
surkovsv93@1584:                 if start_direction in endblock:
surkovsv93@1584:                     if endblock[0] in divergence_types:
surkovsv93@1584:                         poss_div_types.append(endblock[0])
surkovsv93@1584:                     else:
surkovsv93@1584:                         items.append(SFC_WireMenu_Buttons[endblock[0]])
surkovsv93@1584:             if len(poss_div_types) > 0:
surkovsv93@1584:                 items.append((_(u'Divergence'), self.GetAddToWireMenuCallBack(self.AddNewDivergence,
surkovsv93@1584:                                                                               poss_div_types)))
surkovsv93@1584:         elif start_direction == EAST:
andrej@1838:             items.extend([
andrej@1838:                 (_(u'Block'), self.GetAddToWireMenuCallBack(self.AddNewBlock)),
andrej@1838:                 (_(u'Connection'), self.GetAddToWireMenuCallBack(self.AddNewConnection))])
andrej@1838: 
andrej@1838:             if self.CurrentLanguage != "FBD":
andrej@1838:                 items.append((_(u'Contact'), self.GetAddToWireMenuCallBack(self.AddNewContact)))
andrej@1838: 
andrej@1838:             if self.CurrentLanguage == "LD":
surkovsv93@1584:                 items.extend([
andrej@1838:                     (_(u'Coil'), self.GetAddToWireMenuCallBack(self.AddNewCoil)),
andrej@1838:                     (_(u'Power Rail'), self.GetAddToWireMenuCallBack(self.AddNewPowerRail))])
andrej@1838: 
andrej@1838:             if self.CurrentLanguage == "SFC":
andrej@1838:                 items.append(
andrej@1838:                     (_(u'Transition'), self.GetAddToWireMenuCallBack(self.AddNewTransition, True)))
andrej@1838:             else:
andrej@1838:                 items.append(
andrej@1838:                     (_(u'Variable'), self.GetAddToWireMenuCallBack(self.AddNewVariable, True)))
surkovsv93@1584:         return items
surkovsv93@1584: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                          Keyboard event functions
andrej@1782:     # -------------------------------------------------------------------------------
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)
andrej@1763:         elif keycode in self.ARROW_KEY_MOVE:
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]
Edouard@1417:                     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@1042:                 self.RefreshVisibleElements()
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()
andrej@1498:                 event.x, event.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: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                  Model adding functions from Drop Target
andrej@1782:     # -------------------------------------------------------------------------------
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:
andrej@2437:             x = round(x / scaling[0]) * scaling[0]
andrej@2437:             y = round(y / scaling[1]) * scaling[1]
andrej@2437:             width = round(width / scaling[0] + 0.5) * scaling[0]
andrej@2437:             height = round(height / 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@1204:         self.Editor.Refresh(False)
Laurent@814: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                          Model adding functions
andrej@1782:     # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def GetScaledSize(self, width, height):
Laurent@814:         if self.Scaling is not None:
andrej@2437:             width = round(width / self.Scaling[0] + 0.4) * self.Scaling[0]
andrej@2437:             height = round(height / self.Scaling[1] + 0.4) * self.Scaling[1]
Laurent@814:         return width, height
Edouard@1417: 
Laurent@1259:     def AddNewElement(self, element, bbox, wire=None, connector=None):
Laurent@1259:         min_width, min_height = (element.GetMinSize(True)
Laurent@1259:                                  if isinstance(element, (LD_PowerRail,
Laurent@1259:                                                          SFC_Divergence))
Laurent@1259:                                  else element.GetMinSize())
Laurent@1259:         element.SetSize(*self.GetScaledSize(
Laurent@1259:             max(bbox.width, min_width), max(bbox.height, min_height)))
Laurent@1259:         if wire is not None:
Laurent@1259:             if connector is None:
Laurent@1259:                 connector = element.GetConnectors()["inputs"][0]
Laurent@1259:             point = wire.GetPoint(-1)
Laurent@1259:             rel_pos = connector.GetRelPosition()
Laurent@1259:             direction = connector.GetDirection()
Laurent@1259:             element.SetPosition(
Laurent@1259:                 point[0] - rel_pos[0] - direction[0] * CONNECTOR_SIZE,
Laurent@1259:                 point[1] - rel_pos[1] - direction[1] * CONNECTOR_SIZE,
Laurent@1259:             )
Laurent@1259:             connector.Connect((wire, -1))
Laurent@1259:             wire.Refresh()
Laurent@1259:             self.DrawingWire = False
Laurent@1259:         else:
Laurent@1259:             element.SetPosition(bbox.x, bbox.y)
Laurent@1259:         self.AddBlock(element)
Laurent@1259:         element.RefreshModel()
Laurent@1259:         self.RefreshBuffer()
Laurent@1259:         self.RefreshScrollBars()
Laurent@1259:         self.RefreshVisibleElements()
Laurent@1259:         element.Refresh()
Edouard@1417: 
Laurent@1259:     def AddNewBlock(self, bbox, wire=None):
Laurent@1236:         dialog = FBDBlockDialog(self.ParentWindow, self.Controler, self.TagName)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1244:         dialog.SetMinElementSize((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", "")
andrej@1768:             block = FBD_Block(
andrej@1768:                 self, values["type"], values["name"], id,
andrej@1768:                 values["extension"], values["inputs"],
andrej@1768:                 executionControl=values["executionControl"],
andrej@1768:                 executionOrder=values["executionOrder"])
Laurent@1260:             self.Controler.AddEditedElementBlock(self.TagName, id, values["type"], values.get("name", None))
Laurent@1259:             connector = None
Laurent@1287:             if wire is not None:
Laurent@1287:                 for input_connector in block.GetConnectors()["inputs"]:
Laurent@1287:                     if input_connector.IsCompatible(
Laurent@1287:                             wire.GetStartConnectedType()):
Laurent@1287:                         connector = input_connector
Laurent@1287:                         break
Laurent@1259:             self.AddNewElement(block, bbox, wire, connector)
andrej@1671:             self.RefreshVariablePanel()
andrej@1730:             self.ParentWindow.RefreshPouInstanceVariablesPanel()
Laurent@814:         dialog.Destroy()
Edouard@1417: 
Laurent@1259:     def AddNewVariable(self, bbox, exclude_input=False, wire=None):
Laurent@1259:         dialog = FBDVariableDialog(self.ParentWindow, self.Controler, self.TagName, exclude_input)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1244:         dialog.SetMinElementSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@1247:             variable = FBD_Variable(self, values["class"], values["expression"], values["var_type"], id)
Laurent@1248:             variable.SetExecutionOrder(values["executionOrder"])
Laurent@1244:             self.Controler.AddEditedElementVariable(self.TagName, id, values["class"])
Laurent@1259:             self.AddNewElement(variable, bbox, wire)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@1259:     def AddNewConnection(self, bbox, wire=None):
Laurent@1259:         if wire is not None:
Laurent@1259:             values = {
Laurent@1259:                 "type": CONNECTOR,
Laurent@1259:                 "name": self.Controler.GenerateNewName(
Laurent@1259:                     self.TagName, None, "Connection%d", 0)}
Laurent@1259:         else:
Laurent@1259:             dialog = ConnectionDialog(self.ParentWindow, self.Controler, self.TagName)
Laurent@1259:             dialog.SetPreviewFont(self.GetFont())
Laurent@1259:             dialog.SetMinElementSize((bbox.width, bbox.height))
Laurent@1259:             values = (dialog.GetValues()
Laurent@1259:                       if dialog.ShowModal() == wx.ID_OK
Laurent@1259:                       else None)
Laurent@1259:             dialog.Destroy()
Laurent@1259:         if values is not None:
Laurent@814:             id = self.GetNewId()
Laurent@814:             connection = FBD_Connector(self, values["type"], values["name"], id)
Laurent@814:             self.Controler.AddEditedElementConnection(self.TagName, id, values["type"])
Laurent@1259:             self.AddNewElement(connection, bbox, wire)
Edouard@1417: 
Laurent@814:     def AddNewComment(self, bbox):
andrej@2347:         dialog = CommentEditDialog(self.ParentWindow,
andrej@2347:                                    self.GetFont())
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()
andrej@1740:             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@1259:     def AddNewContact(self, bbox, wire=None):
Laurent@1246:         dialog = LDElementDialog(self.ParentWindow, self.Controler, self.TagName, "contact")
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1246:         dialog.SetMinElementSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@1246:             contact = LD_Contact(self, values["modifier"], values["variable"], id)
Laurent@814:             self.Controler.AddEditedElementContact(self.TagName, id)
Laurent@1259:             self.AddNewElement(contact, bbox, wire)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@1259:     def AddNewCoil(self, bbox, wire=None):
Laurent@1246:         dialog = LDElementDialog(self.ParentWindow, self.Controler, self.TagName, "coil")
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1246:         dialog.SetMinElementSize((bbox.width, bbox.height))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@814:             values = dialog.GetValues()
Laurent@1246:             coil = LD_Coil(self, values["modifier"], values["variable"], id)
Laurent@814:             self.Controler.AddEditedElementCoil(self.TagName, id)
Laurent@1259:             self.AddNewElement(coil, bbox, wire)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@1259:     def AddNewPowerRail(self, bbox, wire=None):
Laurent@1259:         if wire is not None:
Laurent@1259:             values = {
Laurent@1259:                 "type": RIGHTRAIL,
Laurent@1259:                 "pin_number": 1}
Laurent@1259:         else:
Laurent@1259:             dialog = LDPowerRailDialog(self.ParentWindow, self.Controler, self.TagName)
Laurent@1259:             dialog.SetPreviewFont(self.GetFont())
Laurent@1259:             dialog.SetMinElementSize((bbox.width, bbox.height))
Laurent@1259:             values = (dialog.GetValues()
Laurent@1259:                       if dialog.ShowModal() == wx.ID_OK
Laurent@1259:                       else None)
Laurent@1259:             dialog.Destroy()
Laurent@1259:         if values is not None:
Laurent@814:             id = self.GetNewId()
Laurent@1249:             powerrail = LD_PowerRail(self, values["type"], id, values["pin_number"])
Laurent@814:             self.Controler.AddEditedElementPowerRail(self.TagName, id, values["type"])
Laurent@1259:             self.AddNewElement(powerrail, bbox, wire)
Laurent@1259: 
Laurent@1259:     def AddNewStep(self, bbox, initial=False, wire=None):
Laurent@1259:         if wire is not None:
Laurent@1259:             values = {
andrej@1740:                 "name":   self.Controler.GenerateNewName(self.TagName, None, "Step%d", 0),
andrej@1740:                 "input":  True,
surkovsv93@1599:                 "output": True,
andrej@1740:                 "action": False
andrej@1740:             }
Laurent@1259:         else:
Laurent@1259:             dialog = SFCStepDialog(self.ParentWindow, self.Controler, self.TagName, initial)
Laurent@1259:             dialog.SetPreviewFont(self.GetFont())
Laurent@1259:             dialog.SetMinElementSize((bbox.width, bbox.height))
Laurent@1259:             values = (dialog.GetValues()
Laurent@1259:                       if dialog.ShowModal() == wx.ID_OK
Laurent@1259:                       else None)
Laurent@1259:             dialog.Destroy()
Laurent@1259:         if values is not None:
Laurent@814:             id = self.GetNewId()
Laurent@814:             step = SFC_Step(self, values["name"], initial, id)
Laurent@1259:             self.Controler.AddEditedElementStep(self.TagName, id)
Laurent@1259:             for connector in ["input", "output", "action"]:
Edouard@1417:                 getattr(step, ("Add"
Edouard@1417:                                if values[connector]
Laurent@1259:                                else "Remove") + connector.capitalize())()
Laurent@1259:             self.AddNewElement(step, bbox, wire)
Edouard@1417: 
Laurent@1259:     def AddNewTransition(self, bbox, connection=False, wire=None):
Laurent@1259:         if wire is not None and connection:
Laurent@1259:             values = {
Laurent@1259:                 "type": "connection",
Laurent@1259:                 "value": None,
Laurent@1259:                 "priority": 0}
Laurent@1259:         else:
Laurent@1259:             dialog = SFCTransitionDialog(self.ParentWindow, self.Controler, self.TagName, self.GetDrawingMode() == FREEDRAWING_MODE)
Laurent@1259:             dialog.SetPreviewFont(self.GetFont())
Laurent@1259:             dialog.SetMinElementSize((bbox.width, bbox.height))
Laurent@1259:             values = (dialog.GetValues()
Laurent@1259:                       if dialog.ShowModal() == wx.ID_OK
Laurent@1259:                       else None)
Laurent@1259:             dialog.Destroy()
Laurent@1259:         if values is not None:
Laurent@1259:             id = self.GetNewId()
Laurent@1259:             transition = SFC_Transition(self, values["type"], values["value"], values["priority"], id)
Laurent@1259:             self.Controler.AddEditedElementTransition(self.TagName, id)
Laurent@1259:             if connection:
Laurent@1259:                 connector = transition.GetConditionConnector()
Laurent@814:             else:
Laurent@1259:                 connector = transition.GetConnectors()["inputs"][0]
Laurent@1259:             self.AddNewElement(transition, bbox, wire, connector)
Edouard@1417: 
andrej@1744:     def AddNewDivergence(self, bbox, poss_div_types=None, wire=None):
surkovsv93@1584:         dialog = SFCDivergenceDialog(self.ParentWindow, self.Controler, self.TagName, poss_div_types)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1251:         dialog.SetMinElementSize((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:             self.Controler.AddEditedElementDivergence(self.TagName, id, values["type"])
Laurent@1259:             self.AddNewElement(divergence, bbox, wire)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@1259:     def AddNewJump(self, bbox, wire=None):
Laurent@814:         choices = []
Laurent@814:         for block in self.Blocks.itervalues():
Laurent@814:             if isinstance(block, SFC_Step):
Laurent@814:                 choices.append(block.GetName())
Edouard@1417:         dialog = wx.SingleChoiceDialog(self.ParentWindow,
andrej@1768:                                        _("Add a new jump"),
andrej@1768:                                        _("Please choose a target"),
andrej@1768:                                        choices,
andrej@1768:                                        wx.DEFAULT_DIALOG_STYLE | wx.OK | wx.CANCEL)
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@1259:             jump = SFC_Jump(self, dialog.GetStringSelection(), id)
Laurent@814:             self.Controler.AddEditedElementJump(self.TagName, id)
Laurent@1259:             self.AddNewElement(jump, bbox, wire)
Laurent@814:         dialog.Destroy()
Laurent@814: 
Laurent@1259:     def AddNewActionBlock(self, bbox, wire=None):
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@1347:         dialog.SetVariableList(self.Controler.GetEditedElementInterfaceVars(self.TagName, debug=self.Debug))
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             id = self.GetNewId()
Laurent@1259:             actionblock = SFC_ActionBlock(self, dialog.GetValues(), id)
Laurent@814:             self.Controler.AddEditedElementActionBlock(self.TagName, id)
Laurent@1259:             self.AddNewElement(actionblock, bbox, wire)
Laurent@814:         dialog.Destroy()
Laurent@814: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                          Edit element content functions
andrej@1782:     # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def EditBlockContent(self, block):
Laurent@1236:         dialog = FBDBlockDialog(self.ParentWindow, self.Controler, self.TagName)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1244:         dialog.SetMinElementSize(block.GetSize())
andrej@1739:         old_values = {
andrej@1739:             "name":             block.GetName(),
andrej@1739:             "type":             block.GetType(),
andrej@1739:             "extension":        block.GetExtension(),
andrej@1739:             "inputs":           block.GetInputTypes(),
andrej@1739:             "executionControl": block.GetExecutionControl(),
andrej@1739:             "executionOrder":   block.GetExecutionOrder()
andrej@1739:         }
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"]))
andrej@1744:             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@1244:         dialog = FBDVariableDialog(self.ParentWindow, self.Controler, self.TagName)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1244:         dialog.SetMinElementSize(variable.GetSize())
andrej@1739:         old_values = {
andrej@1739:             "expression":     variable.GetName(),
andrej@1739:             "class":          variable.GetType(),
andrej@1739:             "executionOrder": variable.GetExecutionOrder()
andrej@1739:         }
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@1244:             variable.SetName(new_values["expression"])
Laurent@1244:             variable.SetType(new_values["class"], new_values["var_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@1244:             if old_values["class"] != new_values["class"]:
Laurent@814:                 id = variable.GetId()
Laurent@814:                 self.Controler.RemoveEditedElementInstance(self.TagName, id)
Laurent@1244:                 self.Controler.AddEditedElementVariable(self.TagName, id, new_values["class"])
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@1245:         dialog = ConnectionDialog(self.ParentWindow, self.Controler, self.TagName, True)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1245:         dialog.SetMinElementSize(connection.GetSize())
andrej@1739:         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)
Edouard@1417: 
Laurent@814:     def EditContactContent(self, contact):
Laurent@1246:         dialog = LDElementDialog(self.ParentWindow, self.Controler, self.TagName, "contact")
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1246:         dialog.SetMinElementSize(contact.GetSize())
andrej@1739:         dialog.SetValues({"variable": contact.GetName(),
andrej@1739:                           "modifier": contact.GetType()})
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = contact.GetRedrawRect(1, 1)
Laurent@1246:             contact.SetName(values["variable"])
Laurent@1246:             contact.SetType(values["modifier"])
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@1246:         dialog = LDElementDialog(self.ParentWindow, self.Controler, self.TagName, "coil")
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1246:         dialog.SetMinElementSize(coil.GetSize())
andrej@1739:         dialog.SetValues({"variable": coil.GetName(),
andrej@1739:                           "modifier": coil.GetType()})
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = coil.GetRedrawRect(1, 1)
Laurent@1246:             coil.SetName(values["variable"])
Laurent@1246:             coil.SetType(values["modifier"])
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@1249:         dialog = LDPowerRailDialog(self.ParentWindow, self.Controler, self.TagName)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1249:         dialog.SetMinElementSize(powerrail.GetSize())
Laurent@1249:         powerrail_type = powerrail.GetType()
Laurent@1249:         dialog.SetValues({
Laurent@1249:             "type": powerrail.GetType(),
Laurent@1249:             "pin_number": len(powerrail.GetConnectors()[
andrej@1878:                 ("outputs" if powerrail_type == LEFTRAIL else "inputs")])})
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = powerrail.GetRedrawRect(1, 1)
Laurent@1249:             powerrail.SetType(values["type"], values["pin_number"])
Laurent@814:             powerrail.SetSize(*self.GetScaledSize(values["width"], values["height"]))
Laurent@814:             rect = rect.Union(powerrail.GetRedrawRect())
Laurent@1249:             if powerrail_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@1250:         dialog = SFCStepDialog(self.ParentWindow, self.Controler, self.TagName, step.GetInitial())
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1250:         dialog.SetMinElementSize(step.GetSize())
Laurent@814:         connectors = step.GetConnectors()
Laurent@1250:         dialog.SetValues({
andrej@1739:             "name": step.GetName(),
Laurent@1250:             "input": len(connectors["inputs"]) > 0,
Laurent@1250:             "output": len(connectors["outputs"]) > 0,
andrej@1743:             "action": step.GetActionConnector() is not None})
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = step.GetRedrawRect(1, 1)
andrej@1607: 
andrej@1607:             new_name = values["name"]
andrej@1607:             if self.GetDrawingMode() == DRIVENDRAWING_MODE:
andrej@1607:                 old_name = step.GetName().upper()
andrej@1607:                 if new_name.upper() != old_name:
andrej@1607:                     for block in self.Blocks.itervalues():
andrej@1607:                         if isinstance(block, SFC_Jump):
andrej@1607:                             if old_name == block.GetTarget().upper():
andrej@1607:                                 block.SetTarget(new_name)
andrej@1607:                                 block.RefreshModel()
andrej@1607:                                 rect = rect.Union(block.GetRedrawRect())
andrej@1607:                                 block.Refresh(rect)
andrej@1607:             step.SetName(new_name)
andrej@1730: 
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"]:
Edouard@1417:                 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)
Edouard@1417: 
Laurent@814:     def EditTransitionContent(self, transition):
Laurent@1252:         dialog = SFCTransitionDialog(self.ParentWindow, self.Controler, self.TagName, self.GetDrawingMode() == FREEDRAWING_MODE)
Laurent@814:         dialog.SetPreviewFont(self.GetFont())
Laurent@1252:         dialog.SetMinElementSize(transition.GetSize())
andrej@1740:         dialog.SetValues({
andrej@1740:             "type":     transition.GetType(),
andrej@1740:             "value":    transition.GetCondition(),
andrej@1740:             "priority": transition.GetPriority()
andrej@1740:         })
Laurent@814:         if dialog.ShowModal() == wx.ID_OK:
Laurent@814:             values = dialog.GetValues()
Laurent@814:             rect = transition.GetRedrawRect(1, 1)
andrej@1740:             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())
Edouard@1417:         dialog = wx.SingleChoiceDialog(self.ParentWindow,
andrej@1768:                                        _("Edit jump target"),
andrej@1768:                                        _("Please choose a target"),
andrej@1768:                                        choices,
andrej@1768:                                        wx.DEFAULT_DIALOG_STYLE | wx.OK | wx.CANCEL)
andrej@1609:         try:
andrej@1609:             indx = choices.index(jump.GetTarget())
andrej@1609:             dialog.SetSelection(indx)
andrej@1609:         except ValueError:
andrej@1609:             pass
andrej@1609: 
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@1347:         dialog.SetVariableList(self.Controler.GetEditedElementInterfaceVars(self.TagName, debug=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):
andrej@2347:         dialog = CommentEditDialog(self.ParentWindow,
andrej@2347:                                    self.GetFont(),
andrej@2347:                                    comment.GetContent(),
andrej@2347:                                    comment.GetSize())
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: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                          Model update functions
andrej@1782:     # -------------------------------------------------------------------------------
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)
Edouard@1417: 
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()))
Edouard@1417: 
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: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                          Model delete functions
andrej@1782:     # -------------------------------------------------------------------------------
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)
Edouard@1417: 
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()
andrej@1608: 
andrej@1608:         if self.GetDrawingMode() == DRIVENDRAWING_MODE:
andrej@1608:             name = step.GetName().upper()
andrej@1608:             remove_jumps = []
andrej@1608:             for block in self.Blocks.itervalues():
andrej@1608:                 if isinstance(block, SFC_Jump):
andrej@1608:                     if name == block.GetTarget().upper():
andrej@1608:                         remove_jumps.append(block)
andrej@1608:             for jump in remove_jumps:
andrej@1608:                 self.DeleteJump(jump)
andrej@1608: 
Laurent@814:         self.RemoveBlock(step)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, step.GetId())
Laurent@814:         for element in elements:
Laurent@814:             element.RefreshModel()
Edouard@1417: 
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()
Edouard@1417: 
Laurent@814:     def DeleteJump(self, jump):
Laurent@814:         jump.Clean()
Laurent@814:         self.RemoveBlock(jump)
Laurent@814:         self.Controler.RemoveEditedElementInstance(self.TagName, jump.GetId())
Edouard@1417: 
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: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                            Editing functions
andrej@1782:     # -------------------------------------------------------------------------------
Edouard@1417: 
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)
Edouard@1417: 
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)
Edouard@1417: 
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)
andrej@2450:             if not isinstance(result, string_types):
Laurent@814:                 self.RefreshBuffer()
Laurent@814:                 self.RefreshView(selection=result)
Laurent@814:                 self.RefreshVariablePanel()
Laurent@814:                 self.ParentWindow.RefreshPouInstanceVariablesPanel()
Laurent@814:             else:
andrej@1745:                 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: 
andrej@1852:     def GenerateNewName(self, element=None, blocktype=None, exclude=None):
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
Edouard@1417:         return self.Controler.GenerateNewName(self.TagName,
Edouard@1417:                                               None,
Edouard@1417:                                               format,
Edouard@1417:                                               exclude=exclude,
Laurent@1122:                                               debug=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
Edouard@1417: 
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())
Edouard@1417:                 self.RefreshStepModel(block)
Laurent@814:             elif isinstance(block, SFC_Transition):
Laurent@814:                 self.Controler.AddEditedElementTransition(self.TagName, block.GetId())
Edouard@1417:                 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())
Edouard@1417:                 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: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                         Find and Replace functions
andrej@1782:     # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def Find(self, direction, search_params):
Laurent@814:         if self.SearchParams != search_params:
Laurent@814:             self.ClearHighlights(SEARCH_RESULT_HIGHLIGHT)
Edouard@1417: 
Laurent@814:             self.SearchParams = search_params
Laurent@814:             self.SearchResults = []
Laurent@814:             blocks = []
andrej@1847:             for infos, start, end, _text in self.Controler.SearchInPou(self.TagName, search_params, self.Debug):
andrej@2417:                 if (infos[0] == self.TagName or self.TagName.split("::")[0] in ['A', 'T']) and infos[1] != 'name':
surkovsv93@1619:                     if infos[1] in ["var_local", "var_input", "var_output", "var_inout"]:
surkovsv93@1619:                         self.SearchResults.append((infos[1:], start, end, SEARCH_RESULT_HIGHLIGHT))
surkovsv93@1619:                     else:
surkovsv93@1619:                         block = self.Blocks.get(infos[2])
surkovsv93@1619:                         if block is not None:
surkovsv93@1619:                             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@1057:             self.CurrentFindHighlight = None
Edouard@1417: 
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)
Edouard@1417: 
Laurent@814:         else:
Laurent@814:             if self.CurrentFindHighlight is not None:
Laurent@814:                 self.RemoveHighlight(*self.CurrentFindHighlight)
Laurent@814:             self.CurrentFindHighlight = None
Edouard@1417: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                        Highlights showing functions
andrej@1782:     # -------------------------------------------------------------------------------
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)
Edouard@1417: 
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)
Edouard@1417: 
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)
Edouard@1417: 
Laurent@814:     def RemoveHighlight(self, infos, start, end, highlight_type):
Laurent@814:         EditorPanel.RemoveHighlight(self, infos, start, end, highlight_type)
Edouard@1417: 
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)
Edouard@1417: 
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)
Edouard@1417: 
andrej@1782:     # -------------------------------------------------------------------------------
andrej@1782:     #                            Drawing functions
andrej@1782:     # -------------------------------------------------------------------------------
Laurent@814: 
Laurent@814:     def OnScrollWindow(self, event):
Laurent@1273:         if self.Editor.HasCapture() and self.StartMousePos is not None:
Laurent@814:             return
Laurent@814:         if wx.Platform == '__WXMSW__':
Laurent@814:             wx.CallAfter(self.RefreshVisibleElements)
Laurent@1204:             self.Editor.Freeze()
Laurent@1208:             wx.CallAfter(self.Editor.Thaw)
Laurent@814:         elif event.GetOrientation() == wx.HORIZONTAL:
andrej@1744:             self.RefreshVisibleElements(xp=event.GetPosition())
Laurent@814:         else:
andrej@1744:             self.RefreshVisibleElements(yp=event.GetPosition())
Edouard@1417: 
Laurent@1203:         # Handle scroll in debug to fully redraw area and ensuring
Laurent@1203:         # instance path is fully draw without flickering
Laurent@1204:         if self.Debug and wx.Platform != '__WXMSW__':
Laurent@1203:             x, y = self.GetViewStart()
Laurent@1203:             if event.GetOrientation() == wx.HORIZONTAL:
Laurent@1203:                 self.Scroll(event.GetPosition(), y)
Laurent@1203:             else:
Laurent@1203:                 self.Scroll(x, event.GetPosition())
Laurent@1203:         else:
Laurent@1203:             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:
andrej@2494:             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]))
andrej@1744:                 self.RefreshVisibleElements(xp=xp)
Laurent@814:                 self.Scroll(xp, y)
Laurent@814:             elif event.ControlDown():
andrej@1744:                 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]))
andrej@1744:                 self.RefreshVisibleElements(yp=yp)
Laurent@814:                 self.Scroll(x, yp)
Edouard@1417: 
Laurent@814:     def OnMoveWindow(self, event):
Laurent@1342:         client_size = self.GetClientSize()
Laurent@1342:         if self.LastClientSize != client_size:
Laurent@1342:             self.LastClientSize = client_size
Laurent@1342:             self.RefreshScrollBars()
Laurent@1342:             self.RefreshVisibleElements()
Laurent@814:         event.Skip()
Laurent@814: 
andrej@1744:     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()
andrej@2341:             if self.PageSize[0] != 0:
andrej@2341:                 for x in xrange(self.PageSize[0] - (xstart * SCROLLBAR_UNIT) % self.PageSize[0], int(window_size[0] / self.ViewScale[0]), self.PageSize[0]):
andrej@2341:                     dc.DrawLine(xstart * SCROLLBAR_UNIT + x + 1, int(ystart * SCROLLBAR_UNIT / self.ViewScale[0]),
andrej@2341:                                 xstart * SCROLLBAR_UNIT + x + 1, int((ystart * SCROLLBAR_UNIT + window_size[1]) / self.ViewScale[0]))
andrej@2341:             if self.PageSize[1] != 0:
andrej@2341:                 for y in xrange(self.PageSize[1] - (ystart * SCROLLBAR_UNIT) % self.PageSize[1], int(window_size[1] / self.ViewScale[1]), self.PageSize[1]):
andrej@2341:                     dc.DrawLine(int(xstart * SCROLLBAR_UNIT / self.ViewScale[0]), ystart * SCROLLBAR_UNIT + y + 1,
andrej@2341:                                 int((xstart * SCROLLBAR_UNIT + window_size[0]) / self.ViewScale[1]), ystart * SCROLLBAR_UNIT + y + 1)
Edouard@1417: 
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):
andrej@1759:                 if not self.Debug or not wire.GetValue():
Laurent@814:                     wire.Draw(dc)
Laurent@814:         if self.Debug:
Laurent@814:             for wire in self.Wires.iterkeys():
andrej@1759:                 if wire != self.SelectedElement and (wire.IsVisible() or printing) and wire.GetValue():
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)
Edouard@1417: 
Laurent@814:         if self.SelectedElement is not None and (self.SelectedElement.IsVisible() or printing):
Laurent@814:             self.SelectedElement.Draw(dc)
Edouard@1417: 
Laurent@814:         if not printing:
Laurent@814:             if self.Debug:
andrej@1674:                 self.InstanceName.Draw(dc)
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()