graphics/GraphicCommons.py
changeset 231 fc2d6cbb8b39
parent 222 8ce5c2635976
child 237 097e8ee006cb
equal deleted inserted replaced
230:45d70748e45a 231:fc2d6cbb8b39
    22 #License along with this library; if not, write to the Free Software
    22 #License along with this library; if not, write to the Free Software
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24 
    24 
    25 import wx
    25 import wx
    26 from math import *
    26 from math import *
    27 from plcopen.structures import IsOfType, IsEndType
       
    28 
    27 
    29 #-------------------------------------------------------------------------------
    28 #-------------------------------------------------------------------------------
    30 #                               Common constants
    29 #                               Common constants
    31 #-------------------------------------------------------------------------------
    30 #-------------------------------------------------------------------------------
    32 
    31 
   273             # Draw current box
   272             # Draw current box
   274             dc.DrawRectangle(self.currentBox.x, self.currentBox.y, self.currentBox.width,
   273             dc.DrawRectangle(self.currentBox.x, self.currentBox.y, self.currentBox.width,
   275                 self.currentBox.height)
   274                 self.currentBox.height)
   276 
   275 
   277 #-------------------------------------------------------------------------------
   276 #-------------------------------------------------------------------------------
       
   277 #                    Helper for highlighting error in drawn text
       
   278 #-------------------------------------------------------------------------------
       
   279 
       
   280 def HighlightErrorZone(dc, x, y, width, height):
       
   281     dc.SetPen(wx.TRANSPARENT_PEN)
       
   282     dc.SetLogicalFunction(wx.AND)
       
   283     dc.SetBrush(wx.Brush(wx.Colour(0,255,0)))
       
   284     dc.DrawRectangle(x, y, width, height)
       
   285     dc.SetLogicalFunction(wx.XOR)
       
   286     dc.SetBrush(wx.Brush(wx.Colour(255,0,0)))
       
   287     dc.DrawRectangle(x, y, width, height)
       
   288     dc.SetLogicalFunction(wx.COPY)
       
   289 
       
   290 #-------------------------------------------------------------------------------
   278 #                           Graphic element base class
   291 #                           Graphic element base class
   279 #-------------------------------------------------------------------------------
   292 #-------------------------------------------------------------------------------
   280 
   293 
   281 """
   294 """
   282 Class that implements a generic graphic element
   295 Class that implements a generic graphic element
   297         self.Size = wx.Size(0, 0)
   310         self.Size = wx.Size(0, 0)
   298         self.BoundingBox = wx.Rect(0, 0, 0, 0)
   311         self.BoundingBox = wx.Rect(0, 0, 0, 0)
   299         self.CurrentCursor = 0
   312         self.CurrentCursor = 0
   300         ResetCursors()
   313         ResetCursors()
   301     
   314     
       
   315     def IsOfType(self, type, reference):
       
   316         return self.Parent.IsOfType(type, reference)
       
   317     
       
   318     def IsEndType(self, type):
       
   319         return self.Parent.IsEndType(type)
       
   320         
   302     def GetDragging(self):
   321     def GetDragging(self):
   303         return self.Dragging
   322         return self.Dragging
   304     
   323     
   305     # Make a clone of this element
   324     # Make a clone of this element
   306     def Clone(self, parent):
   325     def Clone(self, parent):
   584                 movey = round(float(self.Pos.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y
   603                 movey = round(float(self.Pos.y + movey) / float(scaling[1])) * scaling[1] - self.Pos.y
   585             self.Move(movex, movey)
   604             self.Move(movex, movey)
   586             return movex, movey
   605             return movex, movey
   587         return 0, 0
   606         return 0, 0
   588     
   607     
       
   608     def AddError(self, infos, start, end):
       
   609         pass
       
   610     
   589     # Override this method for defining the method to call for refreshing the model of this element
   611     # Override this method for defining the method to call for refreshing the model of this element
   590     def RefreshModel(self, move=True):
   612     def RefreshModel(self, move=True):
   591         pass
   613         pass
   592     
   614     
   593     # Draws the highlightment of this element if it is highlighted (can be overwritten)
   615     # Draws the highlightment of this element if it is highlighted (can be overwritten)
   831         self.Name = name
   853         self.Name = name
   832         self.Type = type
   854         self.Type = type
   833         self.Pos = position
   855         self.Pos = position
   834         self.Direction = direction
   856         self.Direction = direction
   835         self.Wires = []
   857         self.Wires = []
   836         if IsOfType("BOOL", type):
   858         if self.ParentBlock.IsOfType("BOOL", type):
   837             self.Negated = negated
   859             self.Negated = negated
   838             self.Edge = edge
   860             self.Edge = edge
   839         else:
   861         else:
   840             self.Negated = False
   862             self.Negated = False
   841             self.Edge = "none"
   863             self.Edge = "none"
   842         self.OneConnected = onlyone
   864         self.OneConnected = onlyone
   843         self.Pen = wx.BLACK_PEN
   865         self.Pen = wx.BLACK_PEN
       
   866         self.Errors = {}
   844         self.RefreshNameSize()
   867         self.RefreshNameSize()
   845     
   868     
   846     # Returns the RedrawRect
   869     # Returns the RedrawRect
   847     def GetRedrawRect(self, movex = 0, movey = 0):
   870     def GetRedrawRect(self, movex = 0, movey = 0):
   848         parent_pos = self.ParentBlock.GetPosition()
   871         parent_pos = self.ParentBlock.GetPosition()
   873     def GetParentBlock(self):
   896     def GetParentBlock(self):
   874         return self.ParentBlock
   897         return self.ParentBlock
   875     
   898     
   876     # Returns the connector type
   899     # Returns the connector type
   877     def GetType(self, raw = False):
   900     def GetType(self, raw = False):
   878         if IsEndType(self.Type) or raw:
   901         if self.ParentBlock.IsEndType(self.Type) or raw:
   879             return self.Type
   902             return self.Type
   880         elif (self.Negated or self.Edge != "none") and IsOfType("BOOL", self.Type):
   903         elif (self.Negated or self.Edge != "none") and self.ParentBlock.IsOfType("BOOL", self.Type):
   881             return "BOOL"
   904             return "BOOL"
   882         else:
   905         else:
   883             return self.ParentBlock.GetConnectionResultType(self, self.Type)
   906             return self.ParentBlock.GetConnectionResultType(self, self.Type)
   884     
   907     
   885     # Returns the connector type
   908     # Returns the connector type
   886     def GetConnectedType(self):
   909     def GetConnectedType(self):
   887         if IsEndType(self.Type):
   910         if self.ParentBlock.IsEndType(self.Type):
   888             return self.Type
   911             return self.Type
   889         elif len(self.Wires) == 1:
   912         elif len(self.Wires) == 1:
   890             return self.Wires[0][0].GetOtherConnectedType(self.Wires[0][1])
   913             return self.Wires[0][0].GetOtherConnectedType(self.Wires[0][1])
   891         return self.Type
   914         return self.Type
   892     
   915     
   901         return rect
   924         return rect
   902     
   925     
   903     # Returns if connector type is compatible with type given
   926     # Returns if connector type is compatible with type given
   904     def IsCompatible(self, type):
   927     def IsCompatible(self, type):
   905         reference = self.GetType()
   928         reference = self.GetType()
   906         return IsOfType(type, reference) or IsOfType(reference, type)
   929         return self.ParentBlock.IsOfType(type, reference) or self.ParentBlock.IsOfType(reference, type)
   907     
   930     
   908     # Changes the connector name
   931     # Changes the connector name
   909     def SetType(self, type):
   932     def SetType(self, type):
   910         self.Type = type
   933         self.Type = type
   911     
   934     
  1063     def IsNegated(self):
  1086     def IsNegated(self):
  1064         return self.Negated
  1087         return self.Negated
  1065     
  1088     
  1066     # Changes the connector negated property
  1089     # Changes the connector negated property
  1067     def SetNegated(self, negated):
  1090     def SetNegated(self, negated):
  1068         if IsOfType("BOOL", self.Type):
  1091         if self.ParentBlock.IsOfType("BOOL", self.Type):
  1069             self.Negated = negated
  1092             self.Negated = negated
  1070             self.Edge = "none"
  1093             self.Edge = "none"
  1071     
  1094     
  1072     # Returns the connector edge property
  1095     # Returns the connector edge property
  1073     def GetEdge(self):
  1096     def GetEdge(self):
  1074         return self.Edge
  1097         return self.Edge
  1075     
  1098     
  1076     # Changes the connector edge property
  1099     # Changes the connector edge property
  1077     def SetEdge(self, edge):
  1100     def SetEdge(self, edge):
  1078         if IsOfType("BOOL", self.Type):
  1101         if self.ParentBlock.IsOfType("BOOL", self.Type):
  1079             self.Edge = edge    
  1102             self.Edge = edge    
  1080             self.Negated = False
  1103             self.Negated = False
  1081     
  1104     
  1082     # Tests if the point given is near from the end point of this connector
  1105     # Tests if the point given is near from the end point of this connector
  1083     def TestPoint(self, pt, exclude = True):
  1106     def TestPoint(self, pt, exclude = True):
  1113             posy -= 2
  1136             posy -= 2
  1114             height = 5
  1137             height = 5
  1115         dc.DrawRectangle(posx, posy, width, height)
  1138         dc.DrawRectangle(posx, posy, width, height)
  1116         dc.SetLogicalFunction(wx.COPY)
  1139         dc.SetLogicalFunction(wx.COPY)
  1117     
  1140     
       
  1141     def AddError(self, infos, start, end):
       
  1142         if len(infos) == 0:
       
  1143             for wire, handle in self.Wires:
       
  1144                 wire.MarkAsInvalid()
       
  1145         else:
       
  1146             self.Errors[infos[0]] = (start, end)
       
  1147     
  1118     # Draws the connector
  1148     # Draws the connector
  1119     def Draw(self, dc):
  1149     def Draw(self, dc):
  1120         dc.SetPen(self.Pen)
  1150         if len(self.Errors) > 0:
  1121         dc.SetBrush(wx.WHITE_BRUSH)
  1151             dc.SetPen(wx.RED_PEN)
       
  1152             dc.SetBrush(wx.Brush(wx.Colour(255, 255, 0)))
       
  1153         else:
       
  1154             dc.SetPen(self.Pen)
       
  1155             dc.SetBrush(wx.WHITE_BRUSH)
  1122         parent_pos = self.ParentBlock.GetPosition()
  1156         parent_pos = self.ParentBlock.GetPosition()
  1123         
  1157         
  1124         if getattr(dc, "printing", False):
  1158         if getattr(dc, "printing", False):
  1125             name_size = dc.GetTextExtent(self.Name)
  1159             name_size = dc.GetTextExtent(self.Name)
  1126         else:
  1160         else:
  1143                 dc.DrawLine(xstart, ystart, xstart + 4, ystart - 4)
  1177                 dc.DrawLine(xstart, ystart, xstart + 4, ystart - 4)
  1144                 dc.DrawLine(xstart, ystart, xstart + 4, ystart + 4)
  1178                 dc.DrawLine(xstart, ystart, xstart + 4, ystart + 4)
  1145             xend = xstart + CONNECTOR_SIZE * self.Direction[0]
  1179             xend = xstart + CONNECTOR_SIZE * self.Direction[0]
  1146             yend = ystart + CONNECTOR_SIZE * self.Direction[1]
  1180             yend = ystart + CONNECTOR_SIZE * self.Direction[1]
  1147             dc.DrawLine(xstart + self.Direction[0], ystart + self.Direction[1], xend, yend)
  1181             dc.DrawLine(xstart + self.Direction[0], ystart + self.Direction[1], xend, yend)
       
  1182         if len(self.Errors) > 0:
       
  1183             dc.SetPen(self.Pen)
       
  1184             dc.SetBrush(wx.WHITE_BRUSH)
  1148         if self.Direction[0] != 0:
  1185         if self.Direction[0] != 0:
  1149             ytext = parent_pos[1] + self.Pos.y - name_size[1] / 2
  1186             ytext = parent_pos[1] + self.Pos.y - name_size[1] / 2
  1150             if self.Direction[0] < 0:
  1187             if self.Direction[0] < 0:
  1151                 xtext = parent_pos[0] + self.Pos.x + 5
  1188                 xtext = parent_pos[0] + self.Pos.x + 5
  1152             else:
  1189             else: