def setup_undo(self): self.undoActionGroup = Gtk.ActionGroup(name=f'{self.name}UndoActions') self.undoActionGroup.add_actions([ ('Undo', Gtk.STOCK_UNDO, None, '<Control>Z'), ('Redo', Gtk.STOCK_REDO, None, '<Control><Shift>Z'), ('Reapply', None, 'Reapply', '<Control>Y'), ]) self.action_groups.append(self.undoActionGroup) self.history = Undo.UndoHistoryList( self.undoActionGroup.get_action('Undo'), self.undoActionGroup.get_action('Redo'), self.undoActionGroup.get_action('Reapply')) self.history.add_action_hook(self.undo_action_callback)
def undoable_delete_recs(self, recs, history, make_visible=None): """Delete recipes by setting their 'deleted' flag to True and add to UNDO history.""" def do_delete(): for rec in recs: rec.deleted = True if make_visible: make_visible(recs) def undo_delete(): for rec in recs: rec.deleted = False if make_visible: make_visible(recs) obj = Undo.UndoableObject(do_delete, undo_delete, history) obj.perform()
def undoable_delete_ings(self, ings, history, make_visible=None): """Delete ingredients in list ings and add to our undo history.""" def do_delete(): for i in ings: i.deleted = True if make_visible: make_visible(ings) def undo_delete(): for i in ings: i.deleted = False if make_visible: make_visible(ings) obj = Undo.UndoableObject(do_delete, undo_delete, history) obj.perform()
def undoable_delete_recs(self, recs, history, make_visible=None): """Delete recipes by setting their 'deleted' flag to True and add to UNDO history.""" def do_delete(): for rec in recs: debug('rec %s deleted=True' % rec.id, 1) self.modify_rec(rec, {'deleted': True}) if make_visible: make_visible(recs) def undo_delete(): for rec in recs: debug('rec %s deleted=False' % rec.id, 1) self.modify_rec(rec, {'deleted': False}) if make_visible: make_visible(recs) obj = Undo.UndoableObject(do_delete, undo_delete, history) obj.perform()
def undoable_modify_rec(self, rec, dic, history=[], get_current_rec_method=None, select_change_method=None): """Modify our recipe and remember how to undo our modification using history.""" orig_dic = self.get_dict_for_obj(rec, dic.keys()) reundo_name = "Re_apply" reapply_name = "Re_apply " reundo_name += string.join( ["%s <i>%s</i>" % (k, v) for k, v in orig_dic.items()]) reapply_name += string.join( ["%s <i>%s</i>" % (k, v) for k, v in dic.items()]) redo, reundo = None, None if get_current_rec_method: def redo(*args): r = get_current_rec_method() odic = self.get_dict_for_obj(r, dic.keys()) return ([r, dic], [r, odic]) def reundo(*args): r = get_current_rec_method() odic = self.get_dict_for_obj(r, orig_dic.keys()) return ([r, orig_dic], [r, odic]) def action(*args, **kwargs): """Our actual action allows for selecting changes after modifying""" self.modify_rec(*args, **kwargs) if select_change_method: select_change_method(*args, **kwargs) obj = Undo.UndoableObject( action, action, history, action_args=[rec, dic], undo_action_args=[rec, orig_dic], get_reapply_action_args=redo, get_reundo_action_args=reundo, reapply_name=reapply_name, reundo_name=reundo_name, ) obj.perform()
def undoable_modify_ing(self, ing, dic, history, make_visible=None): """modify ingredient object ing based on a dictionary of properties and new values. history is our undo history to be handed to Undo.UndoableObject make_visible is a function that will make our change (or the undo or our change) visible. """ orig_dic = self.get_dict_for_obj(ing, dic.keys()) def do_action(): debug('undoable_modify_ing modifying %s' % dic, 2) self.modify_ing(ing, dic) if make_visible: make_visible(ing, dic) def undo_action(): debug('undoable_modify_ing unmodifying %s' % orig_dic, 2) self.modify_ing(ing, orig_dic) if make_visible: make_visible(ing, orig_dic) obj = Undo.UndoableObject(do_action, undo_action, history) obj.perform()