graphics/GraphicCommons.py
changeset 140 06d28f03f6f4
parent 138 9c74d00ce93e
child 144 b67a5de5a24a
equal deleted inserted replaced
139:c2d093402005 140:06d28f03f6f4
    86 [ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_TOP, ALIGN_MIDDLE, ALIGN_BOTTOM] = range(6)
    86 [ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_TOP, ALIGN_MIDDLE, ALIGN_BOTTOM] = range(6)
    87 
    87 
    88 # Contants for defining which drawing mode is selected for app
    88 # Contants for defining which drawing mode is selected for app
    89 [FREEDRAWING_MODE, DRIVENDRAWING_MODE] = [1, 2]
    89 [FREEDRAWING_MODE, DRIVENDRAWING_MODE] = [1, 2]
    90 
    90 
       
    91 # Color for Highlighting
       
    92 HIGHLIGHTCOLOR = wx.CYAN
       
    93 
    91 CURSORS = None
    94 CURSORS = None
    92 
    95 
    93 def ResetCursors():
    96 def ResetCursors():
    94     global CURSORS
    97     global CURSORS
    95     if CURSORS == None:
    98     if CURSORS == None:
   286         self.Id = id
   289         self.Id = id
   287         self.oldPos = None
   290         self.oldPos = None
   288         self.Handle = False
   291         self.Handle = False
   289         self.Dragging = False
   292         self.Dragging = False
   290         self.Selected = False
   293         self.Selected = False
       
   294         self.Highlighted = False
   291         self.Pos = wx.Point(0, 0)
   295         self.Pos = wx.Point(0, 0)
   292         self.Size = wx.Size(0, 0)
   296         self.Size = wx.Size(0, 0)
   293         self.BoundingBox = wx.Rect(0, 0, 0, 0)
   297         self.BoundingBox = wx.Rect(0, 0, 0, 0)
   294         self.CurrentCursor = 0
   298         self.CurrentCursor = 0
   295         ResetCursors()
   299         ResetCursors()
   362         return self.BoundingBox
   366         return self.BoundingBox
   363     
   367     
   364     # Change the variable that indicates if this element is selected
   368     # Change the variable that indicates if this element is selected
   365     def SetSelected(self, selected):
   369     def SetSelected(self, selected):
   366         self.Selected = selected
   370         self.Selected = selected
       
   371     
       
   372     # Change the variable that indicates if this element is highlighted
       
   373     def SetHighlighted(self, highlighted):
       
   374         self.Highlighted = highlighted
   367     
   375     
   368     # Test if the point is on a handle of this element
   376     # Test if the point is on a handle of this element
   369     def TestHandle(self, pt):
   377     def TestHandle(self, pt):
   370         extern_rect = wx.Rect(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y - HANDLE_SIZE - 2, 
   378         extern_rect = wx.Rect(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y - HANDLE_SIZE - 2, 
   371             self.BoundingBox.width + 2 * HANDLE_SIZE + 4, self.BoundingBox.height + 2 * HANDLE_SIZE + 4)
   379             self.BoundingBox.width + 2 * HANDLE_SIZE + 4, self.BoundingBox.height + 2 * HANDLE_SIZE + 4)
   524     
   532     
   525     # Override this method for defining the method to call for refreshing the model of this element
   533     # Override this method for defining the method to call for refreshing the model of this element
   526     def RefreshModel(self, move=True):
   534     def RefreshModel(self, move=True):
   527         pass
   535         pass
   528     
   536     
       
   537     # Draws the highlightment of this element if it is highlighted (can be overwritten)
       
   538     def DrawHighlightment(self, dc):
       
   539         if self.Highlighted:
       
   540             dc.SetPen(wx.Pen(HIGHLIGHTCOLOR))
       
   541             dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
       
   542             dc.DrawRectangle(self.Pos.x - 2, self.Pos.y - 2, self.Size.width + 5, self.Size.height + 5)
       
   543     
   529     # Draws the handles of this element if it is selected
   544     # Draws the handles of this element if it is selected
   530     def Draw(self, dc):
   545     def Draw(self, dc):
   531         if self.Selected:
   546         if self.Selected:
   532             dc.SetPen(wx.BLACK_PEN)
   547             dc.SetPen(wx.BLACK_PEN)
   533             dc.SetBrush(wx.BLACK_BRUSH)
   548             dc.SetBrush(wx.BLACK_BRUSH)
   542                 self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE)
   557                 self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE)
   543             dc.DrawRectangle(self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2, 
   558             dc.DrawRectangle(self.BoundingBox.x + (self.BoundingBox.width - HANDLE_SIZE) / 2, 
   544                 self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE)
   559                 self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE)
   545             dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE)
   560             dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + self.BoundingBox.height + 2, HANDLE_SIZE, HANDLE_SIZE)
   546             dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2, HANDLE_SIZE, HANDLE_SIZE)
   561             dc.DrawRectangle(self.BoundingBox.x - HANDLE_SIZE - 2, self.BoundingBox.y + (self.BoundingBox.height - HANDLE_SIZE) / 2, HANDLE_SIZE, HANDLE_SIZE)
   547             dc.SetBrush(wx.WHITE_BRUSH)
       
   548 
   562 
   549 
   563 
   550 #-------------------------------------------------------------------------------
   564 #-------------------------------------------------------------------------------
   551 #                           Group of graphic elements
   565 #                           Group of graphic elements
   552 #-------------------------------------------------------------------------------
   566 #-------------------------------------------------------------------------------
   709     
   723     
   710     # Returns the size of this group
   724     # Returns the size of this group
   711     def GetSize(self):
   725     def GetSize(self):
   712         return self.BoundingBox.width, self.BoundingBox.height
   726         return self.BoundingBox.width, self.BoundingBox.height
   713 
   727 
       
   728     # Change the variable that indicates if this element is highlighted
       
   729     def SetHighlighted(self, highlighted):
       
   730         for element in self.Elements:
       
   731             element.SetHighlighted(highlighted)
       
   732 
   714     # Change the variable that indicates if the elemente is selected
   733     # Change the variable that indicates if the elemente is selected
   715     def SetSelected(self, selected):
   734     def SetSelected(self, selected):
   716         for element in self.Elements:
   735         for element in self.Elements:
   717             element.SetSelected(selected)
   736             element.SetSelected(selected)
   718 
   737 
   923     
   942     
   924     # Refreshes the parent block model
   943     # Refreshes the parent block model
   925     def RefreshParentBlock(self):
   944     def RefreshParentBlock(self):
   926         self.ParentBlock.RefreshModel(False)
   945         self.ParentBlock.RefreshModel(False)
   927     
   946     
       
   947     # Highlight the parent block
       
   948     def HighlightParentBlock(self, highlight):
       
   949         self.ParentBlock.SetHighlighted(highlight)
       
   950     
   928     # Returns all the blocks connected to this connector
   951     # Returns all the blocks connected to this connector
   929     def GetConnectedBlocks(self):
   952     def GetConnectedBlocks(self):
   930         blocks = []
   953         blocks = []
   931         for wire, handle in self.Wires:
   954         for wire, handle in self.Wires:
   932             # Get other connector connected to each wire
   955             # Get other connector connected to each wire
   971             width = ANCHOR_DISTANCE * 2 + abs(self.Direction[0]) * CONNECTOR_SIZE
   994             width = ANCHOR_DISTANCE * 2 + abs(self.Direction[0]) * CONNECTOR_SIZE
   972             height = ANCHOR_DISTANCE * 2 + abs(self.Direction[1]) * CONNECTOR_SIZE
   995             height = ANCHOR_DISTANCE * 2 + abs(self.Direction[1]) * CONNECTOR_SIZE
   973             rect = wx.Rect(x, y, width, height)
   996             rect = wx.Rect(x, y, width, height)
   974             return rect.InsideXY(pt.x, pt.y)
   997             return rect.InsideXY(pt.x, pt.y)
   975         return False
   998         return False
       
   999     
       
  1000     # Draws the highlightment of this element if it is highlighted
       
  1001     def DrawHighlightment(self, dc):
       
  1002         dc.SetPen(wx.Pen(HIGHLIGHTCOLOR))
       
  1003         dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
       
  1004         parent_pos = self.ParentBlock.GetPosition()
       
  1005         posx = parent_pos[0] + self.Pos.x
       
  1006         posy = parent_pos[1] + self.Pos.y
       
  1007         width = CONNECTOR_SIZE
       
  1008         height = CONNECTOR_SIZE
       
  1009         if self.Direction[0] < 0:
       
  1010             posx += CONNECTOR_SIZE * self.Direction[0]
       
  1011         elif self.Direction[0] == 0:
       
  1012             posx -= 2
       
  1013             width = 5
       
  1014         if self.Direction[1] < 0:
       
  1015             posy += CONNECTOR_SIZE * self.Direction[1]
       
  1016         elif self.Direction[1] == 0:
       
  1017             posy -= 2
       
  1018             height = 5
       
  1019         dc.DrawRectangle(posx, posy, width, height)
   976     
  1020     
   977     # Draws the connector
  1021     # Draws the connector
   978     def Draw(self, dc):
  1022     def Draw(self, dc):
   979         dc.SetPen(self.Pen)
  1023         dc.SetPen(self.Pen)
   980         dc.SetBrush(wx.WHITE_BRUSH)
  1024         dc.SetBrush(wx.WHITE_BRUSH)
  1770             # Try to connect point to a connector
  1814             # Try to connect point to a connector
  1771             new_pos = wx.Point(self.Points[handle].x + movex, self.Points[handle].y + movey)
  1815             new_pos = wx.Point(self.Points[handle].x + movex, self.Points[handle].y + movey)
  1772             connector = self.Parent.FindBlockConnector(new_pos)
  1816             connector = self.Parent.FindBlockConnector(new_pos)
  1773             if connector:
  1817             if connector:
  1774                 if handle == 0 and self.EndConnected != connector and connector.IsCompatible(self.GetEndConnectedType()):
  1818                 if handle == 0 and self.EndConnected != connector and connector.IsCompatible(self.GetEndConnectedType()):
       
  1819                     connector.HighlightParentBlock(True)
  1775                     connector.Connect((self, handle))
  1820                     connector.Connect((self, handle))
  1776                     self.SetStartPointDirection(connector.GetDirection())
  1821                     self.SetStartPointDirection(connector.GetDirection())
  1777                     self.ConnectStartPoint(connector.GetPosition(), connector)
  1822                     self.ConnectStartPoint(connector.GetPosition(), connector)
  1778                     pos = connector.GetPosition()
  1823                     pos = connector.GetPosition()
  1779                     movex = pos.x - self.oldPos.x
  1824                     movex = pos.x - self.oldPos.x
  1780                     movey = pos.y - self.oldPos.y
  1825                     movey = pos.y - self.oldPos.y
  1781                     self.Dragging = False
  1826                     self.Dragging = False
  1782                 elif handle != 0 and self.StartConnected != connector and connector.IsCompatible(self.GetStartConnectedType()):
  1827                 elif handle != 0 and self.StartConnected != connector and connector.IsCompatible(self.GetStartConnectedType()):
       
  1828                     connector.HighlightParentBlock(True)
  1783                     connector.Connect((self, handle))
  1829                     connector.Connect((self, handle))
  1784                     self.SetEndPointDirection(connector.GetDirection())
  1830                     self.SetEndPointDirection(connector.GetDirection())
  1785                     self.ConnectEndPoint(connector.GetPosition(), connector)
  1831                     self.ConnectEndPoint(connector.GetPosition(), connector)
  1786                     pos = connector.GetPosition()
  1832                     pos = connector.GetPosition()
  1787                     movex = pos.x - self.oldPos.x
  1833                     movex = pos.x - self.oldPos.x
  1793                     self.MoveEndPoint(new_pos)
  1839                     self.MoveEndPoint(new_pos)
  1794             # If there is no connector, move the point
  1840             # If there is no connector, move the point
  1795             elif handle == 0:
  1841             elif handle == 0:
  1796                 if self.StartConnected:
  1842                 if self.StartConnected:
  1797                     self.UnConnectStartPoint()
  1843                     self.UnConnectStartPoint()
       
  1844                     self.StartConnected.HighlightParentBlock(False)
  1798                 self.MoveStartPoint(new_pos)
  1845                 self.MoveStartPoint(new_pos)
  1799             else:
  1846             else:
  1800                 if self.EndConnected:
  1847                 if self.EndConnected:
       
  1848                     self.EndConnected.HighlightParentBlock(False)
  1801                     self.UnConnectEndPoint()
  1849                     self.UnConnectEndPoint()
  1802                 self.MoveEndPoint(new_pos)
  1850                 self.MoveEndPoint(new_pos)
  1803             return movex, movey
  1851             return movex, movey
  1804         # A segment has been handled, move a segment
  1852         # A segment has been handled, move a segment
  1805         elif handle_type == HANDLE_SEGMENT:
  1853         elif handle_type == HANDLE_SEGMENT:
  1813         if self.StartConnected and self.StartPoint[1] in [WEST, NORTH]:
  1861         if self.StartConnected and self.StartPoint[1] in [WEST, NORTH]:
  1814             self.StartConnected.RefreshParentBlock()
  1862             self.StartConnected.RefreshParentBlock()
  1815         if self.EndConnected and self.EndPoint[1] in [WEST, NORTH]:
  1863         if self.EndConnected and self.EndPoint[1] in [WEST, NORTH]:
  1816             self.EndConnected.RefreshParentBlock()
  1864             self.EndConnected.RefreshParentBlock()
  1817     
  1865     
       
  1866     # Draws the highlightment of this element if it is highlighted
       
  1867     def DrawHighlightment(self, dc):
       
  1868         if self.Highlighted:
       
  1869             dc.SetPen(wx.Pen(HIGHLIGHTCOLOR))
       
  1870             dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
       
  1871             # Draw the start and end points if they are not connected or the mouse is over them
       
  1872             if len(self.Points) > 0 and (not self.StartConnected or self.OverStart):
       
  1873                 dc.DrawCircle(self.Points[0].x, self.Points[0].y, POINT_RADIUS + 2)
       
  1874             if len(self.Points) > 1 and (not self.EndConnected or self.OverEnd):
       
  1875                 dc.DrawCircle(self.Points[-1].x, self.Points[-1].y, POINT_RADIUS + 2)
       
  1876             for i in xrange(len(self.Points) - 1):
       
  1877                 posx = min(self.Points[i].x, self.Points[i + 1].x) - 2
       
  1878                 posy = min(self.Points[i].y, self.Points[i + 1].y) - 2
       
  1879                 width = abs(self.Points[i + 1].x - self.Points[i].x) + 5
       
  1880                 height = abs(self.Points[i + 1].y - self.Points[i].y) + 5
       
  1881                 dc.DrawRectangle(posx, posy, width, height)
       
  1882             if self.StartConnected is not None:
       
  1883                 self.StartConnected.DrawHighlightment(dc)
       
  1884             if self.EndConnected is not None:
       
  1885                 self.EndConnected.DrawHighlightment(dc)    
       
  1886         
  1818     # Draws the wire lines and points
  1887     # Draws the wire lines and points
  1819     def Draw(self, dc):
  1888     def Draw(self, dc):
  1820         dc.SetPen(wx.BLACK_PEN)
  1889         dc.SetPen(wx.BLACK_PEN)
  1821         dc.SetBrush(wx.BLACK_BRUSH)
  1890         dc.SetBrush(wx.BLACK_BRUSH)
  1822         # Draw the start and end points if they are not connected or the mouse is over them
  1891         # Draw the start and end points if they are not connected or the mouse is over them
  1834                         self.Points[self.SelectedSegment + 1].x, self.Points[self.SelectedSegment + 1].y)
  1903                         self.Points[self.SelectedSegment + 1].x, self.Points[self.SelectedSegment + 1].y)
  1835             if self.SelectedSegment == len(self.Segments) - 1:
  1904             if self.SelectedSegment == len(self.Segments) - 1:
  1836                 dc.DrawPoint(self.Points[-1].x, self.Points[-1].y)
  1905                 dc.DrawPoint(self.Points[-1].x, self.Points[-1].y)
  1837         Graphic_Element.Draw(self, dc)
  1906         Graphic_Element.Draw(self, dc)
  1838 
  1907 
  1839 
       
  1840 #-------------------------------------------------------------------------------
  1908 #-------------------------------------------------------------------------------
  1841 #                           Graphic comment element
  1909 #                           Graphic comment element
  1842 #-------------------------------------------------------------------------------
  1910 #-------------------------------------------------------------------------------
  1843 
  1911 
  1844 """
  1912 """
  1941     # Method called when a LeftDClick event have been generated
  2009     # Method called when a LeftDClick event have been generated
  1942     def OnLeftDClick(self, event, dc, scaling):
  2010     def OnLeftDClick(self, event, dc, scaling):
  1943         # Edit the comment content
  2011         # Edit the comment content
  1944         self.Parent.EditCommentContent(self)
  2012         self.Parent.EditCommentContent(self)
  1945     
  2013     
       
  2014     # Draws the highlightment of this element if it is highlighted
       
  2015     def DrawHighlightment(self, dc):
       
  2016         if self.Highlighted:
       
  2017             dc.SetPen(wx.Pen(HIGHLIGHTCOLOR))
       
  2018             dc.SetBrush(wx.Brush(HIGHLIGHTCOLOR))
       
  2019             polygon = [wx.Point(self.Pos.x - 2, self.Pos.y - 2), 
       
  2020                        wx.Point(self.Pos.x + self.Size[0] - 8, self.Pos.y - 2),
       
  2021                        wx.Point(self.Pos.x + self.Size[0] + 2, self.Pos.y + 8),
       
  2022                        wx.Point(self.Pos.x + self.Size[0] + 2, self.Pos.y + self.Size[1] + 2),
       
  2023                        wx.Point(self.Pos.x - 2, self.Pos.y + self.Size[1] + 2)]
       
  2024             dc.DrawPolygon(polygon)
       
  2025         
  1946     # Draws the comment and its content
  2026     # Draws the comment and its content
  1947     def Draw(self, dc):
  2027     def Draw(self, dc):
  1948         dc.SetPen(wx.BLACK_PEN)
  2028         dc.SetPen(wx.BLACK_PEN)
  1949         dc.SetBrush(wx.WHITE_BRUSH)
  2029         dc.SetBrush(wx.WHITE_BRUSH)
  1950         # Draws the comment shape
  2030         # Draws the comment shape
  1951         polygon = [wx.Point(self.Pos.x, self.Pos.y), 
  2031         polygon = [wx.Point(self.Pos.x, self.Pos.y), 
  1952                    wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y),
  2032                    wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y),
  1953                    wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10),
  2033                    wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10),
  1954                    wx.Point(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1] + 1),
  2034                    wx.Point(self.Pos.x + self.Size[0], self.Pos.y + self.Size[1]),
  1955                    wx.Point(self.Pos.x, self.Pos.y + self.Size[1] + 1)]
  2035                    wx.Point(self.Pos.x, self.Pos.y + self.Size[1])]
  1956         dc.DrawPolygon(polygon)
  2036         dc.DrawPolygon(polygon)
  1957         lines = [wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y),
  2037         lines = [wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y),
  1958                  wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y + 10),
  2038                  wx.Point(self.Pos.x + self.Size[0] - 10, self.Pos.y + 10),
  1959                  wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10)]
  2039                  wx.Point(self.Pos.x + self.Size[0], self.Pos.y + 10)]
  1960         dc.DrawLines(lines)
  2040         dc.DrawLines(lines)