KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 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, see <https://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <set>
27#include <vector>
28#include <wx/string.h>
29#include <undo_redo_container.h>
30#include <kiid.h>
31#include <eda_item.h>
32
33class EDA_GROUP;
34class BASE_SCREEN;
35
46
47template<typename T>
49{
50 return CHANGE_TYPE( (int) aTypeA | (int) aTypeB );
51}
52
53template<typename T>
55{
56 return CHANGE_TYPE( (int) aTypeA & (int) aTypeB );
57}
58
59
67class COMMIT
68{
69public:
70 COMMIT();
71 virtual ~COMMIT();
72
74 COMMIT& Add( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
75 {
76 return Stage( aItem, CHT_ADD, aScreen );
77 }
78
80 COMMIT& Added( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
81 {
82 return Stage( aItem, CHT_ADD | CHT_DONE, aScreen );
83 }
84
86 COMMIT& Remove( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
87 {
88 return Stage( aItem, CHT_REMOVE, aScreen );
89 }
90
92 COMMIT& Removed( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
93 {
94 return Stage( aItem, CHT_REMOVE | CHT_DONE, aScreen );
95 }
96
102 COMMIT& Modify( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr,
104 {
105 return Stage( aItem, CHT_MODIFY, aScreen, aRecurse );
106 }
107
113 COMMIT& Modified( EDA_ITEM* aItem, EDA_ITEM* aCopy, BASE_SCREEN *aScreen = nullptr );
114
116 virtual COMMIT& Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType, BASE_SCREEN *aScreen = nullptr,
118
119 virtual COMMIT& Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType,
120 BASE_SCREEN *aScreen = nullptr );
121
122 virtual COMMIT& Stage( const PICKED_ITEMS_LIST& aItems, UNDO_REDO aModFlag = UNDO_REDO::UNSPECIFIED,
123 BASE_SCREEN *aScreen = nullptr );
124
125 void Unstage( EDA_ITEM* aItem, BASE_SCREEN* aScreen );
126 void Unmodify( EDA_ITEM* aItem, BASE_SCREEN* aScreen );
127
129 virtual void Push( const wxString& aMessage = wxT( "A commit" ), int aFlags = 0 ) = 0;
130
132 virtual void Revert() = 0;
133
140 virtual EDA_ITEM* ResolveItem( KIID& aID ) = 0;
141
142 bool Empty() const
143 {
144 return m_entries.empty();
145 }
146
148 int GetStatus( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr );
149
150 EDA_ITEM* GetFirst() const { return m_entries.empty() ? nullptr : m_entries[0].m_item; }
151
152protected:
160
162 void clear()
163 {
164 m_addedItems.clear();
165 m_changedItems.clear();
166 m_deletedItems.clear();
167 m_entries.clear();
168 }
169
170 virtual void makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy = nullptr,
171 BASE_SCREEN *aScreen = nullptr );
172
178 COMMIT_LINE* findEntry( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr );
179
180 virtual EDA_ITEM* undoLevelItem( EDA_ITEM* aItem ) const = 0;
181
182 virtual EDA_ITEM* makeImage( EDA_ITEM* aItem ) const = 0;
183
184 CHANGE_TYPE convert( UNDO_REDO aType ) const;
185 UNDO_REDO convert( CHANGE_TYPE aType ) const;
186
187protected:
188 std::set<std::pair<EDA_ITEM*, BASE_SCREEN*>> m_addedItems;
189 std::set<std::pair<EDA_ITEM*, BASE_SCREEN*>> m_changedItems;
190 std::set<std::pair<EDA_ITEM*, BASE_SCREEN*>> m_deletedItems;
191 std::vector<COMMIT_LINE> m_entries;
192};
Handles how to draw a screen (a board, a schematic ...)
Definition base_screen.h:37
Represent a set of changes (additions, deletions or modifications) of a data model (e....
Definition commit.h:68
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition commit.h:86
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_deletedItems
Definition commit.h:190
virtual EDA_ITEM * undoLevelItem(EDA_ITEM *aItem) const =0
virtual void Revert()=0
Revert the commit by restoring the modified items state.
COMMIT & Added(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Notify observers that aItem has been added.
Definition commit.h:80
void Unstage(EDA_ITEM *aItem, BASE_SCREEN *aScreen)
Definition commit.cpp:177
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:221
virtual void Push(const wxString &aMessage=wxT("A commit"), int aFlags=0)=0
Execute the changes.
virtual EDA_ITEM * ResolveItem(KIID &aID)=0
Search for an item in this commit that matches the provided KIID.
void Unmodify(EDA_ITEM *aItem, BASE_SCREEN *aScreen)
Definition commit.cpp:197
bool Empty() const
Definition commit.h:142
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:102
COMMIT()
Definition commit.cpp:30
virtual ~COMMIT()
Definition commit.cpp:35
COMMIT_LINE * findEntry(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Search for an entry describing change for a particular item.
Definition commit.cpp:264
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_addedItems
Definition commit.h:188
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr, BASE_SCREEN *aScreen=nullptr)
Definition commit.cpp:240
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
std::vector< COMMIT_LINE > m_entries
Definition commit.h:191
EDA_ITEM * GetFirst() const
Definition commit.h:150
std::set< std::pair< EDA_ITEM *, BASE_SCREEN * > > m_changedItems
Definition commit.h:189
COMMIT & Removed(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Definition commit.h:92
virtual EDA_ITEM * makeImage(EDA_ITEM *aItem) const =0
int GetStatus(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Returns status of an item.
Definition commit.cpp:232
void clear()
Should be called in Push() & Revert() methods.
Definition commit.h:162
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:42
A set of EDA_ITEMs (i.e., without duplicates).
Definition eda_group.h:43
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
Definition kiid.h:46
A holder to handle information on schematic or board items.
CHANGE_TYPE operator|(CHANGE_TYPE aTypeA, T aTypeB)
Definition commit.h:48
CHANGE_TYPE
Types of changes.
Definition commit.h:37
@ CHT_MODIFY
Definition commit.h:40
@ CHT_REMOVE
Definition commit.h:39
@ CHT_DONE
Flag to indicate the change is already applied.
Definition commit.h:43
@ CHT_TYPE
Definition commit.h:41
@ CHT_ADD
Definition commit.h:38
@ CHT_FLAGS
Definition commit.h:44
CHANGE_TYPE operator&(CHANGE_TYPE aTypeA, T aTypeB)
Definition commit.h:54
RECURSE_MODE
Definition eda_item.h:50
@ NO_RECURSE
Definition eda_item.h:52
EDA_ITEM * m_copy
Optional copy of the item.
Definition commit.h:156
CHANGE_TYPE m_type
Modification type.
Definition commit.h:157
EDA_ITEM * m_item
Main item that is added/deleted/modified.
Definition commit.h:155
BASE_SCREEN * m_screen
Definition commit.h:158
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...