summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmaddcommand.cpp
blob: 2eb78126ca98eac9d6815dac39ab0edf7d89d414 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
**************************************************************************
                                 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 "pmaddcommand.h"
#include "pmcommandmanager.h"
#include "pmpart.h"
#include "pmdeclare.h"
#include "pmerrorflags.h"
#include "pmrecursiveobjectiterator.h"
#include "pmmemento.h"

#include <klocale.h>

PMAddCommand::PMAddCommand( PMObject* obj, PMObject* parent, PMObject* after )
      : PMCommand( i18n( "Add New %1" ).tqarg( obj->description( ) ) )
{
   m_objects.append( obj );
   m_pParent = parent;
   m_pAfter = after;
   m_executed = false;
   m_firstExecution = true;
   m_linksCreated = false;
   m_pParentChangeMemento = 0;
}

PMAddCommand::PMAddCommand( const PMObjectList& list, PMObject* parent,
                            PMObject* after )
      : PMCommand( i18n( "Add Objects" ) )
{
   m_objects = list;
   m_pParent = parent;
   m_pAfter = after;
   m_executed = false;
   m_firstExecution = true;
   m_linksCreated = false;
   m_pParentChangeMemento = 0;
}

PMAddCommand::~PMAddCommand( )
{
   if( !m_executed )
   {
      m_objects.setAutoDelete( true );
      m_objects.clear( );
   }

   m_insertErrors.setAutoDelete( true );
   m_insertErrors.clear( );
}

void PMAddCommand::execute( PMCommandManager* theManager )
{
   if( !m_executed )
   {
      PMObjectListIterator it( m_objects );
      PMObject* prev = m_pAfter;
      PMObjectList errors;
      PMObject* current;
      bool error = false;

      if( m_firstExecution )
         if( m_pParent->dataChangeOnInsertRemove( ) )
            m_pParent->createMemento( );
      
      for( ; it.current( ); ++it )
      {
         current = it.current( );
         if( !prev )
         {
            if( m_pParent->canInsert( current, 0 ) )
            {
               m_pParent->insertChild( current, 0 );
               prev = current;
               theManager->cmdObjectChanged( current, PMCAdd );
            }
            else
               error = true;
         }
         else
         {
            if( m_pParent->canInsert( current, prev ) )
            {
               m_pParent->insertChildAfter( current, prev );
               prev = current;
               theManager->cmdObjectChanged( current, PMCAdd );
            }
            else
               error = true;
         }
         
         if( error )
         {
            errors.append( current );
            theManager->cmdObjectChanged( current, PMCAdd | PMCInsertError );
            if( current->isA( "Declare" ) )
            {
               // the object, that couldn't be inserted was a declare,
               // remove all links
               PMObjectListIterator links =
                  ( ( PMDeclare* ) current )->linkedObjects( );
               for( ; links.current( ); ++links )
               {
                  PMObject* l = links.current( );
                  if( l->parent( ) )
                     l->parent( )->takeChild( l );
                  else
                     m_objects.removeRef( l );
                  m_insertErrors.append( l );
               }
            }
            if( current->linkedObject( ) )
               current->linkedObject( )->removeLinkedObject( current );
            error = false;
         }
      }

      if( m_pParent->mementoCreated( ) )
         m_pParentChangeMemento = m_pParent->takeMemento( );

      if( m_pParentChangeMemento )
      {
         PMObjectChangeListIterator c = m_pParentChangeMemento->changedObjects( );
         for( ; c.current( ); ++c )
            theManager->cmdObjectChanged( c.current( )->object( ),
                                          c.current( )->mode( ) );
      }

      if( m_linksCreated )
      {
         PMObjectListIterator rit( m_links );
         for( ; rit.current( ); ++rit )
            rit.current( )->linkedObject( )->addLinkedObject( rit.current( ) );
         PMObjectListIterator dit( m_linkedDeclares );
         for( ; dit.current( ); ++dit )
            theManager->cmdObjectChanged( dit.current( ), PMCData );
      }
      
      PMObjectListIterator errorit( errors );
      for( ; errorit.current( ); ++errorit )
      {
         m_objects.removeRef( errorit.current( ) );
         m_insertErrors.append( errorit.current( ) );

         PMRecursiveObjectIterator lit( errorit.current( ) );
         for( ; lit.current( ); ++lit )
            if( lit.current( )->linkedObject( ) )
               lit.current( )->linkedObject( )->removeLinkedObject( lit.current( ) );
      }
      
      m_executed = true;
      m_firstExecution = false;
   }
}

void PMAddCommand::undo( PMCommandManager* theManager )
{
   if( m_executed )
   {
      PMObjectListIterator it( m_objects );
      PMObject* obj;
      PMDeclare* decl;

      if( !m_linksCreated )
      {
         for( ; it.current( ); ++it )
         {
            PMRecursiveObjectIterator lit( it.current( ) );
            for( ; lit.current( ); ++lit )
            {
               decl = lit.current( )->linkedObject( );
               if( decl )
               {
                  m_links.append( lit.current( ) );
                  if( !m_linkedDeclares.containsRef( decl ) )
                     m_linkedDeclares.append( decl );
               }
            }
         }
         m_linksCreated = true;
      }

      PMObjectListIterator rit( m_links );
      for( ; rit.current( ); ++rit )
         rit.current( )->linkedObject( )->removeLinkedObject( rit.current( ) );
      
      for( it.toLast( ) ; it.current( ); --it )
      {
         obj = it.current( );
         // signal has to be emitted before the item is removed
         theManager->cmdObjectChanged( obj, PMCRemove );

         if( obj->parent( ) )
            obj->parent( )->takeChild( obj );
      }

      if( m_pParentChangeMemento )
      {
         m_pParent->restoreMemento( m_pParentChangeMemento );
         PMObjectChangeListIterator c = m_pParentChangeMemento->changedObjects( );
         for( ; c.current( ); ++c )
         {
            theManager->cmdObjectChanged( c.current( )->object( ),
                                          c.current( )->mode( ) );
         }
      }
      
      PMObjectListIterator dit( m_linkedDeclares );
      for( ; dit.current( ); ++dit )
         theManager->cmdObjectChanged( dit.current( ), PMCData );
      
      m_executed = false;
   }
}

int PMAddCommand::errorFlags( PMPart* )
{
   return PMENone;
}