etisserant@0: #!/usr/bin/env python
etisserant@0: # -*- coding: utf-8 -*-
etisserant@0: 
etisserant@0: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
etisserant@0: #based on the plcopen standard. 
etisserant@0: #
lbessard@58: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
etisserant@0: #
etisserant@0: #See COPYING file for copyrights details.
etisserant@0: #
etisserant@0: #This library is free software; you can redistribute it and/or
etisserant@5: #modify it under the terms of the GNU General Public
etisserant@0: #License as published by the Free Software Foundation; either
etisserant@0: #version 2.1 of the License, or (at your option) any later version.
etisserant@0: #
etisserant@0: #This library is distributed in the hope that it will be useful,
etisserant@0: #but WITHOUT ANY WARRANTY; without even the implied warranty of
etisserant@0: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
lbessard@58: #General Public License for more details.
etisserant@0: #
etisserant@5: #You should have received a copy of the GNU General Public
etisserant@0: #License along with this library; if not, write to the Free Software
etisserant@0: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
etisserant@0: 
etisserant@0: import wx
etisserant@0: 
etisserant@0: from GraphicCommons import *
etisserant@0: from plcopen.structures import *
etisserant@0: 
etisserant@0: #-------------------------------------------------------------------------------
etisserant@0: #                         Ladder Diagram PowerRail
etisserant@0: #-------------------------------------------------------------------------------
etisserant@0: 
etisserant@0: """
etisserant@0: Class that implements the graphic representation of a power rail
etisserant@0: """
etisserant@0: 
etisserant@0: class LD_PowerRail(Graphic_Element):
etisserant@0:     
etisserant@0:     # Create a new power rail
pizza@550:     def __init__(self, parent, type, id=None, connectors=1):
etisserant@0:         Graphic_Element.__init__(self, parent)
lbessard@61:         self.Type = None
lbessard@61:         self.Connectors = []
lbessard@71:         self.RealConnectors = None
etisserant@0:         self.Id = id
lbessard@27:         self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2]
lbessard@61:         self.SetType(type, connectors)
etisserant@0:         
lbessard@249:     def Flush(self):
lbessard@249:         for connector in self.Connectors:
pizza@550:             connector.Flush()
etisserant@0:         self.Connectors = []
etisserant@0:     
lbessard@112:     # Make a clone of this LD_PowerRail
lbessard@162:     def Clone(self, parent, id = None, pos = None):
lbessard@162:         powerrail = LD_PowerRail(parent, self.Type, id)
lbessard@112:         powerrail.SetSize(self.Size[0], self.Size[1])
lbessard@112:         if pos is not None:
lbessard@112:             powerrail.SetPosition(pos.x, pos.y)
lbessard@283:         else:
lbessard@283:             powerrail.SetPosition(self.Pos.x, self.Pos.y)
lbessard@112:         powerrail.Connectors = []
lbessard@112:         for connector in self.Connectors:
pizza@550:             powerrail.Connectors.append(connector.Clone(powerrail))
lbessard@112:         return powerrail
lbessard@112:     
lbessard@283:     def GetConnectorTranslation(self, element):
pizza@550:         return dict(zip([connector for connector in self.Connectors],
pizza@550:                         [connector for connector in element.Connectors]))
lbessard@283:     
lbessard@144:     # Returns the RedrawRect
lbessard@144:     def GetRedrawRect(self, movex = 0, movey = 0):
lbessard@144:         rect = Graphic_Element.GetRedrawRect(self, movex, movey)
lbessard@144:         for connector in self.Connectors:
pizza@550:             rect = rect.Union(connector.GetRedrawRect(movex, movey))
lbessard@144:         if movex != 0 or movey != 0:
lbessard@144:             for connector in self.Connectors:
pizza@550:                 if connector.IsConnected():
lbessard@144:                     rect = rect.Union(connector.GetConnectedRedrawRect(movex, movey))
lbessard@144:         return rect
lbessard@144:     
etisserant@0:     # Forbids to change the power rail size
etisserant@0:     def SetSize(self, width, height):
lbessard@42:         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
lbessard@27:             Graphic_Element.SetSize(self, width, height)
pizza@550:         else:
pizza@550:             Graphic_Element.SetSize(self, LD_POWERRAIL_WIDTH, height)
pizza@550:         self.RefreshConnectors()
etisserant@0:     
etisserant@0:     # Forbids to select a power rail
laurent@633:     def HitTest(self, pt, connectors=True):
lbessard@42:         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
laurent@633:             return Graphic_Element.HitTest(self, pt, connectors) or self.TestConnector(pt, exclude=False) != None
etisserant@0:         return False
etisserant@0:     
lbessard@42:     # Forbids to select a power rail
lbessard@42:     def IsInSelection(self, rect):
lbessard@42:         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
lbessard@61:             return Graphic_Element.IsInSelection(self, rect)
lbessard@42:         return False
lbessard@42:     
etisserant@0:     # Deletes this power rail by calling the appropriate method
etisserant@0:     def Delete(self):
etisserant@0:         self.Parent.DeletePowerRail(self)
etisserant@0:     
etisserant@0:     # Unconnect all connectors
etisserant@0:     def Clean(self):
etisserant@0:         for connector in self.Connectors:
pizza@550:             connector.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
etisserant@0:                 
etisserant@0:     # Refresh the power rail bounding box
etisserant@0:     def RefreshBoundingBox(self):
lbessard@144:         self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
etisserant@0:     
etisserant@0:     # Refresh the power rail size
etisserant@0:     def RefreshSize(self):
pizza@550:         self.Size = wx.Size(LD_POWERRAIL_WIDTH, max(LD_LINE_SIZE * len(self.Connectors), self.Size[1]))
etisserant@0:         self.RefreshBoundingBox()
etisserant@0:     
lbessard@27:     # Returns the block minimum size
lbessard@27:     def GetMinSize(self):
pizza@550:         return LD_POWERRAIL_WIDTH, self.Extensions[0] + self.Extensions[1]
lbessard@27:     
etisserant@0:     # Add a connector or a blank to this power rail at the last place
pizza@550:     def AddConnector(self):
pizza@550:         self.InsertConnector(len(self.Connectors))
etisserant@0:     
etisserant@0:     # Add a connector or a blank to this power rail at the place given
pizza@550:     def InsertConnector(self, idx):
pizza@550:         if self.Type == LEFTRAIL:
pizza@550:             connector = Connector(self, "", "BOOL", wx.Point(self.Size[0], 0), EAST)
pizza@550:         elif self.Type == RIGHTRAIL:
pizza@550:             connector = Connector(self, "", "BOOL", wx.Point(0, 0), WEST)
pizza@550:         self.Connectors.insert(idx, connector)
etisserant@0:         self.RefreshSize()
etisserant@0:         self.RefreshConnectors()
etisserant@0:     
lbessard@27:     # Moves the divergence connector given
lbessard@27:     def MoveConnector(self, connector, movey):
lbessard@27:         position = connector.GetRelPosition()
lbessard@64:         connector.SetPosition(wx.Point(position.x, position.y + movey))
lbessard@27:         miny = self.Size[1]
lbessard@27:         maxy = 0
lbessard@27:         for connect in self.Connectors:
lbessard@27:             connect_pos = connect.GetRelPosition()
lbessard@80:             miny = min(miny, connect_pos.y - self.Extensions[0])
lbessard@80:             maxy = max(maxy, connect_pos.y - self.Extensions[0])
lbessard@80:         min_pos = self.Pos.y + miny
lbessard@27:         self.Pos.y = min(min_pos, self.Pos.y)
lbessard@27:         if min_pos == self.Pos.y:
lbessard@27:             for connect in self.Connectors:
lbessard@27:                 connect_pos = connect.GetRelPosition()
lbessard@80:                 connect.SetPosition(wx.Point(connect_pos.x, connect_pos.y - miny))
laurent@641:         self.Connectors.sort(lambda x, y: cmp(x.Pos.y, y.Pos.y))
lbessard@80:         maxy = 0
lbessard@80:         for connect in self.Connectors:
lbessard@80:             connect_pos = connect.GetRelPosition()
lbessard@80:             maxy = max(maxy, connect_pos.y)
lbessard@80:         self.Size[1] = max(maxy + self.Extensions[1], self.Size[1])
lbessard@27:         connector.MoveConnected()
lbessard@27:         self.RefreshBoundingBox()
lbessard@27:     
etisserant@0:     # Returns the index in connectors list for the connector given
etisserant@0:     def GetConnectorIndex(self, connector):
etisserant@0:         if connector in self.Connectors:
etisserant@0:             return self.Connectors.index(connector)
etisserant@0:         return None
etisserant@0:     
etisserant@0:     # Delete the connector or blank from connectors list at the index given
etisserant@0:     def DeleteConnector(self, idx):
etisserant@0:         self.Connectors.pop(idx)
etisserant@0:         self.RefreshConnectors()
etisserant@0:         self.RefreshSize()
etisserant@0:     
etisserant@0:     # Refresh the positions of the power rail connectors
etisserant@0:     def RefreshConnectors(self):
lbessard@145:         scaling = self.Parent.GetScaling()
pizza@550:         height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
pizza@550:         interval = float(height) / float(max(len(self.Connectors) - 1, 1))
pizza@550:         for i, connector in enumerate(self.Connectors):
pizza@550:             if self.RealConnectors:
pizza@550:                 position = self.Extensions[0] + int(round(self.RealConnectors[i] * height))
pizza@550:             else:
pizza@550:                 position = self.Extensions[0] + int(round(i * interval))
pizza@550:             if scaling is not None:
pizza@550:                 position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
pizza@550:             if self.Type == LEFTRAIL:
pizza@550:                 connector.SetPosition(wx.Point(self.Size[0], position))
pizza@550:             elif self.Type == RIGHTRAIL:
pizza@550:                 connector.SetPosition(wx.Point(0, position))
etisserant@0:         self.RefreshConnected()
etisserant@0:     
lbessard@7:     # Refresh the position of wires connected to power rail
etisserant@0:     def RefreshConnected(self, exclude = []):
etisserant@0:         for connector in self.Connectors:
pizza@550:             connector.MoveConnected(exclude)
etisserant@0:     
etisserant@0:     # Returns the power rail connector that starts with the point given if it exists 
lbessard@27:     def GetConnector(self, position, name = None):
lbessard@27:         # if a name is given
laurent@633:         if name is not None:
lbessard@27:             # Test each connector if it exists
lbessard@27:             for connector in self.Connectors:
pizza@550:                 if name == connector.GetName():
lbessard@27:                     return connector
laurent@537:         return self.FindNearestConnector(position, [connector for connector in self.Connectors if connector is not None])
etisserant@0:     
etisserant@0:     # Returns all the power rail connectors 
etisserant@0:     def GetConnectors(self):
laurent@383:         connectors = [connector for connector in self.Connectors if connector]
laurent@383:         if self.Type == LEFTRAIL:
laurent@383:             return {"inputs": [], "outputs": connectors}
laurent@383:         else:
laurent@383:             return {"inputs": connectors, "outputs": []}
etisserant@0:     
etisserant@0:     # Test if point given is on one of the power rail connectors
lbessard@243:     def TestConnector(self, pt, direction = None, exclude = True):
etisserant@0:         for connector in self.Connectors:
pizza@550:             if connector.TestPoint(pt, direction, exclude):
etisserant@0:                 return connector
etisserant@0:         return None
etisserant@0:     
etisserant@0:     # Returns the power rail type
lbessard@61:     def SetType(self, type, connectors):
pizza@550:         if type != self.Type or len(self.Connectors) != connectors:
lbessard@61:             # Create a connector or a blank according to 'connectors' and add it in
lbessard@61:             # the connectors list
lbessard@61:             self.Type = type
lbessard@61:             self.Clean()
lbessard@61:             self.Connectors = []
pizza@550:             for connector in xrange(connectors):
pizza@550:                 self.AddConnector()
lbessard@61:             self.RefreshSize()
lbessard@61:     
lbessard@61:     # Returns the power rail type
etisserant@0:     def GetType(self):
etisserant@0:         return self.Type
etisserant@0:     
lbessard@27:     # Method called when a LeftDown event have been generated
lbessard@27:     def OnLeftDown(self, event, dc, scaling):
lbessard@145:         self.RealConnectors = []
lbessard@145:         height = self.Size[1] - self.Extensions[0] - self.Extensions[1]
lbessard@254:         if height > 0:
lbessard@254:             for connector in self.Connectors:
lbessard@254:                 position = connector.GetRelPosition()
lbessard@254:                 self.RealConnectors.append(max(0., min(float(position.y - self.Extensions[0]) / float(height), 1.)))
lbessard@254:         elif len(self.Connectors) > 1:
lbessard@254:             self.RealConnectors = map(lambda x : x * 1 / (len(self.Connectors) - 1), xrange(len(self.Connectors)))
lbessard@254:         else:
lbessard@254:             self.RealConnectors = [0.5]
lbessard@145:         Graphic_Element.OnLeftDown(self, event, dc, scaling)
lbessard@145:     
lbessard@145:     # Method called when a LeftUp event have been generated
lbessard@145:     def OnLeftUp(self, event, dc, scaling):
lbessard@145:         Graphic_Element.OnLeftUp(self, event, dc, scaling)
lbessard@145:         self.RealConnectors = None
lbessard@145:     
lbessard@145:     # Method called when a LeftDown event have been generated
lbessard@145:     def OnRightDown(self, event, dc, scaling):
lbessard@27:         pos = GetScaledEventPosition(event, dc, scaling)
lbessard@27:         # Test if a connector have been handled
lbessard@243:         connector = self.TestConnector(pos, exclude=False)
lbessard@27:         if connector:
lbessard@27:             self.Handle = (HANDLE_CONNECTOR, connector)
laurent@381:             wx.CallAfter(self.Parent.SetCurrentCursor, 1)
lbessard@27:             self.Selected = False
lbessard@27:             # Initializes the last position
lbessard@27:             self.oldPos = GetScaledEventPosition(event, dc, scaling)
lbessard@27:         else:
lbessard@145:             Graphic_Element.OnRightDown(self, event, dc, scaling)
lbessard@145:     
lbessard@145:     # Method called when a LeftDClick event have been generated
lbessard@145:     def OnLeftDClick(self, event, dc, scaling):
lbessard@145:         # Edit the powerrail properties
lbessard@145:         self.Parent.EditPowerRailContent(self)
lbessard@145:     
lbessard@145:     # Method called when a RightUp event have been generated
lbessard@145:     def OnRightUp(self, event, dc, scaling):
lbessard@27:         handle_type, handle = self.Handle
lbessard@27:         if handle_type == HANDLE_CONNECTOR:
lbessard@27:             wires = handle.GetWires()
lbessard@80:             if len(wires) == 1:
lbessard@80:                 if handle == wires[0][0].StartConnected:
lbessard@80:                     block = wires[0][0].EndConnected.GetParentBlock()
lbessard@80:                 else:
lbessard@80:                     block = wires[0][0].StartConnected.GetParentBlock()
lbessard@80:                 block.RefreshModel(False)
lbessard@145:             Graphic_Element.OnRightUp(self, event, dc, scaling)
lbessard@145:         else:
lbessard@145:             self.Parent.PopupDefaultMenu()
lbessard@27:     
etisserant@175:     def Resize(self, x, y, width, height):
etisserant@175:         self.Move(x, y)
lbessard@254:         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
lbessard@254:             self.SetSize(width, height)
lbessard@254:         else:
lbessard@254:             self.SetSize(LD_POWERRAIL_WIDTH, height)
etisserant@175: 
lbessard@110:     # Refreshes the powerrail state according to move defined and handle selected
lbessard@327:     def ProcessDragging(self, movex, movey, event, scaling):
lbessard@27:         handle_type, handle = self.Handle
lbessard@27:         # A connector has been handled
lbessard@27:         if handle_type == HANDLE_CONNECTOR:
lbessard@138:             movey = max(-self.BoundingBox.y, movey)
lbessard@145:             if scaling is not None:
lbessard@145:                 position = handle.GetRelPosition()
lbessard@145:                 movey = round(float(self.Pos.y + position.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y - position.y
lbessard@27:             self.MoveConnector(handle, movey)
lbessard@138:             return 0, movey
laurent@554:         elif self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
lbessard@327:             return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling)
laurent@554:         return 0, 0
lbessard@27:     
etisserant@0:     # Refreshes the power rail model
etisserant@0:     def RefreshModel(self, move=True):
etisserant@0:         self.Parent.RefreshPowerRailModel(self)
etisserant@0:         # If power rail has moved and power rail is of type LEFT, refresh the model 
etisserant@0:         # of wires connected to connectors
etisserant@0:         if move and self.Type == LEFTRAIL:
etisserant@0:             for connector in self.Connectors:
pizza@550:                 connector.RefreshWires()
etisserant@0:     
etisserant@0:     # Draws power rail
etisserant@0:     def Draw(self, dc):
lbessard@144:         Graphic_Element.Draw(self, dc)
laurent@563:         dc.SetPen(MiterPen(wx.BLACK))
lbessard@64:         dc.SetBrush(wx.BLACK_BRUSH)
etisserant@0:         # Draw a rectangle with the power rail size
etisserant@175:         if self.Type == LEFTRAIL:
etisserant@175:             dc.DrawRectangle(self.Pos.x + self.Size[0] - LD_POWERRAIL_WIDTH, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1)
etisserant@175:         else:
etisserant@175:             dc.DrawRectangle(self.Pos.x, self.Pos.y, LD_POWERRAIL_WIDTH + 1, self.Size[1] + 1)
etisserant@0:         # Draw connectors
etisserant@0:         for connector in self.Connectors:
pizza@550:             connector.Draw(dc)
lbessard@140:         
etisserant@0: 
etisserant@0: #-------------------------------------------------------------------------------
etisserant@0: #                         Ladder Diagram Contact
etisserant@0: #-------------------------------------------------------------------------------
etisserant@0: 
etisserant@0: """
etisserant@0: Class that implements the graphic representation of a contact
etisserant@0: """
etisserant@0: 
greg@361: class LD_Contact(Graphic_Element, DebugDataConsumer):
etisserant@0:     
etisserant@0:     # Create a new contact
etisserant@0:     def __init__(self, parent, type, name, id = None):
etisserant@0:         Graphic_Element.__init__(self, parent)
greg@361:         DebugDataConsumer.__init__(self)
etisserant@0:         self.Type = type
etisserant@0:         self.Name = name
etisserant@0:         self.Id = id
lbessard@64:         self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
laurent@566:         self.Highlights = {}
etisserant@0:         # Create an input and output connector
lbessard@64:         self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST)
lbessard@64:         self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST)
lbessard@249:         self.PreviousValue = False
lbessard@249:         self.PreviousSpreading = False
lbessard@42:         self.RefreshNameSize()
lbessard@42:         self.RefreshTypeSize()
etisserant@0:     
lbessard@249:     def Flush(self):
lbessard@249:         if self.Input is not None:
lbessard@249:             self.Input.Flush()
lbessard@249:             self.Input = None
lbessard@249:         if self.Output is not None:
lbessard@249:             self.Output.Flush()
lbessard@249:             self.Output = None
lbessard@249:     
laurent@478:     def SetForced(self, forced):
laurent@478:         if self.Forced != forced:
laurent@478:             self.Forced = forced
laurent@478:             if self.Visible:
laurent@634:                 self.Parent.ElementNeedRefresh(self)
laurent@478:     
lbessard@249:     def SetValue(self, value):
laurent@508:         if self.Type == CONTACT_RISING:
laurent@508:             refresh = self.Value and not self.PreviousValue
laurent@508:         elif self.Type == CONTACT_FALLING:
laurent@508:             refresh = not self.Value and self.PreviousValue
laurent@508:         else:
laurent@508:             refresh = False
lbessard@249:         self.PreviousValue = self.Value
lbessard@249:         self.Value = value
laurent@508:         if self.Value != self.PreviousValue or refresh:
greg@368:             if self.Visible:
laurent@634:                 self.Parent.ElementNeedRefresh(self)
lbessard@249:             self.SpreadCurrent()
lbessard@249:     
lbessard@249:     def SpreadCurrent(self):
lbessard@253:         if self.Parent.Debug:
lbessard@253:             if self.Value is None:
lbessard@253:                 self.Value = False
lbessard@253:             spreading = self.Input.ReceivingCurrent()
lbessard@249:             if self.Type == CONTACT_NORMAL:
lbessard@249:                 spreading &= self.Value
lbessard@249:             elif self.Type == CONTACT_REVERSE:
lbessard@249:                 spreading &= not self.Value
lbessard@249:             elif self.Type == CONTACT_RISING:
lbessard@249:                 spreading &= self.Value and not self.PreviousValue
lbessard@249:             elif self.Type == CONTACT_FALLING:
laurent@508:                 spreading &= not self.Value and self.PreviousValue
lbessard@249:             else:
lbessard@249:                 spreading = False
lbessard@253:             if spreading and not self.PreviousSpreading:
lbessard@253:                 self.Output.SpreadCurrent(True)
lbessard@253:             elif not spreading and self.PreviousSpreading:
lbessard@253:                 self.Output.SpreadCurrent(False)
lbessard@253:             self.PreviousSpreading = spreading
etisserant@0:     
lbessard@112:     # Make a clone of this LD_Contact
lbessard@162:     def Clone(self, parent, id = None, pos = None):
lbessard@162:         contact = LD_Contact(parent, self.Type, self.Name, id)
lbessard@112:         contact.SetSize(self.Size[0], self.Size[1])
lbessard@112:         if pos is not None:
lbessard@112:             contact.SetPosition(pos.x, pos.y)
lbessard@283:         else:
lbessard@283:             contact.SetPosition(self.Pos.x, self.Pos.y)
lbessard@112:         contact.Input = self.Input.Clone(contact)
lbessard@112:         contact.Output = self.Output.Clone(contact)
lbessard@112:         return contact
lbessard@112:     
lbessard@283:     def GetConnectorTranslation(self, element):
lbessard@283:         return {self.Input : element.Input, self.Output : element.Output}
lbessard@283:     
lbessard@144:     # Returns the RedrawRect
lbessard@144:     def GetRedrawRect(self, movex = 0, movey = 0):
lbessard@144:         rect = Graphic_Element.GetRedrawRect(self, movex, movey)
lbessard@144:         rect = rect.Union(self.Input.GetRedrawRect(movex, movey))
lbessard@144:         rect = rect.Union(self.Output.GetRedrawRect(movex, movey))
lbessard@144:         if movex != 0 or movey != 0:
lbessard@144:             if self.Input.IsConnected():
lbessard@144:                 rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
lbessard@144:             if self.Output.IsConnected():
lbessard@144:                 rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
lbessard@144:         return rect
etisserant@180: 
lbessard@327:     def ProcessDragging(self, movex, movey, event, scaling):
laurent@554:         if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
laurent@554:             movex = movey = 0
greg@360:         return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac = 2)
lbessard@144:     
etisserant@0:     # Forbids to change the contact size
etisserant@0:     def SetSize(self, width, height):
lbessard@42:         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
lbessard@27:             Graphic_Element.SetSize(self, width, height)
lbessard@27:             self.RefreshConnectors()
etisserant@0:     
etisserant@0:     # Delete this contact by calling the appropriate method
etisserant@0:     def Delete(self):
etisserant@0:         self.Parent.DeleteContact(self)
etisserant@0:     
etisserant@0:     # Unconnect input and output
etisserant@0:     def Clean(self):
lbessard@61:         self.Input.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
lbessard@61:         self.Output.UnConnect(delete = self.Parent.GetDrawingMode() == FREEDRAWING_MODE)
etisserant@0:     
lbessard@42:     # Refresh the size of text for name
lbessard@42:     def RefreshNameSize(self):
lbessard@42:         if self.Name != "":
lbessard@165:             self.NameSize = self.Parent.GetTextExtent(self.Name)
lbessard@42:         else:
lbessard@42:             self.NameSize = 0, 0
lbessard@42:     
lbessard@42:     # Refresh the size of text for type
lbessard@42:     def RefreshTypeSize(self):
lbessard@42:         typetext = ""
lbessard@42:         if self.Type == CONTACT_REVERSE:
lbessard@42:             typetext = "/"
lbessard@42:         elif self.Type == CONTACT_RISING:
lbessard@42:             typetext = "P"
lbessard@42:         elif self.Type == CONTACT_FALLING:
lbessard@42:             typetext = "N"
lbessard@42:         if typetext != "":
lbessard@165:             self.TypeSize = self.Parent.GetTextExtent(typetext)
lbessard@42:         else:
lbessard@42:             self.TypeSize = 0, 0
lbessard@42:     
etisserant@0:     # Refresh the contact bounding box
etisserant@0:     def RefreshBoundingBox(self):
etisserant@0:         # Calculate the size of the name outside the contact
lbessard@165:         text_width, text_height = self.Parent.GetTextExtent(self.Name)
etisserant@0:         # Calculate the bounding box size
etisserant@0:         if self.Name != "":
etisserant@0:             bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
etisserant@0:             bbx_width = max(self.Size[0], text_width)
etisserant@0:             bbx_y = self.Pos.y - (text_height + 2)
etisserant@0:             bbx_height = self.Size[1] + (text_height + 2)
etisserant@0:         else:
etisserant@0:             bbx_x = self.Pos.x
etisserant@0:             bbx_width = self.Size[0]
etisserant@0:             bbx_y = self.Pos.y
etisserant@0:             bbx_height = self.Size[1]
lbessard@64:         self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
etisserant@0:     
lbessard@27:     # Returns the block minimum size
lbessard@27:     def GetMinSize(self):
lbessard@27:         return LD_ELEMENT_SIZE
lbessard@27:     
etisserant@0:     # Refresh the position of wire connected to contact
etisserant@0:     def RefreshConnected(self, exclude = []):
etisserant@0:         self.Input.MoveConnected(exclude)
etisserant@0:         self.Output.MoveConnected(exclude)
etisserant@0:     
etisserant@0:     # Returns the contact connector that starts with the point given if it exists 
lbessard@27:     def GetConnector(self, position, name = None):
lbessard@27:         # if a name is given
laurent@633:         if name is not None:
lbessard@27:             # Test input and output connector
laurent@633:             #if name == self.Input.GetName():
laurent@633:             #    return self.Input
lbessard@27:             if name == self.Output.GetName():
lbessard@27:                 return self.Output
laurent@537:         return self.FindNearestConnector(position, [self.Input, self.Output])
etisserant@0:     
etisserant@0:     # Returns input and output contact connectors 
etisserant@0:     def GetConnectors(self):
laurent@383:         return {"inputs": [self.Input], "outputs": [self.Output]}
etisserant@0:     
etisserant@0:     # Test if point given is on contact input or output connector
lbessard@243:     def TestConnector(self, pt, direction = None, exclude=True):
etisserant@0:         # Test input connector
lbessard@243:         if self.Input.TestPoint(pt, direction, exclude):
etisserant@0:             return self.Input
etisserant@0:         # Test output connector
lbessard@243:         if self.Output.TestPoint(pt, direction, exclude):
etisserant@0:             return self.Output
etisserant@0:         return None
etisserant@0: 
lbessard@27:     # Refresh the positions of the block connectors
lbessard@27:     def RefreshConnectors(self):
lbessard@145:         scaling = self.Parent.GetScaling()
lbessard@145:         position = self.Size[1] / 2 + 1
lbessard@145:         if scaling is not None:
lbessard@145:             position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
lbessard@145:         self.Input.SetPosition(wx.Point(0, position))
lbessard@145:         self.Output.SetPosition(wx.Point(self.Size[0], position))
lbessard@27:         self.RefreshConnected()
lbessard@27: 
etisserant@0:     # Changes the contact name
etisserant@0:     def SetName(self, name):
etisserant@0:         self.Name = name
lbessard@42:         self.RefreshNameSize()
etisserant@0: 
etisserant@0:     # Returns the contact name
etisserant@0:     def GetName(self):
etisserant@0:         return self.Name
etisserant@0: 
etisserant@0:     # Changes the contact type
etisserant@0:     def SetType(self, type):
etisserant@0:         self.Type = type
lbessard@50:         self.RefreshTypeSize()
etisserant@0: 
etisserant@0:     # Returns the contact type
etisserant@0:     def GetType(self):
etisserant@0:         return self.Type
etisserant@0:     
etisserant@0:     # Method called when a LeftDClick event have been generated
lbessard@27:     def OnLeftDClick(self, event, dc, scaling):
etisserant@0:         # Edit the contact properties
etisserant@0:         self.Parent.EditContactContent(self)
etisserant@0:     
lbessard@127:     # Method called when a RightUp event have been generated
lbessard@127:     def OnRightUp(self, event, dc, scaling):
lbessard@127:         # Popup the default menu
lbessard@127:         self.Parent.PopupDefaultMenu()
lbessard@127:     
etisserant@0:     # Refreshes the contact model
etisserant@0:     def RefreshModel(self, move=True):
etisserant@0:         self.Parent.RefreshContactModel(self)
etisserant@0:         # If contact has moved, refresh the model of wires connected to output
etisserant@0:         if move:
etisserant@0:             self.Output.RefreshWires()
etisserant@0:     
lbessard@140:     # Draws the highlightment of this element if it is highlighted
lbessard@140:     def DrawHighlightment(self, dc):
laurent@563:         scalex, scaley = dc.GetUserScale()
laurent@563:         dc.SetUserScale(1, 1)
laurent@563:         dc.SetPen(MiterPen(HIGHLIGHTCOLOR))
lbessard@144:         dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
lbessard@144:         dc.SetLogicalFunction(wx.AND)
lbessard@144:         # Draw two rectangles for representing the contact
laurent@563:         left_left = (self.Pos.x - 1) * scalex - 2
laurent@563:         right_left = (self.Pos.x + self.Size[0] - 2) * scalex - 2
laurent@563:         top = (self.Pos.y - 1) * scaley - 2
laurent@563:         width = 4 * scalex + 5
laurent@563:         height = (self.Size[1] + 3) * scaley + 5
laurent@563:         
laurent@563:         dc.DrawRectangle(left_left, top, width, height)
laurent@563:         dc.DrawRectangle(right_left, top, width, height)
lbessard@144:         dc.SetLogicalFunction(wx.COPY)
laurent@563:         dc.SetUserScale(scalex, scaley)
lbessard@140:     
laurent@566:     # Adds an highlight to the connection
laurent@566:     def AddHighlight(self, infos, start, end, highlight_type):
laurent@566:         highlights = self.Highlights.setdefault(infos[0], [])
laurent@566:         if infos[0] == "reference":
laurent@566:             if start[0] == 0 and end[0] == 0:
laurent@566:                 AddHighlight(highlights, (start, end, highlight_type))
laurent@566:         else:
laurent@566:             AddHighlight(highlights, ((0, 0), (0, 1), highlight_type))
laurent@566:     
laurent@566:     # Removes an highlight from the connection
laurent@566:     def RemoveHighlight(self, infos, start, end, highlight_type):
laurent@566:         highlights = self.Highlights.get(infos[0], [])
laurent@566:         if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0:
laurent@566:             self.Highlights.pop(infos[0])
laurent@566:     
laurent@566:     # Removes all the highlights of one particular type from the connection
laurent@566:     def ClearHighlight(self, highlight_type=None):
laurent@566:         if highlight_type is None:
laurent@566:             self.Highlights = {}
laurent@566:         else:
laurent@566:             highlight_items = self.Highlights.items()
laurent@566:             for name, highlights in highlight_items:
laurent@566:                 highlights = ClearHighlights(highlight, highlight_type)
laurent@566:                 if len(highlights) == 0:
laurent@566:                     self.Highlights.pop(name)
lbessard@231:     
etisserant@0:     # Draws contact
etisserant@0:     def Draw(self, dc):
lbessard@144:         Graphic_Element.Draw(self, dc)
lbessard@249:         if self.Value is not None:            
laurent@478:             if self.Type == CONTACT_NORMAL and self.Value or \
laurent@478:                self.Type == CONTACT_REVERSE and not self.Value or \
laurent@478:                self.Type == CONTACT_RISING and self.Value and not self.PreviousValue or \
laurent@508:                self.Type == CONTACT_RISING and not self.Value and self.PreviousValue:
laurent@478:                 if self.Forced:
laurent@563:                     dc.SetPen(MiterPen(wx.CYAN))
laurent@478:                 else:
laurent@563:                     dc.SetPen(MiterPen(wx.GREEN))
laurent@478:             elif self.Forced:
laurent@563:                 dc.SetPen(MiterPen(wx.BLUE))
lbessard@249:             else:
laurent@563:                 dc.SetPen(MiterPen(wx.BLACK))
laurent@563:         else:
laurent@563:             dc.SetPen(MiterPen(wx.BLACK))
lbessard@64:         dc.SetBrush(wx.BLACK_BRUSH)
lbessard@213:         
lbessard@213:         # Compiling contact type modifier symbol
etisserant@0:         typetext = ""
etisserant@0:         if self.Type == CONTACT_REVERSE:
etisserant@0:             typetext = "/"
etisserant@0:         elif self.Type == CONTACT_RISING:
etisserant@0:             typetext = "P"
etisserant@0:         elif self.Type == CONTACT_FALLING:
etisserant@0:             typetext = "N"
lbessard@213:         
lbessard@213:         if getattr(dc, "printing", False):
lbessard@213:             name_size = dc.GetTextExtent(self.Name)
lbessard@213:             if typetext != "":
lbessard@213:                 type_size = dc.GetTextExtent(typetext)
lbessard@213:         else:
lbessard@213:             name_size = self.NameSize
lbessard@213:             if typetext != "":
lbessard@213:                 type_size = self.TypeSize
lbessard@213:         
lbessard@213:         # Draw two rectangles for representing the contact
lbessard@213:         dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
lbessard@213:         dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
lbessard@213:         # Draw contact name
lbessard@231:         name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
lbessard@231:                     self.Pos.y - (name_size[1] + 2))
lbessard@231:         dc.DrawText(self.Name, name_pos[0], name_pos[1])
lbessard@213:         # Draw the modifier symbol in the middle of contact
etisserant@0:         if typetext != "":
lbessard@231:             type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1,
lbessard@231:                         self.Pos.y + (self.Size[1] - type_size[1]) / 2)
lbessard@231:             dc.DrawText(typetext, type_pos[0], type_pos[1])
etisserant@0:         # Draw input and output connectors
etisserant@0:         self.Input.Draw(dc)
etisserant@0:         self.Output.Draw(dc)
laurent@566:         
laurent@566:         if not getattr(dc, "printing", False):
laurent@566:             for name, highlights in self.Highlights.iteritems():
laurent@566:                 if name == "reference":
laurent@566:                     DrawHighlightedText(dc, self.Name, highlights, name_pos[0], name_pos[1])
laurent@566:                 elif typetext != "":
laurent@566:                     DrawHighlightedText(dc, typetext, highlights, type_pos[0], type_pos[1])
etisserant@0: 
etisserant@0: #-------------------------------------------------------------------------------
etisserant@0: #                         Ladder Diagram Coil
etisserant@0: #-------------------------------------------------------------------------------
etisserant@0: 
etisserant@0: """
etisserant@0: Class that implements the graphic representation of a coil
etisserant@0: """
etisserant@0: 
etisserant@0: class LD_Coil(Graphic_Element):
etisserant@0:     
etisserant@0:     # Create a new coil
etisserant@0:     def __init__(self, parent, type, name, id = None):
etisserant@0:         Graphic_Element.__init__(self, parent)
etisserant@0:         self.Type = type
etisserant@0:         self.Name = name
etisserant@0:         self.Id = id
lbessard@64:         self.Size = wx.Size(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
laurent@566:         self.Highlights = {}
etisserant@0:         # Create an input and output connector
lbessard@64:         self.Input = Connector(self, "", "BOOL", wx.Point(0, self.Size[1] / 2 + 1), WEST)
lbessard@64:         self.Output = Connector(self, "", "BOOL", wx.Point(self.Size[0], self.Size[1] / 2 + 1), EAST)
lbessard@249:         self.Value = None
lbessard@249:         self.PreviousValue = False
lbessard@42:         self.RefreshNameSize()
lbessard@42:         self.RefreshTypeSize()
etisserant@0:         
lbessard@249:     def Flush(self):
lbessard@249:         if self.Input is not None:
lbessard@249:             self.Input.Flush()
lbessard@249:             self.Input = None
lbessard@249:         if self.Output is not None:
lbessard@249:             self.Output.Flush()
lbessard@249:             self.Output = None
lbessard@249:     
lbessard@249:     def SpreadCurrent(self):
lbessard@253:         if self.Parent.Debug:
lbessard@253:             self.PreviousValue = self.Value
lbessard@253:             self.Value = self.Input.ReceivingCurrent()
lbessard@253:             if self.Value and not self.PreviousValue:
lbessard@253:                 self.Output.SpreadCurrent(True)
lbessard@253:             elif not self.Value and self.PreviousValue:
lbessard@253:                 self.Output.SpreadCurrent(False)
greg@368:             if self.Value != self.PreviousValue and self.Visible:
laurent@634:                 self.Parent.ElementNeedRefresh(self)
etisserant@0:     
lbessard@112:     # Make a clone of this LD_Coil
lbessard@162:     def Clone(self, parent, id = None, pos = None):
lbessard@162:         coil = LD_Coil(parent, self.Type, self.Name, id)
lbessard@112:         coil.SetSize(self.Size[0], self.Size[1])
lbessard@112:         if pos is not None:
lbessard@112:             coil.SetPosition(pos.x, pos.y)
lbessard@283:         else:
lbessard@283:             coil.SetPosition(self.Pos.x, self.Pos.y)
lbessard@112:         coil.Input = self.Input.Clone(coil)
lbessard@112:         coil.Output = self.Output.Clone(coil)
lbessard@112:         return coil
lbessard@112:     
lbessard@283:     def GetConnectorTranslation(self, element):
lbessard@283:         return {self.Input : element.Input, self.Output : element.Output}
lbessard@283:     
lbessard@144:     # Returns the RedrawRect
lbessard@144:     def GetRedrawRect(self, movex = 0, movey = 0):
lbessard@144:         rect = Graphic_Element.GetRedrawRect(self, movex, movey)
lbessard@144:         rect = rect.Union(self.Input.GetRedrawRect(movex, movey))
lbessard@144:         rect = rect.Union(self.Output.GetRedrawRect(movex, movey))
lbessard@144:         if movex != 0 or movey != 0:
lbessard@144:             if self.Input.IsConnected():
lbessard@144:                 rect = rect.Union(self.Input.GetConnectedRedrawRect(movex, movey))
lbessard@144:             if self.Output.IsConnected():
lbessard@144:                 rect = rect.Union(self.Output.GetConnectedRedrawRect(movex, movey))
lbessard@144:         return rect
lbessard@144:     
lbessard@327:     def ProcessDragging(self, movex, movey, event, scaling):
laurent@554:         if self.Parent.GetDrawingMode() != FREEDRAWING_MODE:
laurent@554:             movex = movey = 0
lbessard@327:         return Graphic_Element.ProcessDragging(self, movex, movey, event, scaling, height_fac = 2)    
etisserant@180:     
etisserant@180:     # Forbids to change the Coil size
etisserant@0:     def SetSize(self, width, height):
lbessard@42:         if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
lbessard@27:             Graphic_Element.SetSize(self, width, height)
lbessard@27:             self.RefreshConnectors()
etisserant@0:     
etisserant@0:     # Delete this coil by calling the appropriate method
etisserant@0:     def Delete(self):
etisserant@0:         self.Parent.DeleteCoil(self)
etisserant@0:     
etisserant@0:     # Unconnect input and output
etisserant@0:     def Clean(self):
etisserant@0:         self.Input.UnConnect()
etisserant@0:         self.Output.UnConnect()
etisserant@0:                 
lbessard@42:     # Refresh the size of text for name
lbessard@42:     def RefreshNameSize(self):
lbessard@42:         if self.Name != "":
lbessard@165:             self.NameSize = self.Parent.GetTextExtent(self.Name)
lbessard@42:         else:
lbessard@42:             self.NameSize = 0, 0
lbessard@42:     
lbessard@42:     # Refresh the size of text for type
lbessard@42:     def RefreshTypeSize(self):
lbessard@42:         typetext = ""
lbessard@42:         if self.Type == COIL_REVERSE:
lbessard@42:             typetext = "/"
lbessard@42:         elif self.Type == COIL_SET:
lbessard@42:             typetext = "S"
lbessard@42:         elif self.Type == COIL_RESET:
lbessard@42:             typetext = "R"
lbessard@269:         elif self.Type == COIL_RISING:
lbessard@269:             typetext = "P"
lbessard@269:         elif self.Type == COIL_FALLING:
lbessard@269:             typetext = "N"
lbessard@42:         if typetext != "":
lbessard@165:             self.TypeSize = self.Parent.GetTextExtent(typetext)
lbessard@42:         else:
lbessard@42:             self.TypeSize = 0, 0
lbessard@42:     
etisserant@0:     # Refresh the coil bounding box
etisserant@0:     def RefreshBoundingBox(self):
etisserant@0:         # Calculate the size of the name outside the coil
lbessard@165:         text_width, text_height = self.Parent.GetTextExtent(self.Name)
etisserant@0:         # Calculate the bounding box size
etisserant@0:         if self.Name != "":
etisserant@0:             bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
etisserant@0:             bbx_width = max(self.Size[0], text_width)
etisserant@0:             bbx_y = self.Pos.y - (text_height + 2)
etisserant@0:             bbx_height = self.Size[1] + (text_height + 2)
etisserant@0:         else:
etisserant@0:             bbx_x = self.Pos.x
etisserant@0:             bbx_width = self.Size[0]
etisserant@0:             bbx_y = self.Pos.y
etisserant@0:             bbx_height = self.Size[1]
lbessard@64:         self.BoundingBox = wx.Rect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
lbessard@27:         
lbessard@27:     # Returns the block minimum size
lbessard@27:     def GetMinSize(self):
lbessard@27:         return LD_ELEMENT_SIZE
etisserant@0:     
etisserant@0:     # Refresh the position of wire connected to coil
etisserant@0:     def RefreshConnected(self, exclude = []):
etisserant@0:         self.Input.MoveConnected(exclude)
etisserant@0:         self.Output.MoveConnected(exclude)
etisserant@0:     
etisserant@0:     # Returns the coil connector that starts with the point given if it exists 
lbessard@27:     def GetConnector(self, position, name = None):
lbessard@27:         # if a name is given
laurent@633:         if name is not None:
lbessard@27:             # Test input and output connector
laurent@633:             #if self.Input and name == self.Input.GetName():
laurent@633:             #    return self.Input
lbessard@27:             if self.Output and name == self.Output.GetName():
lbessard@27:                 return self.Output
laurent@537:         return self.FindNearestConnector(position, [self.Input, self.Output])
etisserant@0:     
etisserant@0:     # Returns input and output coil connectors 
etisserant@0:     def GetConnectors(self):
laurent@383:         return {"inputs": [self.Input], "outputs": [self.Output]}
etisserant@0:     
etisserant@0:     # Test if point given is on coil input or output connector
lbessard@243:     def TestConnector(self, pt, direction = None, exclude=True):
etisserant@0:         # Test input connector
lbessard@243:         if self.Input.TestPoint(pt, direction, exclude):
etisserant@0:             return self.Input
etisserant@0:         # Test output connector
lbessard@243:         if self.Output.TestPoint(pt, direction, exclude):
etisserant@0:             return self.Output
etisserant@0:         return None
etisserant@0:     
lbessard@27:     # Refresh the positions of the block connectors
lbessard@27:     def RefreshConnectors(self):
lbessard@145:         scaling = self.Parent.GetScaling()
lbessard@145:         position = self.Size[1] / 2 + 1
lbessard@145:         if scaling is not None:
lbessard@145:             position = round(float(self.Pos.y + position) / float(scaling[1])) * scaling[1] - self.Pos.y
lbessard@145:         self.Input.SetPosition(wx.Point(0, position))
lbessard@145:         self.Output.SetPosition(wx.Point(self.Size[0], position))
lbessard@27:         self.RefreshConnected()
lbessard@27:     
etisserant@0:     # Changes the coil name
etisserant@0:     def SetName(self, name):
etisserant@0:         self.Name = name
lbessard@42:         self.RefreshNameSize()
etisserant@0: 
etisserant@0:     # Returns the coil name
etisserant@0:     def GetName(self):
etisserant@0:         return self.Name
etisserant@0:     
etisserant@0:     # Changes the coil type
etisserant@0:     def SetType(self, type):
etisserant@0:         self.Type = type
lbessard@42:         self.RefreshTypeSize()
etisserant@0:     
etisserant@0:     # Returns the coil type
etisserant@0:     def GetType(self):
etisserant@0:         return self.Type
etisserant@0:     
etisserant@0:     # Method called when a LeftDClick event have been generated
lbessard@27:     def OnLeftDClick(self, event, dc, scaling):
etisserant@0:         # Edit the coil properties
etisserant@0:         self.Parent.EditCoilContent(self)
etisserant@0:     
lbessard@127:     # Method called when a RightUp event have been generated
lbessard@127:     def OnRightUp(self, event, dc, scaling):
lbessard@127:         # Popup the default menu
lbessard@127:         self.Parent.PopupDefaultMenu()
lbessard@127:     
etisserant@0:     # Refreshes the coil model
etisserant@0:     def RefreshModel(self, move=True):
etisserant@0:         self.Parent.RefreshCoilModel(self)
etisserant@0:         # If coil has moved, refresh the model of wires connected to output
etisserant@0:         if move:
etisserant@0:             self.Output.RefreshWires()
etisserant@0:     
lbessard@140:     # Draws the highlightment of this element if it is highlighted
lbessard@140:     def DrawHighlightment(self, dc):
laurent@563:         scalex, scaley = dc.GetUserScale()
laurent@563:         dc.SetUserScale(1, 1)
laurent@563:         dc.SetPen(MiterPen(HIGHLIGHTCOLOR, (3 * scalex + 5), wx.SOLID))
lbessard@144:         dc.SetBrush(wx.TRANSPARENT_BRUSH)
lbessard@144:         dc.SetLogicalFunction(wx.AND)
lbessard@144:         # Draw a two circle arcs for representing the coil
laurent@563:         dc.DrawEllipticArc(round(self.Pos.x * scalex), 
laurent@563:                            round((self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1) * scaley), 
laurent@563:                            round(self.Size[0] * scalex), 
laurent@563:                            round((int(self.Size[1] * sqrt(2)) - 1) * scaley),
laurent@563:                            135, 225)
laurent@563:         dc.DrawEllipticArc(round(self.Pos.x * scalex), 
laurent@563:                            round((self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1) * scaley), 
laurent@563:                            round(self.Size[0] * scalex), 
laurent@563:                            round((int(self.Size[1] * sqrt(2)) - 1) * scaley),
laurent@563:                            -45, 45)
lbessard@144:         dc.SetLogicalFunction(wx.COPY)
laurent@563:         dc.SetUserScale(scalex, scaley)
lbessard@140:     
laurent@566:     # Adds an highlight to the connection
laurent@566:     def AddHighlight(self, infos, start, end, highlight_type):
laurent@566:         highlights = self.Highlights.setdefault(infos[0], [])
laurent@566:         if infos[0] == "reference":
laurent@566:             if start[0] == 0 and end[0] == 0:
laurent@566:                 AddHighlight(highlights, (start, end, highlight_type))
laurent@566:         else:
laurent@566:             AddHighlight(highlights, ((0, 0), (0, 1), highlight_type))
laurent@566:     
laurent@566:     # Removes an highlight from the connection
laurent@566:     def RemoveHighlight(self, infos, start, end, highlight_type):
laurent@566:         highlights = self.Highlights.get(infos[0], [])
laurent@566:         if RemoveHighlight(highlights, (start, end, highlight_type)) and len(highlights) == 0:
laurent@566:             self.Highlights.pop(infos[0])
laurent@566:     
laurent@566:     # Removes all the highlights of one particular type from the connection
laurent@566:     def ClearHighlight(self, highlight_type=None):
laurent@566:         if highlight_type is None:
laurent@566:             self.Highlights = {}
laurent@566:         else:
laurent@566:             highlight_items = self.Highlights.items()
laurent@566:             for name, highlights in highlight_items:
laurent@566:                 highlights = ClearHighlights(highlight, highlight_type)
laurent@566:                 if len(highlights) == 0:
laurent@566:                     self.Highlights.pop(name)
lbessard@231:     
etisserant@0:     # Draws coil
etisserant@0:     def Draw(self, dc):
lbessard@144:         Graphic_Element.Draw(self, dc)
lbessard@249:         if self.Value is not None and self.Value:
laurent@563:             dc.SetPen(MiterPen(wx.GREEN, 2, wx.SOLID))
laurent@563:         else:
laurent@563:             dc.SetPen(MiterPen(wx.BLACK, 2, wx.SOLID))
lbessard@64:         dc.SetBrush(wx.TRANSPARENT_BRUSH)
lbessard@213:         
lbessard@213:         # Compiling coil type modifier symbol 
etisserant@0:         typetext = ""
etisserant@0:         if self.Type == COIL_REVERSE:
etisserant@0:             typetext = "/"
etisserant@0:         elif self.Type == COIL_SET:
etisserant@0:             typetext = "S"
etisserant@0:         elif self.Type == COIL_RESET:
etisserant@0:             typetext = "R"
lbessard@269:         elif self.Type == COIL_RISING:
lbessard@269:             typetext = "P"
lbessard@269:         elif self.Type == COIL_FALLING:
lbessard@269:             typetext = "N"
lbessard@213:         
lbessard@213:         if getattr(dc, "printing", False) and not isinstance(dc, wx.PostScriptDC):
lbessard@213:             # Draw an clipped ellipse for representing the coil
lbessard@213:             clipping_box = dc.GetClippingBox()
lbessard@213:             dc.SetClippingRegion(self.Pos.x - 1, self.Pos.y, self.Size[0] + 2, self.Size[1] + 1)
lbessard@213:             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)
lbessard@213:             dc.DestroyClippingRegion()
lbessard@213:             if clipping_box != (0, 0, 0, 0):
lbessard@213:                 dc.SetClippingRegion(*clipping_box)
lbessard@213:             name_size = dc.GetTextExtent(self.Name)
lbessard@213:             if typetext != "":
lbessard@213:                 type_size = dc.GetTextExtent(typetext)
lbessard@213:         else:
lbessard@213:             # Draw a two ellipse arcs for representing the coil
lbessard@213:             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)
lbessard@213:             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)
lbessard@213:             # Draw a point to avoid hole in left arc
lbessard@213:             if not getattr(dc, "printing", False):
lbessard@249:                 if self.Value is not None and self.Value:
laurent@563:                     dc.SetPen(MiterPen(wx.GREEN))
lbessard@249:                 else:
laurent@563:                     dc.SetPen(MiterPen(wx.BLACK))
lbessard@213:                 dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
lbessard@213:             name_size = self.NameSize
lbessard@213:             if typetext != "":
lbessard@213:                 type_size = self.TypeSize
lbessard@213:             
lbessard@213:         # Draw coil name
lbessard@231:         name_pos = (self.Pos.x + (self.Size[0] - name_size[0]) / 2,
lbessard@231:                     self.Pos.y - (name_size[1] + 2))
lbessard@231:         dc.DrawText(self.Name, name_pos[0], name_pos[1])
lbessard@213:         # Draw the modifier symbol in the middle of coil
etisserant@0:         if typetext != "":
lbessard@231:             type_pos = (self.Pos.x + (self.Size[0] - type_size[0]) / 2 + 1,
lbessard@231:                         self.Pos.y + (self.Size[1] - type_size[1]) / 2)
lbessard@231:             dc.DrawText(typetext, type_pos[0], type_pos[1])
etisserant@0:         # Draw input and output connectors
etisserant@0:         self.Input.Draw(dc)
etisserant@0:         self.Output.Draw(dc)
laurent@566: 
laurent@566:         if not getattr(dc, "printing", False):
laurent@566:             for name, highlights in self.Highlights.iteritems():
laurent@566:                 if name == "reference":
laurent@566:                     DrawHighlightedText(dc, self.Name, highlights, name_pos[0], name_pos[1])
laurent@566:                 elif typetext != "":
laurent@566:                     DrawHighlightedText(dc, typetext, highlights, type_pos[0], type_pos[1])
lbessard@231:             
lbessard@231: