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  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  * @author Maciej Suminski <maciej.suminski@cern.ch>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, you may find one here:
22  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23  * or you may search the http://www.gnu.org website for the version 2 license,
24  * or you may write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26  */
27 
28 #include <algorithm>
29 
30 #include <commit.h>
31 #include <eda_item.h>
32 #include <macros.h>
33 
35 {
36 }
37 
38 
40 {
41  for( COMMIT_LINE& ent : m_changes )
42  {
43  if( ent.m_copy )
44  delete ent.m_copy;
45  }
46 }
47 
48 
49 COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType )
50 {
51  // CHT_MODIFY and CHT_DONE are not compatible
52  assert( ( aChangeType & ( CHT_MODIFY | CHT_DONE ) ) != ( CHT_MODIFY | CHT_DONE ) );
53 
54  int flag = aChangeType & CHT_FLAGS;
55 
56  switch( aChangeType & CHT_TYPE )
57  {
58  case CHT_ADD:
59  assert( m_changedItems.find( aItem ) == m_changedItems.end() );
60  makeEntry( aItem, CHT_ADD | flag );
61  return *this;
62 
63  case CHT_REMOVE:
64  makeEntry( aItem, CHT_REMOVE | flag );
65  return *this;
66 
67  case CHT_MODIFY:
68  {
69  EDA_ITEM* parent = parentObject( aItem );
70  EDA_ITEM* clone = nullptr;
71 
72  assert( parent );
73 
74  if( parent )
75  clone = parent->Clone();
76 
77  assert( clone );
78 
79  if( clone )
80  return createModified( parent, clone, flag );
81 
82  break;
83  }
84 
85  default:
86  assert( false );
87  }
88 
89  return *this;
90 }
91 
92 
93 COMMIT& COMMIT::Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType )
94 {
95  for( EDA_ITEM* item : container )
96  {
97  Stage( item, aChangeType );
98  }
99 
100  return *this;
101 }
102 
103 
104 COMMIT& COMMIT::Stage( const PICKED_ITEMS_LIST& aItems, UNDO_REDO aModFlag )
105 {
106  for( unsigned int i = 0; i < aItems.GetCount(); i++ )
107  {
108  UNDO_REDO change_type = aItems.GetPickedItemStatus( i );
109  EDA_ITEM* item = aItems.GetPickedItem( i );
110  EDA_ITEM* copy = nullptr;
111 
112  if( change_type == UNDO_REDO::UNSPECIFIED )
113  change_type = aModFlag;
114 
115  if( ( copy = aItems.GetPickedItemLink( i ) ) )
116  {
117  assert( change_type == UNDO_REDO::CHANGED );
118 
119  // There was already a copy created, so use it
120  Modified( item, copy );
121  }
122  else
123  {
124  Stage( item, convert( change_type ) );
125  }
126  }
127 
128  return *this;
129 }
130 
131 
133 {
134  COMMIT_LINE* entry = findEntry( parentObject( aItem ) );
135 
136  return entry ? entry->m_type : 0;
137 }
138 
139 
140 template <class Container, class F>
141 void eraseIf( Container& c, F&& f )
142 {
143  c.erase( std::remove_if( c.begin(),
144  c.end(),
145  std::forward<F>( f ) ),
146  c.end() );
147 }
148 
149 
150 COMMIT& COMMIT::createModified( EDA_ITEM* aItem, EDA_ITEM* aCopy, int aExtraFlags )
151 {
152  EDA_ITEM* parent = parentObject( aItem );
153  auto entryIt = m_changedItems.find( parent );
154 
155  if( entryIt != m_changedItems.end() )
156  {
157  delete aCopy;
158  return *this; // item has been already modified once
159  }
160 
161  makeEntry( parent, CHT_MODIFY | aExtraFlags, aCopy );
162 
163  return *this;
164 }
165 
166 
167 void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy )
168 {
169  // Expect an item copy if it is going to be modified
170  wxASSERT( !!aCopy == ( ( aType & CHT_TYPE ) == CHT_MODIFY ) );
171 
172  if( m_changedItems.find( aItem ) != m_changedItems.end() )
173  {
174  eraseIf( m_changes, [aItem] ( const COMMIT_LINE& aEnt )
175  {
176  return aEnt.m_item == aItem;
177  } );
178  }
179 
180  COMMIT_LINE ent;
181 
182  ent.m_item = aItem;
183  ent.m_type = aType;
184  ent.m_copy = aCopy;
185 
186  m_changedItems.insert( aItem );
187  m_changes.push_back( ent );
188 }
189 
190 
192 {
193  for( COMMIT_LINE& change : m_changes )
194  {
195  if( change.m_item == aItem )
196  return &change;
197  }
198 
199  return nullptr;
200 }
201 
202 
204 {
205  switch( aType )
206  {
207  case UNDO_REDO::NEWITEM:
208  return CHT_ADD;
209 
210  case UNDO_REDO::DELETED:
211  return CHT_REMOVE;
212 
213  default:
214  assert( false );
216 
217  case UNDO_REDO::CHANGED:
218  return CHT_MODIFY;
219  }
220 }
221 
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Definition: commit.cpp:49
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:132
#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:167
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.
COMMIT_LINE * findEntry(EDA_ITEM *aItem)
Search for an entry describing change for a particular item.
Definition: commit.cpp:191
void eraseIf(Container &c, F &&f)
Definition: commit.cpp:141
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
virtual ~COMMIT()
Add a new item to the model.
Definition: commit.cpp:39
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:150
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:34
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:203