graphics/GraphicCommons.py
changeset 852 1009f956d2ee
parent 825 0623820aa14a
child 857 9695969796d0
equal deleted inserted replaced
851:666f5bdad301 852:1009f956d2ee
   116     (2, 3) : 5
   116     (2, 3) : 5
   117 }
   117 }
   118 
   118 
   119 def round_scaling(x, n, constraint=0):
   119 def round_scaling(x, n, constraint=0):
   120     fraction = float(x) / float(n)
   120     fraction = float(x) / float(n)
   121     if constraint == - 1:
   121     if constraint == -1:
   122         xround = int(fraction)
   122         xround = int(fraction)
   123     else:
   123     else:
   124         xround = round(fraction)
   124         xround = round(fraction)
   125         if constraint == 1 and int(fraction) == xround:
   125         if constraint == 1 and xround < fraction:
   126             xround += 1
   126             xround += 1 
   127     return xround * n
   127     return int(xround * n)
   128 
   128 
   129 """
   129 """
   130 Basic vector operations for calculate wire points
   130 Basic vector operations for calculate wire points
   131 """
   131 """
   132 
   132 
   673     
   673     
   674     def GetDefinition(self):
   674     def GetDefinition(self):
   675         return [self.Id], []
   675         return [self.Id], []
   676     
   676     
   677     def TestVisible(self, screen):
   677     def TestVisible(self, screen):
   678         self.Visible = self.GetRedrawRect().Intersects(screen)
   678         self.Visible = self.Selected or self.GetRedrawRect().Intersects(screen)
   679     
   679     
   680     def IsVisible(self):
   680     def IsVisible(self):
   681         return self.Visible
   681         return self.Visible
   682     
   682     
   683     def SpreadCurrent(self):
   683     def SpreadCurrent(self):
   734         return self.Size.GetWidth(), self.Size.GetHeight()
   734         return self.Size.GetWidth(), self.Size.GetHeight()
   735     
   735     
   736     # Returns the minimum element size
   736     # Returns the minimum element size
   737     def GetMinSize(self):
   737     def GetMinSize(self):
   738         return 0, 0
   738         return 0, 0
       
   739     
       
   740     # Set size of the element to the minimum size
       
   741     def SetBestSize(self, scaling, x_factor=0.5, y_factor=0.5):
       
   742         width, height = self.GetSize()
       
   743         posx, posy = self.GetPosition()
       
   744         min_width, min_height = self.GetMinSize()
       
   745         if width < min_width:
       
   746             self.Pos.x = max(0, self.Pos.x - (width - min_width) * x_factor)
       
   747             width = min_width
       
   748         if height < min_height:
       
   749             self.Pos.y = max(0, self.Pos.y - (height - min_height) * y_factor)
       
   750             height = min_height
       
   751         if scaling is not None:
       
   752             self.Pos.x = round_scaling(self.Pos.x, scaling[0])
       
   753             self.Pos.y = round_scaling(self.Pos.y, scaling[1])
       
   754             width = round_scaling(width, scaling[0], 1)
       
   755             height = round_scaling(height, scaling[1], 1)
       
   756         self.SetSize(width, height)
       
   757         return self.Pos.x - posx, self.Pos.y - posy
   739     
   758     
   740     # Refresh the element Bounding Box
   759     # Refresh the element Bounding Box
   741     def RefreshBoundingBox(self):
   760     def RefreshBoundingBox(self):
   742         self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1])
   761         self.BoundingBox = wx.Rect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1])
   743     
   762     
   940     
   959     
   941     # Resizes the element from position and size given
   960     # Resizes the element from position and size given
   942     def Resize(self, x, y, width, height):
   961     def Resize(self, x, y, width, height):
   943         self.Move(x, y)
   962         self.Move(x, y)
   944         self.SetSize(width, height)
   963         self.SetSize(width, height)
   945     
       
   946     # Moves and Resizes the element for fitting scaling
       
   947     def AdjustToScaling(self, scaling):
       
   948         if scaling is not None:
       
   949             movex = round_scaling(self.Pos.x, scaling[0]) - self.Pos.x
       
   950             movey = round_scaling(self.Pos.y, scaling[1]) - self.Pos.y
       
   951             min_width, min_height = self.GetMinSize()
       
   952             width = max(round_scaling(min_width, scaling[0], 1),
       
   953                         round_scaling(self.Size.width, scaling[0]))
       
   954             height = max(round_scaling(min_height, scaling[1], 1),
       
   955                          round_scaling(self.Size.height, scaling[1]))
       
   956             self.Resize(movex, movey, width, height)
       
   957             return movex, movey
       
   958         return 0, 0
       
   959     
   964     
   960     # Refreshes the element state according to move defined and handle selected
   965     # Refreshes the element state according to move defined and handle selected
   961     def ProcessDragging(self, movex, movey, event, scaling, width_fac = 1, height_fac = 1):
   966     def ProcessDragging(self, movex, movey, event, scaling, width_fac = 1, height_fac = 1):
   962         handle_type, handle = self.Handle
   967         handle_type, handle = self.Handle
   963         # If it is a resize handle, calculate the values from resizing
   968         # If it is a resize handle, calculate the values from resizing
  1174                         name = parent.GenerateNewName(element, exclude_names)
  1179                         name = parent.GenerateNewName(element, exclude_names)
  1175                         exclude_names[name.upper()] = True
  1180                         exclude_names[name.upper()] = True
  1176                         new_element = element.Clone(parent, newid, name, pos = new_pos)
  1181                         new_element = element.Clone(parent, newid, name, pos = new_pos)
  1177                     else:
  1182                     else:
  1178                         new_element = element.Clone(parent, newid, pos = new_pos)
  1183                         new_element = element.Clone(parent, newid, pos = new_pos)
  1179                     new_element.AdjustToScaling(parent.Scaling)
  1184                     new_element.SetBestSize(parent.Scaling)
  1180                 else:
  1185                 else:
  1181                     new_element = element.Clone(parent)
  1186                     new_element = element.Clone(parent)
  1182                 connectors.update(element.GetConnectorTranslation(new_element))
  1187                 connectors.update(element.GetConnectorTranslation(new_element))
  1183                 group.SelectElement(new_element)
  1188                 group.SelectElement(new_element)
  1184         for element in wires:
  1189         for element in wires:
  1366         pass
  1371         pass
  1367     
  1372     
  1368     # Returns the size of this group
  1373     # Returns the size of this group
  1369     def GetSize(self):
  1374     def GetSize(self):
  1370         return self.BoundingBox.width, self.BoundingBox.height
  1375         return self.BoundingBox.width, self.BoundingBox.height
  1371 
  1376     
  1372     # Moves and Resizes the group elements for fitting scaling
  1377     # Set size of the group elements to their minimum size
  1373     def AdjustToScaling(self, scaling):
  1378     def SetBestSize(self, scaling):
  1374         movex_max = movey_max = 0
  1379         max_movex = max_movey = 0
  1375         for element in self.Elements:
  1380         for element in self.Elements:
  1376             movex, movey = element.AdjustToScaling(scaling)
  1381             movex, movey = element.SetBestSize(scaling)
  1377             movex_max = max(movex_max, abs(movex))
  1382             max_movex = max(max_movex, movex)
  1378             movey_max = max(movey_max, abs(movey))
  1383             max_movey = max(max_movey, movey)
  1379         return movex_max, movey_max
  1384         return max_movex, max_movey
  1380     
  1385     
  1381     # Refreshes the group elements to move defined and handle selected
  1386     # Refreshes the group elements to move defined and handle selected
  1382     def ProcessDragging(self, movex, movey, event, scaling):
  1387     def ProcessDragging(self, movex, movey, event, scaling):
  1383         handle_type, handle = self.Handle
  1388         handle_type, handle = self.Handle
  1384         # If it is a move handle, Move this group elements
  1389         # If it is a move handle, Move this group elements
  1984     
  1989     
  1985     # Forbids to change the wire size
  1990     # Forbids to change the wire size
  1986     def SetSize(width, height):
  1991     def SetSize(width, height):
  1987         pass
  1992         pass
  1988     
  1993     
       
  1994     # Forbids to et size of the group elements to their minimum size
       
  1995         pass
       
  1996     
  1989     # Moves and Resizes the element for fitting scaling
  1997     # Moves and Resizes the element for fitting scaling
  1990     def AdjustToScaling(self, scaling):
  1998     def SetBestSize(self, scaling):
  1991         if scaling is not None:
  1999         if scaling is not None:
  1992             movex_max = movey_max = 0
  2000             movex_max = movey_max = 0
  1993             for idx, point in enumerate(self.Points):
  2001             for idx, point in enumerate(self.Points):
  1994                 if 0 < idx < len(self.Points) - 1:
  2002                 if 0 < idx < len(self.Points) - 1:
  1995                     movex = round_scaling(point.x, scaling[0]) - point.x
  2003                     movex = round_scaling(point.x, scaling[0]) - point.x