31 from classes.logger
import log
37 import simplejson
as json
47 raise NotImplementedError(
"updateStatus() not implemented in UpdateWatcher implementer.")
58 raise NotImplementedError(
"changed() not implemented in UpdateInterface implementer.")
65 def __init__(self, type=None, key=[], values=None, partial_update=False):
77 def json(self, is_array=False, only_value=False):
83 data_dict = {
"type": self.
type,
91 update_action_dict = data_dict
94 update_action_dict = [data_dict]
97 return json.dumps(update_action_dict)
104 update_action_dict = json.loads(value)
107 self.
type = update_action_dict.get(
"type")
108 self.
key = update_action_dict.get(
"key")
109 self.
values = update_action_dict.get(
"value")
110 self.
old_values = update_action_dict.get(
"old_values")
135 history = project.get([
"history"])
138 for actionDict
in history.get(
"redo", []):
140 action.load_json(json.dumps(actionDict))
142 for actionDict
in history.get(
"undo", []):
144 action.load_json(json.dumps(actionDict))
157 history_length_int = int(history_length)
158 for action
in self.
redoHistory[-history_length_int:]:
159 redo_list.append(json.loads(action.json()))
161 undo_list.append(json.loads(action.json()))
165 self.
update([
"history"], {
"redo": redo_list,
"undo": undo_list})
186 log.warning(
"Listener already added.")
195 log.warning(
"Watcher already added.")
204 watcher.updateStatusChanged(*new_status)
216 reverse =
UpdateAction(action.type, action.key, action.values, action.partial_update)
218 if action.type ==
"insert":
219 reverse.type =
"delete" 222 id = action.values[
"id"]
223 action.key.append({
"id": id})
226 elif action.type ==
"delete":
227 reverse.type =
"insert" 229 if reverse.type ==
"insert" and isinstance(reverse.key[-1], dict)
and "id" in reverse.key[-1]:
230 reverse.key = reverse.key[:-1]
234 reverse.old_values = action.values
235 reverse.values = action.old_values
259 next_action = copy.deepcopy(self.
redoHistory.pop())
262 if next_action.type ==
"insert" and isinstance(next_action.key[-1], dict)
and "id" in next_action.key[-1]:
263 next_action.key = next_action.key[:-1]
278 listener.changed(action)
280 except Exception
as ex:
281 log.error(
"Couldn't apply '{}' to update listener: {}\n{}".format(action.type, listener, ex))
308 def update(self, key, values, partial_update=False):
def update_watchers(self)
Notify all watchers if any 'undo' or 'redo' actions are available.
This class is used to track and distribute changes to listeners.
def updateStatusChanged(self, undo_status, redo_status)
Easily be notified each time there are 'undo' or 'redo' actions available in the UpdateManager.
def json(self, is_array=False, only_value=False)
Get the JSON string representing this UpdateAction.
def load_json(self, value)
Load this UpdateAction from a JSON string.
def get_reverse_action(self, action)
Convert an UpdateAction into the opposite type (i.e.
def redo(self)
Redo the last UpdateAction (and notify all listeners and watchers)
def dispatch_action(self, action)
Distribute changes to all listeners (by calling their changed() method)
def set_old_values(self, old_vals)
A data structure representing a single update manager action, including any necessary data to reverse...
def undo(self)
Undo the last UpdateAction (and notify all listeners and watchers)
def reset(self)
Reset the UpdateManager, and clear all UpdateActions and History.
def add_watcher(self, watcher)
Add a new watcher (which will invoke the updateStatusChanged() method each time a 'redo' or 'undo' ac...
def save_history(self, project, history_length)
Save history to project.
def __init__(self, type=None, key=[], values=None, partial_update=False)
def load(self, values)
Load all project data via an UpdateAction into the UpdateManager (this action will then be distribute...
def add_listener(self, listener, index=-1)
Add a new listener (which will invoke the changed(action) method each time an UpdateAction is availab...
Interface for classes that listen for 'undo' and 'redo' events.
def insert(self, key, values)
Insert a new UpdateAction into the UpdateManager (this action will then be distributed to all listene...
def apply_last_action_to_history(self, previous_value)
Apply the last action to the history.
def changed(self, action)
This method is invoked each time the UpdateManager is changed.
def delete(self, key)
Delete an item from the UpdateManager with an UpdateAction (this action will then be distributed to a...
def load_history(self, project)
Load history from project.
Interface for classes that listen for changes (insert, update, and delete).
def update(self, key, values, partial_update=False)
Update the UpdateManager with an UpdateAction (this action will then be distributed to all listeners)...