Changeset 1552

Show
Ignore:
Timestamp:
07/02/07 11:44:46 (1 year ago)
Author:
wrobe..@pld-linux.org
Message:

- added another custom item example - a circle

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphas/branches/hw/demo.py

    r1551 r1552  
    2525import cairo 
    2626from gaphas import Canvas, GtkView, View 
    27 from gaphas.examples import Box, Text, FatLine, DefaultExampleTool 
     27from gaphas.examples import Box, Text, FatLine, Circle, DefaultExampleTool 
    2828from gaphas.item import Line, NW, SE 
    2929from gaphas.tool import PlacementTool, HandleTool 
     
    310310    h1.y = 50 
    311311    fl.matrix.translate(50, 50) 
     312    fl.height = 50 
    312313    c.add(fl) 
    313     fl.height = 50 
    314     fl.request_update() 
     314 
     315 
     316    circle = Circle() 
     317    h1, h2 = circle.handles() 
     318    h1.x = 50 
     319    h1.y = 150 
     320    h2.x = 50 
     321    h2.y = 80 
     322    circle.radius = 10 
     323    circle.matrix.translate(50, 150) 
     324    c.add(circle) 
    315325 
    316326#   bb=Box() 
  • gaphas/branches/hw/gaphas/examples.py

    r1551 r1552  
    1313from constraint import BalanceConstraint, LessThanConstraint, EqualsConstraint 
    1414from geometry import point_on_rectangle, distance_rectangle_point 
    15 from util import text_extents, text_align, text_multiline 
     15from util import text_extents, text_align, text_multiline, path_ellipse 
    1616 
    1717class Box(Element): 
     
    117117        cr.move_to(0, 0) 
    118118        cr.line_to(0, self.height) 
     119        cr.stroke() 
     120 
     121 
     122 
     123class Circle(Item): 
     124    def __init__(self): 
     125        super(Circle, self).__init__() 
     126        self._handles.extend((Handle(), Handle())) 
     127 
     128 
     129    def _set_radius(self, height): 
     130        h1, h2 = self._handles 
     131        return h1.y + height 
     132 
     133 
     134    def _get_radius(self): 
     135        h1, h2 = self._handles 
     136        return ((h2.x - h1.x) ** 2 + (h2.y - h1.y) ** 2) ** 0.5 
     137 
     138 
     139    radius = property(_get_radius, _set_radius) 
     140 
     141 
     142    def _get_pos(self): 
     143        h1, h2 = self._handles 
     144        x, y = h2.x - h1.x, h2.y - h1.y 
     145        if x > 0: 
     146            x = 0 
     147        if y > 0: 
     148            y = 0 
     149        return abs(x), abs(y) 
     150 
     151 
     152    pos = property(_get_pos) 
     153 
     154 
     155    def setup_canvas(self): 
     156        super(Circle, self).setup_canvas() 
     157        h1, h2 = self._handles 
     158        h1.movable = False 
     159 
     160 
     161    def draw(self, context): 
     162        cr = context.cairo 
     163        x, y = self.pos 
     164        path_ellipse(cr, x, y, 2 * self.radius, 2 * self.radius) 
    119165        cr.stroke() 
    120166