summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmdatachangecommand.cpp
blob: 4188d253a530f443568a19811418179f17ff7fad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2001 by Andreas Zehender
    email                : zehender@kde.org
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/


#include "pmdatachangecommand.h"
#include "pmcommandmanager.h"
#include "pmmemento.h"
#include "pmobject.h"
#include <klocale.h>

PMDataChangeCommand::PMDataChangeCommand( PMMemento* memento )
      : PMCommand( )
{
   TQString text = memento->originator( )->name( );
   if( text.isEmpty( ) )
      text = memento->originator( )->description( );
   setText( i18n( "Change %1" ).tqarg( text ) );

   // the data is already changed when the command is created
   m_executed = true;
   m_unexecuted = false;
   m_pOldState = memento;
   m_pNewState = 0;
}

PMDataChangeCommand::~PMDataChangeCommand( )
{
   if( m_pOldState )
      delete m_pOldState;
   if( m_pNewState )
      delete m_pNewState;
}

void PMDataChangeCommand::execute( PMCommandManager* theManager )
{
   PMObject* obj = m_pOldState->originator( );
   if( !m_executed )
   {
      // if the command is not executed
      // restore the memento
      if( m_pNewState )
      {      
         if( m_pNewState->containsChanges( ) )
         {
            obj->restoreMemento( m_pNewState );

            if( m_pOldState->idChanged( ) )
               theManager->cmdIDChanged( obj, m_pOldState->oldID( ) );
            signalChanges( theManager, m_pNewState );
         }
      }
      m_executed = true;
   }
   else if( !m_unexecuted )
   {
      // the data can be changed multiple times
      // if the command was never unexecuted, emit the signal
      // ( the data was already changed, so this is not necessary here )
      if( m_pOldState->idChanged( ) )
         theManager->cmdIDChanged( obj, m_pOldState->oldID( ) );
      signalChanges( theManager, m_pOldState );
   }
}

void PMDataChangeCommand::undo( PMCommandManager* theManager )
{
   if( m_executed )
   {
      if( m_pOldState->containsChanges( ) )
      {
         PMObject* obj = m_pOldState->originator( );
         if( !m_pNewState )
            obj->createMemento( );
      
         obj->restoreMemento( m_pOldState );

         if( !m_pNewState )
            m_pNewState = obj->takeMemento( );
         
         if( m_pNewState->idChanged( ) )
            theManager->cmdIDChanged( obj, m_pNewState->oldID( ) );
         signalChanges( theManager, m_pOldState );
      }
      m_executed = false;
      m_unexecuted = true;
   }
}

void PMDataChangeCommand::signalChanges( PMCommandManager* theManager,
                                         PMMemento* memento )
{
   PMObjectChangeListIterator it( memento->changedObjects( ) );

   for( ; it.current( ); ++it )
      theManager->cmdObjectChanged( it.current( )->object( ),
                                    it.current( )->mode( ) );
}