KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
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 <core/kicad_algo.h>
29#include <commit.h>
30#include <eda_item.h>
31#include <eda_group.h>
32#include <macros.h>
33
37
38
40{
41 for( COMMIT_LINE& ent : m_entries )
42 delete ent.m_copy;
43}
44
45
46COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType, BASE_SCREEN* aScreen, RECURSE_MODE aRecurse )
47{
48 int flags = aChangeType & CHT_FLAGS;
49 int changeType = aChangeType & CHT_TYPE;
50 EDA_ITEM* undoItem = undoLevelItem( aItem );
51
52 if( undoItem != aItem )
53 {
54 changeType = CHT_MODIFY;
55
56 // CHT_DONE means the original add/remove was already applied to the child, but that
57 // semantic doesn't carry over when we remap to a modify of the parent
58 flags &= ~CHT_DONE;
59 }
60
61 wxASSERT( changeType != CHT_MODIFY || ( flags & CHT_DONE ) == 0 );
62
63 switch( changeType )
64 {
65 case CHT_ADD:
66 if( m_addedItems.find( { aItem, aScreen } ) != m_addedItems.end() )
67 break;
68
69 makeEntry( aItem, CHT_ADD | flags, nullptr, aScreen );
70 break;
71
72 case CHT_REMOVE:
73 if( m_deletedItems.find( { aItem, aScreen } ) != m_deletedItems.end() )
74 break;
75
76 makeEntry( aItem, CHT_REMOVE | flags, makeImage( aItem ), aScreen );
77
78 if( EDA_GROUP* parentGroup = aItem->GetParentGroup() )
79 {
80 if( parentGroup->AsEdaItem()->GetFlags() & STRUCT_DELETED )
81 Modify( parentGroup->AsEdaItem(), aScreen, RECURSE_MODE::NO_RECURSE );
82 }
83
84 break;
85
86 case CHT_MODIFY:
87 if( m_addedItems.find( { aItem, aScreen } ) != m_addedItems.end() )
88 break;
89
90 if( m_changedItems.find( { undoItem, aScreen } ) != m_changedItems.end() )
91 break;
92
93 makeEntry( undoItem, CHT_MODIFY | flags, makeImage( undoItem ), aScreen );
94 break;
95
96 default:
97 UNIMPLEMENTED_FOR( undoItem->GetClass() );
98 }
99
100 return *this;
101}
102
103
104COMMIT& COMMIT::Stage( std::vector<EDA_ITEM*> &container, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen )
105{
106 for( EDA_ITEM* item : container )
107 Stage( item, aChangeType, aScreen);
108
109 return *this;
110}
111
112
113COMMIT& COMMIT::Stage( const PICKED_ITEMS_LIST &aItems, UNDO_REDO aModFlag, BASE_SCREEN *aScreen )
114{
115 for( unsigned int i = 0; i < aItems.GetCount(); i++ )
116 {
117 UNDO_REDO change_type = aItems.GetPickedItemStatus( i );
118 EDA_ITEM* item = aItems.GetPickedItem( i );
119
120 if( change_type == UNDO_REDO::UNSPECIFIED )
121 change_type = aModFlag;
122
123 if( EDA_ITEM* copy = aItems.GetPickedItemLink( i ) )
124 {
125 assert( change_type == UNDO_REDO::CHANGED );
126
127 // There was already a copy created, so use it
128 Modified( item, copy, aScreen );
129 }
130 else
131 {
132 Stage( item, convert( change_type ), aScreen );
133 }
134 }
135
136 return *this;
137}
138
139
140void COMMIT::Unstage( EDA_ITEM* aItem, BASE_SCREEN* aScreen )
141{
142 std::erase_if( m_entries,
143 [&]( COMMIT_LINE& line )
144 {
145 if( line.m_item == aItem && line.m_screen == aScreen )
146 {
147 // Only new items which have never been committed can be unstaged
148 wxASSERT( line.m_item->IsNew() );
149
150 delete line.m_item;
151 delete line.m_copy;
152 return true;
153 }
154
155 return false;
156 } );
157}
158
159
161{
162 if( undoLevelItem( aItem ) != aItem )
163 wxFAIL_MSG( "We've no way to get a copy of the undo level item at this point" );
164 else
165 makeEntry( aItem, CHT_MODIFY, aCopy, aScreen );
166
167 return *this;
168}
169
170
172{
173 COMMIT_LINE* entry = findEntry( undoLevelItem( aItem ), aScreen );
174
175 return entry ? entry->m_type : 0;
176}
177
178
179void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy, BASE_SCREEN *aScreen )
180{
181 COMMIT_LINE ent;
182
183 ent.m_item = aItem;
184 ent.m_type = aType;
185 ent.m_copy = aCopy;
186 ent.m_screen = aScreen;
187
188 // N.B. Do not throw an assertion for multiple changed items. An item can be changed
189 // multiple times in a single commit such as when importing graphics and grouping them.
190
191 switch( aType & CHT_TYPE )
192 {
193 case CHT_ADD: m_addedItems.insert( { aItem, aScreen } ); break;
194 case CHT_REMOVE: m_deletedItems.insert( { aItem, aScreen } ); break;
195 case CHT_MODIFY: m_changedItems.insert( { aItem, aScreen } ); break;
196 default: wxFAIL; break;
197 }
198
199 m_entries.push_back( ent );
200}
201
202
204{
205 for( COMMIT_LINE& entry : m_entries )
206 {
207 if( entry.m_item == aItem && entry.m_screen == aScreen )
208 return &entry;
209 }
210
211 return nullptr;
212}
213
214
216{
217 switch( aType )
218 {
219 case UNDO_REDO::NEWITEM: return CHT_ADD;
220 case UNDO_REDO::DELETED: return CHT_REMOVE;
221 case UNDO_REDO::CHANGED: return CHT_MODIFY;
222 default: wxFAIL; return CHT_MODIFY;
223 }
224}
225
226
228{
229 switch( aType )
230 {
231 case CHT_ADD: return UNDO_REDO::NEWITEM;
232 case CHT_REMOVE: return UNDO_REDO::DELETED;
233 case CHT_MODIFY: return UNDO_REDO::CHANGED;
234 default: wxFAIL; return UNDO_REDO::CHANGED;
235 }
236}
237
Handles how to draw a screen (a board, a schematic ...)
Definition base_screen.h:41
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_deletedItems
Definition commit.h:184
virtual EDA_ITEM * undoLevelItem(EDA_ITEM *aItem) const =0
void Unstage(EDA_ITEM *aItem, BASE_SCREEN *aScreen)
Definition commit.cpp:140
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy, BASE_SCREEN *aScreen=nullptr)
Create an undo entry for an item that has been already modified.
Definition commit.cpp:160
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:106
COMMIT()
Definition commit.cpp:34
virtual ~COMMIT()
Definition commit.cpp:39
COMMIT_LINE * findEntry(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Search for an entry describing change for a particular item.
Definition commit.cpp:203
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_addedItems
Definition commit.h:182
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr, BASE_SCREEN *aScreen=nullptr)
Definition commit.cpp:179
std::vector< COMMIT_LINE > m_entries
Definition commit.h:185
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_changedItems
Definition commit.h:183
virtual EDA_ITEM * makeImage(EDA_ITEM *aItem) const =0
CHANGE_TYPE convert(UNDO_REDO aType) const
Definition commit.cpp:215
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:171
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Add a change of the item aItem of type aChangeType to the change list.
Definition commit.cpp:46
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:46
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:99
virtual EDA_GROUP * GetParentGroup() const
Definition eda_item.h:117
bool IsNew() const
Definition eda_item.h:125
virtual wxString GetClass() const =0
Return the class name.
A holder to handle information on schematic or board items.
UNDO_REDO GetPickedItemStatus(unsigned int aIdx) const
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
unsigned GetCount() const
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
CHANGE_TYPE
Types of changes.
Definition commit.h:41
@ CHT_MODIFY
Definition commit.h:44
@ CHT_REMOVE
Definition commit.h:43
@ CHT_DONE
Flag to indicate the change is already applied.
Definition commit.h:47
@ CHT_TYPE
Definition commit.h:45
@ CHT_ADD
Definition commit.h:42
@ CHT_FLAGS
Definition commit.h:48
RECURSE_MODE
Definition eda_item.h:51
@ NO_RECURSE
Definition eda_item.h:53
#define STRUCT_DELETED
flag indication structures to be erased
This file contains miscellaneous commonly used macros and functions.
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:96
EDA_ITEM * m_copy
Optional copy of the item.
Definition commit.h:151
CHANGE_TYPE m_type
Modification type.
Definition commit.h:152
EDA_ITEM * m_item
Main item that is added/deleted/modified.
Definition commit.h:150
BASE_SCREEN * m_screen
Definition commit.h:153
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...