Inhaltsverzeichnis

Undo

Interaction Techniques and Technologies (ITT), SS 2017
Session 20 (18.07.2017), Raphael Wimmer

Overview

These are slides/notes for the lecture, automatically generated from the slide set. Please extend this outline with your own notes.

Overview

Undo

Questions:

History

First documented use: Bravo text editor (Xerox Alto), 1974 (user manual)

Shortcuts

<small>(source: Brad Myers' slide set)</small>

Linear Multi-Level Undo Model

<small>(source: Brad Myers)</small>

Important details

Preserving the complete command history

Selective undo

<small>(source: Brad Myers)</small>

Multi-user undo

<small>(source: Brad Myers)</small>

Practical Implementations

Implementation 1: Memento Pattern

Implementation 2: Command Pattern

Qt Implementation

Qt Undo Example (1/2)

~~~~ undo.py
#!/usr/bin/env python3
from PyQt5.QtWidgets import QUndoCommand, QUndoStack, QUndoGroup 

class SimpleDocument(object):
    def __init__(self, text=None):
        if text is None:
            self.text = ""
        else:
            self.text = text

class InsertCharacter(QUndoCommand):
    def __init__(self, document, position, character):
        super().__init__()
        self.document = document
        self.character = character
        self.position = position
        self.setText("insert a character")
    def undo(self):
        self.document.text = self.document.text[:self.position] \
                           + self.document.text[self.position+1:]
    def redo(self):
        self.document.text = self.document.text[:self.position] \
                           + self.character \
                           + self.document.text[self.position:]
~~~~

Qt Undo Example (2/2)

~~~~ undo.py

if __name__ == "__main__":
    stack = QUndoStack()
    d = SimpleDocument("123456")
    stack.push(InsertCharacter(d, 1, "a"))
    # "1a23456"
    stack.push(InsertCharacter(d, 3, "b"))
    # "1a2b3456"
    stack.undo()
    stack.undo()
    # "123456" 
~~~~

Recap

ENDE