kaddressbook Library API Documentation

undo.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (C) 1999 Don Sanders <sanders@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include "undo.h"
00025 
00027 // StackBase
00028 
00029 void StackBase::push( Command *c )
00030 {
00031   mCommandStack.push( c );
00032   emit changed();
00033 }
00034 
00035 bool StackBase::isEmpty()
00036 {
00037   return mCommandStack.isEmpty();
00038 }
00039 
00040 Command *StackBase::top()
00041 {
00042   return mCommandStack.top();
00043 }
00044 
00045 void StackBase::clear()
00046 {
00047   mCommandStack.clear();
00048   emit changed();
00049 }
00050 
00051 Command *StackBase::pop()
00052 {
00053   Command *c = mCommandStack.pop();
00054   if ( c )
00055     emit changed();
00056 
00057   return c;
00058 }
00059 
00061 // UndoStack
00062 
00063 UndoStack* UndoStack::instance_ = 0;
00064 
00065 UndoStack::UndoStack()
00066   : StackBase()
00067 {
00068   // setAutoDelete( true );
00069 }
00070 
00071 UndoStack* UndoStack::instance()
00072 {
00073   if ( !instance_ )
00074     instance_ = new UndoStack();
00075   return instance_;
00076 }
00077 
00078 void UndoStack::undo()
00079 {
00080   if ( isEmpty() )
00081     return;
00082 
00083   Command *command = pop();
00084   if ( !command->undo() )
00085     push( command );
00086   else
00087     RedoStack::instance()->push( command );
00088 }
00089 
00091 // RedoStack
00092 
00093 RedoStack* RedoStack::instance_ = 0;
00094 
00095 RedoStack::RedoStack()
00096 {
00097   mCommandStack.setAutoDelete( true );
00098 }
00099 
00100 RedoStack* RedoStack::instance()
00101 {
00102   if ( !instance_ )
00103     instance_ = new RedoStack();
00104   return instance_;
00105 }
00106 
00107 void RedoStack::redo()
00108 {
00109   Command *command;
00110   if ( isEmpty() )
00111     return;
00112 
00113   command = pop();
00114   if ( !command->redo() )
00115     push( command );
00116   else
00117     UndoStack::instance()->push( command );
00118 }
00119 
00120 #include "undo.moc"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:53 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003