graphics/LD_Objects.py
changeset 27 dae55dd9ee14
parent 8 7ceec5c40d77
child 28 fc23e1f415d8
equal deleted inserted replaced
26:36d378bd852e 27:dae55dd9ee14
    41     # Create a new power rail
    41     # Create a new power rail
    42     def __init__(self, parent, type, id = None, connectors = [True]):
    42     def __init__(self, parent, type, id = None, connectors = [True]):
    43         Graphic_Element.__init__(self, parent)
    43         Graphic_Element.__init__(self, parent)
    44         self.Type = type
    44         self.Type = type
    45         self.Id = id
    45         self.Id = id
       
    46         self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2]
       
    47         if len(connectors) < 1:
       
    48             connectors = [True]
    46         # Create a connector or a blank according to 'connectors' and add it in
    49         # Create a connector or a blank according to 'connectors' and add it in
    47         # the connectors list
    50         # the connectors list
    48         self.Connectors = []
    51         self.Connectors = []
    49         for connector in connectors:
    52         for connector in connectors:
    50             self.AddConnector(connector)
    53             self.AddConnector(connector)
    54     def __del__(self):
    57     def __del__(self):
    55         self.Connectors = []
    58         self.Connectors = []
    56     
    59     
    57     # Forbids to change the power rail size
    60     # Forbids to change the power rail size
    58     def SetSize(self, width, height):
    61     def SetSize(self, width, height):
    59         pass
    62         if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
       
    63             Graphic_Element.SetSize(self, width, height)
       
    64             self.RefreshConnectors()
    60     
    65     
    61     # Forbids to select a power rail
    66     # Forbids to select a power rail
    62     def HitTest(self, pt):
    67     def HitTest(self, pt):
       
    68         if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
       
    69             return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None
    63         return False
    70         return False
    64     
    71     
    65     # Deletes this power rail by calling the appropriate method
    72     # Deletes this power rail by calling the appropriate method
    66     def Delete(self):
    73     def Delete(self):
    67         self.Parent.DeletePowerRail(self)
    74         self.Parent.DeletePowerRail(self)
    73                 connector.UnConnect()
    80                 connector.UnConnect()
    74                 
    81                 
    75     # Refresh the power rail bounding box
    82     # Refresh the power rail bounding box
    76     def RefreshBoundingBox(self):
    83     def RefreshBoundingBox(self):
    77         dc = wxClientDC(self.Parent)
    84         dc = wxClientDC(self.Parent)
    78         if self.Type == LEFTRAIL:
    85         self.BoundingBox = wxRect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1] + 1)
    79             bbx_x = self.Pos.x
       
    80         elif self.Type == RIGHTRAIL:
       
    81             bbx_x = self.Pos.x - CONNECTOR_SIZE
       
    82         self.BoundingBox = wxRect(bbx_x, self.Pos.y, self.Size[0] + CONNECTOR_SIZE + 1, self.Size[1] + 1)
       
    83     
    86     
    84     # Refresh the power rail size
    87     # Refresh the power rail size
    85     def RefreshSize(self):
    88     def RefreshSize(self):
    86         self.Size = wxSize(2, LD_LINE_SIZE * len(self.Connectors))
    89         self.Size = wxSize(2, LD_LINE_SIZE * len(self.Connectors))
    87         self.RefreshBoundingBox()
    90         self.RefreshBoundingBox()
       
    91     
       
    92     # Returns the block minimum size
       
    93     def GetMinSize(self):
       
    94         return 2, LD_LINE_SIZE * len(self.Connectors)
    88     
    95     
    89     # Add a connector or a blank to this power rail at the last place
    96     # Add a connector or a blank to this power rail at the last place
    90     def AddConnector(self, connector = True):
    97     def AddConnector(self, connector = True):
    91         self.InsertConnector(len(self.Connectors), connector)
    98         self.InsertConnector(len(self.Connectors), connector)
    92     
    99     
   101         else:
   108         else:
   102             self.Connectors.insert(idx, None)
   109             self.Connectors.insert(idx, None)
   103         self.RefreshSize()
   110         self.RefreshSize()
   104         self.RefreshConnectors()
   111         self.RefreshConnectors()
   105     
   112     
       
   113     # Moves the divergence connector given
       
   114     def MoveConnector(self, connector, movey):
       
   115         position = connector.GetRelPosition()
       
   116         connector.SetPosition(wxPoint(position.x, position.y + movey))
       
   117         miny = self.Size[1]
       
   118         maxy = 0
       
   119         for connect in self.Connectors:
       
   120             connect_pos = connect.GetRelPosition()
       
   121             miny = min(miny, connect_pos.y)
       
   122             maxy = max(maxy, connect_pos.y)
       
   123         min_pos = self.Pos.y + miny - self.Extensions[0]
       
   124         self.Pos.y = min(min_pos, self.Pos.y)
       
   125         if min_pos == self.Pos.y:
       
   126             for connect in self.Connectors:
       
   127                 connect_pos = connect.GetRelPosition()
       
   128                 connect.SetPosition(wxPoint(connect_pos.x, connect_pos.y - miny + self.Extensions[0]))
       
   129         self.Size[1] = max(maxy - miny + self.Extensions[0] + self.Extensions[1], self.Size[1])
       
   130         connector.MoveConnected()
       
   131         self.RefreshBoundingBox()
       
   132     
   106     # Returns the index in connectors list for the connector given
   133     # Returns the index in connectors list for the connector given
   107     def GetConnectorIndex(self, connector):
   134     def GetConnectorIndex(self, connector):
   108         if connector in self.Connectors:
   135         if connector in self.Connectors:
   109             return self.Connectors.index(connector)
   136             return self.Connectors.index(connector)
   110         return None
   137         return None
   121         self.RefreshConnectors()
   148         self.RefreshConnectors()
   122         self.RefreshSize()
   149         self.RefreshSize()
   123     
   150     
   124     # Refresh the positions of the power rail connectors
   151     # Refresh the positions of the power rail connectors
   125     def RefreshConnectors(self):
   152     def RefreshConnectors(self):
   126         position = LD_LINE_SIZE / 2
   153         position = self.Extensions[0]
   127         for connector in self.Connectors:
   154         for connector in self.Connectors:
   128             if connector:
   155             if connector:
   129                 if self.Type == LEFTRAIL:
   156                 if self.Type == LEFTRAIL:
   130                     connector.SetPosition(wxPoint(self.Size[0], position))
   157                     connector.SetPosition(wxPoint(self.Size[0], position))
   131                 elif self.Type == RIGHTRAIL:
   158                 elif self.Type == RIGHTRAIL:
   138         for connector in self.Connectors:
   165         for connector in self.Connectors:
   139             if connector:
   166             if connector:
   140                 connector.MoveConnected(exclude)
   167                 connector.MoveConnected(exclude)
   141     
   168     
   142     # Returns the power rail connector that starts with the point given if it exists 
   169     # Returns the power rail connector that starts with the point given if it exists 
   143     def GetConnector(self, position):
   170     def GetConnector(self, position, name = None):
       
   171         # if a name is given
       
   172         if name:
       
   173             # Test each connector if it exists
       
   174             for connector in self.Connectors:
       
   175                 if connector and name == connector.GetName():
       
   176                     return connector
   144         for connector in self.Connectors:
   177         for connector in self.Connectors:
   145             if connector:
   178             if connector:
   146                 connector_pos = connector.GetRelPosition()
   179                 connector_pos = connector.GetRelPosition()
   147                 if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y:
   180                 if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y:
   148                     return connector
   181                     return connector
   160         return None
   193         return None
   161     
   194     
   162     # Returns the power rail type
   195     # Returns the power rail type
   163     def GetType(self):
   196     def GetType(self):
   164         return self.Type
   197         return self.Type
       
   198     
       
   199     # Method called when a LeftDown event have been generated
       
   200     def OnLeftDown(self, event, dc, scaling):
       
   201         pos = GetScaledEventPosition(event, dc, scaling)
       
   202         # Test if a connector have been handled
       
   203         connector = self.TestConnector(pos, False)
       
   204         if connector:
       
   205             self.Handle = (HANDLE_CONNECTOR, connector)
       
   206             self.Parent.SetCursor(wxStockCursor(wxCURSOR_HAND))
       
   207             self.Selected = False
       
   208             # Initializes the last position
       
   209             self.oldPos = GetScaledEventPosition(event, dc, scaling)
       
   210         else:
       
   211             self.RealConnectors = {"Inputs":[],"Outputs":[]}
       
   212             for input in self.Inputs:
       
   213                 position = input.GetRelPosition()
       
   214                 self.RealConnectors["Inputs"].append(float(position.x)/float(self.Size[0]))
       
   215             for output in self.Outputs:
       
   216                 position = output.GetRelPosition()
       
   217                 self.RealConnectors["Outputs"].append(float(position.x)/float(self.Size[0]))
       
   218             Graphic_Element.OnLeftDown(self, event, dc, scaling)
       
   219     
       
   220     # Method called when a LeftUp event have been generated
       
   221     def OnLeftUp(self, event, dc, scaling):
       
   222         self.RealConnectors = None
       
   223         handle_type, handle = self.Handle
       
   224         if handle_type == HANDLE_CONNECTOR:
       
   225             wires = handle.GetWires()
       
   226             if len(wires) != 1:
       
   227                 return
       
   228             if handle == wires[0][0].StartConnected:
       
   229                 block = wires[0][0].EndConnected.GetParentBlock()
       
   230             else:
       
   231                 block = wires[0][0].StartConnected.GetParentBlock()
       
   232             block.RefreshModel(False)
       
   233         Graphic_Element.OnLeftUp(self, event, dc, scaling)
       
   234     
       
   235     # Method called when a RightUp event have been generated
       
   236     def OnRightUp(self, event, dc, scaling):
       
   237         pos = GetScaledEventPosition(event, dc, scaling)
       
   238         # Popup the menu with special items for a block and a connector if one is handled
       
   239         connector = self.TestConnector(pos, False)
       
   240         if connector:
       
   241             self.Handle = (HANDLE_CONNECTOR, connector)
       
   242         #    self.Parent.PopupDivergenceMenu(True)
       
   243         #else:
       
   244             # Popup the divergence menu without delete branch
       
   245         #    self.Parent.PopupDivergenceMenu(False)
       
   246     
       
   247     # Refreshes the divergence state according to move defined and handle selected
       
   248     def ProcessDragging(self, movex, movey):
       
   249         handle_type, handle = self.Handle
       
   250         # A connector has been handled
       
   251         if handle_type == HANDLE_CONNECTOR:
       
   252             self.MoveConnector(handle, movey)
       
   253         else:
       
   254             Graphic_Element.ProcessDragging(self, movex, movey)
   165     
   255     
   166     # Refreshes the power rail model
   256     # Refreshes the power rail model
   167     def RefreshModel(self, move=True):
   257     def RefreshModel(self, move=True):
   168         self.Parent.RefreshPowerRailModel(self)
   258         self.Parent.RefreshPowerRailModel(self)
   169         # If power rail has moved and power rail is of type LEFT, refresh the model 
   259         # If power rail has moved and power rail is of type LEFT, refresh the model 
   212         self.Input = None
   302         self.Input = None
   213         self.Output = None
   303         self.Output = None
   214     
   304     
   215     # Forbids to change the contact size
   305     # Forbids to change the contact size
   216     def SetSize(self, width, height):
   306     def SetSize(self, width, height):
   217         pass
   307         if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
       
   308             Graphic_Element.SetSize(self, width, height)
       
   309             self.RefreshConnectors()
   218     
   310     
   219     # Delete this contact by calling the appropriate method
   311     # Delete this contact by calling the appropriate method
   220     def Delete(self):
   312     def Delete(self):
   221         self.Parent.DeleteContact(self)
   313         self.Parent.DeleteContact(self)
   222     
   314     
   241             bbx_width = self.Size[0]
   333             bbx_width = self.Size[0]
   242             bbx_y = self.Pos.y
   334             bbx_y = self.Pos.y
   243             bbx_height = self.Size[1]
   335             bbx_height = self.Size[1]
   244         self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
   336         self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
   245     
   337     
       
   338     # Returns the block minimum size
       
   339     def GetMinSize(self):
       
   340         return LD_ELEMENT_SIZE
       
   341     
   246     # Refresh the position of wire connected to contact
   342     # Refresh the position of wire connected to contact
   247     def RefreshConnected(self, exclude = []):
   343     def RefreshConnected(self, exclude = []):
   248         self.Input.MoveConnected(exclude)
   344         self.Input.MoveConnected(exclude)
   249         self.Output.MoveConnected(exclude)
   345         self.Output.MoveConnected(exclude)
   250     
   346     
   251     # Returns the contact connector that starts with the point given if it exists 
   347     # Returns the contact connector that starts with the point given if it exists 
   252     def GetConnector(self, position):
   348     def GetConnector(self, position, name = None):
       
   349         # if a name is given
       
   350         if name:
       
   351             # Test input and output connector
       
   352             if name == self.Input.GetName():
       
   353                 return self.Input
       
   354             if name == self.Output.GetName():
       
   355                 return self.Output
   253         # Test input connector
   356         # Test input connector
   254         input_pos = self.Input.GetRelPosition()
   357         input_pos = self.Input.GetRelPosition()
   255         if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
   358         if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
   256             return self.Input
   359             return self.Input
   257         # Test output connector
   360         # Test output connector
   272         # Test output connector
   375         # Test output connector
   273         if self.Output.TestPoint(pt, exclude):
   376         if self.Output.TestPoint(pt, exclude):
   274             return self.Output
   377             return self.Output
   275         return None
   378         return None
   276 
   379 
       
   380     # Refresh the positions of the block connectors
       
   381     def RefreshConnectors(self):
       
   382         self.Input.SetPosition(wxPoint(0, self.Size[1] / 2 + 1))
       
   383         self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2 + 1))
       
   384         self.RefreshConnected()
       
   385 
   277     # Changes the contact name
   386     # Changes the contact name
   278     def SetName(self, name):
   387     def SetName(self, name):
   279         self.Name = name
   388         self.Name = name
   280 
   389 
   281     # Returns the contact name
   390     # Returns the contact name
   289     # Returns the contact type
   398     # Returns the contact type
   290     def GetType(self):
   399     def GetType(self):
   291         return self.Type
   400         return self.Type
   292     
   401     
   293     # Method called when a LeftDClick event have been generated
   402     # Method called when a LeftDClick event have been generated
   294     def OnLeftDClick(self, event, scaling):
   403     def OnLeftDClick(self, event, dc, scaling):
   295         # Edit the contact properties
   404         # Edit the contact properties
   296         self.Parent.EditContactContent(self)
   405         self.Parent.EditContactContent(self)
   297     
   406     
   298     # Refreshes the contact model
   407     # Refreshes the contact model
   299     def RefreshModel(self, move=True):
   408     def RefreshModel(self, move=True):
   357         self.Input = None
   466         self.Input = None
   358         self.Output = None
   467         self.Output = None
   359     
   468     
   360     # Forbids to change the contact size
   469     # Forbids to change the contact size
   361     def SetSize(self, width, height):
   470     def SetSize(self, width, height):
   362         pass
   471         if isinstance(self.Parent, wxPanel) or self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
       
   472             Graphic_Element.SetSize(self, width, height)
       
   473             self.RefreshConnectors()
   363     
   474     
   364     # Delete this coil by calling the appropriate method
   475     # Delete this coil by calling the appropriate method
   365     def Delete(self):
   476     def Delete(self):
   366         self.Parent.DeleteCoil(self)
   477         self.Parent.DeleteCoil(self)
   367     
   478     
   385             bbx_x = self.Pos.x
   496             bbx_x = self.Pos.x
   386             bbx_width = self.Size[0]
   497             bbx_width = self.Size[0]
   387             bbx_y = self.Pos.y
   498             bbx_y = self.Pos.y
   388             bbx_height = self.Size[1]
   499             bbx_height = self.Size[1]
   389         self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
   500         self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
       
   501         
       
   502     # Returns the block minimum size
       
   503     def GetMinSize(self):
       
   504         return LD_ELEMENT_SIZE
   390     
   505     
   391     # Refresh the position of wire connected to coil
   506     # Refresh the position of wire connected to coil
   392     def RefreshConnected(self, exclude = []):
   507     def RefreshConnected(self, exclude = []):
   393         self.Input.MoveConnected(exclude)
   508         self.Input.MoveConnected(exclude)
   394         self.Output.MoveConnected(exclude)
   509         self.Output.MoveConnected(exclude)
   395     
   510     
   396     # Returns the coil connector that starts with the point given if it exists 
   511     # Returns the coil connector that starts with the point given if it exists 
   397     def GetConnector(self, position):
   512     def GetConnector(self, position, name = None):
       
   513         # if a name is given
       
   514         if name:
       
   515             # Test input and output connector
       
   516             if self.Input and name == self.Input.GetName():
       
   517                 return self.Input
       
   518             if self.Output and name == self.Output.GetName():
       
   519                 return self.Output
   398         # Test input connector
   520         # Test input connector
   399         input_pos = self.Input.GetRelPosition()
   521         input_pos = self.Input.GetRelPosition()
   400         if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
   522         if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
   401             return self.Input
   523             return self.Input
   402         # Test output connector
   524         # Test output connector
   417         # Test output connector
   539         # Test output connector
   418         if self.Output.TestPoint(pt, exclude):
   540         if self.Output.TestPoint(pt, exclude):
   419             return self.Output
   541             return self.Output
   420         return None
   542         return None
   421     
   543     
       
   544     # Refresh the positions of the block connectors
       
   545     def RefreshConnectors(self):
       
   546         self.Input.SetPosition(wxPoint(0, self.Size[1] / 2 + 1))
       
   547         self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2 + 1))
       
   548         self.RefreshConnected()
       
   549     
   422     # Changes the coil name
   550     # Changes the coil name
   423     def SetName(self, name):
   551     def SetName(self, name):
   424         self.Name = name
   552         self.Name = name
   425 
   553 
   426     # Returns the coil name
   554     # Returns the coil name
   434     # Returns the coil type
   562     # Returns the coil type
   435     def GetType(self):
   563     def GetType(self):
   436         return self.Type
   564         return self.Type
   437     
   565     
   438     # Method called when a LeftDClick event have been generated
   566     # Method called when a LeftDClick event have been generated
   439     def OnLeftDClick(self, event, scaling):
   567     def OnLeftDClick(self, event, dc, scaling):
   440         # Edit the coil properties
   568         # Edit the coil properties
   441         self.Parent.EditCoilContent(self)
   569         self.Parent.EditCoilContent(self)
   442     
   570     
   443     # Refreshes the coil model
   571     # Refreshes the coil model
   444     def RefreshModel(self, move=True):
   572     def RefreshModel(self, move=True):