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
35{
36}
37
38
40{
41 for( COMMIT_LINE& ent : m_changes )
42 delete ent.m_copy;
43}
44
45
46COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType, BASE_SCREEN* aScreen,
47 RECURSE_MODE aRecurse )
48{
49 // CHT_MODIFY and CHT_DONE are not compatible
50 wxASSERT( ( 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 makeEntry( aItem, CHT_ADD | flag, nullptr, aScreen );
58 break;
59
60 case CHT_REMOVE:
61 if( m_deletedItems.insert( aItem ).second )
62 {
63 makeEntry( aItem, CHT_REMOVE | flag, makeImage( aItem ), aScreen );
64
65 if( EDA_GROUP* parentGroup = aItem->GetParentGroup() )
66 Modify( parentGroup->AsEdaItem(), aScreen, RECURSE_MODE::NO_RECURSE );
67 }
68
69 break;
70
71 case CHT_MODIFY:
72 if( EDA_ITEM* parent = parentObject( aItem ) )
73 createModified( parent, makeImage( parent ), flag, aScreen );
74
75 break;
76
77 default:
78 wxFAIL;
79 }
80
81 return *this;
82}
83
84
85COMMIT& COMMIT::Stage( std::vector<EDA_ITEM*> &container, CHANGE_TYPE aChangeType,
86 BASE_SCREEN *aScreen )
87{
88 for( EDA_ITEM* item : container )
89 Stage( item, aChangeType, aScreen);
90
91 return *this;
92}
93
94
95COMMIT& COMMIT::Stage( const PICKED_ITEMS_LIST &aItems, UNDO_REDO aModFlag, BASE_SCREEN *aScreen )
96{
97 for( unsigned int i = 0; i < aItems.GetCount(); i++ )
98 {
99 UNDO_REDO change_type = aItems.GetPickedItemStatus( i );
100 EDA_ITEM* item = aItems.GetPickedItem( i );
101
102 if( change_type == UNDO_REDO::UNSPECIFIED )
103 change_type = aModFlag;
104
105 if( EDA_ITEM* copy = aItems.GetPickedItemLink( i ) )
106 {
107 assert( change_type == UNDO_REDO::CHANGED );
108
109 // There was already a copy created, so use it
110 Modified( item, copy, aScreen );
111 }
112 else
113 {
114 Stage( item, convert( change_type ), aScreen );
115 }
116 }
117
118 return *this;
119}
120
121
122void COMMIT::Unstage( EDA_ITEM* aItem, BASE_SCREEN* aScreen )
123{
124 std::erase_if( m_changes,
125 [&]( COMMIT_LINE& line )
126 {
127 if( line.m_item == aItem && line.m_screen == aScreen )
128 {
129 // Only new items which have never been committed can be unstaged
130 wxASSERT( line.m_item->IsNew() );
131
132 delete line.m_item;
133 delete line.m_copy;
134 return true;
135 }
136
137 return false;
138 } );
139}
140
141
143{
144 COMMIT_LINE* entry = findEntry( parentObject( aItem ), aScreen );
145
146 return entry ? entry->m_type : 0;
147}
148
149
150COMMIT& COMMIT::createModified( EDA_ITEM* aItem, EDA_ITEM* aCopy, int aExtraFlags,
151 BASE_SCREEN* aScreen )
152{
153 EDA_ITEM* parent = parentObject( aItem );
154
155 if( m_changedItems.find( parent ) != 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, aScreen );
162
163 return *this;
164}
165
166
167void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy, BASE_SCREEN *aScreen )
168{
169 COMMIT_LINE ent;
170
171 ent.m_item = aItem;
172 ent.m_type = aType;
173 ent.m_copy = aCopy;
174 ent.m_screen = aScreen;
175
176 // N.B. Do not throw an assertion for multiple changed items. An item can be changed
177 // multiple times in a single commit such as when importing graphics and grouping them.
178
179 m_changedItems.insert( aItem );
180 m_changes.push_back( ent );
181}
182
183
185{
186 for( COMMIT_LINE& change : m_changes )
187 {
188 if( change.m_item == aItem && change.m_screen == aScreen )
189 return &change;
190 }
191
192 return nullptr;
193}
194
195
197{
198 switch( aType )
199 {
200 case UNDO_REDO::NEWITEM: return CHT_ADD;
201 case UNDO_REDO::DELETED: return CHT_REMOVE;
202 case UNDO_REDO::CHANGED: return CHT_MODIFY;
203 default: wxASSERT( false ); return CHT_MODIFY;
204 }
205}
206
207
209{
210 switch( aType )
211 {
212 case CHT_ADD: return UNDO_REDO::NEWITEM;
213 case CHT_REMOVE: return UNDO_REDO::DELETED;
214 case CHT_MODIFY: return UNDO_REDO::CHANGED;
215 default: wxASSERT( false ); return UNDO_REDO::CHANGED;
216 }
217}
218
Handles how to draw a screen (a board, a schematic ...)
Definition: base_screen.h:41
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:73
COMMIT & createModified(EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0, BASE_SCREEN *aScreen=nullptr)
Definition: commit.cpp:150
void Unstage(EDA_ITEM *aItem, BASE_SCREEN *aScreen)
Definition: commit.cpp:122
std::set< EDA_ITEM * > m_deletedItems
Definition: commit.h:202
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.h:118
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:107
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:184
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr, BASE_SCREEN *aScreen=nullptr)
Definition: commit.cpp:167
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:201
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0
virtual EDA_ITEM * makeImage(EDA_ITEM *aItem) const =0
CHANGE_TYPE convert(UNDO_REDO aType) const
Definition: commit.cpp:196
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition: commit.cpp:142
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:203
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:98
virtual EDA_GROUP * GetParentGroup() const
Definition: eda_item.h:116
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:42
@ CHT_MODIFY
Definition: commit.h:45
@ CHT_REMOVE
Definition: commit.h:44
@ CHT_DONE
Flag to indicate the change is already applied.
Definition: commit.h:48
@ CHT_TYPE
Definition: commit.h:46
@ CHT_ADD
Definition: commit.h:43
@ CHT_FLAGS
Definition: commit.h:49
RECURSE_MODE
Definition: eda_item.h:50
This file contains miscellaneous commonly used macros and functions.
EDA_ITEM * m_copy
Optional copy of the item.
Definition: commit.h:166
CHANGE_TYPE m_type
Modification type.
Definition: commit.h:167
EDA_ITEM * m_item
Main item that is added/deleted/modified.
Definition: commit.h:165
BASE_SCREEN * m_screen
Definition: commit.h:169
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...