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