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