graphics/LD_Objects.py
author laurent
Thu, 23 Jul 2009 18:07:14 +0200
changeset 381 98890d848701
parent 379 e4c26ee9c998
child 383 25ffba02b6a8
permissions -rw-r--r--
Redefine cursor switching procedure in graphic viewers
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     3
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     5
#based on the plcopen standard. 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     6
#
58
39cd981ff242 Changing file headers
lbessard
parents: 50
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     8
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    10
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
5
f8652b073e84 GPL->LGPL
etisserant
parents: 0
diff changeset
    12
#modify it under the terms of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    15
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
58
39cd981ff242 Changing file headers
lbessard
parents: 50
diff changeset
    19
#General Public License for more details.
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    20
#
5
f8652b073e84 GPL->LGPL
etisserant
parents: 0
diff changeset
    21
#You should have received a copy of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    24
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    25
import wx
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    26
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    27
from GraphicCommons import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    28
from plcopen.structures import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    29
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    30
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    31
#                         Ladder Diagram PowerRail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    32
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    33
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    34
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    35
Class that implements the graphic representation of a power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    36
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    37
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    38
class LD_PowerRail(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    39
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    40
    # Create a new power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    41
    def __init__(self, parent, type, id = None, connectors = [True]):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    42
        Graphic_Element.__init__(self, parent)
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
    43
        self.Type = None
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
    44
        self.Connectors = []
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
    45
        self.RealConnectors = None
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    46
        self.Id = id
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    47
        self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2]
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    48
        if len(connectors) < 1:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    49
            connectors = [True]
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
    50
        self.SetType(type, connectors)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    51
        
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
    52
    def Flush(self):
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
    53
        for connector in self.Connectors:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
    54
            if connector is not None:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
    55
                connector.Flush()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    56
        self.Connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    57
    
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    58
    # Make a clone of this LD_PowerRail
162
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
    59
    def Clone(self, parent, id = None, pos = None):
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
    60
        powerrail = LD_PowerRail(parent, self.Type, id)
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    61
        powerrail.SetSize(self.Size[0], self.Size[1])
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    62
        if pos is not None:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    63
            powerrail.SetPosition(pos.x, pos.y)
283
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
    64
        else:
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
    65
            powerrail.SetPosition(self.Pos.x, self.Pos.y)
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    66
        powerrail.Connectors = []
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    67
        for connector in self.Connectors:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    68
            if connector is not None:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    69
                powerrail.Connectors.append(connector.Clone(powerrail))
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    70
            else:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    71
                powerrail.Connectors.append(None)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    72
        return powerrail
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    73
    
283
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
    74
    def GetConnectorTranslation(self, element):
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
    75
        return dict(zip([connector for connector in self.Connectors if connector is not None],
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
    76
                        [connector for connector in element.Connectors if connector is not None]))
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
    77
    
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    78
    # Returns the RedrawRect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    79
    def GetRedrawRect(self, movex = 0, movey = 0):
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    80
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    81
        for connector in self.Connectors:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    82
            if connector is not None:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    83
                rect = rect.Union(connector.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    84
        if movex != 0 or movey != 0:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    85
            for connector in self.Connectors:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    86
                if connector is not None and connector.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    87
                    rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    88
        return rect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    89
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    90
    # Forbids to change the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    91
    def SetSize(self, width, height):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    92
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    93
            Graphic_Element.SetSize(self, width, height)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    94
            self.RefreshConnectors()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    95
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    96
    # Forbids to select a power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    97
    def HitTest(self, pt):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    98
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
    99
            return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, exclude=False) != None
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   100
        return False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   101
    
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   102
    # Forbids to select a power rail
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   103
    def IsInSelection(self, rect):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   104
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   105
            return Graphic_Element.IsInSelection(self, rect)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   106
        return False
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   107
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   108
    # Deletes this power rail by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   109
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   110
        self.Parent.DeletePowerRail(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   111
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   112
    # Unconnect all connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   113
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   114
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   115
            if connector:
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   116
                connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   117
                
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   118
    # Refresh the power rail bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   119
    def RefreshBoundingBox(self):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   120
        self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   121
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   122
    # Refresh the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   123
    def RefreshSize(self):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   124
        self.Size = wx.Size(LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   125
        self.RefreshBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   126
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   127
    # Returns the block minimum size
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   128
    def GetMinSize(self):
96
d178cfa9e77f Bug on LDPowerRail resize fixed
lbessard
parents: 80
diff changeset
   129
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   130
            return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1]
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   131
        else:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   132
            return LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   133
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   134
    # Add a connector or a blank to this power rail at the last place
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   135
    def AddConnector(self, connector = True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   136
        self.InsertConnector(len(self.Connectors), connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   137
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   138
    # Add a connector or a blank to this power rail at the place given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   139
    def InsertConnector(self, idx, connector = True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   140
        if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   141
            if self.Type == LEFTRAIL:
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   142
                connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   143
            elif self.Type == RIGHTRAIL:
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   144
                connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   145
            self.Connectors.insert(idx, connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   146
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   147
            self.Connectors.insert(idx, None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   148
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   149
        self.RefreshConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   150
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   151
    # Moves the divergence connector given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   152
    def MoveConnector(self, connector, movey):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   153
        position = connector.GetRelPosition()
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   154
        connector.SetPosition(wx.Point(position.x, position.y + movey))
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   155
        miny = self.Size[1]
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   156
        maxy = 0
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   157
        for connect in self.Connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   158
            connect_pos = connect.GetRelPosition()
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   159
            miny = min(miny, connect_pos.y - self.Extensions[0])
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   160
            maxy = max(maxy, connect_pos.y - self.Extensions[0])
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   161
        min_pos = self.Pos.y + miny
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   162
        self.Pos.y = min(min_pos, self.Pos.y)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   163
        if min_pos == self.Pos.y:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   164
            for connect in self.Connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   165
                connect_pos = connect.GetRelPosition()
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   166
                connect.SetPosition(wx.Point(connect_pos.x, connect_pos.y - miny))
110
29b6b70e1721 Bug that makes element resizing acting strongly fixed
lbessard
parents: 96
diff changeset
   167
        self.Connectors.sort(lambda x, y: x.Pos.y.__cmp__(y.Pos.y))
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   168
        maxy = 0
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   169
        for connect in self.Connectors:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   170
            connect_pos = connect.GetRelPosition()
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   171
            maxy = max(maxy, connect_pos.y)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   172
        self.Size[1] = max(maxy + self.Extensions[1], self.Size[1])
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   173
        connector.MoveConnected()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   174
        self.RefreshBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   175
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   176
    # Returns the index in connectors list for the connector given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   177
    def GetConnectorIndex(self, connector):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   178
        if connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   179
            return self.Connectors.index(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   180
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   181
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   182
    # Returns if there is a connector in connectors list at the index given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   183
    def IsNullConnector(self, idx):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   184
        if idx < len(self.Connectors):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   185
            return self.Connectors[idx] == None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   186
        return False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   187
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   188
    # Delete the connector or blank from connectors list at the index given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   189
    def DeleteConnector(self, idx):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   190
        self.Connectors.pop(idx)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   191
        self.RefreshConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   192
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   193
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   194
    # Refresh the positions of the power rail connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   195
    def RefreshConnectors(self):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   196
        scaling = self.Parent.GetScaling()
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   197
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   198
            height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   199
            interval = float(height) / float(max(len(self.Connectors) - 1, 1))
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   200
            for i, connector in enumerate(self.Connectors):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   201
                if self.RealConnectors:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   202
                    position = self.Extensions[0] + int(round(self.RealConnectors[i] * height))
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   203
                else:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   204
                    position = self.Extensions[0] + int(round(i * interval))
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   205
                if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   206
                    position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   207
                if self.Type == LEFTRAIL:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   208
                    connector.SetPosition(wx.Point(self.Size[0], position))
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   209
                elif self.Type == RIGHTRAIL:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   210
                    connector.SetPosition(wx.Point(0, position))
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   211
        else:
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   212
            position = self.Extensions[0]
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   213
            for connector in self.Connectors:
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   214
                if connector:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   215
                    if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   216
                        ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   217
                    else:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   218
                        ypos = position
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   219
                    if self.Type == LEFTRAIL:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   220
                        connector.SetPosition(wx.Point(self.Size[0], ypos))
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   221
                    elif self.Type == RIGHTRAIL:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   222
                        connector.SetPosition(wx.Point(0, ypos))
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   223
                position += LD_LINE_SIZE
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   224
        self.RefreshConnected()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   225
    
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 5
diff changeset
   226
    # Refresh the position of wires connected to power rail
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   227
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   228
        for connector in self.Connectors:
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   229
            if connector:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   230
                connector.MoveConnected(exclude)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   231
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   232
    # Returns the power rail connector that starts with the point given if it exists 
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   233
    def GetConnector(self, position, name = None):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   234
        # if a name is given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   235
        if name:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   236
            # Test each connector if it exists
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   237
            for connector in self.Connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   238
                if connector and name == connector.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   239
                    return connector
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   240
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   241
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   242
                connector_pos = connector.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   243
                if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   244
                    return connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   245
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   246
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   247
    # Returns all the power rail connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   248
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   249
        return [connector for connector in self.Connectors if connector]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   250
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   251
    # Test if point given is on one of the power rail connectors
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
   252
    def TestConnector(self, pt, direction = None, exclude = True):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   253
        for connector in self.Connectors:
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
   254
            if connector and connector.TestPoint(pt, direction, exclude):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   255
                return connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   256
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   257
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   258
    # Returns the power rail type
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   259
    def SetType(self, type, connectors):
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   260
        if type != self.Type or len(self.Connectors) != len(connectors):
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   261
            # Create a connector or a blank according to 'connectors' and add it in
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   262
            # the connectors list
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   263
            self.Type = type
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   264
            self.Clean()
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   265
            self.Connectors = []
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   266
            for connector in connectors:
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   267
                self.AddConnector(connector)
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   268
            self.RefreshSize()
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   269
    
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   270
    # Returns the power rail type
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   271
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   272
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   273
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   274
    # Method called when a LeftDown event have been generated
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   275
    def OnLeftDown(self, event, dc, scaling):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   276
        self.RealConnectors = []
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   277
        height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
254
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   278
        if height > 0:
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   279
            for connector in self.Connectors:
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   280
                position = connector.GetRelPosition()
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   281
                self.RealConnectors.append(max(0., min(float(position.y - self.Extensions[0]) / float(height), 1.)))
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   282
        elif len(self.Connectors) > 1:
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   283
            self.RealConnectors = map(lambda x : x * 1 / (len(self.Connectors) - 1), xrange(len(self.Connectors)))
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   284
        else:
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   285
            self.RealConnectors = [0.5]
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   286
        Graphic_Element.OnLeftDown(self, event, dc, scaling)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   287
    
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   288
    # Method called when a LeftUp event have been generated
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   289
    def OnLeftUp(self, event, dc, scaling):
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   290
        Graphic_Element.OnLeftUp(self, event, dc, scaling)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   291
        self.RealConnectors = None
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   292
    
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   293
    # Method called when a LeftDown event have been generated
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   294
    def OnRightDown(self, event, dc, scaling):
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   295
        pos = GetScaledEventPosition(event, dc, scaling)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   296
        # Test if a connector have been handled
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
   297
        connector = self.TestConnector(pos, exclude=False)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   298
        if connector:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   299
            self.Handle = (HANDLE_CONNECTOR, connector)
381
98890d848701 Redefine cursor switching procedure in graphic viewers
laurent
parents: 379
diff changeset
   300
            wx.CallAfter(self.Parent.SetCurrentCursor, 1)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   301
            self.Selected = False
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   302
            # Initializes the last position
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   303
            self.oldPos = GetScaledEventPosition(event, dc, scaling)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   304
        else:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   305
            Graphic_Element.OnRightDown(self, event, dc, scaling)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   306
    
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   307
    # Method called when a LeftDClick event have been generated
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   308
    def OnLeftDClick(self, event, dc, scaling):
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   309
        # Edit the powerrail properties
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   310
        self.Parent.EditPowerRailContent(self)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   311
    
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   312
    # Method called when a RightUp event have been generated
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   313
    def OnRightUp(self, event, dc, scaling):
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   314
        handle_type, handle = self.Handle
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   315
        if handle_type == HANDLE_CONNECTOR:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   316
            wires = handle.GetWires()
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   317
            if len(wires) == 1:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   318
                if handle == wires[0][0].StartConnected:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   319
                    block = wires[0][0].EndConnected.GetParentBlock()
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   320
                else:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   321
                    block = wires[0][0].StartConnected.GetParentBlock()
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   322
                block.RefreshModel(False)
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   323
            Graphic_Element.OnRightUp(self, event, dc, scaling)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   324
        else:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   325
            self.Parent.PopupDefaultMenu()
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   326
    
175
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   327
    def Resize(self, x, y, width, height):
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   328
        self.Move(x, y)
254
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   329
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   330
            self.SetSize(width, height)
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   331
        else:
27abdcf9a460 Bug with PowerRail resize fixed
lbessard
parents: 253
diff changeset
   332
            self.SetSize(LD_POWERRAIL_WIDTH, height)
175
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   333
110
29b6b70e1721 Bug that makes element resizing acting strongly fixed
lbessard
parents: 96
diff changeset
   334
    # Refreshes the powerrail state according to move defined and handle selected
327
7fd5233ce5ce Adding support for contraining move to only one direction when control down
lbessard
parents: 283
diff changeset
   335
    def ProcessDragging(self, movex, movey, event, scaling):
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   336
        handle_type, handle = self.Handle
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   337
        # A connector has been handled
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   338
        if handle_type == HANDLE_CONNECTOR:
138
9c74d00ce93e Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents: 127
diff changeset
   339
            movey = max(-self.BoundingBox.y, movey)
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   340
            if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   341
                position = handle.GetRelPosition()
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   342
                movey = round(float(self.Pos.y + position.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y - position.y
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   343
            self.MoveConnector(handle, movey)
138
9c74d00ce93e Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents: 127
diff changeset
   344
            return 0, movey
110
29b6b70e1721 Bug that makes element resizing acting strongly fixed
lbessard
parents: 96
diff changeset
   345
        else:
327
7fd5233ce5ce Adding support for contraining move to only one direction when control down
lbessard
parents: 283
diff changeset
   346
            return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   347
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   348
    # Refreshes the power rail model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   349
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   350
        self.Parent.RefreshPowerRailModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   351
        # If power rail has moved and power rail is of type LEFT, refresh the model 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   352
        # of wires connected to connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   353
        if move and self.Type == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   354
            for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   355
                if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   356
                    connector.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   357
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   358
    # Draws power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   359
    def Draw(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   360
        Graphic_Element.Draw(self, dc)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   361
        dc.SetPen(wx.BLACK_PEN)
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   362
        dc.SetBrush(wx.BLACK_BRUSH)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   363
        # Draw a rectangle with the power rail size
175
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   364
        if self.Type == LEFTRAIL:
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   365
            dc.DrawRectangle(self.Pos.x + self.Size[0] - LD_POWERRAIL_WIDTH, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1)
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   366
        else:
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   367
            dc.DrawRectangle(self.Pos.x, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   368
        # Draw connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   369
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   370
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   371
                connector.Draw(dc)
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   372
        
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   373
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   374
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   375
#                         Ladder Diagram Contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   376
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   377
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   378
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   379
Class that implements the graphic representation of a contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   380
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   381
361
62570186dad4 Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents: 360
diff changeset
   382
class LD_Contact(Graphic_Element, DebugDataConsumer):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   383
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   384
    # Create a new contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   385
    def __init__(self, parent, type, name, id = None):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   386
        Graphic_Element.__init__(self, parent)
361
62570186dad4 Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents: 360
diff changeset
   387
        DebugDataConsumer.__init__(self)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   388
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   389
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   390
        self.Id = id
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   391
        self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   392
        self.Errors = {}
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   393
        # Create an input and output connector
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   394
        self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST)
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   395
        self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST)
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   396
        self.PreviousValue = False
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   397
        self.PreviousSpreading = False
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   398
        self.RefreshNameSize()
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   399
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   400
    
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   401
    def Flush(self):
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   402
        if self.Input is not None:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   403
            self.Input.Flush()
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   404
            self.Input = None
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   405
        if self.Output is not None:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   406
            self.Output.Flush()
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   407
            self.Output = None
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   408
    
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   409
    def SetValue(self, value):
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   410
        self.PreviousValue = self.Value
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   411
        self.Value = value
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   412
        if self.Value != self.PreviousValue:
368
591ba4003d74 Bug slowing Debug mode fixed
greg
parents: 361
diff changeset
   413
            if self.Visible:
591ba4003d74 Bug slowing Debug mode fixed
greg
parents: 361
diff changeset
   414
                self.Parent.UpdateRefreshRect(self.GetRedrawRect())
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   415
            self.SpreadCurrent()
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   416
    
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   417
    def SpreadCurrent(self):
253
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   418
        if self.Parent.Debug:
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   419
            if self.Value is None:
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   420
                self.Value = False
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   421
            spreading = self.Input.ReceivingCurrent()
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   422
            if self.Type == CONTACT_NORMAL:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   423
                spreading &= self.Value
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   424
            elif self.Type == CONTACT_REVERSE:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   425
                spreading &= not self.Value
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   426
            elif self.Type == CONTACT_RISING:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   427
                spreading &= self.Value and not self.PreviousValue
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   428
            elif self.Type == CONTACT_FALLING:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   429
                spreading &= self.Value and not self.PreviousValue
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   430
            else:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   431
                spreading = False
253
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   432
            if spreading and not self.PreviousSpreading:
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   433
                self.Output.SpreadCurrent(True)
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   434
            elif not spreading and self.PreviousSpreading:
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   435
                self.Output.SpreadCurrent(False)
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   436
            self.PreviousSpreading = spreading
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   437
    
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   438
    # Make a clone of this LD_Contact
162
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
   439
    def Clone(self, parent, id = None, pos = None):
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
   440
        contact = LD_Contact(parent, self.Type, self.Name, id)
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   441
        contact.SetSize(self.Size[0], self.Size[1])
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   442
        if pos is not None:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   443
            contact.SetPosition(pos.x, pos.y)
283
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   444
        else:
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   445
            contact.SetPosition(self.Pos.x, self.Pos.y)
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   446
        contact.Input = self.Input.Clone(contact)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   447
        contact.Output = self.Output.Clone(contact)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   448
        return contact
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   449
    
283
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   450
    def GetConnectorTranslation(self, element):
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   451
        return {self.Input : element.Input, self.Output : element.Output}
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   452
    
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   453
    # Returns the RedrawRect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   454
    def GetRedrawRect(self, movex = 0, movey = 0):
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   455
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   456
        rect = rect.Union(self.Input.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   457
        rect = rect.Union(self.Output.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   458
        if movex != 0 or movey != 0:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   459
            if self.Input.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   460
                rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   461
            if self.Output.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   462
                rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   463
        return rect
180
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   464
327
7fd5233ce5ce Adding support for contraining move to only one direction when control down
lbessard
parents: 283
diff changeset
   465
    def ProcessDragging(self, movex, movey, event, scaling):
360
072f9f830659 Bug while dragging contact fixed
greg
parents: 327
diff changeset
   466
        return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac = 2)
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   467
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   468
    # Forbids to change the contact size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   469
    def SetSize(self, width, height):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   470
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   471
            Graphic_Element.SetSize(self, width, height)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   472
            self.RefreshConnectors()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   473
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   474
    # Delete this contact by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   475
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   476
        self.Parent.DeleteContact(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   477
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   478
    # Unconnect input and output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   479
    def Clean(self):
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   480
        self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   481
        self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   482
    
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   483
    # Refresh the size of text for name
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   484
    def RefreshNameSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   485
        if self.Name != "":
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   486
            self.NameSize = self.Parent.GetTextExtent(self.Name)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   487
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   488
            self.NameSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   489
    
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   490
    # Refresh the size of text for type
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   491
    def RefreshTypeSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   492
        typetext = ""
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   493
        if self.Type == CONTACT_REVERSE:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   494
            typetext = "/"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   495
        elif self.Type == CONTACT_RISING:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   496
            typetext = "P"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   497
        elif self.Type == CONTACT_FALLING:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   498
            typetext = "N"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   499
        if typetext != "":
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   500
            self.TypeSize = self.Parent.GetTextExtent(typetext)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   501
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   502
            self.TypeSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   503
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   504
    # Refresh the contact bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   505
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   506
        # Calculate the size of the name outside the contact
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   507
        text_width, text_height = self.Parent.GetTextExtent(self.Name)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   508
        # Calculate the bounding box size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   509
        if self.Name != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   510
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   511
            bbx_width = max(self.Size[0], text_width)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   512
            bbx_y = self.Pos.y - (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   513
            bbx_height = self.Size[1] + (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   514
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   515
            bbx_x = self.Pos.x
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   516
            bbx_width = self.Size[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   517
            bbx_y = self.Pos.y
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   518
            bbx_height = self.Size[1]
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   519
        self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   520
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   521
    # Returns the block minimum size
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   522
    def GetMinSize(self):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   523
        return LD_ELEMENT_SIZE
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   524
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   525
    # Refresh the position of wire connected to contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   526
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   527
        self.Input.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   528
        self.Output.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   529
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   530
    # Returns the contact connector that starts with the point given if it exists 
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   531
    def GetConnector(self, position, name = None):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   532
        # if a name is given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   533
        if name:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   534
            # Test input and output connector
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   535
            if name == self.Input.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   536
                return self.Input
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   537
            if name == self.Output.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   538
                return self.Output
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   539
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   540
        input_pos = self.Input.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   541
        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   542
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   543
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   544
        output_pos = self.Output.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   545
        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   546
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   547
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   548
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   549
    # Returns input and output contact connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   550
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   551
        return {"input":self.Input,"output":self.Output}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   552
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   553
    # Test if point given is on contact input or output connector
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
   554
    def TestConnector(self, pt, direction = None, exclude=True):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   555
        # Test input connector
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
   556
        if self.Input.TestPoint(pt, direction, exclude):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   557
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   558
        # Test output connector
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
   559
        if self.Output.TestPoint(pt, direction, exclude):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   560
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   561
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   562
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   563
    # Refresh the positions of the block connectors
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   564
    def RefreshConnectors(self):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   565
        scaling = self.Parent.GetScaling()
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   566
        position = self.Size[1] / 2 + 1
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   567
        if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   568
            position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   569
        self.Input.SetPosition(wx.Point(0, position))
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   570
        self.Output.SetPosition(wx.Point(self.Size[0], position))
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   571
        self.RefreshConnected()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   572
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   573
    # Changes the contact name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   574
    def SetName(self, name):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   575
        self.Name = name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   576
        self.RefreshNameSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   577
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   578
    # Returns the contact name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   579
    def GetName(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   580
        return self.Name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   581
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   582
    # Changes the contact type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   583
    def SetType(self, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   584
        self.Type = type
50
4610aafc884e Bugs fixed
lbessard
parents: 42
diff changeset
   585
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   586
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   587
    # Returns the contact type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   588
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   589
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   590
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   591
    # Method called when a LeftDClick event have been generated
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   592
    def OnLeftDClick(self, event, dc, scaling):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   593
        # Edit the contact properties
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   594
        self.Parent.EditContactContent(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   595
    
127
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   596
    # Method called when a RightUp event have been generated
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   597
    def OnRightUp(self, event, dc, scaling):
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   598
        # Popup the default menu
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   599
        self.Parent.PopupDefaultMenu()
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   600
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   601
    # Refreshes the contact model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   602
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   603
        self.Parent.RefreshContactModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   604
        # If contact has moved, refresh the model of wires connected to output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   605
        if move:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   606
            self.Output.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   607
    
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   608
    # Draws the highlightment of this element if it is highlighted
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   609
    def DrawHighlightment(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   610
        dc.SetPen(wx.Pen(HIGHLIGHTCOLOR))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   611
        dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   612
        dc.SetLogicalFunction(wx.AND)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   613
        # Draw two rectangles for representing the contact
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   614
        dc.DrawRectangle(self.Pos.x - 2, self.Pos.y - 2, 6, self.Size[1] + 5)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   615
        dc.DrawRectangle(self.Pos.x + self.Size[0] - 3, self.Pos.y - 2, 6, self.Size[1] + 5)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   616
        dc.SetLogicalFunction(wx.COPY)
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   617
    
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   618
    def AddError(self, infos, start, end):
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   619
        self.Errors[infos[0]] = (start[1], end[1])
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   620
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   621
    # Draws contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   622
    def Draw(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   623
        Graphic_Element.Draw(self, dc)
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   624
        if self.Value is not None:            
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   625
            if self.Type == CONTACT_NORMAL and self.Value:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   626
                dc.SetPen(wx.GREEN_PEN)
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   627
            elif self.Type == CONTACT_REVERSE and not self.Value:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   628
                dc.SetPen(wx.GREEN_PEN)
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   629
            elif self.Type == CONTACT_RISING and self.Value and not self.PreviousValue:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   630
                dc.SetPen(wx.GREEN_PEN)
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   631
            elif self.Type == CONTACT_FALLING and self.Value and not self.PreviousValue:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   632
                dc.SetPen(wx.GREEN_PEN)
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   633
            else:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   634
                dc.SetPen(wx.BLACK_PEN)
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   635
        else:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   636
            dc.SetPen(wx.BLACK_PEN)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   637
        dc.SetBrush(wx.BLACK_BRUSH)
213
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   638
        
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   639
        # Compiling contact type modifier symbol
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   640
        typetext = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   641
        if self.Type == CONTACT_REVERSE:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   642
            typetext = "/"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   643
        elif self.Type == CONTACT_RISING:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   644
            typetext = "P"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   645
        elif self.Type == CONTACT_FALLING:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   646
            typetext = "N"
213
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   647
        
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   648
        if getattr(dc, "printing", False):
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   649
            name_size = dc.GetTextExtent(self.Name)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   650
            if typetext != "":
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   651
                type_size = dc.GetTextExtent(typetext)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   652
        else:
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   653
            name_size = self.NameSize
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   654
            if typetext != "":
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   655
                type_size = self.TypeSize
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   656
        
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   657
        # Draw two rectangles for representing the contact
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   658
        dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   659
        dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   660
        # Draw contact name
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   661
        name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   662
                    self.Pos.y - (name_size[1] + 2))
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   663
        dc.DrawText(self.Name, name_pos[0], name_pos[1])
213
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   664
        # Draw the modifier symbol in the middle of contact
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   665
        if typetext != "":
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   666
            type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1,
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   667
                        self.Pos.y + (self.Size[1] - type_size[1]) / 2)
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   668
            dc.DrawText(typetext, type_pos[0], type_pos[1])
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   669
        # Draw input and output connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   670
        self.Input.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   671
        self.Output.Draw(dc)
379
e4c26ee9c998 Code rewritten, replacing all list containing tests by dict key defining tests
laurent
parents: 368
diff changeset
   672
        if self.Errors.has_key("reference"):
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   673
            HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1])
379
e4c26ee9c998 Code rewritten, replacing all list containing tests by dict key defining tests
laurent
parents: 368
diff changeset
   674
        if typetext != "" and (self.Errors.has_key("negated") or self.Errors.has_key("rising") or self.Errors.has_key("falling")):
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   675
            HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1])
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   676
        
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   677
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   678
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   679
#                         Ladder Diagram Coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   680
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   681
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   682
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   683
Class that implements the graphic representation of a coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   684
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   685
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   686
class LD_Coil(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   687
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   688
    # Create a new coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   689
    def __init__(self, parent, type, name, id = None):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   690
        Graphic_Element.__init__(self, parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   691
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   692
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   693
        self.Id = id
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   694
        self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   695
        self.Errors = {}
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   696
        # Create an input and output connector
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   697
        self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST)
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   698
        self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST)
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   699
        self.Value = None
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   700
        self.PreviousValue = False
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   701
        self.RefreshNameSize()
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   702
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   703
        
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   704
    def Flush(self):
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   705
        if self.Input is not None:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   706
            self.Input.Flush()
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   707
            self.Input = None
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   708
        if self.Output is not None:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   709
            self.Output.Flush()
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   710
            self.Output = None
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   711
    
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   712
    def SpreadCurrent(self):
253
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   713
        if self.Parent.Debug:
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   714
            self.PreviousValue = self.Value
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   715
            self.Value = self.Input.ReceivingCurrent()
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   716
            if self.Value and not self.PreviousValue:
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   717
                self.Output.SpreadCurrent(True)
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   718
            elif not self.Value and self.PreviousValue:
d9391572655f Adding support for Debugging in PLCOpenEditor
lbessard
parents: 249
diff changeset
   719
                self.Output.SpreadCurrent(False)
368
591ba4003d74 Bug slowing Debug mode fixed
greg
parents: 361
diff changeset
   720
            if self.Value != self.PreviousValue and self.Visible:
361
62570186dad4 Adding support for synchronize refreshing with tick and limit it to a defined period
greg
parents: 360
diff changeset
   721
                self.Parent.UpdateRefreshRect(self.GetRedrawRect())
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   722
    
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   723
    # Make a clone of this LD_Coil
162
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
   724
    def Clone(self, parent, id = None, pos = None):
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
   725
        coil = LD_Coil(parent, self.Type, self.Name, id)
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   726
        coil.SetSize(self.Size[0], self.Size[1])
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   727
        if pos is not None:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   728
            coil.SetPosition(pos.x, pos.y)
283
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   729
        else:
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   730
            coil.SetPosition(self.Pos.x, self.Pos.y)
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   731
        coil.Input = self.Input.Clone(coil)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   732
        coil.Output = self.Output.Clone(coil)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   733
        return coil
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   734
    
283
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   735
    def GetConnectorTranslation(self, element):
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   736
        return {self.Input : element.Input, self.Output : element.Output}
c4199b88cf60 Adding support for copying a group of elements
lbessard
parents: 269
diff changeset
   737
    
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   738
    # Returns the RedrawRect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   739
    def GetRedrawRect(self, movex = 0, movey = 0):
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   740
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   741
        rect = rect.Union(self.Input.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   742
        rect = rect.Union(self.Output.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   743
        if movex != 0 or movey != 0:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   744
            if self.Input.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   745
                rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   746
            if self.Output.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   747
                rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   748
        return rect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   749
    
327
7fd5233ce5ce Adding support for contraining move to only one direction when control down
lbessard
parents: 283
diff changeset
   750
    def ProcessDragging(self, movex, movey, event, scaling):
7fd5233ce5ce Adding support for contraining move to only one direction when control down
lbessard
parents: 283
diff changeset
   751
        return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac = 2)    
180
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   752
    
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   753
    # Forbids to change the Coil size
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   754
    def SetSize(self, width, height):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   755
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   756
            Graphic_Element.SetSize(self, width, height)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   757
            self.RefreshConnectors()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   758
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   759
    # Delete this coil by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   760
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   761
        self.Parent.DeleteCoil(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   762
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   763
    # Unconnect input and output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   764
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   765
        self.Input.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   766
        self.Output.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   767
                
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   768
    # Refresh the size of text for name
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   769
    def RefreshNameSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   770
        if self.Name != "":
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   771
            self.NameSize = self.Parent.GetTextExtent(self.Name)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   772
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   773
            self.NameSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   774
    
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   775
    # Refresh the size of text for type
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   776
    def RefreshTypeSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   777
        typetext = ""
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   778
        if self.Type == COIL_REVERSE:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   779
            typetext = "/"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   780
        elif self.Type == COIL_SET:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   781
            typetext = "S"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   782
        elif self.Type == COIL_RESET:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   783
            typetext = "R"
269
34eff05909b0 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 254
diff changeset
   784
        elif self.Type == COIL_RISING:
34eff05909b0 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 254
diff changeset
   785
            typetext = "P"
34eff05909b0 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 254
diff changeset
   786
        elif self.Type == COIL_FALLING:
34eff05909b0 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 254
diff changeset
   787
            typetext = "N"
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   788
        if typetext != "":
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   789
            self.TypeSize = self.Parent.GetTextExtent(typetext)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   790
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   791
            self.TypeSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   792
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   793
    # Refresh the coil bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   794
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   795
        # Calculate the size of the name outside the coil
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   796
        text_width, text_height = self.Parent.GetTextExtent(self.Name)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   797
        # Calculate the bounding box size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   798
        if self.Name != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   799
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   800
            bbx_width = max(self.Size[0], text_width)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   801
            bbx_y = self.Pos.y - (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   802
            bbx_height = self.Size[1] + (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   803
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   804
            bbx_x = self.Pos.x
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   805
            bbx_width = self.Size[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   806
            bbx_y = self.Pos.y
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   807
            bbx_height = self.Size[1]
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   808
        self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   809
        
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   810
    # Returns the block minimum size
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   811
    def GetMinSize(self):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   812
        return LD_ELEMENT_SIZE
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   813
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   814
    # Refresh the position of wire connected to coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   815
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   816
        self.Input.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   817
        self.Output.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   818
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   819
    # Returns the coil connector that starts with the point given if it exists 
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   820
    def GetConnector(self, position, name = None):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   821
        # if a name is given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   822
        if name:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   823
            # Test input and output connector
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   824
            if self.Input and name == self.Input.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   825
                return self.Input
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   826
            if self.Output and name == self.Output.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   827
                return self.Output
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   828
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   829
        input_pos = self.Input.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   830
        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   831
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   832
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   833
        output_pos = self.Output.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   834
        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   835
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   836
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   837
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   838
    # Returns input and output coil connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   839
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   840
        return {"input":self.Input,"output":self.Output}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   841
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   842
    # Test if point given is on coil input or output connector
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
   843
    def TestConnector(self, pt, direction = None, exclude=True):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   844
        # Test input connector
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
   845
        if self.Input.TestPoint(pt, direction, exclude):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   846
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   847
        # Test output connector
243
c5da8b706cde Adding support for allowing connections only between an input and an output connector
lbessard
parents: 237
diff changeset
   848
        if self.Output.TestPoint(pt, direction, exclude):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   849
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   850
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   851
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   852
    # Refresh the positions of the block connectors
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   853
    def RefreshConnectors(self):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   854
        scaling = self.Parent.GetScaling()
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   855
        position = self.Size[1] / 2 + 1
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   856
        if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   857
            position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   858
        self.Input.SetPosition(wx.Point(0, position))
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   859
        self.Output.SetPosition(wx.Point(self.Size[0], position))
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   860
        self.RefreshConnected()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   861
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   862
    # Changes the coil name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   863
    def SetName(self, name):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   864
        self.Name = name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   865
        self.RefreshNameSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   866
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   867
    # Returns the coil name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   868
    def GetName(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   869
        return self.Name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   870
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   871
    # Changes the coil type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   872
    def SetType(self, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   873
        self.Type = type
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   874
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   875
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   876
    # Returns the coil type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   877
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   878
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   879
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   880
    # Method called when a LeftDClick event have been generated
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   881
    def OnLeftDClick(self, event, dc, scaling):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   882
        # Edit the coil properties
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   883
        self.Parent.EditCoilContent(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   884
    
127
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   885
    # Method called when a RightUp event have been generated
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   886
    def OnRightUp(self, event, dc, scaling):
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   887
        # Popup the default menu
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   888
        self.Parent.PopupDefaultMenu()
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   889
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   890
    # Refreshes the coil model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   891
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   892
        self.Parent.RefreshCoilModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   893
        # If coil has moved, refresh the model of wires connected to output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   894
        if move:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   895
            self.Output.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   896
    
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   897
    # Draws the highlightment of this element if it is highlighted
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   898
    def DrawHighlightment(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   899
        dc.SetPen(wx.Pen(HIGHLIGHTCOLOR, 6, wx.SOLID))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   900
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   901
        dc.SetLogicalFunction(wx.AND)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   902
        # Draw a two circle arcs for representing the coil
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   903
        dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, 135, 225)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   904
        dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, -45, 45)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   905
        dc.SetLogicalFunction(wx.COPY)
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   906
    
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   907
    def AddError(self, infos, start, end):
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   908
        self.Errors[infos[0]] = (start[1], end[1])
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   909
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   910
    # Draws coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   911
    def Draw(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   912
        Graphic_Element.Draw(self, dc)
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   913
        if self.Value is not None and self.Value:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   914
            dc.SetPen(wx.Pen(wx.GREEN, 2, wx.SOLID))
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   915
        else:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   916
            dc.SetPen(wx.Pen(wx.BLACK, 2, wx.SOLID))
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   917
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
213
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   918
        
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   919
        # Compiling coil type modifier symbol 
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   920
        typetext = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   921
        if self.Type == COIL_REVERSE:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   922
            typetext = "/"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   923
        elif self.Type == COIL_SET:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   924
            typetext = "S"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   925
        elif self.Type == COIL_RESET:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   926
            typetext = "R"
269
34eff05909b0 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 254
diff changeset
   927
        elif self.Type == COIL_RISING:
34eff05909b0 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 254
diff changeset
   928
            typetext = "P"
34eff05909b0 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 254
diff changeset
   929
        elif self.Type == COIL_FALLING:
34eff05909b0 Adding support for EN/ENO variables (temporarily disabled, waiting for matiec support)
lbessard
parents: 254
diff changeset
   930
            typetext = "N"
213
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   931
        
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   932
        if getattr(dc, "printing", False) and not isinstance(dc, wx.PostScriptDC):
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   933
            # Draw an clipped ellipse for representing the coil
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   934
            clipping_box = dc.GetClippingBox()
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   935
            dc.SetClippingRegion(self.Pos.x - 1, self.Pos.y, self.Size[0] + 2, self.Size[1] + 1)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   936
            dc.DrawEllipse(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   937
            dc.DestroyClippingRegion()
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   938
            if clipping_box != (0, 0, 0, 0):
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   939
                dc.SetClippingRegion(*clipping_box)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   940
            name_size = dc.GetTextExtent(self.Name)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   941
            if typetext != "":
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   942
                type_size = dc.GetTextExtent(typetext)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   943
        else:
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   944
            # Draw a two ellipse arcs for representing the coil
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   945
            dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, 135, 225)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   946
            dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, -45, 45)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   947
            # Draw a point to avoid hole in left arc
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   948
            if not getattr(dc, "printing", False):
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   949
                if self.Value is not None and self.Value:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   950
                    dc.SetPen(wx.GREEN_PEN)
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   951
                else:
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 243
diff changeset
   952
                    dc.SetPen(wx.BLACK_PEN)
213
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   953
                dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   954
            name_size = self.NameSize
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   955
            if typetext != "":
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   956
                type_size = self.TypeSize
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   957
            
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   958
        # Draw coil name
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   959
        name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   960
                    self.Pos.y - (name_size[1] + 2))
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   961
        dc.DrawText(self.Name, name_pos[0], name_pos[1])
213
4931959ea256 Adding support for printing graphical languages
lbessard
parents: 180
diff changeset
   962
        # Draw the modifier symbol in the middle of coil
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   963
        if typetext != "":
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   964
            type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1,
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   965
                        self.Pos.y + (self.Size[1] - type_size[1]) / 2)
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   966
            dc.DrawText(typetext, type_pos[0], type_pos[1])
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   967
        # Draw input and output connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   968
        self.Input.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   969
        self.Output.Draw(dc)
379
e4c26ee9c998 Code rewritten, replacing all list containing tests by dict key defining tests
laurent
parents: 368
diff changeset
   970
        if self.Errors.has_key("reference"):
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   971
            HighlightErrorZone(dc, name_pos[0], name_pos[1], name_size[0], name_size[1])
379
e4c26ee9c998 Code rewritten, replacing all list containing tests by dict key defining tests
laurent
parents: 368
diff changeset
   972
        if typetext != "" and (self.Errors.has_key("negated") or self.Errors.has_key("rising") or self.Errors.has_key("falling")):
231
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   973
            HighlightErrorZone(dc, type_pos[0], type_pos[1], type_size[0], type_size[1])
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   974
            
fc2d6cbb8b39 Adding support for highlighing compiling errors from matiec
lbessard
parents: 213
diff changeset
   975