Changeset 1002

Show
Ignore:
Timestamp:
09/08/06 03:14:48 (2 years ago)
Author:
arjanmol
Message:

added arrow heads to Line

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphas/trunk/ChangeLog

    r1001 r1002  
     12006-09-08  Arjan Molenaar  <arjan_at_yirdis_dot_nl> 
     2 
     3        * item.py: Implemented arrow heads on Line elements. 
     4 
    152006-09-07  Arjan Molenaar  <arjan_at_yirdis_dot_nl> 
    26 
  • gaphas/trunk/demo.py

    r1001 r1002  
    2525    """Line with experimental connection protocol. 
    2626    """ 
    27     pass 
     27     
     28    def draw_head(self, context): 
     29        cr = context.cairo 
     30        cr.move_to(0, 0) 
     31        cr.line_to(10, 10) 
     32        cr.stroke() 
     33        # Start point for the line to the next handle 
     34        cr.move_to(0, 0) 
     35 
     36    def draw_tail(self, context): 
     37        cr = context.cairo 
     38        cr.line_to(0, 0) 
     39        cr.line_to(10, 10) 
     40        cr.stroke() 
     41 
    2842 
    2943 
  • gaphas/trunk/gaphas/examples.py

    r1001 r1002  
    9393                    # Transform distance to world coordinates 
    9494                    distance, dumy = matrix_i2w(i).transform_distance(distance, 0) 
    95                     if distance < glue_distance: 
     95                    if distance <= glue_distance: 
    9696                        glue_distance = distance 
    9797                        glue_point = matrix_i2w(i).transform_point(*point) 
  • gaphas/trunk/gaphas/item.py

    r999 r1002  
    55__version__ = "$Revision$" 
    66# $HeadURL$ 
     7 
     8from math import atan2 
    79 
    810from geometry import Matrix, distance_line_point, distance_rectangle_point 
     
    323325        h = self._handles 
    324326        hnw, hse = h[NW], h[SE] 
    325         #print ((hnw.x, hnw.y, hse.x, hse.y), (x, y)), \ 
    326          #distance_rectangle_point((hnw.x, hnw.y, hse.x, hse.y), (x, y)) 
    327327        return distance_rectangle_point(map(float, (hnw.x, hnw.y, hse.x, hse.y)), (x, y)) 
    328328 
     
    337337         (only straight angles) 
    338338     - line_width: width of the line to be drawn 
     339 
     340    This line also supports arrow heads on both the begin and end of the 
     341    line. These are drawn with the methods draw_head(context) and 
     342    draw_tail(context). The coordinate system is altered so the methods do 
     343    not have to know about the angle of the line segment (e.g. drawing a line 
     344    from (10, 10) via (0, 0) to (10, -10) will draw an arrow point). 
    339345    """ 
    340346 
     
    346352        self.fuzzyness = 0 
    347353        self._orthogonal = [] 
     354        self._head_angle = self._tail_angle = 0 
    348355 
    349356    def _set_orthogonal(self, orthogonal): 
     
    372379                cons.append(add(eq(a=h0.y, b=h1.y))) 
    373380            self.canvas.solver.mark_dirty(h1.x, h1.y) 
    374         # Mark first handle dirty, forcing recalculayion 
    375         print 'updated ortho constraints' 
    376         #self.canvas.solver.mark_dirty(self._handles[0].x, self._handles[0].y) 
    377      
     381        self.request_update() 
     382 
    378383    orthogonal = property(lambda s: s._orthogonal != [], _set_orthogonal) 
    379384 
     
    461466            raise KeyError('Handle is not an end handle') 
    462467 
     468    def update(self, context): 
     469        """ 
     470        """ 
     471        super(Line, self).update(context) 
     472        h0, h1 = self._handles[:2] 
     473        self._head_angle = atan2(h1.y - h0.y, h1.x - h0.x) 
     474        h1, h0 = self._handles[-2:] 
     475        self._tail_angle = atan2(h1.y - h0.y, h1.x - h0.x) 
     476 
    463477    def _closest_segment(self, x, y): 
    464478        """Obtain a tuple (distance, point_on_line, segment). 
     
    495509        return max(0, distance - self.fuzzyness) 
    496510 
    497     def _draw_line(self, context): 
     511    def draw_head(self, context): 
     512        """Default head drawer: move cursor to the first handle. 
     513        """ 
     514        context.cairo.move_to(0, 0) 
     515 
     516    def draw_tail(self, context): 
     517        """Default tail drawer: draw line to the last handle. 
     518        """ 
     519        context.cairo.line_to(0, 0) 
     520 
     521 
     522    def draw(self, context): 
    498523        """Draw the line itself. 
    499         """ 
    500         c = context.cairo 
    501         h = self._handles[0] 
    502         c.set_line_width(self.line_width) 
    503         c.move_to(float(h.x), float(h.y)) 
    504         for h in self._handles[1:]: 
    505             c.line_to(float(h.x), float(h.y)) 
    506         c.stroke() 
    507  
    508     def draw(self, context): 
    509         """See Item.draw(context). 
    510         """ 
    511         self._draw_line(context) 
     524        See Item.draw(context). 
     525        """ 
     526        def draw_line_end(handle, angle, draw): 
     527            cr = context.cairo 
     528            cr.save() 
     529            try: 
     530                cr.translate(handle.x, handle.y) 
     531                cr.rotate(angle) 
     532                draw(context) 
     533            finally: 
     534                cr.restore() 
     535        cr = context.cairo 
     536        cr.set_line_width(self.line_width) 
     537        draw_line_end(self._handles[0], self._head_angle, self.draw_head) 
     538        for h in self._handles[1:-1]: 
     539            cr.line_to(float(h.x), float(h.y)) 
     540        h0, h1 = self._handles[-2:] 
     541        draw_line_end(self._handles[-1], self._tail_angle, self.draw_tail) 
     542        cr.stroke() 
    512543 
    513544