/*!
    \example tools/undoframework 
    \title Undo Framework Example

    This example shows how to implement undo/redo functionality 
    with the Qt undo framework.

    \image undoframeworkexample.png The Undo Diagram Example

    In the Qt undo framework, all actions that the user performs are
    implemented in classes that inherit QUndoCommand. An undo command
    class knows how to both \l{QUndoCommand::}{redo()} - or just do
    the first time - and \l{QUndoCommand::}{undo()} an action. For
    each action the user performs, a command is placed on a
    QUndoStack.  Since the stack contains all commands executed
    (stacked in chronological order) on the document, it can roll the
    state of the document backwards and forwards by undoing and redoing
    its commands. See the \l{Overview of Qt's Undo Framework}{overview
    document} for a high-level introduction to the undo framework.

    The undo example implements a simple diagram application. It is
    possible to add and delete items, which are either box or
    rectangular shaped, and move the items by dragging them with the
    mouse. The undo stack is shown in a QUndoView, which is a list in
    which the commands are shown as list items. Undo and redo are
    available through the edit menu. The user can also select a command
    from the undo view.

    We use the \l{The Graphics View Framework}{graphics view
    framework} to implement the diagram. We only treat the related
    code briefly as the framework has examples of its own (e.g., the
    \l{Diagram Scene Example}).

    The example consists of the following classes:

    \list 
	\o \c MainWindow is the main window and arranges the
	      example's widgets. It creates the commands based 
	      on user input and keeps them on the command stack.
	\o \c AddCommand adds an item to the scene.
	\o \c DeleteCommand deletes an item from the scene.
	\o \c MoveCommand when an item is moved the MoveCommand keeps record
              of the start and stop positions of the move, and it
	      moves the item according to these when \c redo() and \c undo()
	      is called.
	\o \c DiagramScene inherits QGraphicsScene and
              emits signals for the \c MoveComands when an item is moved.
	\o \c DiagramItem inherits QGraphicsPolygonItem and represents
	      an item in the diagram.
    \endlist

    \section1 MainWindow Class Definition

    \quotefromfile tools/undoframework/mainwindow.h
    \skipto /class.*MainWindow/
    \printuntil /^\};/

    The \c MainWindow class maintains the undo stack, i.e., it creates
    \l{QUndoCommand}s and pushes and pops them from the stack when it
    receives the \c triggered() signal from \c undoAction and \c
    redoAction.
    
    \section1 MainWindow Class Implementation

    We will start with a look at the constructor:    

    \quotefromfile tools/undoframework/mainwindow.cpp
    \skipto /::MainWindow/
    \printuntil /^\}/

    By connecting the undo stack's \c canRedoChanged() and
    canUndoChanged() signals to our undo and redo action's
    setEnabled() slot we make the actions disabled when the stack
    cannot undo and redo commands. The rest of the constructor sets up
    the DiagramScene and QGraphicsView. 

    Here is the \c createUndoView() function:    

    \skipto /::createUndoView/
    \printuntil /^\}/

    The QUndoView is a widget that display the text, which is set with
    the \l{QUndoCommand::}{setText()} function, for each QUndoCommand
    in the undo stack in a list.
    
    Here is the \c createActions() function:

    \skipto /::createActions/
    \printuntil /connect/

    \printline
    \dots
    \printline    

    \skipto undoAction
    \printuntil /connect\(redoAction/

    The \c createActions() function sets up all the examples actions
    in the manner shown above. We can connect our \c undoAction and \c
    redoAction directly to the stack's \c undo() and \c redo() slots 
    as we disable the actions when the stack cannot do undo and redo. 
    For the other actions we have implemented slots in the \c MainWindow 
    class.

    Here is the \c createMenues() function:

    \skipto /::createMenus/
    \printuntil /^\{/

    \dots
    \skipto /editMenu/
    \printto /itemMenu\s/
    \dots
    \skipto /^\}/
    \printline }

    We have to use the QMenu \c aboutToShow() and \c aboutToHide()
    signals since we only want \c deleteAction to be enabled when we
    have selected an item. We also want the \l{QUndoCommand::}{text()} 
    to be shown in the \c undoAction and \c redoAction menu items.
    
    Here is the \c itemMoved() slot:

    \skipto /::itemMoved/
    \printuntil /^\}/

    We simply push a MoveCommand on the stack, which calls \c redo()
    on it.

    Here is the \c deleteItem() slot:

    \skipto /::deleteItem/
    \printuntil /^\}/

    An item must be selected to be deleted.  We need to check if it is
    selected as the \c deleteAction may be enabled even if an item is
    not selected. This can happen as we do not catch a signal or event
    when an item is selected.

    Here is the \c itemMenuAboutToShow() and itemMenuAboutToHide() slots:

    \skipto /::itemMenuAboutToHide/
    \printuntil /^\}/

    \skipto /::itemMenuAboutToShow/
    \printuntil /^\}/

    We implement \c itemMenuAboutToShow() and \c itemMenuAboutToHide()
    to get a dynamic item menu. These slots are connected to the 
    \l{QMenu::}{aboutToShow()} and \l{QMenu::}{aboutToHide()} signals.
    We need this to disable or enable the \c deleteAction and fill the
    \c redoAction and \c undoAction menu item with the text from the
    next QUndoCommand that will be redone or undone.

    Here is the \c addBox() slot:

    \skipto /::addBox/
    \printuntil /^\}/

    The \c addBox() function creates an AddCommand and pushes it on
    the undo stack.

    Here is the \c addTriangle() sot:

    \skipto /::addTriangle/
    \printuntil /^\}/

    The \c addTriangle() function creates an AddCommand and pushes it
    on the undo stack.    

    Here is the implementation of \c about():

    \skipto /::about/
    \printuntil /^\}/
    
    The about slot is triggered by the \c aboutAction and displays an
    about box for the example.

    \section1 AddCommand Class Definition
    
    \quotefromfile tools/undoframework/commands.h
    \skipto /class.*AddCommand/
    \printuntil /^\};/

    The \c AddCommand class adds DiagramItem graphics items to the
    DiagramScene.  

    \section1 AddCommand Class Implementation

    We start with the constructor:

    \quotefromfile tools/undoframework/commands.cpp
    \skipto /::AddCommand/
    \printuntil /^\}/ 

    We first create the DiagramItem to add to the DiagramScene.  The
    \l{QUndoCommand::}{setText()} function let us set a QString that
    describes the command. We use this to get custom messages in the
    QUndoView and in the menu of the main window.

    \skipto /::undo()/
    \printuntil /^\}/

    \c undo() removes the item from the scene. We need to update the
    scene as ...(ask Andreas)

    \skipto/::redo()/
    \printuntil /^\}/

    We set the position of the item as we do not do this in the
    constructor.

    \section1 DeleteCommand Class Definition
    
    \quotefromfile tools/undoframework/commands.h
    \skipto /class.*DeleteCommand/
    \printuntil /^\};/

    The DeleteCommand class implements the functionality to remove an
    item from the scene.
    
    \section1 DeleteCommand Class Implementation

    \quotefromfile tools/undoframework/commands.cpp
    \skipto /::DeleteCommand/
    \printuntil /^\}/

    We know that there must be one selected item as it is not possible
    to create a DeleteCommand unless the item to be deleted is
    selected and that only one item can be selected at any time. 
    The item must be unselected if it is inserted back into the 
    scene.

    \skipto /::undo()/
    \printuntil /\}/

    The item is simply reinserted into the scene.

    \skipto /::redo()/
    \printuntil /^\}/

    The item is removed from the scene.

    \section1 MoveCommand Class Definition

    \quotefromfile tools/undoframework/commands.h
    \skipto /class.*MoveCommand/
    \printuntil /^\};/

    The \l{QUndoCommand::}{mergeWith()} is reimplemented to make
    consecutive moves of an item one MoveCommand, i.e, the item will
    be moved back to the start position of the first move.

    \section1 MoveCommand Class Implementation

    \quotefromfile tools/undoframework/commands.cpp
    
    The constructor of MoveCommand looks like this:

    \skipto /::MoveCommand/
    \printuntil /^\}/

    We save both the old and new positions for undo and redo
    respectively.

    \skipto /::undo()/
    \printuntil /^\}/

    We simply set the items old position and update the scene.    

    \skipto /::redo()/
    \printuntil /^\}/

    We set the item to its new position.
    
    \quotefromfile tools/undoframework/commands.cpp
    \skipto /::mergeWith/
    \printuntil /^\}/

    Whenever a MoveCommand is created, this function is called to
    check if it should be merged with the previous command. It is the
    previous command object that is kept on the stack. The function
    returns true if the command is merged; otherwise false.

    We first check whether it is the same item that has been moved
    twice, in which case we merge the commands. We update the position
    of the item so that it will take the last position in the move
    sequence when undone. 

    \section1 DiagramScene Class Definition

    \quotefromfile tools/undoframework/diagramscene.h
    \skipto /class.*DiagramScene/
    \printuntil /^\};/

    The DiagramScene implements the functionality to move a
    DiagramItem with the mouse. It emits a signal when a move is
    completed. This is caught by the \c MainWindow, which makes
    MoveCommands. We do not examine the implementation of DiagramScene
    as it only deals with graphics framework issues.

    \section1 The \c main() Function
    
    The \c main() function of the program looks like this:

    \quotefromfile tools/undoframework/main.cpp
    \skipto /main.*\(/
    \printuntil /^\}/

    We draw a grid in the background of the DiagramScene, so we use a
    resource file. The rest of the function creates the \c MainWindow and
    shows it as a top level window.
*/
