31 from classes.logger
import log
36 import simplejson
as json
46 raise NotImplementedError(
"updateStatus() not implemented in UpdateWatcher implementer.")
57 raise NotImplementedError(
"changed() not implemented in UpdateInterface implementer.")
64 def __init__(self, type=None, key=[], values=None, partial_update=False):
76 def json(self, is_array=False, only_value=False):
82 data_dict = {
"type": self.
type,
89 update_action_dict = data_dict
92 update_action_dict = [data_dict]
95 return json.dumps(update_action_dict)
102 update_action_dict = json.loads(value)
105 self.
type = update_action_dict[
"type"]
106 self.
key = update_action_dict[
"key"]
107 self.
values = update_action_dict[
"value"]
108 self.
old_values = update_action_dict[
"old_value"]
127 self.actionHistory.clear()
128 self.redoHistory.clear()
137 self.updateListeners.append(listener)
140 self.updateListeners.insert(index, listener)
142 log.warning(
"Listener already added.")
149 self.statusWatchers.append(watcher)
151 log.warning(
"Watcher already added.")
160 watcher.updateStatusChanged(*new_status)
172 reverse =
UpdateAction(action.type, action.key, action.values, action.partial_update)
174 if action.type ==
"insert":
175 reverse.type =
"delete" 178 id = action.values[
"id"]
179 action.key.append({
"id": id})
182 elif action.type ==
"delete":
183 reverse.type =
"insert" 187 reverse.old_values = action.values
188 reverse.values = action.old_values
198 last_action = self.actionHistory.pop()
199 self.redoHistory.append(last_action)
210 next_action = self.redoHistory.pop()
213 if next_action.type ==
"insert" and isinstance(next_action.key[-1], dict)
and "id" in next_action.key[-1]:
214 next_action.key = next_action.key[:-1]
216 self.actionHistory.append(next_action)
229 listener.changed(action)
231 except Exception
as ex:
232 log.error(
"Couldn't apply '{}' to update listener: {}\n{}".
format(action.type, listener, ex))
240 self.redoHistory.clear()
241 self.actionHistory.append(
UpdateAction(
'load',
'', values))
249 self.redoHistory.clear()
250 self.actionHistory.append(
UpdateAction(
'insert', key, values))
255 def update(self, key, values, partial_update=False):
257 self.redoHistory.clear()
258 self.actionHistory.append(
UpdateAction(
'update', key, values, partial_update))
265 self.redoHistory.clear()
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 __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 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...
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)...