KiCad PCB EDA Suite
commit.h
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) 2020-2021 KiCad Developers, see change_log.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#ifndef __COMMIT_H
29#define __COMMIT_H
30
31#include <set>
32#include <vector>
33#include <wx/string.h>
34#include <undo_redo_container.h>
35
36class EDA_ITEM;
37
44
49};
50
51template<typename T>
53{
54 return CHANGE_TYPE( (int) aTypeA | (int) aTypeB );
55}
56
57template<typename T>
59{
60 return CHANGE_TYPE( (int) aTypeA & (int) aTypeB );
61}
62
63
71class COMMIT
72{
73public:
74 COMMIT();
75 virtual ~COMMIT();
76
78 COMMIT& Add( EDA_ITEM* aItem )
79 {
80 return Stage( aItem, CHT_ADD );
81 }
82
85 {
86 return Stage( aItem, CHT_ADD | CHT_DONE );
87 }
88
91 {
92 return Stage( aItem, CHT_REMOVE );
93 }
94
97 {
98 return Stage( aItem, CHT_REMOVE | CHT_DONE );
99 }
100
104 {
105 return Stage( aItem, CHT_MODIFY );
106 }
107
110 COMMIT& Modified( EDA_ITEM* aItem, EDA_ITEM* aCopy )
111 {
112 return createModified( aItem, aCopy );
113 }
114
115 template<class Range>
116
117 COMMIT& StageItems( const Range& aRange, CHANGE_TYPE aChangeType )
118 {
119 for( const auto& item : aRange )
120 Stage( item, aChangeType );
121
122 return *this;
123 }
124
126 virtual COMMIT& Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType );
127
128 virtual COMMIT& Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType );
129
130 virtual COMMIT& Stage( const PICKED_ITEMS_LIST& aItems,
132
134 virtual void Push( const wxString& aMessage = wxT( "A commit" ), int aFlags = 0 ) = 0;
135
137 virtual void Revert() = 0;
138
139 bool Empty() const
140 {
141 return m_changes.empty();
142 }
143
145 int GetStatus( EDA_ITEM* aItem );
146
147protected:
149 {
153 };
154
155 // Should be called in Push() & Revert() methods
156 void clear()
157 {
158 m_changedItems.clear();
159 m_changes.clear();
160 }
161
162 COMMIT& createModified( EDA_ITEM* aItem, EDA_ITEM* aCopy, int aExtraFlags = 0 );
163
164 virtual void makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy = nullptr );
165
171 COMMIT_LINE* findEntry( EDA_ITEM* aItem );
172
173 virtual EDA_ITEM* parentObject( EDA_ITEM* aItem ) const = 0;
174
175 CHANGE_TYPE convert( UNDO_REDO aType ) const;
176
177 std::set<EDA_ITEM*> m_changedItems;
178 std::vector<COMMIT_LINE> m_changes;
179};
180
181#endif
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition: commit.h:72
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Definition: commit.cpp:48
COMMIT & Modify(EDA_ITEM *aItem)
Create an undo entry for an item that has been already modified.
Definition: commit.h:103
virtual void Revert()=0
COMMIT & StageItems(const Range &aRange, CHANGE_TYPE aChangeType)
Add a change of the item aItem of type aChangeType to the change list.
Definition: commit.h:117
COMMIT & Added(EDA_ITEM *aItem)
Remove a new item from the model.
Definition: commit.h:84
virtual void Push(const wxString &aMessage=wxT("A commit"), int aFlags=0)=0
Revert the commit by restoring the modified items state.
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy)
Definition: commit.h:110
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr)
Definition: commit.cpp:156
bool Empty() const
Returns status of an item.
Definition: commit.h:139
COMMIT & Removed(EDA_ITEM *aItem)
Modify a given item in the model.
Definition: commit.h:96
COMMIT()
Definition: commit.cpp:33
virtual ~COMMIT()
Add a new item to the model.
Definition: commit.cpp:38
COMMIT & Remove(EDA_ITEM *aItem)
Notify observers that aItem has been removed.
Definition: commit.h:90
int GetStatus(EDA_ITEM *aItem)
Definition: commit.cpp:131
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:177
COMMIT & createModified(EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0)
Definition: commit.cpp:139
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0
CHANGE_TYPE convert(UNDO_REDO aType) const
Definition: commit.cpp:192
void clear()
Definition: commit.h:156
COMMIT & Add(EDA_ITEM *aItem)
Notify observers that aItem has been added.
Definition: commit.h:78
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:178
COMMIT_LINE * findEntry(EDA_ITEM *aItem)
Search for an entry describing change for a particular item.
Definition: commit.cpp:180
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:85
A holder to handle information on schematic or board items.
CHANGE_TYPE operator|(CHANGE_TYPE aTypeA, T aTypeB)
Definition: commit.h:52
CHANGE_TYPE
Types of changes.
Definition: commit.h:39
@ CHT_MODIFY
Definition: commit.h:42
@ CHT_REMOVE
Definition: commit.h:41
@ CHT_DONE
Definition: commit.h:47
@ CHT_TYPE
Flag to indicate the change is already applied, just notify observers (not compatible with CHT_MODIFY...
Definition: commit.h:43
@ CHT_ADD
Definition: commit.h:40
@ CHT_FLAGS
Definition: commit.h:48
CHANGE_TYPE operator&(CHANGE_TYPE aTypeA, T aTypeB)
Definition: commit.h:58
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
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...