Changeset 1840

Show
Ignore:
Timestamp:
08/06/07 05:01:06 (1 year ago)
Author:
arj..@yirdis.nl
Message:
  • some docs updated
  • Be more picky with releasing resources from the view.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphas/trunk/gaphas/item.py

    r1839 r1840  
    119119    Attributes: 
    120120 
    121     :matrix: item's transformation matrix 
    122     :canvas: canvas, which owns an item 
     121    - matrix: item's transformation matrix 
     122    - canvas: canvas, which owns an item 
    123123     
    124124    Private: 
    125125 
    126     :_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 
     126    - _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 
    134134    """ 
    135135 
     
    144144        self._matrix_2ci = None 
    145145 
    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) 
    147147        self._matrix_i2v = WeakKeyDictionary() 
    148148        self._matrix_v2i = WeakKeyDictionary() 
     
    217217        """ 
    218218        Perform any changes before item update here, for example: 
    219         * change matrix 
    220         * move handles 
     219 
     220        - change matrix 
     221        - move handles 
    221222 
    222223        Gaphas does not guarantee that any canvas invariant is valid at 
     
    274275class Element(Item): 
    275276    """ 
    276     An Element has 4 handles (for a start): 
     277    An Element has 4 handles (for a start):: 
     278 
    277279     NW +---+ NE 
     280        |   | 
    278281     SW +---+ SE 
    279282    """ 
  • gaphas/trunk/gaphas/tests/test_view.py

    r1838 r1840  
    120120        assert len(canvas._registered_views) == 0 
    121121         
     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 
    122129        # GTK view does register for updates though 
    123130 
     
    125132        assert len(canvas._registered_views) == 1 
    126133         
     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 
    127146        view.canvas = None 
    128147        assert len(canvas._registered_views) == 0 
    129148 
     149        assert not box._matrix_i2v.has_key(view) 
     150        assert not box._matrix_v2i.has_key(view) 
     151 
    130152        view.canvas = canvas 
    131153        assert len(canvas._registered_views) == 1 
     154 
     155        assert box._matrix_i2v.has_key(view) 
     156        assert box._matrix_v2i.has_key(view) 
     157 
    132158 
    133159    def test_view_registration_2(self): 
  • gaphas/trunk/gaphas/view.py

    r1839 r1840  
    4141        self._dropzone_item = None 
    4242 
    43         self._dirty_items = set() 
    44         self._dirty_matrix_items = set() 
    45  
    4643        self._qtree = Quadtree() 
    4744 
     
    263260        updates can occur. 
    264261        """ 
    265         # Converting from item to canvas coordinates doesn't work properly, 
    266         # since items should take into account their child objects when 
    267         # bounding boxes are calculated. Now, the child objects should not 
    268         # be hindered by their own matrix settings. 
    269262        v2i = self.get_matrix_v2i(item).transform_point 
    270263        ix0, iy0 = v2i(bounds.x, bounds.y) 
     
    346339        item._matrix_v2i[self] = v2i 
    347340 
     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 
    348351 
    349352 
     
    362365 
    363366class GtkView(gtk.DrawingArea, View): 
    364     # NOTE: Ingerit from GTK+ class first, otherwise BusErrors may occur! 
     367    # NOTE: Inherit from GTK+ class first, otherwise BusErrors may occur! 
    365368    """ 
    366369    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. 
    369377    """ 
    370378 
    371     # just defined a name to make GTK register this entity
     379    # Just defined a name to make GTK register this class
    372380    __gtype_name__ = 'GaphasView' 
    373381     
    374     # Signals: emited before the change takes effect. 
     382    # Signals: emited after the change takes effect. 
    375383    __gsignals__ = { 
    376384        'dropzone-changed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, 
     
    391399    def __init__(self, canvas=None): 
    392400        gtk.DrawingArea.__init__(self) 
     401 
     402        self._dirty_items = set() 
     403        self._dirty_matrix_items = set() 
     404 
    393405        View.__init__(self, canvas) 
     406 
    394407        self.set_flags(gtk.CAN_FOCUS) 
    395408        self.add_events(gtk.gdk.BUTTON_PRESS_MASK 
     
    425438        """ 
    426439        if self._canvas: 
     440            self._clear_matrices() 
    427441            self._canvas.unregister_view(self) 
    428442 
     
    456470    def zoom(self, factor): 
    457471        """ 
    458         Zoom in/out by factor @factor
     472        Zoom in/out by factor ``factor``
    459473        """ 
    460474        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() 
    463476 
    464477 
     
    509522    def queue_draw_item(self, *items): 
    510523        """ 
    511         Like DrawingArea.queue_draw_area, but use the bounds of the 
     524        Like ``DrawingArea.queue_draw_area``, but use the bounds of the 
    512525        item as update areas. Of course with a pythonic flavor: update 
    513526        any number of items at once. 
     
    529542 
    530543 
     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 
    531551    def request_update(self, items, matrix_only_items=(), removed_items=()): 
    532552        """ 
    533553        Request update for items. Items will get a full update treatment, while 
    534         matrix_only_items will only have their bounding box recalculated. 
     554        ``matrix_only_items`` will only have their bounding box recalculated. 
    535555        """ 
    536556        if items: 
     
    578598                    continue 
    579599 
    580                 bounds = self._qtree.get_data(i) 
     600                # Mark old bb section for update 
    581601                self.queue_draw_item(i) 
    582602 
     
    586606                    # Only matrix has changed, so calculate new bb based 
    587607                    # on quadtree data (= bb in item coordinates). 
     608                    bounds = self._qtree.get_data(i) 
    588609                    i2v = self.get_matrix_i2v(i).transform_point 
    589610                    x0, y0 = i2v(bounds.x, bounds.y) 
     
    593614                    self.update_adjustments() 
    594615 
    595                 # Request bb recalculation for all 'really' dirty items 
    596616                self.queue_draw_item(i) 
    597617 
     618            # Request bb recalculation for all 'really' dirty items 
    598619            self.update_bounding_box(set(dirty_items)) 
    599620        finally: 
     
    605626    def update_bounding_box(self, items): 
    606627        """ 
    607         Update bounding box is not necessary 
     628        Update bounding box is not necessary. 
    608629        """ 
    609630        cr = self.window.cairo_create() 
     
    623644    def do_size_allocate(self, allocation): 
    624645        """ 
    625         Allocate the widget size (x, y, width, height)
     646        Allocate the widget size ``(x, y, width, height)``
    626647        """ 
    627648        gtk.DrawingArea.do_size_allocate(self, allocation) 
     
    641662            # Although Item._matrix_{i2v|v2i} keys are automatically removed 
    642663            # (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() 
    649665            self.canvas = None 
    650666        self._qtree = None 
     
    657673    def do_expose_event(self, event): 
    658674        """ 
    659         Render some text to the screen. 
     675        Render canvas to the screen. 
    660676        """ 
    661677        if not self._canvas: 
     
    686702            cr.restore() 
    687703 
    688         # TODO: draw Quadtree structure 
     704        # Draw Quadtree structure 
    689705        if DEBUG_DRAW_QUADTREE: 
    690706            def draw_qtree_bucket(bucket): 
     
    702718    def do_event(self, event): 
    703719        """ 
    704         Handle GDK events. Events are delegated to a Tool
     720        Handle GDK events. Events are delegated to a `tool.Tool`
    705721        """ 
    706722        handler = EVENT_HANDLERS.get(event.type) 
     
    724740        self.request_update((), self._canvas.get_all_items()) 
    725741 
    726         a = self.allocation 
    727         super(GtkView, self).queue_draw_area(0, 0, a.width, a.height) 
     742        self.queue_draw_refresh() 
    728743 
    729744