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: from math import * lbessard@99: from plcopen.structures import IsOfType, IsEndType etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Common constants etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Definition of constants for dimensions of graphic elements etisserant@0: """ etisserant@0: etisserant@0: # FBD and SFC constants lbessard@28: MIN_MOVE = 5 # Minimum move before starting a element dragging lbessard@28: CONNECTOR_SIZE = 8 # Size of connectors lbessard@28: BLOCK_LINE_SIZE = 20 # Minimum size of each line in a block lbessard@28: HANDLE_SIZE = 6 # Size of the squares for handles lbessard@28: ANCHOR_DISTANCE = 5 # Distance where wire is automativally attached to a connector lbessard@28: POINT_RADIUS = 2 # Radius of the point of wire ends lbessard@28: MIN_SEGMENT_SIZE = 2 # Minimum size of the endling segments of a wire etisserant@0: etisserant@0: # LD constants lbessard@28: LD_LINE_SIZE = 40 # Distance between two lines in a ladder rung lbessard@28: LD_ELEMENT_SIZE = (21, 15) # Size (width, height) of a ladder element (contact or coil) lbessard@28: LD_WIRE_SIZE = 30 # Size of a wire between two contact lbessard@28: LD_WIRECOIL_SIZE = 70 # Size of a wire between a coil and a contact lbessard@144: LD_POWERRAIL_WIDTH = 3 # Width of a Powerrail lbessard@28: LD_OFFSET = (10, 10) # Distance (x, y) between each comment and rung of the ladder lbessard@28: LD_COMMENT_DEFAULTSIZE = (600, 40) # Size (width, height) of a comment box etisserant@0: etisserant@0: # SFC constants lbessard@28: SFC_STEP_DEFAULT_SIZE = (40, 30) # Default size of a SFC step lbessard@28: SFC_TRANSITION_SIZE = (20, 2) # Size of a SFC transition lbessard@28: SFC_DEFAULT_SEQUENCE_INTERVAL = 40 # Default size of the interval between two divergence branches lbessard@28: SFC_SIMULTANEOUS_SEQUENCE_EXTRA = 20 # Size of extra lines for simultaneous divergence and convergence lbessard@28: SFC_JUMP_SIZE = (12, 13) # Size of a SFC jump to step lbessard@28: SFC_WIRE_MIN_SIZE = 25 # Size of a wire between two elements lbessard@28: SFC_ACTION_MIN_SIZE = (100, 30) # Minimum size of an action block line etisserant@0: etisserant@0: # Type definition constants for graphic elements etisserant@0: [INPUT, OUTPUT, INOUT] = range(3) etisserant@0: [CONNECTOR, CONTINUATION] = range(2) etisserant@0: [LEFTRAIL, RIGHTRAIL] = range(2) etisserant@0: [CONTACT_NORMAL, CONTACT_REVERSE, CONTACT_RISING, CONTACT_FALLING] = range(4) etisserant@0: [COIL_NORMAL, COIL_REVERSE, COIL_SET, COIL_RESET] = range(4) etisserant@0: [SELECTION_DIVERGENCE, SELECTION_CONVERGENCE, SIMULTANEOUS_DIVERGENCE, SIMULTANEOUS_CONVERGENCE] = range(4) etisserant@0: etisserant@0: # Constants for defining the type of dragging that has been selected etisserant@0: [HANDLE_MOVE, HANDLE_RESIZE, HANDLE_POINT, HANDLE_SEGMENT, HANDLE_CONNECTOR] = range(5) etisserant@0: etisserant@0: # List of value for resize handle that are valid etisserant@0: VALID_HANDLES = [(1,1), (1,2), (1,3), (2,3), (3,3), (3,2), (3,1), (2,1)] etisserant@0: etisserant@0: # Contants for defining the direction of a connector etisserant@0: [EAST, NORTH, WEST, SOUTH] = [(1,0), (0,-1), (-1,0), (0,1)] etisserant@0: etisserant@0: # Contants for defining which mode is selected for each view lbessard@138: [MODE_SELECTION, MODE_BLOCK, MODE_VARIABLE, MODE_CONNECTION, MODE_COMMENT, lbessard@138: MODE_COIL, MODE_CONTACT, MODE_POWERRAIL, MODE_INITIALSTEP, MODE_STEP, lbessard@138: MODE_TRANSITION, MODE_DIVERGENCE, MODE_JUMP, MODE_ACTION] = range(14) lbessard@138: lbessard@138: # Contants for defining alignment types for graphic group lbessard@138: [ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_TOP, ALIGN_MIDDLE, ALIGN_BOTTOM] = range(6) lbessard@27: lbessard@27: # Contants for defining which drawing mode is selected for app lbessard@27: [FREEDRAWING_MODE, DRIVENDRAWING_MODE] = [1, 2] lbessard@27: lbessard@140: # Color for Highlighting lbessard@140: HIGHLIGHTCOLOR = wx.CYAN lbessard@140: lbessard@58: CURSORS = None lbessard@58: lbessard@58: def ResetCursors(): lbessard@58: global CURSORS lbessard@58: if CURSORS == None: lbessard@64: CURSORS = [wx.NullCursor, lbessard@64: wx.StockCursor(wx.CURSOR_HAND), lbessard@64: wx.StockCursor(wx.CURSOR_SIZENWSE), lbessard@64: wx.StockCursor(wx.CURSOR_SIZENESW), lbessard@64: wx.StockCursor(wx.CURSOR_SIZEWE), lbessard@64: wx.StockCursor(wx.CURSOR_SIZENS)] lbessard@58: lbessard@58: HANDLE_CURSORS = { lbessard@58: (1, 1) : 2, lbessard@58: (3, 3) : 2, lbessard@58: (1, 3) : 3, lbessard@58: (3, 1) : 3, lbessard@58: (1, 2) : 4, lbessard@58: (3, 2) : 4, lbessard@58: (2, 1) : 5, lbessard@58: (2, 3) : 5 lbessard@58: } etisserant@0: etisserant@0: """ etisserant@0: Basic vector operations for calculate wire points etisserant@0: """ etisserant@0: etisserant@0: # Calculate the scalar product of two vectors etisserant@0: def product(v1, v2): etisserant@0: return v1[0] * v2[0] + v1[1] * v2[1] etisserant@0: etisserant@0: # Create a vector from two points and define if vector must be normal etisserant@0: def vector(p1, p2, normal = True): etisserant@0: vector = (p2.x - p1.x, p2.y - p1.y) etisserant@0: if normal: etisserant@0: return normalize(vector) etisserant@0: return vector etisserant@0: etisserant@0: # Calculate the norm of a given vector etisserant@0: def norm(v): etisserant@0: return sqrt(v[0] * v[0] + v[1] * v[1]) etisserant@0: etisserant@0: # Normalize a given vector etisserant@0: def normalize(v): etisserant@0: v_norm = norm(v) etisserant@0: # Verifie if it is not a null vector etisserant@0: if v_norm > 0: etisserant@0: return (v[0] / v_norm, v[1] / v_norm) etisserant@0: else: etisserant@0: return v etisserant@0: etisserant@0: etisserant@0: """ etisserant@0: Function that calculates the nearest point of the grid defined by scaling for the given point etisserant@0: """ etisserant@0: lbessard@27: def GetScaledEventPosition(event, dc, scaling): lbessard@27: pos = event.GetLogicalPosition(dc) etisserant@0: if scaling: etisserant@0: pos.x = round(float(pos.x) / float(scaling[0])) * scaling[0] etisserant@0: pos.y = round(float(pos.y) / float(scaling[1])) * scaling[1] etisserant@0: return pos etisserant@0: etisserant@0: etisserant@0: """ etisserant@0: Function that choose a direction during the wire points generation etisserant@0: """ etisserant@0: etisserant@0: def DirectionChoice(v_base, v_target, dir_target): etisserant@0: dir_product = product(v_base, v_target) etisserant@0: if dir_product < 0: etisserant@0: return (-v_base[0], -v_base[1]) etisserant@0: elif dir_product == 0 and product(v_base, dir_target) != 0: etisserant@0: return dir_target etisserant@0: return v_base etisserant@0: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Viewer Rubberband etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements a rubberband etisserant@0: """ etisserant@0: etisserant@0: class RubberBand: etisserant@0: etisserant@0: # Create a rubberband by indicated on which window it must be drawn etisserant@0: def __init__(self, drawingSurface): etisserant@0: self.drawingSurface = drawingSurface etisserant@0: self.Reset() etisserant@0: etisserant@0: # Method that initializes the internal attributes of the rubberband etisserant@0: def Reset(self): etisserant@0: self.startPoint = None etisserant@0: self.currentBox = None etisserant@0: self.lastBox = None etisserant@0: etisserant@0: # Method that return if a box is currently edited etisserant@0: def IsShown(self): etisserant@0: return self.currentBox != None etisserant@0: etisserant@0: # Method that returns the currently edited box etisserant@0: def GetCurrentExtent(self): etisserant@0: return self.currentBox etisserant@0: etisserant@0: # Method called when a new box starts to be edited lbessard@27: def OnLeftDown(self, event, dc, scaling): lbessard@27: pos = GetScaledEventPosition(event, dc, scaling) etisserant@0: # Save the point for calculate the box position and size etisserant@0: self.startPoint = pos lbessard@64: self.currentBox = wx.Rect(pos.x, pos.y, 0, 0) lbessard@64: self.drawingSurface.SetCursor(wx.StockCursor(wx.CURSOR_CROSS)) etisserant@0: self.Redraw() etisserant@0: etisserant@0: # Method called when dragging with a box edited lbessard@27: def OnMotion(self, event, dc, scaling): lbessard@27: pos = GetScaledEventPosition(event, dc, scaling) etisserant@0: # Save the last position and size of the box for erasing it lbessard@64: self.lastBox = wx.Rect(self.currentBox.x, self.currentBox.y, self.currentBox.width, etisserant@0: self.currentBox.height) etisserant@0: # Calculate new position and size of the box etisserant@0: if pos.x >= self.startPoint.x: etisserant@0: self.currentBox.x = self.startPoint.x etisserant@0: self.currentBox.width = pos.x - self.startPoint.x + 1 etisserant@0: else: etisserant@0: self.currentBox.x = pos.x etisserant@0: self.currentBox.width = self.startPoint.x - pos.x + 1 etisserant@0: if pos.y >= self.startPoint.y: etisserant@0: self.currentBox.y = self.startPoint.y etisserant@0: self.currentBox.height = pos.y - self.startPoint.y + 1 etisserant@0: else: etisserant@0: self.currentBox.y = pos.y etisserant@0: self.currentBox.height = self.startPoint.y - pos.y + 1 etisserant@0: self.Redraw() etisserant@0: etisserant@0: # Method called when dragging is stopped lbessard@27: def OnLeftUp(self, event, dc, scaling): lbessard@64: self.drawingSurface.SetCursor(wx.NullCursor) etisserant@0: self.lastBox = self.currentBox etisserant@0: self.currentBox = None etisserant@0: self.Redraw() etisserant@0: etisserant@0: # Method that erase the last box and draw the new box lbessard@90: def Redraw(self, dc = None): lbessard@90: if not dc: lbessard@90: dc = self.drawingSurface.GetLogicalDC() lbessard@64: dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT)) lbessard@64: dc.SetBrush(wx.TRANSPARENT_BRUSH) lbessard@64: dc.SetLogicalFunction(wx.XOR) etisserant@0: if self.lastBox: etisserant@0: # Erase last box etisserant@0: dc.DrawRectangle(self.lastBox.x, self.lastBox.y, self.lastBox.width, etisserant@0: self.lastBox.height) etisserant@0: if self.currentBox: etisserant@0: # Draw current box etisserant@0: dc.DrawRectangle(self.currentBox.x, self.currentBox.y, self.currentBox.width, etisserant@0: self.currentBox.height) lbessard@90: lbessard@27: # Erase last box lbessard@90: def Erase(self, dc = None): lbessard@90: if not dc: lbessard@90: dc = self.drawingSurface.GetLogicalDC() lbessard@64: dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT)) lbessard@64: dc.SetBrush(wx.TRANSPARENT_BRUSH) lbessard@64: dc.SetLogicalFunction(wx.XOR) lbessard@27: if self.lastBox: lbessard@27: dc.DrawRectangle(self.lastBox.x, self.lastBox.y, self.lastBox.width, lbessard@27: self.lastBox.height) lbessard@90: lbessard@27: # Draw current box lbessard@90: def Draw(self, dc = None): lbessard@90: if not dc: lbessard@90: dc = self.drawingSurface.GetLogicalDC() lbessard@64: dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT)) lbessard@64: dc.SetBrush(wx.TRANSPARENT_BRUSH) lbessard@64: dc.SetLogicalFunction(wx.XOR) lbessard@27: if self.currentBox: lbessard@27: # Draw current box lbessard@27: dc.DrawRectangle(self.currentBox.x, self.currentBox.y, self.currentBox.width, lbessard@27: self.currentBox.height) lbessard@27: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Graphic element base class etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements a generic graphic element etisserant@0: """ etisserant@0: etisserant@0: class Graphic_Element: etisserant@0: etisserant@0: # Create a new graphic element etisserant@0: def __init__(self, parent, id = None): etisserant@0: self.Parent = parent etisserant@0: self.Id = id etisserant@0: self.oldPos = None etisserant@180: self.Handle = (None,None) etisserant@0: self.Dragging = False etisserant@0: self.Selected = False lbessard@140: self.Highlighted = False lbessard@64: self.Pos = wx.Point(0, 0) lbessard@64: self.Size = wx.Size(0, 0) lbessard@64: self.BoundingBox = wx.Rect(0, 0, 0, 0) lbessard@58: self.CurrentCursor = 0 lbessard@58: ResetCursors() etisserant@0: lbessard@144: def GetDragging(self): lbessard@144: return self.Dragging lbessard@144: etisserant@0: # Make a clone of this element lbessard@162: def Clone(self, parent): lbessard@162: return Graphic_Element(parent, self.Id) etisserant@0: etisserant@0: # Changes the block position etisserant@0: def SetPosition(self, x, y): etisserant@0: self.Pos.x = x etisserant@0: self.Pos.y = y etisserant@0: self.RefreshConnected() etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Returns the block position etisserant@0: def GetPosition(self): etisserant@0: return self.Pos.x, self.Pos.y etisserant@0: etisserant@0: # Changes the element size etisserant@0: def SetSize(self, width, height): etisserant@0: self.Size.SetWidth(width) etisserant@0: self.Size.SetHeight(height) etisserant@0: self.RefreshConnectors() etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Returns the element size etisserant@0: def GetSize(self): etisserant@0: return self.Size.GetWidth(), self.Size.GetHeight() etisserant@0: etisserant@0: # Refresh the element Bounding Box etisserant@0: def RefreshBoundingBox(self): lbessard@64: self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1]) etisserant@0: etisserant@0: # Refresh the element connectors position etisserant@0: def RefreshConnectors(self): etisserant@0: pass etisserant@0: etisserant@0: # Refresh the position of wires connected to element inputs and outputs etisserant@0: def RefreshConnected(self): etisserant@0: pass etisserant@0: etisserant@0: # Change the parent etisserant@0: def SetParent(self, parent): etisserant@0: self.Parent = parent etisserant@0: etisserant@0: # Override this method for defining the method to call for deleting this element etisserant@0: def Delete(self): etisserant@0: pass etisserant@0: etisserant@0: # Returns the Id etisserant@0: def GetId(self): etisserant@0: return self.Id etisserant@0: etisserant@0: # Returns if the point given is in the bounding box etisserant@0: def HitTest(self, pt): etisserant@0: rect = self.BoundingBox etisserant@0: return rect.InsideXY(pt.x, pt.y) etisserant@0: lbessard@42: # Returns if the point given is in the bounding box lbessard@42: def IsInSelection(self, rect): lbessard@42: return rect.InsideXY(self.BoundingBox.x, self.BoundingBox.y) and rect.InsideXY(self.BoundingBox.x + self.BoundingBox.width, self.BoundingBox.y + self.BoundingBox.height) lbessard@42: etisserant@0: # Override this method for refreshing the bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: pass etisserant@0: etisserant@0: # Returns the bounding box etisserant@0: def GetBoundingBox(self): etisserant@0: return self.BoundingBox etisserant@0: lbessard@144: # Returns the RedrawRect lbessard@144: def GetRedrawRect(self, movex = 0, movey = 0): lbessard@144: rect = wx.Rect() lbessard@144: rect.x = self.BoundingBox.x - HANDLE_SIZE - 2 - abs(movex) lbessard@144: rect.y = self.BoundingBox.y - HANDLE_SIZE - 2 - abs(movey) lbessard@144: rect.width = self.BoundingBox.width + 2 * (HANDLE_SIZE + abs(movex)) + 4 lbessard@144: rect.height = self.BoundingBox.height + 2 * (HANDLE_SIZE + abs(movey)) + 4 lbessard@144: return rect lbessard@144: lbessard@144: def Refresh(self, rect = None): lbessard@145: if rect is not None: lbessard@155: self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False) lbessard@155: else: lbessard@155: self.Parent.RefreshRect(self.Parent.GetScrolledRect(self.GetRedrawRect()), False) lbessard@144: etisserant@0: # Change the variable that indicates if this element is selected etisserant@0: def SetSelected(self, selected): etisserant@0: self.Selected = selected lbessard@144: self.Refresh() etisserant@0: lbessard@140: # Change the variable that indicates if this element is highlighted lbessard@140: def SetHighlighted(self, highlighted): lbessard@140: self.Highlighted = highlighted lbessard@144: self.Refresh() lbessard@140: etisserant@0: # Test if the point is on a handle of this element etisserant@0: def TestHandle(self, pt): lbessard@64: extern_rect = wx.Rect(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y - HANDLE_SIZE - 2, lbessard@58: self.BoundingBox.width + 2 * HANDLE_SIZE + 4, self.BoundingBox.height + 2 * HANDLE_SIZE + 4) lbessard@64: intern_rect = wx.Rect(self.BoundingBox.x - 2, self.BoundingBox.y - 2, lbessard@58: self.BoundingBox.width + 4, self.BoundingBox.height + 4) etisserant@0: # Verify that this element is selected lbessard@58: if self.Selected and extern_rect.InsideXY(pt.x, pt.y) and not intern_rect.InsideXY(pt.x, pt.y): etisserant@0: # Find if point is on a handle horizontally etisserant@0: if self.BoundingBox.x - HANDLE_SIZE - 2 <= pt.x < self.BoundingBox.x - 2: etisserant@0: handle_x = 1 etisserant@0: elif self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2 <= pt.x < self.BoundingBox.x + (self.BoundingBox.width + HANDLE_SIZE) / 2: etisserant@0: handle_x = 2 etisserant@0: elif self.BoundingBox.x + self.BoundingBox.width + 2 <= pt.x < self.BoundingBox.x + self.BoundingBox.width + HANDLE_SIZE + 2: etisserant@0: handle_x = 3 etisserant@0: else: etisserant@0: handle_x = 0 etisserant@0: # Find if point is on a handle vertically etisserant@0: if self.BoundingBox.y - HANDLE_SIZE - 2 <= pt.y < self.BoundingBox.y - 2: etisserant@0: handle_y = 1 etisserant@0: elif self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2 <= pt.y < self.BoundingBox.y + (self.BoundingBox.height + HANDLE_SIZE) / 2: etisserant@0: handle_y = 2 etisserant@0: elif self.BoundingBox.y + self.BoundingBox.height - 2 <= pt.y < self.BoundingBox.y + self.BoundingBox.height + HANDLE_SIZE + 2: etisserant@0: handle_y = 3 etisserant@0: else: etisserant@0: handle_y = 0 etisserant@0: # Verify that the result is valid etisserant@0: if (handle_x, handle_y) in VALID_HANDLES: etisserant@0: return handle_x, handle_y etisserant@0: return 0, 0 etisserant@0: etisserant@0: # Method called when a LeftDown event have been generated lbessard@27: def OnLeftDown(self, event, dc, scaling): lbessard@27: pos = event.GetLogicalPosition(dc) etisserant@0: # Test if an handle have been clicked lbessard@58: handle = self.TestHandle(pos) etisserant@0: # Find which type of handle have been clicked, etisserant@0: # Save a resize event and change the cursor lbessard@58: cursor = HANDLE_CURSORS.get(handle, 1) lbessard@58: if cursor != self.CurrentCursor: lbessard@58: self.Parent.SetCursor(CURSORS[cursor]) lbessard@58: self.CurrentCursor = cursor lbessard@58: if cursor > 1: lbessard@58: self.Handle = (HANDLE_RESIZE, handle) etisserant@0: else: etisserant@0: self.Handle = (HANDLE_MOVE, None) etisserant@0: self.SetSelected(False) etisserant@0: # Initializes the last position lbessard@27: self.oldPos = GetScaledEventPosition(event, dc, scaling) etisserant@0: etisserant@0: # Method called when a LeftUp event have been generated lbessard@27: def OnLeftUp(self, event, dc, scaling): etisserant@0: # If a dragging have been initiated etisserant@0: if self.Dragging and self.oldPos: etisserant@0: self.RefreshModel() lbessard@56: self.Parent.RefreshBuffer() lbessard@58: if self.CurrentCursor != 0: lbessard@58: self.Parent.SetCursor(CURSORS[0]) lbessard@58: self.CurrentCursor = 0 etisserant@0: self.SetSelected(True) etisserant@0: self.oldPos = None etisserant@0: lbessard@145: # Method called when a RightDown event have been generated lbessard@145: def OnRightDown(self, event, dc, scaling): lbessard@145: pass lbessard@145: etisserant@0: # Method called when a RightUp event have been generated lbessard@27: def OnRightUp(self, event, dc, scaling): lbessard@145: if self.Dragging and self.oldPos: lbessard@145: self.RefreshModel() lbessard@145: self.Parent.RefreshBuffer() lbessard@145: if self.CurrentCursor != 0: lbessard@145: self.Parent.SetCursor(CURSORS[0]) lbessard@145: self.CurrentCursor = 0 etisserant@0: self.SetSelected(True) etisserant@0: self.oldPos = None etisserant@0: etisserant@0: # Method called when a LeftDClick event have been generated lbessard@27: def OnLeftDClick(self, event, dc, scaling): etisserant@0: pass etisserant@0: etisserant@0: # Method called when a Motion event have been generated lbessard@27: def OnMotion(self, event, dc, scaling): etisserant@0: # If the cursor is dragging and the element have been clicked etisserant@0: if event.Dragging() and self.oldPos: etisserant@0: # Calculate the movement of cursor lbessard@145: pos = event.GetLogicalPosition(dc) etisserant@0: movex = pos.x - self.oldPos.x etisserant@0: movey = pos.y - self.oldPos.y etisserant@0: # If movement is greater than MIN_MOVE then a dragging is initiated etisserant@0: if not self.Dragging and (abs(movex) > MIN_MOVE or abs(movey) > MIN_MOVE): etisserant@0: self.Dragging = True etisserant@0: # If a dragging have been initiated, refreshes the element state etisserant@0: if self.Dragging: lbessard@165: dragx, dragy = self.ProcessDragging(movex, movey, event.ShiftDown(), scaling) lbessard@138: self.oldPos.x += dragx lbessard@138: self.oldPos.y += dragy lbessard@144: return dragx, dragy lbessard@144: return movex, movey etisserant@0: # If cursor just pass over the element, changes the cursor if it is on a handle etisserant@0: else: lbessard@27: pos = event.GetLogicalPosition(dc) etisserant@0: handle = self.TestHandle(pos) lbessard@58: # Find which type of handle have been clicked, lbessard@58: # Save a resize event and change the cursor lbessard@58: cursor = HANDLE_CURSORS.get(handle, 0) lbessard@58: if cursor != self.CurrentCursor: lbessard@58: self.Parent.SetCursor(CURSORS[cursor]) lbessard@58: self.CurrentCursor = cursor lbessard@144: return 0, 0 lbessard@58: etisserant@0: # Moves the element etisserant@0: def Move(self, dx, dy, exclude = []): etisserant@0: self.Pos.x += dx etisserant@0: self.Pos.y += dy etisserant@0: self.RefreshConnected(exclude) etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Resizes the element from position and size given etisserant@0: def Resize(self, x, y, width, height): etisserant@0: self.Move(x, y) etisserant@0: self.SetSize(width, height) etisserant@0: etisserant@0: # Refreshes the element state according to move defined and handle selected etisserant@175: def ProcessDragging(self, movex, movey, centered, scaling, width_fac = 1, height_fac = 1): etisserant@0: handle_type, handle = self.Handle etisserant@0: # If it is a resize handle, calculate the values from resizing etisserant@0: if handle_type == HANDLE_RESIZE: etisserant@175: if scaling is not None: etisserant@175: scaling = (scaling[0] * width_fac, scaling[1] * height_fac) lbessard@110: x = y = start_x = start_y = 0 lbessard@110: width, height = start_width, start_height = self.GetSize() etisserant@0: if handle[0] == 1: lbessard@145: movex = max(-self.BoundingBox.x, movex) lbessard@145: if scaling is not None: etisserant@180: movex = -(round(float(width - movex) / float(scaling[0])) * scaling[0] - width) lbessard@145: x = movex lbessard@165: if centered: lbessard@165: width -= 2 * movex lbessard@165: else: lbessard@165: width -= movex etisserant@0: elif handle[0] == 3: lbessard@145: if scaling is not None: etisserant@180: movex = round(float(width + movex) / float(scaling[0])) * scaling[0] - width lbessard@165: if centered: lbessard@165: x = -movex lbessard@165: width += 2 * movex lbessard@165: else: lbessard@165: width += movex etisserant@0: if handle[1] == 1: lbessard@145: movey = max(-self.BoundingBox.y, movey) lbessard@145: if scaling is not None: etisserant@180: movey = -(round(float(height - movey) / float(scaling[1])) * scaling[1] - height) lbessard@145: y = movey lbessard@165: if centered: lbessard@165: height -= 2 * movey lbessard@165: else: lbessard@165: height -= movey etisserant@0: elif handle[1] == 3: lbessard@145: if scaling is not None: etisserant@180: movey = round(float(height + movey) / float(scaling[1])) * scaling[1] - height lbessard@165: if centered: lbessard@165: y = -movey lbessard@165: height += 2 * movey lbessard@165: else: lbessard@165: height += movey etisserant@0: # Verify that new size is not lesser than minimum etisserant@0: min_width, min_height = self.GetMinSize() lbessard@110: if handle[0] != 2 and (width >= min_width or width > self.Size[0]): lbessard@110: start_x = x lbessard@110: start_width = width lbessard@138: else: lbessard@138: movex = 0 lbessard@110: if handle[1] != 2 and (height >= min_height or height > self.Size[1]): lbessard@110: start_y = y lbessard@110: start_height = height lbessard@138: else: lbessard@138: movey = 0 lbessard@138: if movex != 0 or movey != 0: lbessard@110: self.Resize(start_x, start_y, start_width, start_height) lbessard@138: return movex, movey etisserant@0: # If it is a move handle, Move this element etisserant@0: elif handle_type == HANDLE_MOVE: lbessard@138: movex = max(-self.BoundingBox.x, movex) lbessard@138: movey = max(-self.BoundingBox.y, movey) lbessard@145: if scaling is not None: lbessard@145: movex = round(float(self.Pos.x + movex) / float(scaling[0])) * scaling[0] - self.Pos.x lbessard@145: movey = round(float(self.Pos.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y etisserant@0: self.Move(movex, movey) lbessard@138: return movex, movey lbessard@138: return 0, 0 etisserant@0: etisserant@0: # Override this method for defining the method to call for refreshing the model of this element etisserant@0: def RefreshModel(self, move=True): etisserant@0: pass etisserant@0: lbessard@140: # Draws the highlightment of this element if it is highlighted (can be overwritten) lbessard@140: def DrawHighlightment(self, dc): lbessard@144: dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) lbessard@144: dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) lbessard@144: dc.SetLogicalFunction(wx.AND) lbessard@144: dc.DrawRectangle(self.Pos.x - 2, self.Pos.y - 2, self.Size.width + 5, self.Size.height + 5) lbessard@144: dc.SetLogicalFunction(wx.COPY) lbessard@140: etisserant@0: # Draws the handles of this element if it is selected etisserant@0: def Draw(self, dc): lbessard@144: if self.Highlighted: lbessard@144: self.DrawHighlightment(dc) etisserant@0: if self.Selected: lbessard@64: dc.SetPen(wx.BLACK_PEN) lbessard@64: dc.SetBrush(wx.BLACK_BRUSH) etisserant@0: dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y - HANDLE_SIZE - 2, HANDLE_SIZE, HANDLE_SIZE) etisserant@0: dc.DrawRectangle(self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2, etisserant@0: self.BoundingBox.y - HANDLE_SIZE - 2, HANDLE_SIZE, HANDLE_SIZE) etisserant@0: dc.DrawRectangle(self.BoundingBox.x + self.BoundingBox.width + 2, etisserant@0: self.BoundingBox.y - HANDLE_SIZE - 2, HANDLE_SIZE, HANDLE_SIZE) etisserant@0: dc.DrawRectangle(self.BoundingBox.x + self.BoundingBox.width + 2, etisserant@0: self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2, HANDLE_SIZE, HANDLE_SIZE) etisserant@0: dc.DrawRectangle(self.BoundingBox.x + self.BoundingBox.width + 2, etisserant@0: self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE) etisserant@0: dc.DrawRectangle(self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2, etisserant@0: self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE) etisserant@0: dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE) etisserant@0: dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2, HANDLE_SIZE, HANDLE_SIZE) etisserant@0: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Group of graphic elements etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements a group of graphic elements etisserant@0: """ etisserant@0: etisserant@0: class Graphic_Group(Graphic_Element): etisserant@0: etisserant@0: # Create a new group of graphic elements etisserant@0: def __init__(self, parent): etisserant@0: Graphic_Element.__init__(self, parent) etisserant@0: self.Elements = [] lbessard@42: self.RefreshWireExclusion() etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Destructor etisserant@0: def __del__(self): etisserant@0: self.Elements = [] etisserant@0: lbessard@42: # Refresh the list of wire excluded lbessard@42: def RefreshWireExclusion(self): lbessard@42: self.WireExcluded = [] lbessard@42: for element in self.Elements: lbessard@42: if isinstance(element, Wire): lbessard@42: startblock = element.StartConnected.GetParentBlock() lbessard@42: endblock = element.EndConnected.GetParentBlock() lbessard@42: if startblock in self.Elements and endblock in self.Elements: lbessard@42: self.WireExcluded.append(element) lbessard@42: etisserant@0: # Make a clone of this group lbessard@162: def Clone(self, parent): lbessard@162: clone = Graphic_Group(parent) etisserant@0: elements = [] etisserant@0: # Makes a clone of all the elements in this group etisserant@0: for element in self.Elements: lbessard@162: elements.append(element.Clone(parent)) etisserant@0: clone.SetElements(elements) etisserant@0: return clone etisserant@0: lbessard@144: # Returns the RedrawRect lbessard@144: def GetRedrawRect(self, movex = 0, movey = 0): lbessard@144: rect = None lbessard@144: for element in self.Elements: lbessard@144: if rect is None: lbessard@144: rect = element.GetRedrawRect(movex, movey) lbessard@144: else: lbessard@144: rect = rect.Union(element.GetRedrawRect(movex, movey)) lbessard@144: return rect lbessard@144: etisserant@0: # Clean this group of elements etisserant@0: def Clean(self): etisserant@0: # Clean all the elements of the group etisserant@0: for element in self.Elements: etisserant@0: element.Clean() etisserant@0: etisserant@0: # Delete this group of elements etisserant@0: def Delete(self): etisserant@0: # Delete all the elements of the group etisserant@0: for element in self.Elements: etisserant@0: element.Delete() lbessard@42: self.WireExcluded = [] etisserant@0: etisserant@0: # Returns if the point given is in the bounding box of one of the elements of this group etisserant@0: def HitTest(self, pt): etisserant@0: result = False etisserant@0: for element in self.Elements: etisserant@0: result |= element.HitTest(pt) etisserant@0: return result etisserant@0: etisserant@0: # Returns if the element given is in this group etisserant@0: def IsElementIn(self, element): etisserant@0: return element in self.Elements etisserant@0: etisserant@0: # Change the elements of the group etisserant@0: def SetElements(self, elements): etisserant@0: self.Elements = elements lbessard@42: self.RefreshWireExclusion() etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Returns the elements of the group etisserant@0: def GetElements(self): etisserant@0: return self.Elements etisserant@0: lbessard@138: # Align the group elements lbessard@138: def AlignElements(self, horizontally, vertically): lbessard@138: minx = self.BoundingBox.x + self.BoundingBox.width lbessard@138: miny = self.BoundingBox.y + self.BoundingBox.height lbessard@138: maxx = self.BoundingBox.x lbessard@138: maxy = self.BoundingBox.y lbessard@138: for element in self.Elements: lbessard@138: if not isinstance(element, Wire): lbessard@138: posx, posy = element.GetPosition() lbessard@138: width, height = element.GetSize() lbessard@138: minx = min(minx, posx) lbessard@138: miny = min(miny, posy) lbessard@138: maxx = max(maxx, posx + width) lbessard@138: maxy = max(maxy, posy + height) lbessard@138: for element in self.Elements: lbessard@138: if not isinstance(element, Wire): lbessard@138: posx, posy = element.GetPosition() lbessard@138: width, height = element.GetSize() lbessard@138: movex = movey = 0 lbessard@138: if horizontally == ALIGN_LEFT: lbessard@138: movex = minx - posx lbessard@138: elif horizontally == ALIGN_CENTER: lbessard@138: movex = (maxx + minx - width) / 2 - posx lbessard@138: elif horizontally == ALIGN_RIGHT: lbessard@138: movex = maxx - width - posx lbessard@138: if vertically == ALIGN_TOP: lbessard@138: movey = miny - posy lbessard@138: elif vertically == ALIGN_MIDDLE: lbessard@138: movey = (maxy + miny - height) / 2 - posy lbessard@138: elif vertically == ALIGN_BOTTOM: lbessard@138: movey = maxy - height - posy lbessard@138: if movex != 0 or movey != 0: lbessard@138: element.Move(movex, movey) lbessard@138: element.RefreshModel() lbessard@138: self.RefreshWireExclusion() lbessard@138: self.RefreshBoundingBox() lbessard@138: etisserant@0: # Remove or select the given element if it is or not in the group etisserant@0: def SelectElement(self, element): etisserant@0: if element in self.Elements: etisserant@0: self.Elements.remove(element) etisserant@0: else: etisserant@0: self.Elements.append(element) lbessard@42: self.RefreshWireExclusion() etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Move this group of elements etisserant@0: def Move(self, movex, movey): etisserant@0: # Move all the elements of the group etisserant@0: for element in self.Elements: lbessard@42: if not isinstance(element, Wire): lbessard@42: element.Move(movex, movey, self.WireExcluded) lbessard@42: elif element in self.WireExcluded: etisserant@0: element.Move(movex, movey, True) etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Refreshes the bounding box of this group of elements etisserant@0: def RefreshBoundingBox(self): etisserant@0: if len(self.Elements) > 0: etisserant@0: bbox = self.Elements[0].GetBoundingBox() etisserant@0: minx, miny = bbox.x, bbox.y etisserant@0: maxx = bbox.x + bbox.width etisserant@0: maxy = bbox.y + bbox.height etisserant@0: for element in self.Elements[1:]: etisserant@0: bbox = element.GetBoundingBox() etisserant@0: minx = min(minx, bbox.x) etisserant@0: miny = min(miny, bbox.y) etisserant@0: maxx = max(maxx, bbox.x + bbox.width) etisserant@0: maxy = max(maxy, bbox.y + bbox.height) lbessard@64: self.BoundingBox = wx.Rect(minx, miny, maxx - minx, maxy - miny) etisserant@0: else: lbessard@64: self.BoundingBox = wx.Rect(0, 0, 0, 0) etisserant@0: etisserant@0: # Forbids to change the group position etisserant@0: def SetPosition(x, y): etisserant@0: pass etisserant@0: etisserant@0: # Returns the position of this group etisserant@0: def GetPosition(self): etisserant@0: return self.BoundingBox.x, self.BoundingBox.y etisserant@0: etisserant@0: # Forbids to change the group size etisserant@0: def SetSize(width, height): etisserant@0: pass etisserant@0: etisserant@0: # Returns the size of this group etisserant@0: def GetSize(self): etisserant@0: return self.BoundingBox.width, self.BoundingBox.height etisserant@0: lbessard@140: # Change the variable that indicates if this element is highlighted lbessard@140: def SetHighlighted(self, highlighted): lbessard@140: for element in self.Elements: lbessard@140: element.SetHighlighted(highlighted) lbessard@140: etisserant@0: # Change the variable that indicates if the elemente is selected etisserant@0: def SetSelected(self, selected): etisserant@0: for element in self.Elements: etisserant@0: element.SetSelected(selected) etisserant@0: lbessard@138: # Method called when a RightUp event has been generated lbessard@138: def OnRightUp(self, event, dc, scaling): lbessard@138: # Popup the menu with special items for a group lbessard@138: self.Parent.PopupGroupMenu() lbessard@138: etisserant@0: # Refreshes the model of all the elements of this group etisserant@0: def RefreshModel(self): etisserant@0: for element in self.Elements: etisserant@0: element.RefreshModel() etisserant@0: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Connector for all types of blocks etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements a connector for any type of block etisserant@0: """ etisserant@0: etisserant@0: class Connector: etisserant@0: etisserant@0: # Create a new connector lbessard@27: def __init__(self, parent, name, type, position, direction, negated = False, edge = "none", onlyone = False): etisserant@0: self.ParentBlock = parent etisserant@0: self.Name = name etisserant@0: self.Type = type etisserant@0: self.Pos = position etisserant@0: self.Direction = direction etisserant@0: self.Wires = [] lbessard@102: if IsOfType("BOOL", type): lbessard@102: self.Negated = negated lbessard@102: self.Edge = edge lbessard@102: else: lbessard@102: self.Negated = False lbessard@102: self.Edge = "none" lbessard@27: self.OneConnected = onlyone lbessard@64: self.Pen = wx.BLACK_PEN lbessard@42: self.RefreshNameSize() etisserant@0: lbessard@144: # Returns the RedrawRect lbessard@144: def GetRedrawRect(self, movex = 0, movey = 0): lbessard@144: parent_pos = self.ParentBlock.GetPosition() lbessard@144: x = min(parent_pos[0] + self.Pos.x, parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE) lbessard@144: y = min(parent_pos[1] + self.Pos.y, parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE) lbessard@144: if self.Direction[0] == 0: lbessard@144: width = 5 lbessard@144: else: lbessard@144: width = CONNECTOR_SIZE lbessard@144: if self.Direction[1] == 0: lbessard@144: height = 5 lbessard@144: else: lbessard@144: height = CONNECTOR_SIZE lbessard@144: return wx.Rect(x - abs(movex), y - abs(movey), width + 2 * abs(movex), height + 2 * abs(movey)) lbessard@144: etisserant@0: # Change the connector pen etisserant@0: def SetPen(self, pen): etisserant@0: self.Pen = pen etisserant@0: etisserant@0: # Make a clone of the connector lbessard@112: def Clone(self, parent = None): lbessard@112: if parent is None: lbessard@112: parent = self.ParentBlock lbessard@112: return Connector(parent, self.Name, self.Type, wx.Point(self.Pos[0], self.Pos[1]), etisserant@0: self.Direction, self.Negated) etisserant@0: etisserant@0: # Returns the connector parent block etisserant@0: def GetParentBlock(self): etisserant@0: return self.ParentBlock etisserant@0: lbessard@99: # Returns the connector type lbessard@112: def GetType(self, raw = False): lbessard@112: if IsEndType(self.Type) or raw: lbessard@99: return self.Type lbessard@102: elif (self.Negated or self.Edge != "none") and IsOfType("BOOL", self.Type): lbessard@102: return "BOOL" lbessard@99: else: lbessard@102: return self.ParentBlock.GetConnectionResultType(self, self.Type) lbessard@99: lbessard@99: # Returns the connector type lbessard@99: def GetConnectedType(self): lbessard@99: if IsEndType(self.Type): lbessard@99: return self.Type lbessard@99: elif len(self.Wires) == 1: lbessard@99: return self.Wires[0][0].GetOtherConnectedType(self.Wires[0][1]) lbessard@3: return self.Type lbessard@3: lbessard@144: # Returns the connector type lbessard@144: def GetConnectedRedrawRect(self, movex, movey): lbessard@144: rect = None lbessard@144: for wire, handle in self.Wires: lbessard@144: if rect is None: lbessard@144: rect = wire.GetRedrawRect() lbessard@144: else: lbessard@144: rect = rect.Union(wire.GetRedrawRect()) lbessard@144: return rect lbessard@144: lbessard@98: # Returns if connector type is compatible with type given lbessard@98: def IsCompatible(self, type): lbessard@99: reference = self.GetType() lbessard@99: return IsOfType(type, reference) or IsOfType(reference, type) lbessard@98: lbessard@3: # Changes the connector name lbessard@3: def SetType(self, type): lbessard@3: self.Type = type lbessard@3: lbessard@3: # Returns the connector name etisserant@0: def GetName(self): etisserant@0: return self.Name etisserant@0: etisserant@0: # Changes the connector name etisserant@0: def SetName(self, name): etisserant@0: self.Name = name lbessard@42: self.RefreshNameSize() lbessard@102: lbessard@102: def SetValue(self, value): lbessard@102: for wire, handle in self.Wires: lbessard@102: wire.SetValue(value) lbessard@42: lbessard@42: # Changes the connector name size lbessard@42: def RefreshNameSize(self): lbessard@42: if self.Name != "": lbessard@64: dc = wx.ClientDC(self.ParentBlock.Parent) lbessard@42: self.NameSize = dc.GetTextExtent(self.Name) lbessard@42: else: lbessard@42: self.NameSize = 0, 0 lbessard@42: lbessard@42: # Returns the connector name size lbessard@42: def GetNameSize(self): lbessard@42: return self.NameSize etisserant@0: etisserant@0: # Returns the wires connected to the connector etisserant@0: def GetWires(self): etisserant@0: return self.Wires etisserant@0: etisserant@0: # Returns the parent block Id etisserant@0: def GetBlockId(self): etisserant@0: return self.ParentBlock.GetId() etisserant@0: etisserant@0: # Returns the connector relative position etisserant@0: def GetRelPosition(self): etisserant@0: return self.Pos etisserant@0: etisserant@0: # Returns the connector absolute position etisserant@0: def GetPosition(self, size = True): etisserant@0: parent_pos = self.ParentBlock.GetPosition() etisserant@0: # If the position of the end of the connector is asked etisserant@0: if size: etisserant@0: x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE etisserant@0: y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE etisserant@0: else: etisserant@0: x = parent_pos[0] + self.Pos.x etisserant@0: y = parent_pos[1] + self.Pos.y lbessard@64: return wx.Point(x, y) etisserant@0: etisserant@0: # Change the connector relative position etisserant@0: def SetPosition(self, pos): etisserant@0: self.Pos = pos etisserant@0: etisserant@0: # Returns the connector direction etisserant@0: def GetDirection(self): etisserant@0: return self.Direction etisserant@0: etisserant@0: # Change the connector direction etisserant@0: def SetDirection(self, direction): etisserant@0: self.Direction = direction etisserant@0: etisserant@0: # Connect a wire to this connector at the last place etisserant@0: def Connect(self, wire, refresh = True): etisserant@0: self.InsertConnect(len(self.Wires), wire, refresh) etisserant@0: etisserant@0: # Connect a wire to this connector at the place given etisserant@0: def InsertConnect(self, idx, wire, refresh = True): etisserant@0: if wire not in self.Wires: etisserant@0: self.Wires.insert(idx, wire) etisserant@0: if refresh: etisserant@0: self.ParentBlock.RefreshModel(False) etisserant@0: etisserant@0: # Returns the index of the wire given in the list of connected etisserant@0: def GetWireIndex(self, wire): etisserant@0: for i, (tmp_wire, handle) in enumerate(self.Wires): etisserant@0: if tmp_wire == wire: etisserant@0: return i etisserant@0: return None etisserant@0: etisserant@0: # Unconnect a wire or all wires connected to the connector lbessard@2: def UnConnect(self, wire = None, unconnect = True, delete = False): etisserant@0: i = 0 etisserant@0: found = False etisserant@0: while i < len(self.Wires) and not found: etisserant@0: if not wire or self.Wires[i][0] == wire: etisserant@0: # If Unconnect haven't been called from a wire, disconnect the connector in the wire etisserant@0: if unconnect: etisserant@0: if self.Wires[i][1] == 0: lbessard@2: self.Wires[i][0].UnConnectStartPoint(delete) etisserant@0: else: lbessard@2: self.Wires[i][0].UnConnectEndPoint(delete) etisserant@0: # Remove wire from connected etisserant@0: if wire: etisserant@0: self.Wires.pop(i) etisserant@0: found = True etisserant@0: i += 1 etisserant@0: # If no wire defined, unconnect all wires etisserant@0: if not wire: etisserant@0: self.Wires = [] etisserant@0: self.ParentBlock.RefreshModel(False) etisserant@0: etisserant@0: # Returns if connector has one or more wire connected etisserant@0: def IsConnected(self): etisserant@0: return len(self.Wires) > 0 etisserant@0: etisserant@0: # Move the wires connected etisserant@0: def MoveConnected(self, exclude = []): etisserant@0: if len(self.Wires) > 0: etisserant@0: # Calculate the new position of the end point etisserant@0: parent_pos = self.ParentBlock.GetPosition() etisserant@0: x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE etisserant@0: y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE etisserant@0: # Move the corresponding point on all the wires connected etisserant@0: for wire, index in self.Wires: etisserant@0: if wire not in exclude: etisserant@0: if index == 0: lbessard@64: wire.MoveStartPoint(wx.Point(x, y)) etisserant@0: else: lbessard@64: wire.MoveEndPoint(wx.Point(x, y)) etisserant@0: etisserant@0: # Refreshes the model of all the wires connected etisserant@0: def RefreshWires(self): etisserant@0: for wire in self.Wires: etisserant@0: wire[0].RefreshModel() etisserant@0: etisserant@0: # Refreshes the parent block model etisserant@0: def RefreshParentBlock(self): etisserant@0: self.ParentBlock.RefreshModel(False) etisserant@0: lbessard@140: # Highlight the parent block lbessard@140: def HighlightParentBlock(self, highlight): lbessard@140: self.ParentBlock.SetHighlighted(highlight) lbessard@144: self.ParentBlock.Refresh() lbessard@140: lbessard@27: # Returns all the blocks connected to this connector lbessard@27: def GetConnectedBlocks(self): lbessard@27: blocks = [] lbessard@27: for wire, handle in self.Wires: lbessard@27: # Get other connector connected to each wire lbessard@27: if handle == 0: lbessard@64: connector = wire.GetEndConnected() lbessard@27: else: lbessard@64: connector = wire.GetStartConnected() lbessard@27: # Get parent block for this connector lbessard@27: if connector: lbessard@27: block = connector.GetParentBlock() lbessard@27: if block not in blocks: lbessard@27: blocks.append(block) lbessard@27: return blocks lbessard@27: etisserant@0: # Returns the connector negated property etisserant@0: def IsNegated(self): etisserant@0: return self.Negated etisserant@0: etisserant@0: # Changes the connector negated property etisserant@0: def SetNegated(self, negated): lbessard@102: if IsOfType("BOOL", self.Type): lbessard@102: self.Negated = negated lbessard@102: self.Edge = "none" etisserant@0: etisserant@0: # Returns the connector edge property etisserant@0: def GetEdge(self): etisserant@0: return self.Edge etisserant@0: etisserant@0: # Changes the connector edge property etisserant@0: def SetEdge(self, edge): lbessard@102: if IsOfType("BOOL", self.Type): lbessard@102: self.Edge = edge lbessard@102: self.Negated = False etisserant@0: etisserant@0: # Tests if the point given is near from the end point of this connector etisserant@0: def TestPoint(self, pt, exclude = True): etisserant@0: parent_pos = self.ParentBlock.GetPosition() lbessard@27: if not (len(self.Wires) > 0 and self.OneConnected and exclude): etisserant@0: # Calculate a square around the end point of this connector etisserant@0: x = parent_pos[0] + self.Pos.x + self.Direction[0] * CONNECTOR_SIZE - ANCHOR_DISTANCE etisserant@0: y = parent_pos[1] + self.Pos.y + self.Direction[1] * CONNECTOR_SIZE - ANCHOR_DISTANCE etisserant@0: width = ANCHOR_DISTANCE * 2 + abs(self.Direction[0]) * CONNECTOR_SIZE etisserant@0: height = ANCHOR_DISTANCE * 2 + abs(self.Direction[1]) * CONNECTOR_SIZE lbessard@64: rect = wx.Rect(x, y, width, height) etisserant@0: return rect.InsideXY(pt.x, pt.y) etisserant@0: return False etisserant@0: lbessard@140: # Draws the highlightment of this element if it is highlighted lbessard@140: def DrawHighlightment(self, dc): lbessard@140: dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) lbessard@140: dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) lbessard@144: dc.SetLogicalFunction(wx.AND) lbessard@140: parent_pos = self.ParentBlock.GetPosition() lbessard@140: posx = parent_pos[0] + self.Pos.x lbessard@140: posy = parent_pos[1] + self.Pos.y lbessard@140: width = CONNECTOR_SIZE lbessard@140: height = CONNECTOR_SIZE lbessard@140: if self.Direction[0] < 0: lbessard@140: posx += CONNECTOR_SIZE * self.Direction[0] lbessard@140: elif self.Direction[0] == 0: lbessard@140: posx -= 2 lbessard@140: width = 5 lbessard@140: if self.Direction[1] < 0: lbessard@140: posy += CONNECTOR_SIZE * self.Direction[1] lbessard@140: elif self.Direction[1] == 0: lbessard@140: posy -= 2 lbessard@140: height = 5 lbessard@140: dc.DrawRectangle(posx, posy, width, height) lbessard@144: dc.SetLogicalFunction(wx.COPY) lbessard@140: etisserant@0: # Draws the connector etisserant@0: def Draw(self, dc): etisserant@0: dc.SetPen(self.Pen) lbessard@64: dc.SetBrush(wx.WHITE_BRUSH) etisserant@0: parent_pos = self.ParentBlock.GetPosition() lbessard@213: lbessard@213: if getattr(dc, "printing", False): lbessard@213: name_size = dc.GetTextExtent(self.Name) lbessard@213: else: lbessard@213: name_size = self.NameSize lbessard@213: etisserant@0: if self.Negated: etisserant@0: # If connector is negated, draw a circle etisserant@0: xcenter = parent_pos[0] + self.Pos.x + (CONNECTOR_SIZE * self.Direction[0]) / 2 etisserant@0: ycenter = parent_pos[1] + self.Pos.y + (CONNECTOR_SIZE * self.Direction[1]) / 2 etisserant@0: dc.DrawCircle(xcenter, ycenter, CONNECTOR_SIZE / 2) etisserant@0: else: etisserant@0: xstart = parent_pos[0] + self.Pos.x etisserant@0: ystart = parent_pos[1] + self.Pos.y etisserant@0: if self.Edge == "rising": etisserant@0: # If connector has a rising edge, draw a right arrow etisserant@0: dc.DrawLine(xstart, ystart, xstart - 4, ystart - 4) etisserant@0: dc.DrawLine(xstart, ystart, xstart - 4, ystart + 4) etisserant@0: elif self.Edge == "falling": etisserant@0: # If connector has a falling edge, draw a left arrow etisserant@0: dc.DrawLine(xstart, ystart, xstart + 4, ystart - 4) etisserant@0: dc.DrawLine(xstart, ystart, xstart + 4, ystart + 4) etisserant@0: xend = xstart + CONNECTOR_SIZE * self.Direction[0] etisserant@0: yend = ystart + CONNECTOR_SIZE * self.Direction[1] etisserant@0: dc.DrawLine(xstart + self.Direction[0], ystart + self.Direction[1], xend, yend) etisserant@0: if self.Direction[0] != 0: lbessard@213: ytext = parent_pos[1] + self.Pos.y - name_size[1] / 2 etisserant@0: if self.Direction[0] < 0: etisserant@0: xtext = parent_pos[0] + self.Pos.x + 5 etisserant@0: else: lbessard@213: xtext = parent_pos[0] + self.Pos.x - (name_size[0] + 5) etisserant@0: if self.Direction[1] != 0: lbessard@213: xtext = parent_pos[0] + self.Pos.x - name_size[0] / 2 etisserant@0: if self.Direction[1] < 0: etisserant@0: ytext = parent_pos[1] + self.Pos.y + 5 etisserant@0: else: lbessard@213: ytext = parent_pos[1] + self.Pos.y - (name_size[1] + 5) etisserant@0: # Draw the text etisserant@0: dc.DrawText(self.Name, xtext, ytext) etisserant@0: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Common Wire Element etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements a wire for connecting two blocks etisserant@0: """ etisserant@0: etisserant@0: class Wire(Graphic_Element): etisserant@0: etisserant@0: # Create a new wire etisserant@0: def __init__(self, parent, start = None, end = None): etisserant@0: Graphic_Element.__init__(self, parent) etisserant@0: self.StartPoint = start etisserant@0: self.EndPoint = end etisserant@0: self.StartConnected = None etisserant@0: self.EndConnected = None etisserant@0: # If the start and end points are defined, calculate the wire etisserant@0: if start and end: etisserant@0: self.ResetPoints() etisserant@0: self.GeneratePoints() etisserant@0: else: etisserant@0: self.Points = [] etisserant@0: self.Segments = [] etisserant@0: self.SelectedSegment = None lbessard@102: self.Value = None etisserant@0: self.OverStart = False etisserant@0: self.OverEnd = False lbessard@208: self.ComputingType = False etisserant@0: etisserant@0: # Destructor of a wire etisserant@0: def __del__(self): etisserant@0: self.StartConnected = None etisserant@0: self.EndConnected = None etisserant@0: 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: if self.StartConnected: lbessard@144: rect = rect.Union(self.StartConnected.GetRedrawRect(movex, movey)) lbessard@144: if self.EndConnected: lbessard@144: rect = rect.Union(self.EndConnected.GetRedrawRect(movex, movey)) lbessard@144: return rect lbessard@144: etisserant@0: # Forbids to change the wire position etisserant@0: def SetPosition(x, y): etisserant@0: pass etisserant@0: etisserant@0: # Forbids to change the wire size etisserant@0: def SetSize(width, height): etisserant@0: pass etisserant@0: lbessard@27: # Returns connector to which start point is connected lbessard@27: def GetStartConnected(self): lbessard@27: return self.StartConnected lbessard@27: lbessard@98: # Returns connector to which start point is connected lbessard@98: def GetStartConnectedType(self): lbessard@208: if self.StartConnected and not self.ComputingType: lbessard@208: self.ComputingType = True lbessard@208: computed_type = self.StartConnected.GetType() lbessard@208: self.ComputingType = False lbessard@208: return computed_type lbessard@98: return None lbessard@98: lbessard@27: # Returns connector to which end point is connected lbessard@27: def GetEndConnected(self): lbessard@27: return self.EndConnected lbessard@27: lbessard@98: # Returns connector to which end point is connected lbessard@98: def GetEndConnectedType(self): lbessard@208: if self.EndConnected and not self.ComputingType: lbessard@208: self.ComputingType = True lbessard@208: computed_type = self.EndConnected.GetType() lbessard@208: self.ComputingType = False lbessard@208: return computed_type lbessard@98: return None lbessard@98: lbessard@145: def GetOtherConnected(self, connector): lbessard@145: if self.StartConnected == connector: lbessard@145: return self.EndConnected lbessard@145: else: lbessard@145: return self.StartConnected lbessard@145: lbessard@99: def GetOtherConnectedType(self, handle): lbessard@99: if handle == 0: lbessard@99: return self.GetEndConnectedType() lbessard@99: else: lbessard@99: return self.GetStartConnectedType() lbessard@99: lbessard@98: def IsConnectedCompatible(self): lbessard@98: if self.StartConnected: lbessard@98: return self.StartConnected.IsCompatible(self.GetEndConnectedType()) lbessard@98: elif self.EndConnected: lbessard@98: return True lbessard@98: return False lbessard@98: lbessard@102: def SetValue(self, value): lbessard@102: self.Value = value lbessard@102: etisserant@0: # Unconnect the start and end points etisserant@0: def Clean(self): etisserant@0: if self.StartConnected: etisserant@0: self.UnConnectStartPoint() etisserant@0: if self.EndConnected: etisserant@0: self.UnConnectEndPoint() etisserant@0: etisserant@0: # Delete this wire by calling the corresponding method etisserant@0: def Delete(self): etisserant@0: self.Parent.DeleteWire(self) etisserant@0: etisserant@0: # Select a segment and not the whole wire. It's useful for Ladder Diagram etisserant@0: def SetSelectedSegment(self, segment): etisserant@0: # The last segment is indicated etisserant@0: if segment == -1: etisserant@0: segment = len(self.Segments) - 1 etisserant@0: # The selected segment is reinitialised etisserant@0: if segment == None: etisserant@0: if self.StartConnected: lbessard@64: self.StartConnected.SetPen(wx.BLACK_PEN) etisserant@0: if self.EndConnected: lbessard@64: self.EndConnected.SetPen(wx.BLACK_PEN) etisserant@0: # The segment selected is the first etisserant@0: elif segment == 0: etisserant@0: if self.StartConnected: lbessard@64: self.StartConnected.SetPen(wx.RED_PEN) etisserant@0: if self.EndConnected: etisserant@0: # There is only one segment etisserant@0: if len(self.Segments) == 1: lbessard@64: self.EndConnected.SetPen(wx.RED_PEN) etisserant@0: else: lbessard@64: self.EndConnected.SetPen(wx.BLACK_PEN) etisserant@0: # The segment selected is the last etisserant@0: elif segment == len(self.Segments) - 1: etisserant@0: if self.StartConnected: lbessard@64: self.StartConnected.SetPen(wx.BLACK_PEN) etisserant@0: if self.EndConnected: lbessard@64: self.EndConnected.SetPen(wx.RED_PEN) etisserant@0: self.SelectedSegment = segment lbessard@144: self.Refresh() etisserant@0: etisserant@0: # Reinitialize the wire points etisserant@0: def ResetPoints(self): etisserant@0: if self.StartPoint and self.EndPoint: etisserant@0: self.Points = [self.StartPoint[0], self.EndPoint[0]] etisserant@0: self.Segments = [self.StartPoint[1]] etisserant@0: else: etisserant@0: self.Points = [] etisserant@0: self.Segments = [] etisserant@0: etisserant@0: # Refresh the wire bounding box etisserant@0: def RefreshBoundingBox(self): etisserant@0: if len(self.Points) > 0: etisserant@0: # If startpoint or endpoint is connected, save the point radius etisserant@0: start_radius = end_radius = 0 etisserant@0: if not self.StartConnected: etisserant@0: start_radius = POINT_RADIUS etisserant@0: if not self.EndConnected: etisserant@0: end_radius = POINT_RADIUS etisserant@0: # Initialize minimum and maximum from the first point etisserant@0: minx, minbbxx = self.Points[0].x, self.Points[0].x - start_radius etisserant@0: maxx, maxbbxx = self.Points[0].x, self.Points[0].x + start_radius etisserant@0: miny, minbbxy = self.Points[0].y, self.Points[0].y - start_radius etisserant@0: maxy, maxbbxy = self.Points[0].y, self.Points[0].y + start_radius etisserant@0: # Actualize minimum and maximum with the other points etisserant@0: for point in self.Points[1:-1]: etisserant@0: minx, minbbxx = min(minx, point.x), min(minbbxx, point.x) etisserant@0: maxx, maxbbxx = max(maxx, point.x), max(maxbbxx, point.x) etisserant@0: miny, minbbxy = min(miny, point.y), min(minbbxy, point.y) etisserant@0: maxy, maxbbxy = max(maxy, point.y), max(maxbbxy, point.y) etisserant@0: if len(self.Points) > 1: etisserant@0: minx, minbbxx = min(minx, self.Points[-1].x), min(minbbxx, self.Points[-1].x - end_radius) etisserant@0: maxx, maxbbxx = max(maxx, self.Points[-1].x), max(maxbbxx, self.Points[-1].x + end_radius) etisserant@0: miny, minbbxy = min(miny, self.Points[-1].y), min(minbbxy, self.Points[-1].y - end_radius) etisserant@0: maxy, maxbbxy = max(maxy, self.Points[-1].y), max(maxbbxy, self.Points[-1].y + end_radius) lbessard@108: self.Pos.x, self.Pos.y = minx, miny lbessard@208: self.Size = wx.Size(maxx - minx, maxy - miny) lbessard@64: self.BoundingBox = wx.Rect(minbbxx, minbbxy, maxbbxx - minbbxx + 1, maxbbxy - minbbxy + 1) etisserant@0: etisserant@0: # Refresh the realpoints that permits to keep the proportionality in wire during resizing etisserant@0: def RefreshRealPoints(self): etisserant@0: if len(self.Points) > 0: etisserant@0: self.RealPoints = [] etisserant@0: # Calculate float relative position of each point with the minimum point etisserant@0: for point in self.Points: etisserant@0: self.RealPoints.append([float(point.x - self.Pos.x), float(point.y - self.Pos.y)]) etisserant@0: etisserant@0: # Returns the wire minimum size etisserant@0: def GetMinSize(self): etisserant@0: width = 1 etisserant@0: height = 1 etisserant@0: dir_product = product(self.StartPoint[1], self.EndPoint[1]) etisserant@0: # The directions are opposed etisserant@0: if dir_product < 0: etisserant@0: if self.StartPoint[0] != 0: etisserant@0: width = MIN_SEGMENT_SIZE * 2 etisserant@0: if self.StartPoint[1] != 0: etisserant@0: height = MIN_SEGMENT_SIZE * 2 etisserant@0: # The directions are the same etisserant@0: elif dir_product > 0: etisserant@0: if self.StartPoint[0] != 0: etisserant@0: width = MIN_SEGMENT_SIZE etisserant@0: if self.StartPoint[1] != 0: etisserant@0: height = MIN_SEGMENT_SIZE etisserant@0: # The directions are perpendiculars etisserant@0: else: etisserant@0: width = MIN_SEGMENT_SIZE etisserant@0: height = MIN_SEGMENT_SIZE etisserant@0: return width + 1, height + 1 etisserant@0: etisserant@0: # Returns if the point given is on one of the wire segments etisserant@0: def HitTest(self, pt): etisserant@0: test = False etisserant@0: for i in xrange(len(self.Points) - 1): lbessard@64: rect = wx.Rect(0, 0, 0, 0) etisserant@0: x1, y1 = self.Points[i].x, self.Points[i].y etisserant@0: x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y etisserant@0: # Calculate a rectangle around the segment lbessard@64: rect = wx.Rect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, etisserant@0: abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) etisserant@0: test |= rect.InsideXY(pt.x, pt.y) etisserant@0: return test etisserant@0: etisserant@0: # Returns the wire start or end point if the point given is on one of them etisserant@0: def TestPoint(self, pt): etisserant@0: # Test the wire start point lbessard@64: rect = wx.Rect(self.Points[0].x - ANCHOR_DISTANCE, self.Points[0].y - ANCHOR_DISTANCE, etisserant@0: 2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) etisserant@0: if rect.InsideXY(pt.x, pt.y): etisserant@0: return 0 etisserant@0: # Test the wire end point etisserant@0: if len(self.Points) > 1: lbessard@64: rect = wx.Rect(self.Points[-1].x - ANCHOR_DISTANCE, self.Points[-1].y - ANCHOR_DISTANCE, etisserant@0: 2 * ANCHOR_DISTANCE, 2 * ANCHOR_DISTANCE) etisserant@0: if rect.InsideXY(pt.x, pt.y): etisserant@0: return -1 etisserant@0: return None etisserant@0: etisserant@0: # Returns the wire segment if the point given is on it etisserant@0: def TestSegment(self, pt, all=False): etisserant@0: for i in xrange(len(self.Segments)): etisserant@0: # If wire is not in a Ladder Diagram, first and last segments are excluded etisserant@0: if 0 < i < len(self.Segments) - 1 or all: etisserant@0: x1, y1 = self.Points[i].x, self.Points[i].y etisserant@0: x2, y2 = self.Points[i + 1].x, self.Points[i + 1].y etisserant@0: # Calculate a rectangle around the segment lbessard@64: rect = wx.Rect(min(x1, x2) - ANCHOR_DISTANCE, min(y1, y2) - ANCHOR_DISTANCE, etisserant@0: abs(x1 - x2) + 2 * ANCHOR_DISTANCE, abs(y1 - y2) + 2 * ANCHOR_DISTANCE) etisserant@0: if rect.InsideXY(pt.x, pt.y): etisserant@0: return i, self.Segments[i] etisserant@0: return None etisserant@0: etisserant@0: # Define the wire points etisserant@0: def SetPoints(self, points): etisserant@0: if len(points) > 1: lbessard@64: self.Points = [wx.Point(x, y) for x, y in points] etisserant@0: # Calculate the start and end directions etisserant@0: self.StartPoint = [None, vector(self.Points[0], self.Points[1])] etisserant@0: self.EndPoint = [None, vector(self.Points[-1], self.Points[-2])] etisserant@0: # Calculate the start and end points lbessard@64: self.StartPoint[0] = wx.Point(self.Points[0].x + CONNECTOR_SIZE * self.StartPoint[1][0], etisserant@0: self.Points[0].y + CONNECTOR_SIZE * self.StartPoint[1][1]) lbessard@64: self.EndPoint[0] = wx.Point(self.Points[-1].x + CONNECTOR_SIZE * self.EndPoint[1][0], etisserant@0: self.Points[-1].y + CONNECTOR_SIZE * self.EndPoint[1][1]) etisserant@0: self.Points[0] = self.StartPoint[0] etisserant@0: self.Points[-1] = self.EndPoint[0] etisserant@0: # Calculate the segments directions etisserant@0: self.Segments = [] etisserant@0: for i in xrange(len(self.Points) - 1): etisserant@0: self.Segments.append(vector(self.Points[i], self.Points[i + 1])) etisserant@0: self.RefreshBoundingBox() etisserant@0: self.RefreshRealPoints() etisserant@0: etisserant@0: # Returns the position of the point indicated etisserant@0: def GetPoint(self, index): etisserant@0: if index < len(self.Points): etisserant@0: return self.Points[index].x, self.Points[index].y etisserant@0: return None etisserant@0: etisserant@0: # Returns a list of the position of all wire points etisserant@0: def GetPoints(self, invert = False): etisserant@0: points = self.VerifyPoints() lbessard@64: points[0] = wx.Point(points[0].x - CONNECTOR_SIZE * self.StartPoint[1][0], etisserant@0: points[0].y - CONNECTOR_SIZE * self.StartPoint[1][1]) lbessard@64: points[-1] = wx.Point(points[-1].x - CONNECTOR_SIZE * self.EndPoint[1][0], etisserant@0: points[-1].y - CONNECTOR_SIZE * self.EndPoint[1][1]) etisserant@0: # An inversion of the list is asked etisserant@0: if invert: etisserant@0: points.reverse() etisserant@0: return points etisserant@0: etisserant@0: # Returns the position of the two selected segment points etisserant@0: def GetSelectedSegmentPoints(self): etisserant@0: if self.SelectedSegment != None and len(self.Points) > 1: etisserant@0: return self.Points[self.SelectedSegment:self.SelectedSegment + 2] etisserant@0: return [] etisserant@0: etisserant@0: # Returns if the selected segment is the first and/or the last of the wire etisserant@0: def GetSelectedSegmentConnections(self): etisserant@0: if self.SelectedSegment != None and len(self.Points) > 1: etisserant@0: return self.SelectedSegment == 0, self.SelectedSegment == len(self.Segments) - 1 etisserant@0: return (True, True) etisserant@0: etisserant@0: # Returns the connectors on which the wire is connected etisserant@0: def GetConnected(self): etisserant@0: connected = [] etisserant@0: if self.StartConnected and self.StartPoint[1] == WEST: etisserant@0: connected.append(self.StartConnected) etisserant@0: if self.EndConnected and self.EndPoint[1] == WEST: etisserant@0: connected.append(self.EndConnected) etisserant@0: return connected etisserant@0: etisserant@0: # Returns the id of the block connected to the first or the last wire point lbessard@27: def GetConnectedInfos(self, index): etisserant@0: if index == 0 and self.StartConnected: lbessard@27: return self.StartConnected.GetBlockId(), self.StartConnected.GetName() etisserant@0: elif index == -1 and self.EndConnected: lbessard@42: return self.EndConnected.GetBlockId(), self.EndConnected.GetName() etisserant@0: return None etisserant@0: etisserant@0: # Update the wire points position by keeping at most possible the current positions etisserant@0: def GeneratePoints(self, realpoints = True): etisserant@0: i = 0 etisserant@0: # Calculate the start enad end points with the minimum segment size in the right direction lbessard@64: end = wx.Point(self.EndPoint[0].x + self.EndPoint[1][0] * MIN_SEGMENT_SIZE, etisserant@0: self.EndPoint[0].y + self.EndPoint[1][1] * MIN_SEGMENT_SIZE) lbessard@64: start = wx.Point(self.StartPoint[0].x + self.StartPoint[1][0] * MIN_SEGMENT_SIZE, etisserant@0: self.StartPoint[0].y + self.StartPoint[1][1] * MIN_SEGMENT_SIZE) etisserant@0: # Evaluate the point till it's the last etisserant@0: while i < len(self.Points) - 1: etisserant@0: # The next point is the last etisserant@0: if i + 1 == len(self.Points) - 1: etisserant@0: # Calculate the direction from current point to end point etisserant@0: v_end = vector(self.Points[i], end) etisserant@0: # The current point is the first etisserant@0: if i == 0: etisserant@0: # If the end point is not in the start direction, a point is added etisserant@0: if v_end != self.Segments[0] or v_end == self.EndPoint[1]: lbessard@64: self.Points.insert(1, wx.Point(start.x, start.y)) etisserant@0: self.Segments.insert(1, DirectionChoice((self.Segments[0][1], etisserant@0: self.Segments[0][0]), v_end, self.EndPoint[1])) etisserant@0: # The current point is the second etisserant@0: elif i == 1: etisserant@0: # The previous direction and the target direction are mainly opposed, a point is added etisserant@0: if product(v_end, self.Segments[0]) < 0: lbessard@64: self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) etisserant@0: self.Segments.insert(2, DirectionChoice((self.Segments[1][1], etisserant@0: self.Segments[1][0]), v_end, self.EndPoint[1])) etisserant@0: # The previous direction and the end direction are the same or they are etisserant@0: # perpendiculars and the end direction points towards current segment etisserant@0: elif product(self.Segments[0], self.EndPoint[1]) >= 0 and product(self.Segments[1], self.EndPoint[1]) <= 0: etisserant@0: # Current point and end point are aligned etisserant@0: if self.Segments[0][0] != 0: etisserant@0: self.Points[1].x = end.x etisserant@0: if self.Segments[0][1] != 0: etisserant@0: self.Points[1].y = end.y etisserant@0: # If the previous direction and the end direction are the same, a point is added etisserant@0: if product(self.Segments[0], self.EndPoint[1]) > 0: lbessard@64: self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) etisserant@0: self.Segments.insert(2, DirectionChoice((self.Segments[1][1], etisserant@0: self.Segments[1][0]), v_end, self.EndPoint[1])) etisserant@0: else: etisserant@0: # Current point is positioned in the middle of start point etisserant@0: # and end point on the current direction and a point is added etisserant@0: if self.Segments[0][0] != 0: etisserant@0: self.Points[1].x = (end.x + start.x) / 2 etisserant@0: if self.Segments[0][1] != 0: etisserant@0: self.Points[1].y = (end.y + start.y) / 2 lbessard@64: self.Points.insert(2, wx.Point(self.Points[1].x, self.Points[1].y)) etisserant@0: self.Segments.insert(2, DirectionChoice((self.Segments[1][1], etisserant@0: self.Segments[1][0]), v_end, self.EndPoint[1])) etisserant@0: else: etisserant@0: # The previous direction and the end direction are perpendiculars etisserant@0: if product(self.Segments[i - 1], self.EndPoint[1]) == 0: etisserant@0: # The target direction and the end direction aren't mainly the same etisserant@0: if product(v_end, self.EndPoint[1]) <= 0: etisserant@0: # Current point and end point are aligned etisserant@0: if self.Segments[i - 1][0] != 0: etisserant@0: self.Points[i].x = end.x etisserant@0: if self.Segments[i - 1][1] != 0: etisserant@0: self.Points[i].y = end.y etisserant@0: # Previous direction is updated from the new point etisserant@0: if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: etisserant@0: self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) etisserant@0: else: etisserant@0: test = True etisserant@0: # If the current point is the third, test if the second etisserant@0: # point can be aligned with the end point etisserant@0: if i == 2: lbessard@64: test_point = wx.Point(self.Points[1].x, self.Points[1].y) etisserant@0: if self.Segments[1][0] != 0: etisserant@0: test_point.y = end.y etisserant@0: if self.Segments[1][1] != 0: etisserant@0: test_point.x = end.x lbessard@64: vector_test = vector(self.Points[0], test_point, False) lbessard@64: test = norm(vector_test) > MIN_SEGMENT_SIZE and product(self.Segments[0], vector_test) > 0 etisserant@0: # The previous point can be aligned etisserant@0: if test: etisserant@0: self.Points[i].x, self.Points[i].y = end.x, end.y etisserant@0: if self.Segments[i - 1][0] != 0: etisserant@0: self.Points[i - 1].y = end.y etisserant@0: if self.Segments[i - 1][1] != 0: etisserant@0: self.Points[i - 1].x = end.x etisserant@0: self.Segments[i] = (-self.EndPoint[1][0], -self.EndPoint[1][1]) etisserant@0: else: etisserant@0: # Current point is positioned in the middle of previous point etisserant@0: # and end point on the current direction and a point is added etisserant@0: if self.Segments[1][0] != 0: etisserant@0: self.Points[2].x = (self.Points[1].x + end.x) / 2 etisserant@0: if self.Segments[1][1] != 0: etisserant@0: self.Points[2].y = (self.Points[1].y + end.y) / 2 lbessard@64: self.Points.insert(3, wx.Point(self.Points[2].x, self.Points[2].y)) etisserant@0: self.Segments.insert(3, DirectionChoice((self.Segments[2][1], etisserant@0: self.Segments[2][0]), v_end, self.EndPoint[1])) etisserant@0: else: etisserant@0: # Current point is aligned with end point etisserant@0: if self.Segments[i - 1][0] != 0: etisserant@0: self.Points[i].x = end.x etisserant@0: if self.Segments[i - 1][1] != 0: etisserant@0: self.Points[i].y = end.y etisserant@0: # Previous direction is updated from the new point etisserant@0: if product(vector(self.Points[i - 1], self.Points[i]), self.Segments[i - 1]) < 0: etisserant@0: self.Segments[i - 1] = (-self.Segments[i - 1][0], -self.Segments[i - 1][1]) etisserant@0: # If previous direction and end direction are opposed etisserant@0: if product(self.Segments[i - 1], self.EndPoint[1]) < 0: etisserant@0: # Current point is positioned in the middle of previous point etisserant@0: # and end point on the current direction etisserant@0: if self.Segments[i - 1][0] != 0: etisserant@0: self.Points[i].x = (end.x + self.Points[i - 1].x) / 2 etisserant@0: if self.Segments[i - 1][1] != 0: etisserant@0: self.Points[i].y = (end.y + self.Points[i - 1].y) / 2 etisserant@0: # A point is added lbessard@64: self.Points.insert(i + 1, wx.Point(self.Points[i].x, self.Points[i].y)) etisserant@0: self.Segments.insert(i + 1, DirectionChoice((self.Segments[i][1], etisserant@0: self.Segments[i][0]), v_end, self.EndPoint[1])) etisserant@0: else: etisserant@0: # Current point is the first, and second is not mainly in the first direction etisserant@0: if i == 0 and product(vector(start, self.Points[1]), self.Segments[0]) < 0: etisserant@0: # If first and second directions aren't perpendiculars, a point is added etisserant@0: if product(self.Segments[0], self.Segments[1]) != 0: lbessard@64: self.Points.insert(1, wx.Point(start.x, start.y)) etisserant@0: self.Segments.insert(1, DirectionChoice((self.Segments[0][1], etisserant@0: self.Segments[0][0]), vector(start, self.Points[1]), self.Segments[1])) etisserant@0: else: etisserant@0: self.Points[1].x, self.Points[1].y = start.x, start.y etisserant@0: else: etisserant@0: # Next point is aligned with current point etisserant@0: if self.Segments[i][0] != 0: etisserant@0: self.Points[i + 1].y = self.Points[i].y etisserant@0: if self.Segments[i][1] != 0: etisserant@0: self.Points[i + 1].x = self.Points[i].x etisserant@0: # Current direction is updated from the new point etisserant@0: if product(vector(self.Points[i], self.Points[i + 1]), self.Segments[i]) < 0: etisserant@0: self.Segments[i] = (-self.Segments[i][0], -self.Segments[i][1]) etisserant@0: i += 1 etisserant@0: self.RefreshBoundingBox() etisserant@0: if realpoints: etisserant@0: self.RefreshRealPoints() etisserant@0: etisserant@0: # Verify that two consecutive points haven't the same position etisserant@0: def VerifyPoints(self): etisserant@0: points = [point for point in self.Points] etisserant@0: segments = [segment for segment in self.Segments] etisserant@0: i = 1 etisserant@0: while i < len(points) - 1: etisserant@0: if points[i] == points[i + 1] and segments[i - 1] == segments[i + 1]: etisserant@0: for j in xrange(2): etisserant@0: points.pop(i) etisserant@0: segments.pop(i) etisserant@0: else: etisserant@0: i += 1 etisserant@0: # If the wire isn't in a Ladder Diagram, save the new point list etisserant@0: if self.Parent.__class__.__name__ != "LD_Viewer": etisserant@0: self.Points = [point for point in points] etisserant@0: self.Segments = [segment for segment in segments] etisserant@0: self.RefreshBoundingBox() etisserant@0: self.RefreshRealPoints() etisserant@0: return points etisserant@0: etisserant@0: # Moves all the wire points except the first and the last if they are connected etisserant@0: def Move(self, dx, dy, endpoints = False): etisserant@0: for i, point in enumerate(self.Points): etisserant@0: if endpoints or not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): etisserant@0: point.x += dx etisserant@0: point.y += dy etisserant@0: self.StartPoint[0] = self.Points[0] etisserant@0: self.EndPoint[0] = self.Points[-1] etisserant@0: self.GeneratePoints() etisserant@0: etisserant@0: # Resize the wire from position and size given etisserant@0: def Resize(self, x, y, width, height): etisserant@0: if len(self.Points) > 1: etisserant@0: # Calculate the new position of each point for testing the new size etisserant@0: minx, miny = self.Pos.x, self.Pos.y etisserant@0: lastwidth, lastheight = self.Size.width, self.Size.height etisserant@0: for i, point in enumerate(self.RealPoints): etisserant@0: # If start or end point is connected, it's not calculate etisserant@0: if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): etisserant@0: if i == 0: etisserant@0: dir = self.StartPoint[1] etisserant@0: elif i == len(self.Points) - 1: etisserant@0: dir = self.EndPoint[1] etisserant@0: else: etisserant@0: dir = (0, 0) lbessard@208: pointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0] * width / float(max(lastwidth, 1)))), lbessard@208: width - dir[0] * MIN_SEGMENT_SIZE)) lbessard@208: pointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1] * height / float(max(lastheight, 1)))), lbessard@208: height - dir[1] * MIN_SEGMENT_SIZE)) lbessard@64: self.Points[i] = wx.Point(minx + x + pointx, miny + y + pointy) etisserant@0: self.StartPoint[0] = self.Points[0] etisserant@0: self.EndPoint[0] = self.Points[-1] etisserant@0: self.GeneratePoints(False) etisserant@0: # Test if the wire position or size have changed etisserant@0: if x != 0 and minx == self.Pos.x: etisserant@0: x = 0 etisserant@0: width = lastwidth etisserant@0: if y != 0 and miny == self.Pos.y: etisserant@0: y = 0 etisserant@0: height = lastwidth etisserant@0: if width != lastwidth and lastwidth == self.Size.width: etisserant@0: width = lastwidth etisserant@0: if height != lastheight and lastheight == self.Size.height: etisserant@0: height = lastheight etisserant@0: # Calculate the real points from the new size, it's important for etisserant@0: # keeping a proportionality in the points position with the size etisserant@0: # duringa resize dragging etisserant@0: for i, point in enumerate(self.RealPoints): etisserant@0: if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): lbessard@208: point[0] = point[0] * width / float(max(lastwidth, 1)) lbessard@208: point[1] = point[1] * height / float(max(lastheight, 1)) etisserant@0: # Calculate the correct position of the points from real points etisserant@0: for i, point in enumerate(self.RealPoints): etisserant@0: if not (i == 0 and self.StartConnected) and not (i == len(self.Points) - 1 and self.EndConnected): etisserant@0: if i == 0: etisserant@0: dir = self.StartPoint[1] etisserant@0: elif i == len(self.Points) - 1: etisserant@0: dir = self.EndPoint[1] etisserant@0: else: etisserant@0: dir = (0, 0) etisserant@0: realpointx = max(-dir[0] * MIN_SEGMENT_SIZE, min(int(round(point[0])), lbessard@208: width - dir[0] * MIN_SEGMENT_SIZE)) etisserant@0: realpointy = max(-dir[1] * MIN_SEGMENT_SIZE, min(int(round(point[1])), lbessard@208: height - dir[1] * MIN_SEGMENT_SIZE)) lbessard@64: self.Points[i] = wx.Point(minx + x + realpointx, miny + y + realpointy) etisserant@0: self.StartPoint[0] = self.Points[0] etisserant@0: self.EndPoint[0] = self.Points[-1] etisserant@0: self.GeneratePoints(False) etisserant@0: etisserant@0: # Moves the wire start point and update the wire points etisserant@0: def MoveStartPoint(self, point): etisserant@0: if len(self.Points) > 1: etisserant@0: self.StartPoint[0] = point etisserant@0: self.Points[0] = point etisserant@0: self.GeneratePoints() etisserant@0: etisserant@0: # Changes the wire start direction and update the wire points etisserant@0: def SetStartPointDirection(self, dir): etisserant@0: if len(self.Points) > 1: etisserant@0: self.StartPoint[1] = dir etisserant@0: self.Segments[0] = dir etisserant@0: self.GeneratePoints() etisserant@0: etisserant@0: # Rotates the wire start direction by an angle of 90 degrees anticlockwise etisserant@0: def RotateStartPoint(self): etisserant@0: self.SetStartPointDirection((self.StartPoint[1][1], -self.StartPoint[1][0])) etisserant@0: etisserant@0: # Connects wire start point to the connector given and moves wire start point etisserant@0: # to given point etisserant@0: def ConnectStartPoint(self, point, connector): etisserant@0: if point: etisserant@0: self.MoveStartPoint(point) etisserant@0: self.StartConnected = connector lbessard@145: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Unconnects wire start point lbessard@2: def UnConnectStartPoint(self, delete = False): lbessard@2: if delete: lbessard@60: self.StartConnected = None lbessard@2: self.Delete() lbessard@60: elif self.StartConnected: lbessard@60: self.StartConnected.UnConnect(self, unconnect = False) lbessard@2: self.StartConnected = None lbessard@145: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Moves the wire end point and update the wire points etisserant@0: def MoveEndPoint(self, point): etisserant@0: if len(self.Points) > 1: etisserant@0: self.EndPoint[0] = point etisserant@0: self.Points[-1] = point etisserant@0: self.GeneratePoints() etisserant@0: etisserant@0: # Changes the wire end direction and update the wire points etisserant@0: def SetEndPointDirection(self, dir): etisserant@0: if len(self.Points) > 1: etisserant@0: self.EndPoint[1] = dir etisserant@0: self.GeneratePoints() etisserant@0: etisserant@0: # Rotates the wire end direction by an angle of 90 degrees anticlockwise etisserant@0: def RotateEndPoint(self): etisserant@0: self.SetEndPointDirection((self.EndPoint[1][1], -self.EndPoint[1][0])) etisserant@0: etisserant@0: # Connects wire end point to the connector given and moves wire end point etisserant@0: # to given point etisserant@0: def ConnectEndPoint(self, point, connector): etisserant@0: if point: etisserant@0: self.MoveEndPoint(point) etisserant@0: self.EndConnected = connector lbessard@145: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Unconnects wire end point lbessard@2: def UnConnectEndPoint(self, delete = False): lbessard@2: if delete: lbessard@60: self.EndConnected = None lbessard@2: self.Delete() lbessard@60: elif self.EndConnected: lbessard@60: self.EndConnected.UnConnect(self, unconnect = False) lbessard@2: self.EndConnected = None lbessard@145: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Moves the wire segment given by its index lbessard@145: def MoveSegment(self, idx, movex, movey, scaling): etisserant@0: if 0 < idx < len(self.Segments) - 1: etisserant@0: if self.Segments[idx] in (NORTH, SOUTH): lbessard@112: start_x = self.Points[idx].x lbessard@145: if scaling is not None: lbessard@145: movex = round(float(self.Points[idx].x + movex) / float(scaling[0])) * scaling[0] - self.Points[idx].x etisserant@0: self.Points[idx].x += movex etisserant@0: self.Points[idx + 1].x += movex lbessard@112: self.GeneratePoints() lbessard@112: if start_x != self.Points[idx].x: lbessard@138: return self.Points[idx].x - start_x, 0 etisserant@0: elif self.Segments[idx] in (EAST, WEST): lbessard@112: start_y = self.Points[idx].y lbessard@145: if scaling is not None: lbessard@145: movey = round(float(self.Points[idx].y + movey) / float(scaling[1])) * scaling[1] - self.Points[idx].y etisserant@0: self.Points[idx].y += movey etisserant@0: self.Points[idx + 1].y += movey lbessard@112: self.GeneratePoints() lbessard@112: if start_y != self.Points[idx].y: lbessard@138: return 0, self.Points[idx].y - start_y lbessard@138: return 0, 0 etisserant@0: etisserant@0: # Adds two points in the middle of the handled segment etisserant@0: def AddSegment(self): etisserant@0: handle_type, handle = self.Handle etisserant@0: if handle_type == HANDLE_SEGMENT: etisserant@0: segment, dir = handle etisserant@0: pointx = self.Points[segment].x etisserant@0: pointy = self.Points[segment].y etisserant@0: if dir[0] != 0: etisserant@0: pointx = (self.Points[segment].x + self.Points[segment + 1].x) / 2 etisserant@0: if dir[1] != 0: etisserant@0: pointy = (self.Points[segment].y + self.Points[segment + 1].y) / 2 lbessard@64: self.Points.insert(segment + 1, wx.Point(pointx, pointy)) etisserant@0: self.Segments.insert(segment + 1, (dir[1], dir[0])) lbessard@64: self.Points.insert(segment + 2, wx.Point(pointx, pointy)) etisserant@0: self.Segments.insert(segment + 2, dir) etisserant@0: self.GeneratePoints() etisserant@0: etisserant@0: # Delete the handled segment by removing the two segment points etisserant@0: def DeleteSegment(self): etisserant@0: handle_type, handle = self.Handle etisserant@0: if handle_type == HANDLE_SEGMENT: etisserant@0: segment, dir = handle etisserant@0: for i in xrange(2): etisserant@0: self.Points.pop(segment) etisserant@0: self.Segments.pop(segment) etisserant@0: self.GeneratePoints() etisserant@0: self.RefreshModel() etisserant@0: etisserant@0: # Method called when a LeftDown event have been generated lbessard@27: def OnLeftDown(self, event, dc, scaling): lbessard@27: pos = GetScaledEventPosition(event, dc, scaling) etisserant@0: # Test if a point have been handled lbessard@3: #result = self.TestPoint(pos) lbessard@3: #if result != None: lbessard@3: # self.Handle = (HANDLE_POINT, result) lbessard@64: # self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_HAND)) lbessard@3: #else: lbessard@3: # Test if a segment have been handled lbessard@3: result = self.TestSegment(pos) etisserant@0: if result != None: lbessard@3: if result[1] in (NORTH, SOUTH): lbessard@64: self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_SIZEWE)) lbessard@3: elif result[1] in (EAST, WEST): lbessard@64: self.Parent.SetCursor(wx.StockCursor(wx.CURSOR_SIZENS)) lbessard@3: self.Handle = (HANDLE_SEGMENT, result) lbessard@3: # Execute the default method for a graphic element etisserant@0: else: lbessard@27: Graphic_Element.OnLeftDown(self, event, dc, scaling) etisserant@0: self.oldPos = pos etisserant@0: lbessard@80: # Method called when a RightUp event has been generated lbessard@27: def OnRightUp(self, event, dc, scaling): lbessard@27: pos = GetScaledEventPosition(event, dc, scaling) etisserant@0: # Test if a segment has been handled etisserant@0: result = self.TestSegment(pos) etisserant@0: if result != None: etisserant@0: self.Handle = (HANDLE_SEGMENT, result) etisserant@0: # Popup the menu with special items for a wire etisserant@0: self.Parent.PopupWireMenu() etisserant@0: else: etisserant@0: # Execute the default method for a graphic element lbessard@27: Graphic_Element.OnRightUp(self, event, dc, scaling) etisserant@0: lbessard@80: # Method called when a LeftDClick event has been generated lbessard@27: def OnLeftDClick(self, event, dc, scaling): lbessard@138: if event.ControlDown(): lbessard@138: direction = (self.StartPoint[1], self.EndPoint[1]) lbessard@138: if direction in [(EAST, WEST), (WEST, EAST)]: lbessard@138: avgy = (self.StartPoint[0].y + self.EndPoint[0].y) / 2 lbessard@138: if self.StartConnected is not None: lbessard@138: startblock = self.StartConnected.GetParentBlock() lbessard@138: startblock.Move(0, avgy - self.StartPoint[0].y) lbessard@138: startblock.RefreshModel() lbessard@138: else: lbessard@138: self.MoveStartPoint(wx.Point(self.StartPoint[0].x, avgy)) lbessard@138: if self.EndConnected is not None: lbessard@138: endblock = self.EndConnected.GetParentBlock() lbessard@138: endblock.Move(0, avgy - self.EndPoint[0].y) lbessard@138: endblock.RefreshModel() lbessard@138: else: lbessard@138: self.MoveEndPoint(wx.Point(self.EndPoint[0].x, avgy)) lbessard@138: elif direction in [(NORTH, SOUTH), (SOUTH, NORTH)]: lbessard@138: avgx = (self.StartPoint[0].x + self.EndPoint[0].x) / 2 lbessard@138: if self.StartConnected is not None: lbessard@138: startblock = self.StartConnected.GetParentBlock() lbessard@138: startblock.Move(avgx - self.StartPoint[0].x, 0) lbessard@138: startblock.RefreshModel() lbessard@138: else: lbessard@138: self.MoveStartPoint(wx.Point(avgx, self.StartPoint[0].y)) lbessard@138: if self.EndConnected is not None: lbessard@138: endblock = self.EndConnected.GetParentBlock() lbessard@138: endblock.Move(avgx - self.EndPoint[0].x, 0) lbessard@138: endblock.RefreshModel() lbessard@138: else: lbessard@138: self.MoveEndPoint(wx.Point(avgx, self.EndPoint[0].y)) lbessard@138: self.Parent.RefreshBuffer() lbessard@138: else: lbessard@145: rect = self.GetRedrawRect() lbessard@138: self.ResetPoints() lbessard@138: self.GeneratePoints() lbessard@138: self.RefreshModel() lbessard@138: self.Parent.RefreshBuffer() lbessard@155: self.Parent.RefreshRect(self.Parent.GetScrolledRect(rect), False) etisserant@0: lbessard@80: # Method called when a Motion event has been generated lbessard@27: def OnMotion(self, event, dc, scaling): lbessard@27: pos = GetScaledEventPosition(event, dc, scaling) etisserant@0: if not event.Dragging(): etisserant@0: # Test if a segment has been handled etisserant@0: result = self.TestSegment(pos) etisserant@0: if result: etisserant@0: if result[1] in (NORTH, SOUTH): lbessard@64: if self.CurrentCursor != 4: lbessard@64: self.CurrentCursor = 4 lbessard@64: wx.CallAfter(self.Parent.SetCursor, CURSORS[4]) etisserant@0: elif result[1] in (EAST, WEST): lbessard@64: if self.CurrentCursor != 5: lbessard@64: self.CurrentCursor = 5 lbessard@64: wx.CallAfter(self.Parent.SetCursor, CURSORS[5]) lbessard@144: return 0, 0 etisserant@0: else: etisserant@0: # Test if a point has been handled lbessard@3: #result = self.TestPoint(pos) lbessard@3: #if result != None: lbessard@3: # if result == 0 and self.StartConnected: lbessard@3: # self.OverStart = True lbessard@3: # elif result != 0 and self.EndConnected: lbessard@3: # self.OverEnd = True lbessard@3: #else: lbessard@3: # self.OverStart = False lbessard@3: # self.OverEnd = False lbessard@3: # Execute the default method for a graphic element lbessard@90: return Graphic_Element.OnMotion(self, event, dc, scaling) etisserant@0: else: etisserant@0: # Execute the default method for a graphic element lbessard@90: return Graphic_Element.OnMotion(self, event, dc, scaling) etisserant@0: etisserant@0: # Refreshes the wire state according to move defined and handle selected lbessard@165: def ProcessDragging(self, movex, movey, centered, scaling): etisserant@0: handle_type, handle = self.Handle etisserant@0: # A point has been handled etisserant@0: if handle_type == HANDLE_POINT: lbessard@138: movex = max(-self.Points[handle].x + POINT_RADIUS, movex) lbessard@138: movey = max(-self.Points[handle].y + POINT_RADIUS, movey) lbessard@145: if scaling is not None: lbessard@145: movex = round(float(self.Points[handle].x + movex) / float(scaling[0])) * scaling[0] - self.Points[handle].x lbessard@145: movey = round(float(self.Points[handle].y + movey) / float(scaling[1])) * scaling[1] - self.Points[handle].y etisserant@0: # Try to connect point to a connector lbessard@64: new_pos = wx.Point(self.Points[handle].x + movex, self.Points[handle].y + movey) etisserant@0: connector = self.Parent.FindBlockConnector(new_pos) etisserant@0: if connector: lbessard@98: if handle == 0 and self.EndConnected != connector and connector.IsCompatible(self.GetEndConnectedType()): lbessard@140: connector.HighlightParentBlock(True) etisserant@0: connector.Connect((self, handle)) etisserant@0: self.SetStartPointDirection(connector.GetDirection()) etisserant@0: self.ConnectStartPoint(connector.GetPosition(), connector) lbessard@138: pos = connector.GetPosition() lbessard@138: movex = pos.x - self.oldPos.x lbessard@138: movey = pos.y - self.oldPos.y etisserant@0: self.Dragging = False lbessard@98: elif handle != 0 and self.StartConnected != connector and connector.IsCompatible(self.GetStartConnectedType()): lbessard@140: connector.HighlightParentBlock(True) etisserant@0: connector.Connect((self, handle)) etisserant@0: self.SetEndPointDirection(connector.GetDirection()) etisserant@0: self.ConnectEndPoint(connector.GetPosition(), connector) lbessard@138: pos = connector.GetPosition() lbessard@138: movex = pos.x - self.oldPos.x lbessard@138: movey = pos.y - self.oldPos.y etisserant@0: self.Dragging = False etisserant@0: elif handle == 0: etisserant@0: self.MoveStartPoint(new_pos) etisserant@0: else: etisserant@0: self.MoveEndPoint(new_pos) etisserant@0: # If there is no connector, move the point etisserant@0: elif handle == 0: etisserant@0: if self.StartConnected: etisserant@0: self.UnConnectStartPoint() lbessard@140: self.StartConnected.HighlightParentBlock(False) etisserant@0: self.MoveStartPoint(new_pos) etisserant@0: else: etisserant@0: if self.EndConnected: lbessard@140: self.EndConnected.HighlightParentBlock(False) etisserant@0: self.UnConnectEndPoint() etisserant@0: self.MoveEndPoint(new_pos) lbessard@138: return movex, movey etisserant@0: # A segment has been handled, move a segment etisserant@0: elif handle_type == HANDLE_SEGMENT: lbessard@145: return self.MoveSegment(handle[0], movex, movey, scaling) etisserant@0: # Execute the default method for a graphic element etisserant@0: else: lbessard@165: return Graphic_Element.ProcessDragging(self, movex, movey, centered, scaling) etisserant@0: etisserant@0: # Refreshes the wire model etisserant@0: def RefreshModel(self, move=True): etisserant@0: if self.StartConnected and self.StartPoint[1] in [WEST, NORTH]: etisserant@0: self.StartConnected.RefreshParentBlock() etisserant@0: if self.EndConnected and self.EndPoint[1] in [WEST, NORTH]: etisserant@0: self.EndConnected.RefreshParentBlock() etisserant@0: lbessard@140: # Draws the highlightment of this element if it is highlighted lbessard@140: def DrawHighlightment(self, dc): lbessard@144: dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) lbessard@144: dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) lbessard@144: dc.SetLogicalFunction(wx.AND) lbessard@144: # Draw the start and end points if they are not connected or the mouse is over them lbessard@144: if len(self.Points) > 0 and (not self.StartConnected or self.OverStart): lbessard@144: dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS + 2) lbessard@144: if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd): lbessard@144: dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS + 2) lbessard@144: for i in xrange(len(self.Points) - 1): lbessard@144: posx = min(self.Points[i].x, self.Points[i + 1].x) - 2 lbessard@144: posy = min(self.Points[i].y, self.Points[i + 1].y) - 2 lbessard@144: width = abs(self.Points[i + 1].x - self.Points[i].x) + 5 lbessard@144: height = abs(self.Points[i + 1].y - self.Points[i].y) + 5 lbessard@144: dc.DrawRectangle(posx, posy, width, height) lbessard@145: dc.SetLogicalFunction(wx.COPY) lbessard@144: if self.StartConnected is not None: lbessard@144: self.StartConnected.DrawHighlightment(dc) lbessard@145: self.StartConnected.Draw(dc) lbessard@144: if self.EndConnected is not None: lbessard@144: self.EndConnected.DrawHighlightment(dc) lbessard@145: self.EndConnected.Draw(dc) lbessard@140: etisserant@0: # Draws the wire lines and points etisserant@0: def Draw(self, dc): lbessard@144: Graphic_Element.Draw(self, dc) lbessard@64: dc.SetPen(wx.BLACK_PEN) lbessard@64: dc.SetBrush(wx.BLACK_BRUSH) etisserant@0: # Draw the start and end points if they are not connected or the mouse is over them etisserant@0: if len(self.Points) > 0 and (not self.StartConnected or self.OverStart): etisserant@0: dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS) etisserant@0: if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd): etisserant@0: dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS) etisserant@0: # Draw the wire lines and the last point (it seems that DrawLines stop before the last point) etisserant@0: dc.DrawLines(self.Points) etisserant@0: dc.DrawPoint(self.Points[-1].x, self.Points[-1].y) etisserant@0: # Draw the segment selected in red etisserant@0: if self.SelectedSegment != None: lbessard@64: dc.SetPen(wx.RED_PEN) etisserant@0: dc.DrawLine(self.Points[self.SelectedSegment].x, self.Points[self.SelectedSegment].y, etisserant@0: self.Points[self.SelectedSegment + 1].x, self.Points[self.SelectedSegment + 1].y) etisserant@0: if self.SelectedSegment == len(self.Segments) - 1: etisserant@0: dc.DrawPoint(self.Points[-1].x, self.Points[-1].y) lbessard@144: etisserant@0: etisserant@0: #------------------------------------------------------------------------------- etisserant@0: # Graphic comment element etisserant@0: #------------------------------------------------------------------------------- etisserant@0: etisserant@0: """ etisserant@0: Class that implements a comment etisserant@0: """ etisserant@0: etisserant@0: class Comment(Graphic_Element): etisserant@0: etisserant@0: # Create a new comment etisserant@0: def __init__(self, parent, content, id = None): etisserant@0: Graphic_Element.__init__(self, parent) etisserant@0: self.Id = id etisserant@0: self.Content = content lbessard@64: self.Pos = wx.Point(0, 0) lbessard@64: self.Size = wx.Size(0, 0) etisserant@0: lbessard@112: # Make a clone of this comment lbessard@162: def Clone(self, parent, id = None, pos = None): lbessard@162: comment = Comment(parent, self.Content, id) lbessard@145: if pos is not None: lbessard@145: comment.SetPosition(pos.x, pos.y) lbessard@112: comment.SetSize(self.Size[0], self.Size[1]) lbessard@112: return comment lbessard@112: etisserant@0: # Method for keeping compatibility with others etisserant@0: def Clean(self): etisserant@0: pass etisserant@0: etisserant@0: # Delete this comment by calling the corresponding method etisserant@0: def Delete(self): etisserant@0: self.Parent.DeleteComment(self) etisserant@0: etisserant@0: # Refresh the comment bounding box etisserant@0: def RefreshBoundingBox(self): lbessard@64: self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1) etisserant@0: etisserant@0: # Changes the comment size etisserant@0: def SetSize(self, width, height): etisserant@0: self.Size.SetWidth(width) etisserant@0: self.Size.SetHeight(height) etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Returns the comment size etisserant@0: def GetSize(self): etisserant@0: return self.Size.GetWidth(), self.Size.GetHeight() etisserant@0: etisserant@0: # Returns the comment minimum size etisserant@0: def GetMinSize(self): lbessard@64: dc = wx.ClientDC(self.Parent) etisserant@0: min_width = 0 etisserant@0: min_height = 0 etisserant@0: # The comment minimum size is the maximum size of words in the content etisserant@0: for line in self.Content.splitlines(): etisserant@0: for word in line.split(" "): etisserant@0: wordwidth, wordheight = dc.GetTextExtent(word) etisserant@0: min_width = max(min_width, wordwidth) etisserant@0: min_height = max(min_height, wordheight) etisserant@0: return min_width + 20, min_height + 20 etisserant@0: etisserant@0: # Changes the comment position etisserant@0: def SetPosition(self, x, y): etisserant@0: self.Pos.x = x etisserant@0: self.Pos.y = y etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Changes the comment content etisserant@0: def SetContent(self, content): etisserant@0: self.Content = content etisserant@0: min_width, min_height = self.GetMinSize() etisserant@0: self.Size[0] = max(self.Size[0], min_width) etisserant@0: self.Size[1] = max(self.Size[1], min_height) etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Returns the comment content etisserant@0: def GetContent(self): etisserant@0: return self.Content etisserant@0: etisserant@0: # Returns the comment position etisserant@0: def GetPosition(self): etisserant@0: return self.Pos.x, self.Pos.y etisserant@0: etisserant@0: # Moves the comment etisserant@0: def Move(self, dx, dy, connected = True): etisserant@0: self.Pos.x += dx etisserant@0: self.Pos.y += dy etisserant@0: self.RefreshBoundingBox() etisserant@0: etisserant@0: # Resizes the comment with the position and the size given etisserant@0: def Resize(self, x, y, width, height): etisserant@0: self.Move(x, y) etisserant@0: self.SetSize(width, height) etisserant@0: etisserant@0: # Method called when a RightUp event have been generated lbessard@27: def OnRightUp(self, event, dc, scaling): etisserant@0: # Popup the default menu etisserant@0: self.Parent.PopupDefaultMenu() etisserant@0: etisserant@0: # Refreshes the comment model etisserant@0: def RefreshModel(self, move=True): etisserant@0: self.Parent.RefreshCommentModel(self) 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 comment content etisserant@0: self.Parent.EditCommentContent(self) etisserant@0: lbessard@140: # Draws the highlightment of this element if it is highlighted lbessard@140: def DrawHighlightment(self, dc): lbessard@144: dc.SetPen(wx.Pen(HIGHLIGHTCOLOR)) lbessard@144: dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR)) lbessard@144: dc.SetLogicalFunction(wx.AND) lbessard@144: polygon = [wx.Point(self.Pos.x - 2, self.Pos.y - 2), lbessard@144: wx.Point(self.Pos.x + self.Size[0] - 8, self.Pos.y - 2), lbessard@144: wx.Point(self.Pos.x + self.Size[0] + 2, self.Pos.y + 8), lbessard@144: wx.Point(self.Pos.x + self.Size[0] + 2, self.Pos.y + self.Size[1] + 2), lbessard@144: wx.Point(self.Pos.x - 2, self.Pos.y + self.Size[1] + 2)] lbessard@144: dc.DrawPolygon(polygon) lbessard@144: dc.SetLogicalFunction(wx.COPY) lbessard@140: etisserant@0: # Draws the comment and its content etisserant@0: def Draw(self, dc): lbessard@144: Graphic_Element.Draw(self, dc) lbessard@64: dc.SetPen(wx.BLACK_PEN) lbessard@64: dc.SetBrush(wx.WHITE_BRUSH) etisserant@0: # Draws the comment shape lbessard@64: polygon = [wx.Point(self.Pos.x, self.Pos.y), lbessard@64: wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y), lbessard@64: wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10), lbessard@140: wx.Point(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1]), lbessard@140: wx.Point(self.Pos.x, self.Pos.y + self.Size[1])] etisserant@0: dc.DrawPolygon(polygon) lbessard@64: lines = [wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y), lbessard@64: wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y + 10), lbessard@64: wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10)] etisserant@0: dc.DrawLines(lines) etisserant@0: # Draws the comment content etisserant@0: y = self.Pos.y + 10 etisserant@0: for line in self.Content.splitlines(): etisserant@0: first = True etisserant@0: words = line.split(" ") etisserant@0: for i, word in enumerate(words): etisserant@0: if first: etisserant@0: test = word etisserant@0: else: etisserant@0: test = linetext + " " + word etisserant@0: wordwidth, wordheight = dc.GetTextExtent(test) etisserant@0: if y + wordheight > self.Pos.y + self.Size[1] - 10: etisserant@0: break etisserant@0: if wordwidth < self.Size[0] - 20 and i < len(words) - 1: etisserant@0: linetext = test etisserant@0: first = False etisserant@0: else: etisserant@0: if wordwidth < self.Size[0] - 20 and i == len(words) - 1: etisserant@0: dc.DrawText(test, self.Pos.x + 10, y) etisserant@0: else: etisserant@0: dc.DrawText(linetext, self.Pos.x + 10, y) etisserant@0: if i == len(words) - 1: etisserant@0: y += wordheight + 5 etisserant@0: if y + wordheight > self.Pos.y + self.Size[1] - 10: etisserant@0: break etisserant@0: dc.DrawText(word, self.Pos.x + 10, y) etisserant@0: else: etisserant@0: linetext = word etisserant@0: y += wordheight + 5 etisserant@0: if y + wordheight > self.Pos.y + self.Size[1] - 10: etisserant@0: break