Changeset 1840
- Timestamp:
- 08/06/07 05:01:06 (1 year ago)
- Files:
-
- gaphas/trunk/gaphas/item.py (modified) (4 diffs)
- gaphas/trunk/gaphas/tests/test_view.py (modified) (2 diffs)
- gaphas/trunk/gaphas/view.py (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
gaphas/trunk/gaphas/item.py
r1839 r1840 119 119 Attributes: 120 120 121 :matrix: item's transformation matrix122 :canvas: canvas, which owns an item121 - matrix: item's transformation matrix 122 - canvas: canvas, which owns an item 123 123 124 124 Private: 125 125 126 :_canvas: canvas, which owns an item127 :_handles: list of handles owned by an item128 :_constraints: item's constraints129 :_matrix_i2c: item to canvas coordinates matrix130 :_matrix_c2i: canvas to item coordinates matrix131 :_matrix_i2v: item to view coordinates matrices132 :_matrix_v2i: view to item coordinates matrices133 :_sort_key: used to sort items126 - _canvas: canvas, which owns an item 127 - _handles: list of handles owned by an item 128 - _constraints: item's constraints 129 - _matrix_i2c: item to canvas coordinates matrix 130 - _matrix_c2i: canvas to item coordinates matrix 131 - _matrix_i2v: item to view coordinates matrices 132 - _matrix_v2i: view to item coordinates matrices 133 - _sort_key: used to sort items 134 134 """ 135 135 … … 144 144 self._matrix_2ci = None 145 145 146 # used by gaphas.view. View to hold item 2 view matrices (view=key)146 # used by gaphas.view.GtkView to hold item 2 view matrices (view=key) 147 147 self._matrix_i2v = WeakKeyDictionary() 148 148 self._matrix_v2i = WeakKeyDictionary() … … 217 217 """ 218 218 Perform any changes before item update here, for example: 219 * change matrix 220 * move handles 219 220 - change matrix 221 - move handles 221 222 222 223 Gaphas does not guarantee that any canvas invariant is valid at … … 274 275 class Element(Item): 275 276 """ 276 An Element has 4 handles (for a start): 277 An Element has 4 handles (for a start):: 278 277 279 NW +---+ NE 280 | | 278 281 SW +---+ SE 279 282 """ gaphas/trunk/gaphas/tests/test_view.py
r1838 r1840 120 120 assert len(canvas._registered_views) == 0 121 121 122 box = Box() 123 canvas.add(box) 124 125 # By default no complex updating/calculations are done: 126 assert not box._matrix_i2v.has_key(view) 127 assert not box._matrix_v2i.has_key(view) 128 122 129 # GTK view does register for updates though 123 130 … … 125 132 assert len(canvas._registered_views) == 1 126 133 134 # No entry, since GtkView is not realized and has no window 135 assert not box._matrix_i2v.has_key(view) 136 assert not box._matrix_v2i.has_key(view) 137 138 window = gtk.Window(gtk.WINDOW_TOPLEVEL) 139 window.add(view) 140 window.show_all() 141 142 # Now everything is realized and updated 143 assert box._matrix_i2v.has_key(view) 144 assert box._matrix_v2i.has_key(view) 145 127 146 view.canvas = None 128 147 assert len(canvas._registered_views) == 0 129 148 149 assert not box._matrix_i2v.has_key(view) 150 assert not box._matrix_v2i.has_key(view) 151 130 152 view.canvas = canvas 131 153 assert len(canvas._registered_views) == 1 154 155 assert box._matrix_i2v.has_key(view) 156 assert box._matrix_v2i.has_key(view) 157 132 158 133 159 def test_view_registration_2(self): gaphas/trunk/gaphas/view.py
r1839 r1840 41 41 self._dropzone_item = None 42 42 43 self._dirty_items = set()44 self._dirty_matrix_items = set()45 46 43 self._qtree = Quadtree() 47 44 … … 263 260 updates can occur. 264 261 """ 265 # Converting from item to canvas coordinates doesn't work properly,266 # since items should take into account their child objects when267 # bounding boxes are calculated. Now, the child objects should not268 # be hindered by their own matrix settings.269 262 v2i = self.get_matrix_v2i(item).transform_point 270 263 ix0, iy0 = v2i(bounds.x, bounds.y) … … 346 339 item._matrix_v2i[self] = v2i 347 340 341 def _clear_matrices(self): 342 """ 343 Clear registered data in Item's _matrix{i2c|v2i} attributes. 344 """ 345 for item in self.canvas.get_all_items(): 346 try: 347 del item._matrix_i2v[self] 348 del item._matrix_v2i[self] 349 except KeyError: 350 pass 348 351 349 352 … … 362 365 363 366 class GtkView(gtk.DrawingArea, View): 364 # NOTE: In gerit from GTK+ class first, otherwise BusErrors may occur!367 # NOTE: Inherit from GTK+ class first, otherwise BusErrors may occur! 365 368 """ 366 369 GTK+ widget for rendering a canvas.Canvas to a screen. 367 The view uses Tools from tool.py to handle events and Painters 368 from painter.py to draw. Both are configurable. 370 The view uses Tools from `tool.py` to handle events and Painters 371 from `painter.py` to draw. Both are configurable. 372 373 The widget already contains adjustment objects (`hadjustment`, 374 `vadjustment`) to be used for scrollbars. 375 376 This view registers itself on the canvas, so it will receive update events. 369 377 """ 370 378 371 # just defined a name to make GTK register this entity.379 # Just defined a name to make GTK register this class. 372 380 __gtype_name__ = 'GaphasView' 373 381 374 # Signals: emited beforethe change takes effect.382 # Signals: emited after the change takes effect. 375 383 __gsignals__ = { 376 384 'dropzone-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, … … 391 399 def __init__(self, canvas=None): 392 400 gtk.DrawingArea.__init__(self) 401 402 self._dirty_items = set() 403 self._dirty_matrix_items = set() 404 393 405 View.__init__(self, canvas) 406 394 407 self.set_flags(gtk.CAN_FOCUS) 395 408 self.add_events(gtk.gdk.BUTTON_PRESS_MASK … … 425 438 """ 426 439 if self._canvas: 440 self._clear_matrices() 427 441 self._canvas.unregister_view(self) 428 442 … … 456 470 def zoom(self, factor): 457 471 """ 458 Zoom in/out by factor @factor.472 Zoom in/out by factor ``factor``. 459 473 """ 460 474 super(GtkView, self).zoom(factor) 461 a = self.allocation 462 super(GtkView, self).queue_draw_area(0, 0, a.width, a.height) 475 self.queue_draw_refresh() 463 476 464 477 … … 509 522 def queue_draw_item(self, *items): 510 523 """ 511 Like DrawingArea.queue_draw_area, but use the bounds of the524 Like ``DrawingArea.queue_draw_area``, but use the bounds of the 512 525 item as update areas. Of course with a pythonic flavor: update 513 526 any number of items at once. … … 529 542 530 543 544 def queue_draw_refresh(self): 545 """ 546 Redraw the entire view. 547 """ 548 a = self.allocation 549 super(GtkView, self).queue_draw_area(0, 0, a.width, a.height) 550 531 551 def request_update(self, items, matrix_only_items=(), removed_items=()): 532 552 """ 533 553 Request update for items. Items will get a full update treatment, while 534 matrix_only_itemswill only have their bounding box recalculated.554 ``matrix_only_items`` will only have their bounding box recalculated. 535 555 """ 536 556 if items: … … 578 598 continue 579 599 580 bounds = self._qtree.get_data(i)600 # Mark old bb section for update 581 601 self.queue_draw_item(i) 582 602 … … 586 606 # Only matrix has changed, so calculate new bb based 587 607 # on quadtree data (= bb in item coordinates). 608 bounds = self._qtree.get_data(i) 588 609 i2v = self.get_matrix_i2v(i).transform_point 589 610 x0, y0 = i2v(bounds.x, bounds.y) … … 593 614 self.update_adjustments() 594 615 595 # Request bb recalculation for all 'really' dirty items596 616 self.queue_draw_item(i) 597 617 618 # Request bb recalculation for all 'really' dirty items 598 619 self.update_bounding_box(set(dirty_items)) 599 620 finally: … … 605 626 def update_bounding_box(self, items): 606 627 """ 607 Update bounding box is not necessary 628 Update bounding box is not necessary. 608 629 """ 609 630 cr = self.window.cairo_create() … … 623 644 def do_size_allocate(self, allocation): 624 645 """ 625 Allocate the widget size (x, y, width, height).646 Allocate the widget size ``(x, y, width, height)``. 626 647 """ 627 648 gtk.DrawingArea.do_size_allocate(self, allocation) … … 641 662 # Although Item._matrix_{i2v|v2i} keys are automatically removed 642 663 # (weak refs), better do it explicitly to be sure. 643 for item in self.canvas.get_all_items(): 644 try: 645 del item._matrix_i2v[self] 646 del item._matrix_v2i[self] 647 except KeyError: 648 pass 664 self._clear_matrices() 649 665 self.canvas = None 650 666 self._qtree = None … … 657 673 def do_expose_event(self, event): 658 674 """ 659 Render some textto the screen.675 Render canvas to the screen. 660 676 """ 661 677 if not self._canvas: … … 686 702 cr.restore() 687 703 688 # TODO: draw Quadtree structure704 # Draw Quadtree structure 689 705 if DEBUG_DRAW_QUADTREE: 690 706 def draw_qtree_bucket(bucket): … … 702 718 def do_event(self, event): 703 719 """ 704 Handle GDK events. Events are delegated to a Tool.720 Handle GDK events. Events are delegated to a `tool.Tool`. 705 721 """ 706 722 handler = EVENT_HANDLERS.get(event.type) … … 724 740 self.request_update((), self._canvas.get_all_items()) 725 741 726 a = self.allocation 727 super(GtkView, self).queue_draw_area(0, 0, a.width, a.height) 742 self.queue_draw_refresh() 728 743 729 744
