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 2016-2017 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 <algorithm>
27 
28 #include <commit.h>
29 #include <base_struct.h>
30 
32 {
33 }
34 
35 
37 {
38  for( COMMIT_LINE& ent : m_changes )
39  {
40  if( ent.m_copy )
41  delete ent.m_copy;
42  }
43 }
44 
45 
46 COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType )
47 {
48  // CHT_MODIFY and CHT_DONE are not compatible
49  assert( ( aChangeType & ( CHT_MODIFY | CHT_DONE ) ) != ( CHT_MODIFY | CHT_DONE ) );
50 
51  int flag = aChangeType & CHT_FLAGS;
52 
53  switch( aChangeType & CHT_TYPE )
54  {
55  case CHT_ADD:
56  assert( m_changedItems.find( aItem ) == m_changedItems.end() );
57  makeEntry( aItem, CHT_ADD | flag );
58  return *this;
59 
60  case CHT_REMOVE:
61  makeEntry( aItem, CHT_REMOVE | flag );
62  return *this;
63 
64  case CHT_MODIFY:
65  {
66  EDA_ITEM* parent = parentObject( aItem );
67  return createModified( parent, parent->Clone(), flag );
68  }
69 
70  default:
71  assert( false );
72  }
73 
74  return *this;
75 }
76 
77 
78 COMMIT& COMMIT::Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType )
79 {
80  for( EDA_ITEM* item : container )
81  {
82  Stage( item, aChangeType );
83  }
84 
85  return *this;
86 }
87 
88 
90 {
91  for( unsigned int i = 0; i < aItems.GetCount(); i++ )
92  {
93  UNDO_REDO_T change_type = aItems.GetPickedItemStatus( i );
94  EDA_ITEM* item = aItems.GetPickedItem( i );
95  EDA_ITEM* copy = NULL;
96 
97  if( change_type == UR_UNSPECIFIED )
98  change_type = aItems.m_Status;
99 
100  if( change_type == UR_UNSPECIFIED )
101  change_type = aModFlag;
102 
103  if( ( copy = aItems.GetPickedItemLink( i ) ) )
104  {
105  assert( change_type == UR_CHANGED );
106 
107  // There was already a copy created, so use it
108  Modified( item, copy );
109  }
110  else
111  {
112  Stage( item, convert( change_type ) );
113  }
114  }
115 
116  return *this;
117 }
118 
119 
121 {
122  COMMIT_LINE* entry = findEntry( parentObject( aItem ) );
123 
124  return entry ? entry->m_type : 0;
125 }
126 
127 
128 template <class Container, class F>
129 void eraseIf( Container& c, F&& f )
130 {
131  c.erase( std::remove_if( c.begin(),
132  c.end(),
133  std::forward<F>( f ) ),
134  c.end() );
135 }
136 
137 
138 COMMIT& COMMIT::createModified( EDA_ITEM* aItem, EDA_ITEM* aCopy, int aExtraFlags )
139 {
140  EDA_ITEM* parent = parentObject( aItem );
141  auto entryIt = m_changedItems.find( parent );
142 
143  if( entryIt != m_changedItems.end() )
144  {
145  delete aCopy;
146  return *this; // item has been already modified once
147  }
148 
149  makeEntry( parent, CHT_MODIFY | aExtraFlags, aCopy );
150 
151  return *this;
152 }
153 
154 
155 void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy )
156 {
157  // Expect an item copy if it is going to be modified
158  assert( !!aCopy == ( ( aType & CHT_TYPE ) == CHT_MODIFY ) );
159 
160  if( m_changedItems.find( aItem ) != m_changedItems.end() )
161  {
162  eraseIf( m_changes, [aItem] ( const COMMIT_LINE& aEnt ) {
163  return aEnt.m_item == aItem;
164  } );
165  }
166 
167  COMMIT_LINE ent;
168 
169  ent.m_item = aItem;
170  ent.m_type = aType;
171  ent.m_copy = aCopy;
172 
173  m_changedItems.insert( aItem );
174  m_changes.push_back( ent );
175 }
176 
177 
179 {
180  for( COMMIT_LINE& change : m_changes )
181  {
182  if( change.m_item == aItem )
183  return &change;
184  }
185 
186  return nullptr;
187 }
188 
189 
191 {
192  switch( aType )
193  {
194  case UR_NEW:
195  return CHT_ADD;
196 
197  case UR_DELETED:
198  return CHT_REMOVE;
199 
200  default:
201  assert( false );
202  // fall through
203 
204  case UR_CHANGED:
205  case UR_MOVED:
206  case UR_MIRRORED_X:
207  case UR_MIRRORED_Y:
208  case UR_ROTATED:
210  case UR_FLIPPED:
211  return CHT_MODIFY;
212  }
213 }
214 
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:46
virtual EDA_ITEM * Clone() const
Function Clone creates a duplicate of this item with linked list members set to NULL.
UNDO_REDO_T
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
CHANGE_TYPE m_type
Modification type
Definition: commit.h:157
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
int GetStatus(EDA_ITEM *aItem)
Returns status of an item.
Definition: commit.cpp:120
EDA_ITEM * m_item
Main item that is added/deleted/modified
Definition: commit.h:151
CHANGE_TYPE convert(UNDO_REDO_T aType) const
Definition: commit.cpp:190
UNDO_REDO_T GetPickedItemStatus(unsigned int aIdx) const
Function GetPickedItemStatus.
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
Function GetPickedItemLink.
COMMIT_LINE * findEntry(EDA_ITEM *aItem)
Searches for an entry describing change for a particular item.
Definition: commit.cpp:178
void eraseIf(Container &c, F &&f)
Definition: commit.cpp:129
virtual ~COMMIT()
Definition: commit.cpp:36
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:181
#define F(x, y, z)
Definition: md5_hash.cpp:15
Class PICKED_ITEMS_LIST is a holder to handle information on schematic or board items.
COMMIT & createModified(EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0)
Definition: commit.cpp:138
unsigned GetCount() const
Function GetCount.
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy)
Creates an undo entry for an item that has been already modified.
Definition: commit.h:110
CHANGE_TYPE
Types of changes
Definition: commit.h:37
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
Function GetPickedItem.
size_t i
Definition: json11.cpp:597
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:182
COMMIT()
Definition: commit.cpp:31
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
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:155