Changeset 817
- Timestamp:
- 03/27/06 05:28:33 (3 years ago)
- Files:
-
- trunk/gaphas/examples.py (modified) (1 diff)
- trunk/gaphas/item.py (modified) (1 diff)
- trunk/gaphas/view.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/gaphas/examples.py
r815 r817 7 7 class Box(Item): 8 8 def __init__(self): 9 print 'Box.__init__'10 9 super(Box, self).__init__() 11 10 self._width = 10 12 11 self._height = 10 12 self._handles = [Handle(0, 0)] 13 14 def handles(self): 15 return iter(self._handles) 16 17 def update(self, context): 18 self._handles[0].x = self._width 19 self._handles[0].y = self._height 13 20 14 21 def draw(self, context): 15 print 'Box.draw' 22 print 'Box.draw', self 16 23 c = context.cairo 17 24 c.rectangle(0,0, self._width, self._height) 18 c.set_source_rgb(0,0, 80)25 c.set_source_rgb(0,0,0.8) 19 26 c.stroke() 20 27 context.draw_children() trunk/gaphas/item.py
r815 r817 11 11 x = solvable() 12 12 y = solvable() 13 def __init__(self, x, y): 13 14 def __init__(self, x=0, y=0): 14 15 self.x = x 15 16 self.y = y trunk/gaphas/view.py
r815 r817 2 2 This module contains everything to display a Canvas on a screen. 3 3 """ 4 5 if __name__ == '__main__': 6 import pygtk 7 pygtk.require('2.0') 4 8 5 9 import gtk … … 17 21 """ 18 22 self.view._draw_items(self.children, self.cairo) 23 24 25 class CairoContextWrapper(object): 26 """Delegate all calls to the wrapped CairoContext, intercept 27 stroke(), fill() and a few others so the bounding box of the 28 item involved can be calculated. 29 """ 30 31 def __init__(self, cairo_context): 32 self._cairo_context = cairo_context 33 self._bounds = None 34 35 def __getattr__(self, key): 36 return getattr(self._cairo_context, key) 37 38 def _update_bounds(self, bounds): 39 #print 'bounds', bounds 40 if not self._bounds: 41 self._bounds = bounds 42 else: 43 b = self._bounds 44 self._bounds = (min(b[0], bounds[0]), min(b[1], bounds[1]), 45 max(b[2], bounds[2]), max(b[3], bounds[3])) 46 47 def fill(self): 48 ctx = self._cairo_context 49 ctx.save() 50 ctx.identity_matrix() 51 self._update_bounds(ctx.fill_extents()) 52 ctx.restore() 53 return ctx.fill() 54 55 def fill_preserve(self): 56 ctx = self._cairo_context 57 ctx.save() 58 ctx.identity_matrix() 59 self._update_bounds(ctx.fill_extents()) 60 ctx.restore() 61 return ctx.fill_preserve() 62 63 def stroke(self): 64 ctx = self._cairo_context 65 ctx.save() 66 ctx.identity_matrix() 67 self._update_bounds(ctx.stroke_extents()) 68 ctx.restore() 69 return ctx.stroke() 70 71 def stroke_preserve(self): 72 ctx = self._cairo_context 73 ctx.save() 74 ctx.identity_matrix() 75 self._update_bounds(ctx.stroke_extents()) 76 ctx.restore() 77 return ctx.stroke_preserve() 78 79 def text_show(self, utf8): 80 e = self._cairo_context.text_extents(utf8) 81 # Do something with it 82 return ctx.show_text(utf8) 83 19 84 20 85 class View(gtk.DrawingArea): … … 30 95 | gtk.gdk.KEY_PRESS_MASK 31 96 | gtk.gdk.KEY_RELEASE_MASK) 32 self.width = 033 self.height = 034 97 self._canvas = canvas 35 98 self._cairo_context = None 36 99 self._tool = None 100 self._calculate_bounding_box = False 101 102 # Handy debug flag for drawing bounding boxes around the items. 103 self._debug_draw_bounding_box = True 37 104 38 105 def _set_canvas(self, canvas): … … 54 121 cairo_context.set_matrix(item._matrix_w2i) 55 122 #cairo_context.transform(Matrix(*item.matrix)) 123 if self._calculate_bounding_box: 124 wrapper = CairoContextWrapper(cairo_context) 125 else: 126 # No wrapper: 127 wrapper = cairo_context 128 56 129 item.draw(DrawContext(view=self, 57 cairo= cairo_context,130 cairo=wrapper, 58 131 children=self._canvas.get_children(item))) 132 133 if self._calculate_bounding_box: 134 item._view_bounds = wrapper._bounds 135 #print item, wrapper._bounds 136 137 if self._debug_draw_bounding_box: 138 ctx = cairo_context 139 ctx.save() 140 ctx.identity_matrix() 141 ctx.set_source_rgb(.8, 0, 0) 142 ctx.set_line_width(1.0) 143 b = item._view_bounds 144 ctx.rectangle(b[0], b[1], b[2] - b[0], b[3] - b[1]) 145 ctx.stroke() 146 ctx.restore() 147 59 148 self._draw_handles(item, cairo_context) 60 149 finally: … … 62 151 63 152 def _draw_handles(self, item, cairo_context): 64 size = 765 cairo_context.save()66 cairo_context.rectangle(0, 0, size, size)67 cairo_context.set_source_rgba(0, 1, 0, .6)68 cairo_context.fill_preserve()69 cairo_context.move_to(2, 2)70 cairo_context.line_to(5, 5)71 cairo_context.move_to(5, 2)72 cairo_context.line_to(2, 5)73 cairo_context.set_source_rgba(0, 0, 0, 0.6)74 cairo_context.set_line_width(1.0)75 cairo_context.stroke()76 77 cairo_context.restore()153 for handle in item.handles(): 154 cairo_context.save() 155 cairo_context.translate(handle.x - 4, handle.y - 4) 156 cairo_context.rectangle(0, 0, 9, 9) 157 cairo_context.set_source_rgba(0, 1, 0, .6) 158 cairo_context.fill_preserve() 159 cairo_context.move_to(2, 2) 160 cairo_context.line_to(7, 7) 161 cairo_context.move_to(7, 2) 162 cairo_context.line_to(2, 7) 163 cairo_context.set_source_rgba(0, .2, 0, 0.9) 164 cairo_context.set_line_width(1.1) 165 cairo_context.stroke() 166 cairo_context.restore() 78 167 79 168 def do_expose_event(self, event): 80 169 """Render some text to the screen. 81 170 """ 171 82 172 # Set this to some idle function 83 173 if self._canvas.require_update(): 84 174 self._canvas.update_now() 175 self._calculate_bounding_box = True 85 176 86 177 viewport = self.get_allocation() … … 97 188 # TODO: add move/zoom matrix 98 189 self._draw_items(self._canvas.get_root_items(), context) 190 191 self._calculate_bounding_box = False 192 99 193 return False 100 194 … … 108 202 109 203 def do_motion_notify_event(self, event): 110 print 'do motion notify', event204 #print 'do motion notify', event 111 205 return False 112 206 … … 137 231 print 'view', v 138 232 b=Box() 233 print 'box', b 139 234 b.matrix=(1.0, 0.0, 0.0, 1, 20,20) 140 235 b._width=b._height = 40 141 236 c.add(b) 142 237 bb=Box() 238 print 'box', bb 143 239 bb.matrix=(1.0, 0.0, 0.0, 1, 10,10) 144 240 c.add(bb, parent=b) 145 241 bb=Box() 242 print 'box', bb 146 243 bb.matrix.rotate(math.pi/4.) 147 244 c.add(bb, parent=b)
