Skip to content

Commit

Permalink
classes/updates: Add pending_acton tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed Jan 19, 2020
1 parent 3623382 commit c7d0692
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/classes/updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ def __init__(self):
self.actionHistory = [] # List of actions performed to current state
self.redoHistory = [] # List of actions undone
self.currentStatus = [None, None] # Status of Undo and Redo buttons (true/false for should be enabled)
self.ignore_history = False # Ignore saving actions to history, to prevent a huge undo/redo list
self.last_action = None
self.ignore_history = False # Ignore saving actions to history, to prevent a huge undo/redo list
self.last_action = None # The last action processed
self.pending_action = None # Last action not added to actionHistory list

def load_history(self, project):
"""Load history from project"""
self.redoHistory.clear()
self.actionHistory.clear()
self.reset()

# Get history from project data
history = project.get("history")
Expand Down Expand Up @@ -186,15 +186,15 @@ def save_history(self, project, history_length):
log.info("Saving undo history, skipped key: %s" % str(action.key))

# Set history data in project
self.ignore_history = True
self.update(["history"], { "redo": redo_list, "undo": undo_list})
self.ignore_history = False
self.update_untracked(["history"], {"redo": redo_list, "undo": undo_list})

def reset(self):
""" Reset the UpdateManager, and clear all UpdateActions and History.
This does not clear listeners and watchers. """
self.actionHistory.clear()
self.redoHistory.clear()
self.pending_action = None
self.last_action = None

def add_listener(self, listener, index=-1):
""" Add a new listener (which will invoke the changed(action) method
Expand Down Expand Up @@ -265,6 +265,7 @@ def undo(self):
last_action = copy.deepcopy(self.actionHistory.pop())

self.redoHistory.append(last_action)
self.pending_action = None
# Get reverse of last action and perform it
reverse_action = self.get_reverse_action(last_action)
self.dispatch_action(reverse_action)
Expand All @@ -281,6 +282,7 @@ def redo(self):
next_action.key = next_action.key[:-1]

self.actionHistory.append(next_action)
self.pending_action = None
# Perform next redo action
self.dispatch_action(next_action)

Expand All @@ -306,6 +308,7 @@ def load(self, values):
self.last_action = UpdateAction('load', '', values)
self.redoHistory.clear()
self.actionHistory.clear()
self.pending_action = None
self.dispatch_action(self.last_action)

# Perform new actions, clearing redo history for taking a new path
Expand All @@ -314,8 +317,11 @@ def insert(self, key, values):
(this action will then be distributed to all listeners) """

self.last_action = UpdateAction('insert', key, values)
if not self.ignore_history:
if self.ignore_history:
self.pending_action = self.last_action
else:
self.redoHistory.clear()
self.pending_action = None
self.actionHistory.append(self.last_action)
self.dispatch_action(self.last_action)

Expand All @@ -324,10 +330,13 @@ def update(self, key, values, partial_update=False):
(this action will then be distributed to all listeners) """

self.last_action = UpdateAction('update', key, values, partial_update)
if not self.ignore_history:
if self.ignore_history:
self.pending_action = self.last_action
else:
if self.last_action.key and self.last_action.key[0] != "history":
# Clear redo history for any update except a "history" update
self.redoHistory.clear()
self.pending_action = None
self.actionHistory.append(self.last_action)
self.dispatch_action(self.last_action)

Expand All @@ -336,22 +345,29 @@ def update_untracked(self, key, values, partial_update=False):
a new entry in the history table
(this action will then be distributed to all listeners) """
previous_ignore = self.ignore_history
previous_pending = self.pending_action
self.ignore_history = True
self.update(key, values, partial_update)
self.ignore_history = previous_ignore
self.pending_action = previous_pending

def delete(self, key):
""" Delete an item from the UpdateManager with an UpdateAction
(this action will then be distributed to all listeners) """

self.last_action = UpdateAction('delete', key)
if not self.ignore_history:
if self.ignore_history:
self.pending_action = self.last_action
else:
self.redoHistory.clear()
self.pending_action = None
self.actionHistory.append(self.last_action)
self.dispatch_action(self.last_action)

def apply_last_action_to_history(self, previous_value):
""" Apply the last action to the history """
if self.last_action:
self.last_action.set_old_values(previous_value)
self.actionHistory.append(self.last_action)
if self.pending_action:
self.pending_action.set_old_values(previous_value)
self.actionHistory.append(self.pending_action)
self.last_action = self.pending_action
self.pending_action = None

0 comments on commit c7d0692

Please sign in to comment.