graphics/GraphicCommons.py
changeset 1736 7e61baa047f0
parent 1730 64d8f52bc8c8
child 1739 ec153828ded2
equal deleted inserted replaced
1735:c02818d7e29f 1736:7e61baa047f0
   113     (3, 2) : 4,
   113     (3, 2) : 4,
   114     (2, 1) : 5,
   114     (2, 1) : 5,
   115     (2, 3) : 5
   115     (2, 3) : 5
   116 }
   116 }
   117 
   117 
       
   118 
   118 def round_scaling(x, n, constraint=0):
   119 def round_scaling(x, n, constraint=0):
   119     fraction = float(x) / float(n)
   120     fraction = float(x) / float(n)
   120     if constraint == -1:
   121     if constraint == -1:
   121         xround = int(fraction)
   122         xround = int(fraction)
   122     else:
   123     else:
   127 
   128 
   128 """
   129 """
   129 Basic vector operations for calculate wire points
   130 Basic vector operations for calculate wire points
   130 """
   131 """
   131 
   132 
   132 # Create a vector from two points and define if vector must be normal
   133 
   133 def vector(p1, p2, normal = True):
   134 def vector(p1, p2, normal = True):
       
   135     """
       
   136     Create a vector from two points and define if vector must be normal
       
   137     """
   134     vector = (p2.x - p1.x, p2.y - p1.y)
   138     vector = (p2.x - p1.x, p2.y - p1.y)
   135     if normal:
   139     if normal:
   136         return normalize(vector)
   140         return normalize(vector)
   137     return vector
   141     return vector
   138 
   142 
   139 # Calculate the norm of a given vector
   143 
   140 def norm(v):
   144 def norm(v):
       
   145     """
       
   146     Calculate the norm of a given vector
       
   147     """
   141     return sqrt(v[0] * v[0] + v[1] * v[1])
   148     return sqrt(v[0] * v[0] + v[1] * v[1])
   142 
   149 
   143 # Normalize a given vector
   150 
   144 def normalize(v):
   151 def normalize(v):
       
   152     """
       
   153     Normalize a given vector
       
   154     """
   145     v_norm = norm(v)
   155     v_norm = norm(v)
   146     # Verifie if it is not a null vector
   156     # Verifie if it is not a null vector
   147     if v_norm > 0:
   157     if v_norm > 0:
   148         return (v[0] / v_norm, v[1] / v_norm)
   158         return (v[0] / v_norm, v[1] / v_norm)
   149     else:
   159     else:
   150         return v
   160         return v
   151 
   161 
   152 # Calculate the scalar product of two vectors
   162 
   153 def is_null_vector(v):
   163 def is_null_vector(v):
       
   164     """
       
   165     Calculate the scalar product of two vectors
       
   166     """
   154     return v == (0, 0)
   167     return v == (0, 0)
   155 
   168 
   156 # Calculate the scalar product of two vectors
   169 
   157 def add_vectors(v1, v2):
   170 def add_vectors(v1, v2):
       
   171     """
       
   172     Calculate the scalar product of two vectors
       
   173     """
   158     return (v1[0] + v2[0], v1[1] + v2[1])
   174     return (v1[0] + v2[0], v1[1] + v2[1])
   159 
   175 
   160 # Calculate the scalar product of two vectors
   176 
   161 def product(v1, v2):
   177 def product(v1, v2):
       
   178     """
       
   179     Calculate the scalar product of two vectors
       
   180     """
   162     return v1[0] * v2[0] + v1[1] * v2[1]
   181     return v1[0] * v2[0] + v1[1] * v2[1]
   163 
   182 
   164 
   183 
   165 """
       
   166 Function that calculates the nearest point of the grid defined by scaling for the given point
       
   167 """
       
   168 
       
   169 def GetScaledEventPosition(event, dc, scaling):
   184 def GetScaledEventPosition(event, dc, scaling):
       
   185     """
       
   186     Function that calculates the nearest point of the grid defined by scaling for the given point
       
   187     """
   170     pos = event.GetLogicalPosition(dc)
   188     pos = event.GetLogicalPosition(dc)
   171     if scaling:
   189     if scaling:
   172         pos.x = round(float(pos.x) / float(scaling[0])) * scaling[0]
   190         pos.x = round(float(pos.x) / float(scaling[0])) * scaling[0]
   173         pos.y = round(float(pos.y) / float(scaling[1])) * scaling[1]
   191         pos.y = round(float(pos.y) / float(scaling[1])) * scaling[1]
   174     return pos
   192     return pos
   175 
   193 
   176 
   194 
   177 """
       
   178 Function that choose a direction during the wire points generation
       
   179 """
       
   180 
   195 
   181 def DirectionChoice(v_base, v_target, dir_target):
   196 def DirectionChoice(v_base, v_target, dir_target):
       
   197     """
       
   198     Function that choose a direction during the wire points generation
       
   199     """
   182     dir_product = product(v_base, v_target)
   200     dir_product = product(v_base, v_target)
   183     if dir_product < 0:
   201     if dir_product < 0:
   184         return (-v_base[0], -v_base[1])
   202         return (-v_base[0], -v_base[1])
   185     elif dir_product == 0 and product(v_base, dir_target) != 0:
   203     elif dir_product == 0 and product(v_base, dir_target) != 0:
   186         return dir_target
   204         return dir_target
   187     return v_base
   205     return v_base
   188 
   206 
       
   207 
   189 def MiterPen(colour, width=1, style=wx.SOLID):
   208 def MiterPen(colour, width=1, style=wx.SOLID):
   190     pen = wx.Pen(colour, width, style)
   209     pen = wx.Pen(colour, width, style)
   191     pen.SetJoin(wx.JOIN_MITER)
   210     pen.SetJoin(wx.JOIN_MITER)
   192     pen.SetCap(wx.CAP_PROJECTING)
   211     pen.SetCap(wx.CAP_PROJECTING)
   193     return pen
   212     return pen
   194 
   213 
   195 #-------------------------------------------------------------------------------
   214 #-------------------------------------------------------------------------------
   196 #                    Helpers for highlighting text
   215 #                    Helpers for highlighting text
   197 #-------------------------------------------------------------------------------
   216 #-------------------------------------------------------------------------------
   198 
   217 
       
   218 
   199 def AddHighlight(highlights, infos):
   219 def AddHighlight(highlights, infos):
   200     RemoveHighlight(highlights, infos)
   220     RemoveHighlight(highlights, infos)
   201     highlights.append(infos)
   221     highlights.append(infos)
       
   222 
   202 
   223 
   203 def RemoveHighlight(highlights, infos):
   224 def RemoveHighlight(highlights, infos):
   204     if infos in highlights:
   225     if infos in highlights:
   205         highlights.remove(infos)
   226         highlights.remove(infos)
   206         return True
   227         return True
   207     return False
   228     return False
   208 
   229 
       
   230 
   209 def ClearHighlight(highlights, highlight_type=None):
   231 def ClearHighlight(highlights, highlight_type=None):
   210     if highlight_type is not None:
   232     if highlight_type is not None:
   211         return [highlight for highlight in highlights if highlight[2] != highlight_type]
   233         return [highlight for highlight in highlights if highlight[2] != highlight_type]
   212     return []
   234     return []
       
   235 
   213 
   236 
   214 def DrawHighlightedText(dc, text, highlights, x, y):
   237 def DrawHighlightedText(dc, text, highlights, x, y):
   215     current_pen = dc.GetPen()
   238     current_pen = dc.GetPen()
   216     dc.SetPen(wx.TRANSPARENT_PEN)
   239     dc.SetPen(wx.TRANSPARENT_PEN)
   217     for start, end, highlight_type in highlights:
   240     for start, end, highlight_type in highlights:
   227 
   250 
   228 #-------------------------------------------------------------------------------
   251 #-------------------------------------------------------------------------------
   229 #                           Graphic element base class
   252 #                           Graphic element base class
   230 #-------------------------------------------------------------------------------
   253 #-------------------------------------------------------------------------------
   231 
   254 
   232 """
       
   233 Class that implements a generic graphic element
       
   234 """
       
   235 
   255 
   236 class Graphic_Element(ToolTipProducer):
   256 class Graphic_Element(ToolTipProducer):
       
   257     """
       
   258     Class that implements a generic graphic element
       
   259     """
   237 
   260 
   238     # Create a new graphic element
   261     # Create a new graphic element
   239     def __init__(self, parent, id = None):
   262     def __init__(self, parent, id = None):
   240         ToolTipProducer.__init__(self, parent)
   263         ToolTipProducer.__init__(self, parent)
   241         self.Parent = parent
   264         self.Parent = parent
   682 
   705 
   683 #-------------------------------------------------------------------------------
   706 #-------------------------------------------------------------------------------
   684 #                           Group of graphic elements
   707 #                           Group of graphic elements
   685 #-------------------------------------------------------------------------------
   708 #-------------------------------------------------------------------------------
   686 
   709 
   687 """
       
   688 Class that implements a group of graphic elements
       
   689 """
       
   690 
   710 
   691 class Graphic_Group(Graphic_Element):
   711 class Graphic_Group(Graphic_Element):
       
   712     """
       
   713     Class that implements a group of graphic elements
       
   714     """
   692 
   715 
   693     # Create a new group of graphic elements
   716     # Create a new group of graphic elements
   694     def __init__(self, parent):
   717     def __init__(self, parent):
   695         Graphic_Element.__init__(self, parent)
   718         Graphic_Element.__init__(self, parent)
   696         self.Elements = []
   719         self.Elements = []
  1000 
  1023 
  1001 #-------------------------------------------------------------------------------
  1024 #-------------------------------------------------------------------------------
  1002 #                         Connector for all types of blocks
  1025 #                         Connector for all types of blocks
  1003 #-------------------------------------------------------------------------------
  1026 #-------------------------------------------------------------------------------
  1004 
  1027 
  1005 """
       
  1006 Class that implements a connector for any type of block
       
  1007 """
       
  1008 
  1028 
  1009 class Connector(DebugDataConsumer, ToolTipProducer):
  1029 class Connector(DebugDataConsumer, ToolTipProducer):
       
  1030     """
       
  1031     Class that implements a connector for any type of block
       
  1032     """
  1010 
  1033 
  1011     # Create a new connector
  1034     # Create a new connector
  1012     def __init__(self, parent, name, type, position, direction, negated = False, edge = "none", onlyone = False):
  1035     def __init__(self, parent, name, type, position, direction, negated = False, edge = "none", onlyone = False):
  1013         DebugDataConsumer.__init__(self)
  1036         DebugDataConsumer.__init__(self)
  1014         ToolTipProducer.__init__(self, parent.Parent)
  1037         ToolTipProducer.__init__(self, parent.Parent)
  1534 
  1557 
  1535 #-------------------------------------------------------------------------------
  1558 #-------------------------------------------------------------------------------
  1536 #                           Common Wire Element
  1559 #                           Common Wire Element
  1537 #-------------------------------------------------------------------------------
  1560 #-------------------------------------------------------------------------------
  1538 
  1561 
  1539 """
       
  1540 Class that implements a wire for connecting two blocks
       
  1541 """
       
  1542 
  1562 
  1543 class Wire(Graphic_Element, DebugDataConsumer):
  1563 class Wire(Graphic_Element, DebugDataConsumer):
       
  1564     """
       
  1565     Class that implements a wire for connecting two blocks
       
  1566     """
  1544 
  1567 
  1545     # Create a new wire
  1568     # Create a new wire
  1546     def __init__(self, parent, start = None, end = None):
  1569     def __init__(self, parent, start = None, end = None):
  1547         Graphic_Element.__init__(self, parent)
  1570         Graphic_Element.__init__(self, parent)
  1548         DebugDataConsumer.__init__(self)
  1571         DebugDataConsumer.__init__(self)
  2714 
  2737 
  2715 #-------------------------------------------------------------------------------
  2738 #-------------------------------------------------------------------------------
  2716 #                           Graphic comment element
  2739 #                           Graphic comment element
  2717 #-------------------------------------------------------------------------------
  2740 #-------------------------------------------------------------------------------
  2718 
  2741 
       
  2742 
  2719 def FilterHighlightsByRow(highlights, row, length):
  2743 def FilterHighlightsByRow(highlights, row, length):
  2720     _highlights = []
  2744     _highlights = []
  2721     for start, end, highlight_type in highlights:
  2745     for start, end, highlight_type in highlights:
  2722         if start[0] <= row and end[0] >= row:
  2746         if start[0] <= row and end[0] >= row:
  2723             if start[0] < row:
  2747             if start[0] < row:
  2725             if end[0] > row:
  2749             if end[0] > row:
  2726                 end = (row, length)
  2750                 end = (row, length)
  2727             _highlights.append((start, end, highlight_type))
  2751             _highlights.append((start, end, highlight_type))
  2728     return _highlights
  2752     return _highlights
  2729 
  2753 
       
  2754 
  2730 def FilterHighlightsByColumn(highlights, start_col, end_col):
  2755 def FilterHighlightsByColumn(highlights, start_col, end_col):
  2731     _highlights = []
  2756     _highlights = []
  2732     for start, end, highlight_type in highlights:
  2757     for start, end, highlight_type in highlights:
  2733         if end[1] > start_col and start[1] < end_col:
  2758         if end[1] > start_col and start[1] < end_col:
  2734             start = (start[0], max(start[1], start_col) - start_col)
  2759             start = (start[0], max(start[1], start_col) - start_col)
  2735             end = (end[0], min(end[1], end_col) - start_col)
  2760             end = (end[0], min(end[1], end_col) - start_col)
  2736             _highlights.append((start, end, highlight_type))
  2761             _highlights.append((start, end, highlight_type))
  2737     return _highlights
  2762     return _highlights
  2738 
  2763 
  2739 """
       
  2740 Class that implements a comment
       
  2741 """
       
  2742 
  2764 
  2743 class Comment(Graphic_Element):
  2765 class Comment(Graphic_Element):
       
  2766     """
       
  2767     Class that implements a comment
       
  2768     """
  2744 
  2769 
  2745     # Create a new comment
  2770     # Create a new comment
  2746     def __init__(self, parent, content, id = None):
  2771     def __init__(self, parent, content, id = None):
  2747         Graphic_Element.__init__(self, parent)
  2772         Graphic_Element.__init__(self, parent)
  2748         self.Id = id
  2773         self.Id = id