Changeset 1177

Show
Ignore:
Timestamp:
03/27/07 23:20:07 (2 years ago)
Author:
arj..@yirdis.nl
Message:

updated undo manager

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphor/trunk/TODO

    r1176 r1177  
    88 
    99 - Undo functionality 
    10     - make canvas events transactional. 
    1110 
    1211 - use easysetup tools, move gaphas to a separate package and use 
  • gaphor/trunk/gaphor/diagram/tool.py

    r1121 r1177  
    1313import gaphas 
    1414from gaphas.geometry import distance_point_point 
    15 from gaphas.tool import Tool, HandleTool 
     15from gaphas.tool import Tool, HandleTool, ToolChain 
    1616 
    1717from gaphor import resource 
    18 from gaphor.undomanager import get_undo_manager 
     18from gaphor.services.undomanager import get_undo_manager, transactional 
    1919 
    2020from interfaces import IEditor, IConnect 
     
    2222__version__ = '$Revision$' 
    2323 
     24 
    2425class ConnectHandleTool(HandleTool): 
    25     """Handle Tool (acts on item handles) that uses the IConnect protocol 
     26    """ 
     27    Handle Tool (acts on item handles) that uses the IConnect protocol 
    2628    to connect items to one-another. 
    2729    """ 
    2830 
    2931    def glue(self, view, item, handle, wx, wy): 
    30         """Find the nearest item that the handle may connect to. 
     32        """ 
     33        Find the nearest item that the handle may connect to. 
    3134 
    3235        This is done by iterating over all items and query for an IConnect 
     
    7174 
    7275    def connect(self, view, item, handle, wx, wy): 
    73         """Find an item near @handle that @item can connect to and connect. 
     76        """ 
     77        Find an item near @handle that @item can connect to and connect. 
    7478         
    7579        This is done by attempting a glue() operation. If there is something 
     
    9296 
    9397    def disconnect(self, view, item, handle): 
    94         """Disconnect the handle from the element by removing constraints. 
     98        """ 
     99        Disconnect the handle from the element by removing constraints. 
    95100        Do not yet release the connection on model level, since the handle 
    96101        may be connected to the same item on some other place. 
     
    102107 
    103108class TextEditTool(Tool): 
    104     """Text edit tool. Allows for elements that can adapt to the  
     109    """ 
     110    Text edit tool. Allows for elements that can adapt to the  
    105111    IEditable interface to be edited. 
    106112    """ 
    107113 
    108114    def create_edit_window(self, view, x, y, text, *args): 
    109         """Create a popup window with some editable text. 
     115        """ 
     116        Create a popup window with some editable text. 
    110117        """ 
    111118        window = gtk.Window() 
     
    135142        #window.focus 
    136143 
     144    @transactional 
    137145    def submit_text(self, widget, buffer, editor): 
    138         """Submit the final text to the edited item. 
     146        """ 
     147        Submit the final text to the edited item. 
    139148        """ 
    140149        text = buffer.get_text(buffer.get_start_iter(), buffer.get_end_iter()) 
     
    172181 
    173182class PlacementTool(gaphas.tool.PlacementTool): 
    174     """PlacementTool is used to place items on the canvas. 
     183    """ 
     184    PlacementTool is used to place items on the canvas. 
    175185    """ 
    176186 
    177187    def __init__(self, item_factory, action_id, handle_index=-1): 
    178         """item_factory is a callable. It is used to create a CanvasItem 
     188        """ 
     189        item_factory is a callable. It is used to create a CanvasItem 
    179190        that is displayed on the diagram. 
    180191        """ 
     
    186197 
    187198    def on_button_press(self, context, event): 
     199        get_undo_manager().begin_transaction() 
    188200        self.is_released = False 
    189         view = context.view #resource('MainWindow').get_current_diagram_view() 
     201        view = context.view 
    190202        view.unselect_all() 
    191         get_undo_manager().begin_transaction() 
    192203        if gaphas.tool.PlacementTool.on_button_press(self, context, event): 
    193204            try: 
     
    205216    def on_button_release(self, context, event): 
    206217        self.is_released = True 
    207         if resource('reset-tool-after-create', False): 
    208             pool = resource('MainWindow').get_action_pool() 
    209             pool.get_action('Pointer').active = True 
    210         get_undo_manager().commit_transaction() 
    211         return gaphas.tool.PlacementTool.on_button_release(self, context, event) 
     218        try: 
     219            if resource('reset-tool-after-create', False): 
     220                pool = resource('MainWindow').get_action_pool() 
     221                pool.get_action('Pointer').active = True 
     222            return gaphas.tool.PlacementTool.on_button_release(self, context, event) 
     223        finally: 
     224            get_undo_manager().commit_transaction() 
     225 
     226 
     227class TransactionalToolChain(ToolChain): 
     228    """ 
     229    In addition to a normal toolchain, this chain begins an undo-transaction 
     230    at button-press and commits the transaction at button-release. 
     231    """ 
     232 
     233    def on_button_press(self, context, event): 
     234        get_undo_manager().begin_transaction() 
     235        return ToolChain.on_button_press(self, context, event) 
     236 
     237    def on_button_release(self, context, event): 
     238        try: 
     239            return ToolChain.on_button_release(self, context, event) 
     240        finally: 
     241            get_undo_manager().commit_transaction() 
     242 
     243    def on_double_click(self, context, event): 
     244        get_undo_manager().begin_transaction() 
     245        try: 
     246            return ToolChain.on_double_click(self, context, event) 
     247        finally: 
     248            get_undo_manager().commit_transaction() 
     249 
     250    def on_triple_click(self, context, event): 
     251        get_undo_manager().begin_transaction() 
     252        try: 
     253            return ToolChain.on_triple_click(self, context, event) 
     254        finally: 
     255            get_undo_manager().commit_transaction() 
    212256 
    213257 
    214258from gaphas.tool import ToolChain, HoverTool, ItemTool, RubberbandTool 
    215259 
     260 
    216261def DefaultTool(): 
    217     """The default tool chain build from HoverTool, ItemTool and HandleTool. 
    218     """ 
    219     chain = ToolChain() 
     262    """ 
     263    The default tool chain build from HoverTool, ItemTool and HandleTool. 
     264    """ 
     265    chain = TransactionalToolChain() 
    220266    chain.append(HoverTool()) 
    221267    chain.append(ConnectHandleTool()) 
  • gaphor/trunk/gaphor/services/undomanager.py

    r1171 r1177  
    2020 
    2121def get_undo_manager(): 
    22     """Return the default undo manager. 
     22    """ 
     23    Return the default undo manager. 
    2324    """ 
    2425    return _default_undo_manager 
     
    156157 
    157158            self._current_transaction = None 
     159        component.handle(UndoManagerStateChanged(self)) 
    158160 
    159161    def rollback_transaction(self):