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