graphics/LD_Objects.py
author etisserant
Fri, 29 Feb 2008 11:01:03 +0100
changeset 183 99d77995b4ea
parent 180 3b0d3ea35ee5
child 213 4931959ea256
permissions -rw-r--r--
.ico icons work with either windows or linux
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
        
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    52
    # Destructor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    53
    def __del__(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    54
        self.Connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    55
    
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    56
    # Make a clone of this LD_PowerRail
162
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
    57
    def Clone(self, parent, id = None, pos = None):
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
    58
        powerrail = LD_PowerRail(parent, self.Type, id)
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    59
        powerrail.SetSize(self.Size[0], self.Size[1])
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    60
        if pos is not None:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    61
            powerrail.SetPosition(pos.x, pos.y)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    62
        powerrail.Connectors = []
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    63
        for connector in self.Connectors:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    64
            if connector is not None:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    65
                powerrail.Connectors.append(connector.Clone(powerrail))
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    66
            else:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    67
                powerrail.Connectors.append(None)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    68
        return powerrail
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
    69
    
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    70
    # Returns the RedrawRect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    71
    def GetRedrawRect(self, movex = 0, movey = 0):
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    72
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    73
        for connector in self.Connectors:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    74
            if connector is not None:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    75
                rect = rect.Union(connector.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    76
        if movex != 0 or movey != 0:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    77
            for connector in self.Connectors:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    78
                if connector is not None and connector.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    79
                    rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    80
        return rect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
    81
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    82
    # Forbids to change the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    83
    def SetSize(self, width, height):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    84
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    85
            Graphic_Element.SetSize(self, width, height)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    86
            self.RefreshConnectors()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    87
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    88
    # Forbids to select a power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    89
    def HitTest(self, pt):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    90
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    91
            return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    92
        return False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    93
    
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    94
    # Forbids to select a power rail
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    95
    def IsInSelection(self, rect):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    96
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
    97
            return Graphic_Element.IsInSelection(self, rect)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    98
        return False
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    99
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   100
    # Deletes this power rail by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   101
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   102
        self.Parent.DeletePowerRail(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   103
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   104
    # Unconnect all connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   105
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   106
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   107
            if connector:
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   108
                connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   109
                
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   110
    # Refresh the power rail bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   111
    def RefreshBoundingBox(self):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   112
        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
   113
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   114
    # Refresh the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   115
    def RefreshSize(self):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   116
        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
   117
        self.RefreshBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   118
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   119
    # Returns the block minimum size
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   120
    def GetMinSize(self):
96
d178cfa9e77f Bug on LDPowerRail resize fixed
lbessard
parents: 80
diff changeset
   121
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   122
            return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1]
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   123
        else:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   124
            return LD_POWERRAIL_WIDTH, LD_LINE_SIZE * len(self.Connectors)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   125
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   126
    # 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
   127
    def AddConnector(self, connector = True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   128
        self.InsertConnector(len(self.Connectors), connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   129
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   130
    # 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
   131
    def InsertConnector(self, idx, connector = True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   132
        if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   133
            if self.Type == LEFTRAIL:
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   134
                connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   135
            elif self.Type == RIGHTRAIL:
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   136
                connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   137
            self.Connectors.insert(idx, connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   138
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   139
            self.Connectors.insert(idx, None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   140
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   141
        self.RefreshConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   142
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   143
    # Moves the divergence connector given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   144
    def MoveConnector(self, connector, movey):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   145
        position = connector.GetRelPosition()
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   146
        connector.SetPosition(wx.Point(position.x, position.y + movey))
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   147
        miny = self.Size[1]
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   148
        maxy = 0
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   149
        for connect in self.Connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   150
            connect_pos = connect.GetRelPosition()
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   151
            miny = min(miny, connect_pos.y - self.Extensions[0])
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   152
            maxy = max(maxy, connect_pos.y - self.Extensions[0])
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   153
        min_pos = self.Pos.y + miny
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   154
        self.Pos.y = min(min_pos, self.Pos.y)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   155
        if min_pos == self.Pos.y:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   156
            for connect in self.Connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   157
                connect_pos = connect.GetRelPosition()
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   158
                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
   159
        self.Connectors.sort(lambda x, y: x.Pos.y.__cmp__(y.Pos.y))
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   160
        maxy = 0
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   161
        for connect in self.Connectors:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   162
            connect_pos = connect.GetRelPosition()
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   163
            maxy = max(maxy, connect_pos.y)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   164
        self.Size[1] = max(maxy + self.Extensions[1], self.Size[1])
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   165
        connector.MoveConnected()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   166
        self.RefreshBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   167
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   168
    # Returns the index in connectors list for the connector given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   169
    def GetConnectorIndex(self, connector):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   170
        if connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   171
            return self.Connectors.index(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   172
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   173
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   174
    # Returns if there is a connector in connectors list at the index given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   175
    def IsNullConnector(self, idx):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   176
        if idx < len(self.Connectors):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   177
            return self.Connectors[idx] == None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   178
        return False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   179
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   180
    # Delete the connector or blank from connectors list at the index given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   181
    def DeleteConnector(self, idx):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   182
        self.Connectors.pop(idx)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   183
        self.RefreshConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   184
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   185
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   186
    # Refresh the positions of the power rail connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   187
    def RefreshConnectors(self):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   188
        scaling = self.Parent.GetScaling()
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   189
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   190
            height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   191
            interval = float(height) / float(max(len(self.Connectors) - 1, 1))
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   192
            for i, connector in enumerate(self.Connectors):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   193
                if self.RealConnectors:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   194
                    position = self.Extensions[0] + int(round(self.RealConnectors[i] * height))
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   195
                else:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   196
                    position = self.Extensions[0] + int(round(i * interval))
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   197
                if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   198
                    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
   199
                if self.Type == LEFTRAIL:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   200
                    connector.SetPosition(wx.Point(self.Size[0], position))
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   201
                elif self.Type == RIGHTRAIL:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   202
                    connector.SetPosition(wx.Point(0, position))
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   203
        else:
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   204
            position = self.Extensions[0]
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   205
            for connector in self.Connectors:
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   206
                if connector:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   207
                    if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   208
                        ypos = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   209
                    else:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   210
                        ypos = position
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   211
                    if self.Type == LEFTRAIL:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   212
                        connector.SetPosition(wx.Point(self.Size[0], ypos))
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   213
                    elif self.Type == RIGHTRAIL:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   214
                        connector.SetPosition(wx.Point(0, ypos))
71
0578bc212c20 Adding Dialog for Step in free drawing
lbessard
parents: 64
diff changeset
   215
                position += LD_LINE_SIZE
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   216
        self.RefreshConnected()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   217
    
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 5
diff changeset
   218
    # Refresh the position of wires connected to power rail
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   219
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   220
        for connector in self.Connectors:
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   221
            if connector:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   222
                connector.MoveConnected(exclude)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   223
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   224
    # Returns the power rail connector that starts with the point given if it exists 
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   225
    def GetConnector(self, position, name = None):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   226
        # if a name is given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   227
        if name:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   228
            # Test each connector if it exists
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   229
            for connector in self.Connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   230
                if connector and name == connector.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   231
                    return connector
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   232
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   233
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   234
                connector_pos = connector.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   235
                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
   236
                    return connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   237
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   238
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   239
    # Returns all the power rail connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   240
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   241
        return [connector for connector in self.Connectors if connector]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   242
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   243
    # Test if point given is on one of the power rail connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   244
    def TestConnector(self, pt, exclude=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   245
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   246
            if connector and connector.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   247
                return connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   248
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   249
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   250
    # Returns the power rail type
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   251
    def SetType(self, type, connectors):
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   252
        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
   253
            # 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
   254
            # the connectors list
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   255
            self.Type = type
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   256
            self.Clean()
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   257
            self.Connectors = []
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   258
            for connector in connectors:
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   259
                self.AddConnector(connector)
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   260
            self.RefreshSize()
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   261
    
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   262
    # Returns the power rail type
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   263
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   264
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   265
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   266
    # Method called when a LeftDown event have been generated
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   267
    def OnLeftDown(self, event, dc, scaling):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   268
        self.RealConnectors = []
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   269
        height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   270
        for connector in self.Connectors:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   271
            position = connector.GetRelPosition()
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   272
            self.RealConnectors.append(float(position.y - self.Extensions[0])/float(max(1, height)))
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   273
        Graphic_Element.OnLeftDown(self, event, dc, scaling)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   274
    
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   275
    # Method called when a LeftUp event have been generated
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   276
    def OnLeftUp(self, event, dc, scaling):
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   277
        Graphic_Element.OnLeftUp(self, event, dc, scaling)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   278
        self.RealConnectors = None
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   279
    
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   280
    # Method called when a LeftDown event have been generated
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   281
    def OnRightDown(self, event, dc, scaling):
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   282
        pos = GetScaledEventPosition(event, dc, scaling)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   283
        # Test if a connector have been handled
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   284
        connector = self.TestConnector(pos, False)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   285
        if connector:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   286
            self.Handle = (HANDLE_CONNECTOR, connector)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   287
            self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND))
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   288
            self.Selected = False
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   289
            # Initializes the last position
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   290
            self.oldPos = GetScaledEventPosition(event, dc, scaling)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   291
        else:
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   292
            Graphic_Element.OnRightDown(self, event, dc, scaling)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   293
    
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   294
    # Method called when a LeftDClick event have been generated
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   295
    def OnLeftDClick(self, event, dc, scaling):
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   296
        # Edit the powerrail properties
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   297
        self.Parent.EditPowerRailContent(self)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   298
    
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   299
    # Method called when a RightUp event have been generated
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   300
    def OnRightUp(self, event, dc, scaling):
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   301
        handle_type, handle = self.Handle
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   302
        if handle_type == HANDLE_CONNECTOR:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   303
            wires = handle.GetWires()
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   304
            if len(wires) == 1:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   305
                if handle == wires[0][0].StartConnected:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   306
                    block = wires[0][0].EndConnected.GetParentBlock()
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   307
                else:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   308
                    block = wires[0][0].StartConnected.GetParentBlock()
c798a68c5560 Lots of bugs fixed
lbessard
parents: 71
diff changeset
   309
                block.RefreshModel(False)
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   310
            Graphic_Element.OnRightUp(self, event, dc, scaling)
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   311
        else:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   312
            self.Parent.PopupDefaultMenu()
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   313
    
175
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   314
    def Resize(self, x, y, width, height):
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   315
        self.Move(x, y)
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   316
        self.SetSize(LD_POWERRAIL_WIDTH, height)
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   317
110
29b6b70e1721 Bug that makes element resizing acting strongly fixed
lbessard
parents: 96
diff changeset
   318
    # Refreshes the powerrail state according to move defined and handle selected
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   319
    def ProcessDragging(self, movex, movey, centered, scaling):
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   320
        handle_type, handle = self.Handle
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   321
        # A connector has been handled
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   322
        if handle_type == HANDLE_CONNECTOR:
138
9c74d00ce93e Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents: 127
diff changeset
   323
            movey = max(-self.BoundingBox.y, movey)
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   324
            if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   325
                position = handle.GetRelPosition()
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   326
                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
   327
            self.MoveConnector(handle, movey)
138
9c74d00ce93e Last bugs on block and wire moving, resizing with cursor fixed
lbessard
parents: 127
diff changeset
   328
            return 0, movey
110
29b6b70e1721 Bug that makes element resizing acting strongly fixed
lbessard
parents: 96
diff changeset
   329
        else:
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   330
            return Graphic_Element.ProcessDragging(self, movex, movey, centered, scaling)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   331
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   332
    # Refreshes the power rail model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   333
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   334
        self.Parent.RefreshPowerRailModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   335
        # 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
   336
        # of wires connected to connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   337
        if move and self.Type == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   338
            for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   339
                if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   340
                    connector.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   341
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   342
    # Draws power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   343
    def Draw(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   344
        Graphic_Element.Draw(self, dc)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   345
        dc.SetPen(wx.BLACK_PEN)
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   346
        dc.SetBrush(wx.BLACK_BRUSH)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   347
        # Draw a rectangle with the power rail size
175
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   348
        if self.Type == LEFTRAIL:
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   349
            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
   350
        else:
cc78572dfbbc Some minor SFC/LD drawing enhancements
etisserant
parents: 165
diff changeset
   351
            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
   352
        # Draw connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   353
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   354
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   355
                connector.Draw(dc)
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   356
        
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   357
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   358
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   359
#                         Ladder Diagram Contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   360
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   361
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   362
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   363
Class that implements the graphic representation of a contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   364
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   365
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   366
class LD_Contact(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   367
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   368
    # Create a new contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   369
    def __init__(self, parent, type, name, id = None):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   370
        Graphic_Element.__init__(self, parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   371
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   372
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   373
        self.Id = id
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   374
        self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   375
        # Create an input and output connector
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   376
        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
   377
        self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   378
        self.RefreshNameSize()
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   379
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   380
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   381
    # Destructor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   382
    def __del__(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   383
        self.Input = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   384
        self.Output = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   385
    
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   386
    # Make a clone of this LD_Contact
162
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
   387
    def Clone(self, parent, id = None, pos = None):
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
   388
        contact = LD_Contact(parent, self.Type, self.Name, id)
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   389
        contact.SetSize(self.Size[0], self.Size[1])
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   390
        if pos is not None:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   391
            contact.SetPosition(pos.x, pos.y)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   392
        contact.Input = self.Input.Clone(contact)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   393
        contact.Output = self.Output.Clone(contact)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   394
        return contact
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   395
    
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   396
    # Returns the RedrawRect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   397
    def GetRedrawRect(self, movex = 0, movey = 0):
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   398
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   399
        rect = rect.Union(self.Input.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   400
        rect = rect.Union(self.Output.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   401
        if movex != 0 or movey != 0:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   402
            if self.Input.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   403
                rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   404
            if self.Output.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   405
                rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   406
        return rect
180
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   407
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   408
    def ProcessDragging(self, movex, movey, centered, scaling):
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   409
        return Graphic_Element.ProcessDragging(self, movex, movey, centered, scaling, height_fac = 2)
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   410
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   411
    # Forbids to change the contact size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   412
    def SetSize(self, width, height):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   413
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   414
            Graphic_Element.SetSize(self, width, height)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   415
            self.RefreshConnectors()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   416
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   417
    # Delete this contact by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   418
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   419
        self.Parent.DeleteContact(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   420
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   421
    # Unconnect input and output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   422
    def Clean(self):
61
dc7142ae9438 Adding possibility to change Type and Pin number of power rails
lbessard
parents: 58
diff changeset
   423
        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
   424
        self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   425
    
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   426
    # Refresh the size of text for name
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   427
    def RefreshNameSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   428
        if self.Name != "":
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   429
            self.NameSize = self.Parent.GetTextExtent(self.Name)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   430
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   431
            self.NameSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   432
    
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   433
    # Refresh the size of text for type
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   434
    def RefreshTypeSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   435
        typetext = ""
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   436
        if self.Type == CONTACT_REVERSE:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   437
            typetext = "/"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   438
        elif self.Type == CONTACT_RISING:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   439
            typetext = "P"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   440
        elif self.Type == CONTACT_FALLING:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   441
            typetext = "N"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   442
        if typetext != "":
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   443
            self.TypeSize = self.Parent.GetTextExtent(typetext)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   444
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   445
            self.TypeSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   446
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   447
    # Refresh the contact bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   448
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   449
        # Calculate the size of the name outside the contact
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   450
        text_width, text_height = self.Parent.GetTextExtent(self.Name)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   451
        # Calculate the bounding box size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   452
        if self.Name != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   453
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   454
            bbx_width = max(self.Size[0], text_width)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   455
            bbx_y = self.Pos.y - (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   456
            bbx_height = self.Size[1] + (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   457
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   458
            bbx_x = self.Pos.x
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   459
            bbx_width = self.Size[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   460
            bbx_y = self.Pos.y
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   461
            bbx_height = self.Size[1]
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   462
        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
   463
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   464
    # Returns the block minimum size
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   465
    def GetMinSize(self):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   466
        return LD_ELEMENT_SIZE
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   467
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   468
    # Refresh the position of wire connected to contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   469
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   470
        self.Input.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   471
        self.Output.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   472
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   473
    # Returns the contact connector that starts with the point given if it exists 
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   474
    def GetConnector(self, position, name = None):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   475
        # if a name is given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   476
        if name:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   477
            # Test input and output connector
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   478
            if name == self.Input.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   479
                return self.Input
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   480
            if name == self.Output.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   481
                return self.Output
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   482
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   483
        input_pos = self.Input.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   484
        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
   485
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   486
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   487
        output_pos = self.Output.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   488
        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
   489
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   490
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   491
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   492
    # Returns input and output contact connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   493
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   494
        return {"input":self.Input,"output":self.Output}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   495
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   496
    # Test if point given is on contact input or output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   497
    def TestConnector(self, pt, exclude=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   498
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   499
        if self.Input.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   500
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   501
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   502
        if self.Output.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   503
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   504
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   505
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   506
    # Refresh the positions of the block connectors
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   507
    def RefreshConnectors(self):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   508
        scaling = self.Parent.GetScaling()
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   509
        position = self.Size[1] / 2 + 1
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   510
        if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   511
            position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   512
        self.Input.SetPosition(wx.Point(0, position))
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   513
        self.Output.SetPosition(wx.Point(self.Size[0], position))
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   514
        self.RefreshConnected()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   515
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   516
    # Changes the contact name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   517
    def SetName(self, name):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   518
        self.Name = name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   519
        self.RefreshNameSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   520
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   521
    # Returns the contact name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   522
    def GetName(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   523
        return self.Name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   524
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   525
    # Changes the contact type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   526
    def SetType(self, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   527
        self.Type = type
50
4610aafc884e Bugs fixed
lbessard
parents: 42
diff changeset
   528
        self.RefreshTypeSize()
0
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 type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   531
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   532
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   533
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   534
    # Method called when a LeftDClick event have been generated
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   535
    def OnLeftDClick(self, event, dc, scaling):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   536
        # Edit the contact properties
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   537
        self.Parent.EditContactContent(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   538
    
127
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   539
    # 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
   540
    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
   541
        # Popup the default menu
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   542
        self.Parent.PopupDefaultMenu()
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   543
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   544
    # Refreshes the contact model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   545
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   546
        self.Parent.RefreshContactModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   547
        # If contact has moved, refresh the model of wires connected to output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   548
        if move:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   549
            self.Output.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   550
    
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   551
    # 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
   552
    def DrawHighlightment(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   553
        dc.SetPen(wx.Pen(HIGHLIGHTCOLOR))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   554
        dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   555
        dc.SetLogicalFunction(wx.AND)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   556
        # Draw two rectangles for representing the contact
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   557
        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
   558
        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
   559
        dc.SetLogicalFunction(wx.COPY)
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   560
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   561
    # Draws contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   562
    def Draw(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   563
        Graphic_Element.Draw(self, dc)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   564
        dc.SetPen(wx.BLACK_PEN)
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   565
        dc.SetBrush(wx.BLACK_BRUSH)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   566
        # Draw two rectangles for representing the contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   567
        dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   568
        dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   569
        # Draw contact name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   570
        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   571
                self.Pos.y - (self.NameSize[1] + 2))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   572
        # Draw the modifier symbol in the middle of contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   573
        typetext = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   574
        if self.Type == CONTACT_REVERSE:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   575
            typetext = "/"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   576
        elif self.Type == CONTACT_RISING:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   577
            typetext = "P"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   578
        elif self.Type == CONTACT_FALLING:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   579
            typetext = "N"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   580
        if typetext != "":
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   581
            dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1,
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   582
                    self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   583
        # Draw input and output connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   584
        self.Input.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   585
        self.Output.Draw(dc)
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   586
        
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   587
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   588
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   589
#                         Ladder Diagram Coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   590
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   591
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   592
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   593
Class that implements the graphic representation of a coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   594
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   595
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   596
class LD_Coil(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   597
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   598
    # Create a new coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   599
    def __init__(self, parent, type, name, id = None):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   600
        Graphic_Element.__init__(self, parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   601
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   602
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   603
        self.Id = id
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   604
        self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   605
        # Create an input and output connector
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   606
        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
   607
        self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   608
        self.RefreshNameSize()
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   609
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   610
        
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   611
    # Destructor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   612
    def __del__(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   613
        self.Input = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   614
        self.Output = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   615
    
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   616
    # Make a clone of this LD_Coil
162
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
   617
    def Clone(self, parent, id = None, pos = None):
e746ff4aa8be Bug on Element Paste fixed
lbessard
parents: 145
diff changeset
   618
        coil = LD_Coil(parent, self.Type, self.Name, id)
112
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   619
        coil.SetSize(self.Size[0], self.Size[1])
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   620
        if pos is not None:
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   621
            coil.SetPosition(pos.x, pos.y)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   622
        coil.Input = self.Input.Clone(coil)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   623
        coil.Output = self.Output.Clone(coil)
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   624
        return coil
317148fc1225 Adding support for block copy
lbessard
parents: 110
diff changeset
   625
    
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   626
    # Returns the RedrawRect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   627
    def GetRedrawRect(self, movex = 0, movey = 0):
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   628
        rect = Graphic_Element.GetRedrawRect(self, movex, movey)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   629
        rect = rect.Union(self.Input.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   630
        rect = rect.Union(self.Output.GetRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   631
        if movex != 0 or movey != 0:
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   632
            if self.Input.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   633
                rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   634
            if self.Output.IsConnected():
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   635
                rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   636
        return rect
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   637
    
180
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   638
    def ProcessDragging(self, movex, movey, centered, scaling):
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   639
        return Graphic_Element.ProcessDragging(self, movex, movey, centered, scaling, height_fac = 2)    
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   640
    
3b0d3ea35ee5 Fixed bad handle initialisation, causing exception on rightup event in some cases.
etisserant
parents: 175
diff changeset
   641
    # Forbids to change the Coil size
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   642
    def SetSize(self, width, height):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   643
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   644
            Graphic_Element.SetSize(self, width, height)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   645
            self.RefreshConnectors()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   646
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   647
    # Delete this coil by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   648
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   649
        self.Parent.DeleteCoil(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   650
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   651
    # Unconnect input and output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   652
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   653
        self.Input.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   654
        self.Output.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   655
                
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   656
    # Refresh the size of text for name
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   657
    def RefreshNameSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   658
        if self.Name != "":
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   659
            self.NameSize = self.Parent.GetTextExtent(self.Name)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   660
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   661
            self.NameSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   662
    
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   663
    # Refresh the size of text for type
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   664
    def RefreshTypeSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   665
        typetext = ""
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   666
        if self.Type == COIL_REVERSE:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   667
            typetext = "/"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   668
        elif self.Type == COIL_SET:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   669
            typetext = "S"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   670
        elif self.Type == COIL_RESET:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   671
            typetext = "R"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   672
        if typetext != "":
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   673
            self.TypeSize = self.Parent.GetTextExtent(typetext)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   674
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   675
            self.TypeSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   676
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   677
    # Refresh the coil bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   678
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   679
        # Calculate the size of the name outside the coil
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 162
diff changeset
   680
        text_width, text_height = self.Parent.GetTextExtent(self.Name)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   681
        # Calculate the bounding box size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   682
        if self.Name != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   683
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   684
            bbx_width = max(self.Size[0], text_width)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   685
            bbx_y = self.Pos.y - (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   686
            bbx_height = self.Size[1] + (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   687
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   688
            bbx_x = self.Pos.x
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   689
            bbx_width = self.Size[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   690
            bbx_y = self.Pos.y
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   691
            bbx_height = self.Size[1]
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   692
        self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   693
        
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   694
    # Returns the block minimum size
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   695
    def GetMinSize(self):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   696
        return LD_ELEMENT_SIZE
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   697
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   698
    # Refresh the position of wire connected to coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   699
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   700
        self.Input.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   701
        self.Output.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   702
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   703
    # Returns the coil connector that starts with the point given if it exists 
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   704
    def GetConnector(self, position, name = None):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   705
        # if a name is given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   706
        if name:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   707
            # Test input and output connector
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   708
            if self.Input and name == self.Input.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   709
                return self.Input
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   710
            if self.Output and name == self.Output.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   711
                return self.Output
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   712
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   713
        input_pos = self.Input.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   714
        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
   715
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   716
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   717
        output_pos = self.Output.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   718
        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
   719
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   720
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   721
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   722
    # Returns input and output coil connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   723
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   724
        return {"input":self.Input,"output":self.Output}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   725
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   726
    # Test if point given is on coil input or output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   727
    def TestConnector(self, pt, exclude=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   728
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   729
        if self.Input.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   730
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   731
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   732
        if self.Output.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   733
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   734
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   735
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   736
    # Refresh the positions of the block connectors
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   737
    def RefreshConnectors(self):
145
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   738
        scaling = self.Parent.GetScaling()
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   739
        position = self.Size[1] / 2 + 1
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   740
        if scaling is not None:
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   741
            position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   742
        self.Input.SetPosition(wx.Point(0, position))
4fb225afddf4 Adding scaling
lbessard
parents: 144
diff changeset
   743
        self.Output.SetPosition(wx.Point(self.Size[0], position))
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   744
        self.RefreshConnected()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   745
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   746
    # Changes the coil name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   747
    def SetName(self, name):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   748
        self.Name = name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   749
        self.RefreshNameSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   750
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   751
    # Returns the coil name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   752
    def GetName(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   753
        return self.Name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   754
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   755
    # Changes the coil type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   756
    def SetType(self, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   757
        self.Type = type
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   758
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   759
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   760
    # Returns the coil type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   761
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   762
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   763
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   764
    # Method called when a LeftDClick event have been generated
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   765
    def OnLeftDClick(self, event, dc, scaling):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   766
        # Edit the coil properties
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   767
        self.Parent.EditCoilContent(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   768
    
127
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   769
    # 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
   770
    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
   771
        # Popup the default menu
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   772
        self.Parent.PopupDefaultMenu()
436268f31dae Adding contextual menu on LD_Contact, LD_Coil, LD_PowerRail and SFC_ActionBlock
lbessard
parents: 112
diff changeset
   773
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   774
    # Refreshes the coil model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   775
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   776
        self.Parent.RefreshCoilModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   777
        # If coil has moved, refresh the model of wires connected to output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   778
        if move:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   779
            self.Output.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   780
    
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   781
    # 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
   782
    def DrawHighlightment(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   783
        dc.SetPen(wx.Pen(HIGHLIGHTCOLOR, 6, wx.SOLID))
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   784
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   785
        dc.SetLogicalFunction(wx.AND)
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   786
        # Draw a two circle arcs for representing the coil
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   787
        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
   788
        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
   789
        dc.SetLogicalFunction(wx.COPY)
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   790
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   791
    # Draws coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   792
    def Draw(self, dc):
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 140
diff changeset
   793
        Graphic_Element.Draw(self, dc)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   794
        dc.SetPen(wx.Pen(wx.BLACK, 2, wx.SOLID))
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   795
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   796
        # Draw a two circle arcs for representing the coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   797
        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)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   798
        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)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 61
diff changeset
   799
        dc.SetPen(wx.BLACK_PEN)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   800
        dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   801
        # Draw coil name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   802
        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   803
                self.Pos.y - (self.NameSize[1] + 2))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   804
        # Draw the modifier symbol in the middle of coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   805
        typetext = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   806
        if self.Type == COIL_REVERSE:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   807
            typetext = "/"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   808
        elif self.Type == COIL_SET:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   809
            typetext = "S"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   810
        elif self.Type == COIL_RESET:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   811
            typetext = "R"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   812
        if typetext != "":
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   813
            dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1,
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   814
                    self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   815
        # Draw input and output connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   816
        self.Input.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   817
        self.Output.Draw(dc)
140
06d28f03f6f4 Adding highlighting on group or element when mouse is over
lbessard
parents: 138
diff changeset
   818