def _linearUndo(self, target): mapview = self.editor.getActiveMapView() if mapview is None: return undomanager = mapview.getController().getUndoManager() current_item = undomanager.current_item # Redo? item = current_item count = 0 while item is not None: if item == target: undomanager.redo(count) break count += 1 item = item.next else: # Undo? count = 0 item = current_item while item is not None: if item == target: undomanager.undo(count) break count += 1 item = item.previous else: print "HistoryManager: Didn't find target item!" # Select the current item, important to see if the undo/redo didn't work as expected self.update()
def _itemSelected(self): mapview = self.editor.getActiveMapView() if mapview is None: return undomanager = mapview.getController().getUndoManager() stackitem = self.list.selected_item.item if stackitem == undomanager.current_item: return if undomanager.getBranchMode() is False: self._linearUndo(stackitem) return searchlist = [] searchlist2 = [] parent = stackitem branch = parent.next while parent is not None: if parent is undomanager.first_item or len(parent._branches) > 1: searchlist.append( (parent, branch) ) branch = parent parent = parent.parent current_item = undomanager.current_item parent = current_item branch = parent.next while parent is not None: if parent is undomanager.first_item or len(parent._branches) > 1: searchlist2.append( (parent, branch) ) branch = parent parent = parent.parent searchlist.reverse() searchlist2.reverse() # Remove duplicate entries, except the last duplicate, so we don't undo # more items than necessary sl = len(searchlist); if len(searchlist2) < sl: sl = len(searchlist2) for s in range(sl): if searchlist[s][0] != searchlist[s][0]: searchlist = searchlist[s-1:] s_item = searchlist[0][0] # Undo until we reach the first shared parent i = 0 item = current_item while item is not None: if item == s_item: undomanager.undo(i) current_item = item break i += 1 item = item.previous else: print "Nada (undo)" return # Switch branches for s_item in searchlist: if s_item[0].setBranch(s_item[1]) is False: print "Warning: HistoryManager: Switching branch failed for: ", s_item # Redo to stackitem item = current_item i = 0 while item is not None: if item == stackitem: undomanager.redo(i) break i += 1 item = item.next else: print "Nada (redo)" # Select the current item, important to see if the undo/redo didn't work as expected self.update()