KiCad PCB EDA Suite
commit.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #include <commit.h>
27 #include <base_struct.h>
28 
30 {
31 }
32 
33 
35 {
36  for( COMMIT_LINE& ent : m_changes )
37  {
38  if( ent.m_copy )
39  delete ent.m_copy;
40  }
41 }
42 
43 
44 COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType )
45 {
46  // CHT_MODIFY and CHT_DONE are not compatible
47  assert( ( aChangeType & ( CHT_MODIFY | CHT_DONE ) ) != ( CHT_MODIFY | CHT_DONE ) );
48 
49  int flag = aChangeType & CHT_FLAGS;
50 
51  switch( aChangeType & CHT_TYPE )
52  {
53  case CHT_ADD:
54  assert( m_changedItems.find( aItem ) == m_changedItems.end() );
55  makeEntry( aItem, CHT_ADD | flag );
56  return *this;
57 
58  case CHT_REMOVE:
59  assert( m_changedItems.find( aItem ) == m_changedItems.end() );
60  makeEntry( aItem, CHT_REMOVE | flag );
61  return *this;
62 
63  case CHT_MODIFY:
64  {
65  EDA_ITEM* parent = parentObject( aItem );
66 
67  if( m_changedItems.find( parent ) != m_changedItems.end() )
68  return *this; // item has been already modified once
69 
70  makeEntry( parent, CHT_MODIFY | flag, parent->Clone() );
71 
72  return *this;
73  }
74 
75  default:
76  assert( false );
77  }
78 
79  return *this;
80 }
81 
82 
84 {
85  EDA_ITEM* parent = parentObject( aItem );
86 
87  if( m_changedItems.find( parent ) != m_changedItems.end() )
88  return *this; // item has been already modified once
89 
90  makeEntry( parent, CHT_MODIFY, aCopy );
91 
92  return *this;
93 }
94 
95 
96 COMMIT& COMMIT::Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType )
97 {
98  for( EDA_ITEM* item : container )
99  {
100  Stage( item, aChangeType );
101  }
102 
103  return *this;
104 }
105 
106 
108 {
109  for( unsigned int i = 0; i < aItems.GetCount(); i++ )
110  {
111  UNDO_REDO_T change_type = aItems.GetPickedItemStatus( i );
112  EDA_ITEM* item = aItems.GetPickedItem( i );
113  EDA_ITEM* copy = NULL;
114 
115  if( change_type == UR_UNSPECIFIED )
116  change_type = aItems.m_Status;
117 
118  if( change_type == UR_UNSPECIFIED )
119  change_type = aModFlag;
120 
121  if( ( copy = aItems.GetPickedItemLink( i ) ) )
122  {
123  assert( change_type == UR_CHANGED );
124 
125  // There was already a copy created, so use it
126  Modified( item, copy );
127  }
128  else
129  {
130  Stage( item, convert( change_type ) );
131  }
132  }
133 
134  return *this;
135 }
136 
137 
138 void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy )
139 {
140  // Expect an item copy if it is going to be modified
141  assert( !!aCopy == ( ( aType & CHT_TYPE ) == CHT_MODIFY ) );
142 
143  COMMIT_LINE ent;
144 
145  ent.m_item = aItem;
146  ent.m_type = aType;
147  ent.m_copy = aCopy;
148 
149  m_changedItems.insert( aItem );
150  m_changes.push_back( ent );
151 }
152 
153 
155 {
156  switch( aType )
157  {
158  case UR_NEW:
159  return CHT_ADD;
160 
161  case UR_DELETED:
162  return CHT_REMOVE;
163 
164  default:
165  assert( false );
166  // fall through
167 
168  case UR_CHANGED:
169  case UR_MOVED:
170  case UR_MIRRORED_X:
171  case UR_MIRRORED_Y:
172  case UR_ROTATED:
174  case UR_FLIPPED:
175  return CHT_MODIFY;
176  }
177 }
178 
COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Adds a change of the item aItem of type aChangeType to the change list.
Definition: commit.cpp:44
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy)
Creates an undo entry for an item that has been already modified.
Definition: commit.cpp:83
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
CHANGE_TYPE m_type
Modification type
Definition: commit.h:150
Definition: commit.h:38
Flag to indicate the change is already applied, just notify observers (not compatible with CHT_MODIFY...
Definition: commit.h:45
Class COMMIT.
Definition: commit.h:71
EDA_ITEM * m_item
Main item that is added/deleted/modified
Definition: commit.h:144
CHANGE_TYPE convert(UNDO_REDO_T aType) const
Definition: commit.cpp:154
UNDO_REDO_T GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
virtual ~COMMIT()
Definition: commit.cpp:34
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:166
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
unsigned GetCount() const
Function GetCount.
CHANGE_TYPE
Types of changes
Definition: commit.h:37
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
Function GetPickedItem.
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:167
COMMIT()
Definition: commit.cpp:29
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
EDA_ITEM * m_copy
Optional copy of the item
Definition: commit.h:147
Basic classes for most KiCad items.
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=NULL)
Definition: commit.cpp:138