Changeset 1159

Show
Ignore:
Timestamp:
03/14/07 00:29:35 (2 years ago)
Author:
arjanmol
Message:

Added matrix.py (observed Matrix) and start of undo testing document, removed Matrix import from geometry.py

Files:

Legend:

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

    r1149 r1159  
    1818from gaphas.tool import PlacementTool, HandleTool 
    1919from gaphas.painter import ItemPainter 
    20  
    2120from gaphas import state 
     21 
     22# Global undo list 
     23undo_list = [] 
     24 
     25def undo_handler(event): 
     26    global undo_list 
     27    undo_list.append(event) 
    2228 
    2329 
     
    125131            canvas.remove(view.focused_item) 
    126132            print 'items:', canvas.get_all_items() 
     133 
     134    b.connect('clicked', on_clicked) 
     135    v.add(b) 
     136 
     137    v.add(gtk.Label('State:')) 
     138    b = gtk.ToggleButton('Record') 
     139 
     140    def on_toggled(button): 
     141        global undo_list 
     142        if button.get_active(): 
     143            print 'start recording' 
     144            del undo_list[:] 
     145            state.subscribers.add(undo_handler) 
     146        else: 
     147            print 'stop recording' 
     148            state.subscribers.remove(undo_handler) 
     149 
     150    b.connect('toggled', on_toggled) 
     151    v.add(b) 
     152 
     153    b = gtk.Button('Play back') 
     154     
     155    def on_clicked(self): 
     156        global undo_list 
     157        apply_me = list(undo_list) 
     158        del undo_list[:] 
     159        apply_me.reverse() 
     160        saveapply = state.saveapply 
     161        for event in apply_me: 
     162            saveapply(*event) 
    127163 
    128164    b.connect('clicked', on_clicked) 
     
    265301 
    266302# First, activate the revert handler: 
    267 state.subscribers.append(state.revert_handler) 
     303state.observers.add(state.revert_handler) 
    268304 
    269305def print_handler(event): 
    270306    print 'event:', event 
    271307 
    272 state.observers.append(print_handler) 
     308state.subscribers.add(print_handler) 
    273309 
    274310 
  • gaphas/trunk/gaphas/canvas.py

    r1152 r1159  
    1212 
    1313import cairo 
     14from cairo import Matrix 
    1415from gaphas import tree 
    1516from gaphas import solver 
    16 from gaphas.geometry import Matrix 
    1717from gaphas.decorators import async, PRIORITY_HIGH_IDLE 
    1818from state import observed, reversible_pair 
  • gaphas/trunk/gaphas/geometry.py

    r1092 r1159  
    22Geometry functions. 
    33 
    4 Matrix is imported from cairo. 
    5  
    64Rectangle is a utility class for working with rectangles (unions and 
    75intersections) 
     
    1513 
    1614from math import sqrt 
    17 # This saves me a lot of coding: 
    18 from cairo import Matrix 
    1915 
    2016 
  • gaphas/trunk/gaphas/item.py

    r1158 r1159  
    88from math import atan2 
    99 
    10 from geometry import Matrix, distance_line_point, distance_rectangle_point 
     10from matrix import Matrix 
     11from geometry import distance_line_point, distance_rectangle_point 
    1112from solver import solvable, WEAK, NORMAL, STRONG 
    1213from constraint import EqualsConstraint, LessThanConstraint 
  • gaphas/trunk/gaphas/state.py

    r1157 r1159  
    132132 
    133133_reverse = dict() 
     134 
     135 
     136def reversible_function(func, reverse, bind={}): 
     137    """ 
     138    Straight forward reversible method, if func is invoked, reverse 
     139    is dispatched with bind as arguments. 
     140    """ 
     141    global _reverse 
     142    func = getfunction(func) 
     143    _reverse[func] = (reverse, inspect.getargspec(reverse), bind) 
     144 
     145 
     146reversible_method = reversible_function 
     147 
    134148 
    135149def reversible_pair(func1, func2, bind1={}, bind2={}): 
     
    236250 
    237251    """ 
    238     #print 'in handler!', event 
    239252    global _reverse 
    240253    func, args, kwargs = event 
     
    242255    reverse, revspec, bind = _reverse.get(func, (None, None, {})) 
    243256    if not reverse: 
    244         #print 'no reverse' 
    245257        return 
    246258 
     
    271283    for arg in argnames: 
    272284        kwargs[arg] = kw.get(arg) 
    273     #args = map(dict.get, [kw]*len(argnames), argnames) 
    274     #print 'args:', func, args 
    275285    return func(**kwargs) 
    276286 
  • gaphas/trunk/state.txt

    r1157 r1159  
    1919    >>> from gaphas.tree import Tree 
    2020    >>> tree = Tree() 
     21 
     22For this demonistration let's use the Tree class (which contains an add/remove 
     23method pair). It's methods are not dispatched by default though (because they're 
     24only used in the Canvas class. So first dispatching should be enabled: 
     25 
     26    >>> state.enable_dispatching(Tree.add) 
     27    >>> state.enable_dispatching(Tree.remove) 
     28 
     29It works: 
     30 
    2131    >>> def handler(event): 
    2232    ...     print 'event handled', event 
     
    181191     add() and remove() 
    182192 
     193 matrix.py: 
     194   Matrix: 
     195    invert, translate, rotate and scale 
     196 
     197Testcases are described in test_state.txt. 
     198