KiCad PCB EDA Suite
COMMIT Class Referenceabstract

Represent a set of changes (additions, deletions or modifications) of a data model (e.g. More...

#include <commit.h>

Inheritance diagram for COMMIT:
BOARD_COMMIT

Classes

struct  COMMIT_LINE
 

Public Member Functions

 COMMIT ()
 
virtual ~COMMIT ()
 Add a new item to the model. More...
 
COMMITAdd (EDA_ITEM *aItem)
 Notify observers that aItem has been added. More...
 
COMMITAdded (EDA_ITEM *aItem)
 Remove a new item from the model. More...
 
COMMITRemove (EDA_ITEM *aItem)
 Notify observers that aItem has been removed. More...
 
COMMITRemoved (EDA_ITEM *aItem)
 Modify a given item in the model. More...
 
COMMITModify (EDA_ITEM *aItem)
 Create an undo entry for an item that has been already modified. More...
 
COMMITModified (EDA_ITEM *aItem, EDA_ITEM *aCopy)
 
template<class Range >
COMMITStageItems (const Range &aRange, CHANGE_TYPE aChangeType)
 Add a change of the item aItem of type aChangeType to the change list. More...
 
virtual COMMITStage (EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
 
virtual COMMITStage (std::vector< EDA_ITEM * > &container, CHANGE_TYPE aChangeType)
 
virtual COMMITStage (const PICKED_ITEMS_LIST &aItems, UNDO_REDO aModFlag=UNDO_REDO::UNSPECIFIED)
 Execute the changes. More...
 
virtual void Push (const wxString &aMessage=wxT("A commit"), bool aCreateUndoEntry=true, bool aSetDirtyBit=true)=0
 Revert the commit by restoring the modified items state. More...
 
virtual void Revert ()=0
 
bool Empty () const
 Returns status of an item. More...
 
int GetStatus (EDA_ITEM *aItem)
 

Protected Member Functions

void clear ()
 
COMMITcreateModified (EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0)
 
virtual void makeEntry (EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr)
 
COMMIT_LINEfindEntry (EDA_ITEM *aItem)
 Search for an entry describing change for a particular item. More...
 
virtual EDA_ITEMparentObject (EDA_ITEM *aItem) const =0
 
CHANGE_TYPE convert (UNDO_REDO aType) const
 

Protected Attributes

std::set< EDA_ITEM * > m_changedItems
 
std::vector< COMMIT_LINEm_changes
 

Detailed Description

Represent a set of changes (additions, deletions or modifications) of a data model (e.g.

the BOARD) class.

The class can be used to propagate changes to subscribed objects (e.g. views, ratsnest), and automatically create undo/redo points.

Definition at line 71 of file commit.h.

Constructor & Destructor Documentation

◆ COMMIT()

COMMIT::COMMIT ( )

Definition at line 32 of file commit.cpp.

33 {
34 }

◆ ~COMMIT()

COMMIT::~COMMIT ( )
virtual

Add a new item to the model.

Definition at line 37 of file commit.cpp.

38 {
39  for( COMMIT_LINE& ent : m_changes )
40  {
41  if( ent.m_copy )
42  delete ent.m_copy;
43  }
44 }
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:179

References m_changes.

Member Function Documentation

◆ Add()

◆ Added()

COMMIT& COMMIT::Added ( EDA_ITEM aItem)
inline

Remove a new item from the model.

Definition at line 84 of file commit.h.

References CHT_ADD, CHT_DONE, and Stage().

Referenced by PCB_EDIT_FRAME::RunActionPlugin().

◆ clear()

void COMMIT::clear ( )
inlineprotected

Definition at line 157 of file commit.h.

158  {
159  m_changedItems.clear();
160  m_changes.clear();
161  }
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:178
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:179

References m_changedItems, and m_changes.

Referenced by BOARD_COMMIT::Push(), and BOARD_COMMIT::Revert().

◆ convert()

CHANGE_TYPE COMMIT::convert ( UNDO_REDO  aType) const
protected

Definition at line 200 of file commit.cpp.

201 {
202  switch( aType )
203  {
204  case UNDO_REDO::NEWITEM:
205  return CHT_ADD;
206 
207  case UNDO_REDO::DELETED:
208  return CHT_REMOVE;
209 
210  default:
211  assert( false );
213 
214  case UNDO_REDO::CHANGED:
215  return CHT_MODIFY;
216  }
217 }
Definition: commit.h:40
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition: macros.h:83

References CHANGED, CHT_ADD, CHT_MODIFY, CHT_REMOVE, DELETED, KI_FALLTHROUGH, and NEWITEM.

Referenced by Stage().

◆ createModified()

COMMIT & COMMIT::createModified ( EDA_ITEM aItem,
EDA_ITEM aCopy,
int  aExtraFlags = 0 
)
protected

Definition at line 148 of file commit.cpp.

149 {
150  EDA_ITEM* parent = parentObject( aItem );
151  auto entryIt = m_changedItems.find( parent );
152 
153  if( entryIt != m_changedItems.end() )
154  {
155  delete aCopy;
156  return *this; // item has been already modified once
157  }
158 
159  makeEntry( parent, CHT_MODIFY | aExtraFlags, aCopy );
160 
161  return *this;
162 }
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr)
Definition: commit.cpp:165
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:178
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0

References CHT_MODIFY, m_changedItems, makeEntry(), and parentObject().

Referenced by Modified(), and Stage().

◆ Empty()

bool COMMIT::Empty ( ) const
inline

Returns status of an item.

Definition at line 140 of file commit.h.

References m_changes.

Referenced by DIALOG_CLEANUP_GRAPHICS::doCleanup(), DIALOG_CLEANUP_TRACKS_AND_VIAS::doCleanup(), and BOARD_COMMIT::Push().

◆ findEntry()

COMMIT::COMMIT_LINE * COMMIT::findEntry ( EDA_ITEM aItem)
protected

Search for an entry describing change for a particular item.

Returns
null if there is no related entry.

Definition at line 188 of file commit.cpp.

189 {
190  for( COMMIT_LINE& change : m_changes )
191  {
192  if( change.m_item == aItem )
193  return &change;
194  }
195 
196  return nullptr;
197 }
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:179

References m_changes.

Referenced by GetStatus().

◆ GetStatus()

int COMMIT::GetStatus ( EDA_ITEM aItem)

Definition at line 130 of file commit.cpp.

131 {
132  COMMIT_LINE* entry = findEntry( parentObject( aItem ) );
133 
134  return entry ? entry->m_type : 0;
135 }
COMMIT_LINE * findEntry(EDA_ITEM *aItem)
Search for an entry describing change for a particular item.
Definition: commit.cpp:188
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0

References findEntry(), COMMIT::COMMIT_LINE::m_type, and parentObject().

Referenced by BOARD_NETLIST_UPDATER::updateComponentPadConnections(), and BOARD_NETLIST_UPDATER::updateFootprintParameters().

◆ makeEntry()

void COMMIT::makeEntry ( EDA_ITEM aItem,
CHANGE_TYPE  aType,
EDA_ITEM aCopy = nullptr 
)
protectedvirtual

Definition at line 165 of file commit.cpp.

166 {
167  // Expect an item copy if it is going to be modified
168  wxASSERT( !!aCopy == ( ( aType & CHT_TYPE ) == CHT_MODIFY ) );
169 
170  if( m_changedItems.find( aItem ) != m_changedItems.end() )
171  {
172  eraseIf( m_changes, [aItem] ( const COMMIT_LINE& aEnt ) {
173  return aEnt.m_item == aItem;
174  } );
175  }
176 
177  COMMIT_LINE ent;
178 
179  ent.m_item = aItem;
180  ent.m_type = aType;
181  ent.m_copy = aCopy;
182 
183  m_changedItems.insert( aItem );
184  m_changes.push_back( ent );
185 }
Flag to indicate the change is already applied, just notify observers (not compatible with CHT_MODIFY...
Definition: commit.h:43
void eraseIf(Container &c, F &&f)
Definition: commit.cpp:139
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:178
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:179

References CHT_MODIFY, CHT_TYPE, eraseIf(), m_changedItems, m_changes, COMMIT::COMMIT_LINE::m_copy, COMMIT::COMMIT_LINE::m_item, and COMMIT::COMMIT_LINE::m_type.

Referenced by createModified(), and Stage().

◆ Modified()

COMMIT& COMMIT::Modified ( EDA_ITEM aItem,
EDA_ITEM aCopy 
)
inline

Definition at line 110 of file commit.h.

111  {
112  return createModified( aItem, aCopy );
113  }
COMMIT & createModified(EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0)
Definition: commit.cpp:148

References createModified().

Referenced by Stage(), BOARD_NETLIST_UPDATER::updateComponentPadConnections(), and BOARD_NETLIST_UPDATER::updateFootprintParameters().

◆ Modify()

COMMIT& COMMIT::Modify ( EDA_ITEM aItem)
inline

Create an undo entry for an item that has been already modified.

Requires a copy done before the modification.

Definition at line 103 of file commit.h.

References CHT_MODIFY, and Stage().

Referenced by PCB_POINT_EDITOR::addCorner(), AR_AUTOPLACER::AutoplaceFootprints(), doPushPadProperties(), DRAWING_TOOL::DrawVia(), PAD_TOOL::EnumeratePads(), PAD_TOOL::explodePad(), ZONE_FILLER::Fill(), ARRAY_CREATOR::Invoke(), TRACKS_CLEANER::mergeCollinearSegments(), mergeZones(), BOARD_EDITOR_CONTROL::modifyLockSelected(), FOOTPRINT_EDIT_FRAME::OnEditItemRequest(), PAD_TOOL::pastePadProperties(), DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS::processItem(), CN_CONNECTIVITY_ALGO::propagateConnections(), DIALOG_BOARD_REANNOTATE::ReannotateBoard(), PAD_TOOL::recombinePad(), PCB_POINT_EDITOR::removeCorner(), GROUP_TOOL::RemoveFromGroup(), DRAWING_TOOL::SetAnchor(), BOARD_EDITOR_CONTROL::TrackWidthDec(), BOARD_EDITOR_CONTROL::TrackWidthInc(), DIALOG_TRACK_VIA_PROPERTIES::TransferDataFromWindow(), DIALOG_UNUSED_PAD_LAYERS::TransferDataFromWindow(), DIALOG_DIMENSION_PROPERTIES::TransferDataFromWindow(), DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::TransferDataFromWindow(), DIALOG_GROUP_PROPERTIES::TransferDataFromWindow(), DIALOG_TARGET_PROPERTIES::TransferDataFromWindow(), DIALOG_FOOTPRINT_PROPERTIES::TransferDataFromWindow(), DIALOG_TEXT_PROPERTIES::TransferDataFromWindow(), DIALOG_GRAPHIC_ITEM_PROPERTIES::TransferDataFromWindow(), DIALOG_PAD_PROPERTIES::TransferDataFromWindow(), BOARD_NETLIST_UPDATER::updateCopperZoneNets(), BOARD_EDITOR_CONTROL::ViaSizeDec(), BOARD_EDITOR_CONTROL::ViaSizeInc(), ZONE_FILLER_TOOL::ZoneUnfill(), and ZONE_FILLER_TOOL::ZoneUnfillAll().

◆ parentObject()

virtual EDA_ITEM* COMMIT::parentObject ( EDA_ITEM aItem) const
protectedpure virtual

Implemented in BOARD_COMMIT.

Referenced by createModified(), GetStatus(), and Stage().

◆ Push()

virtual void COMMIT::Push ( const wxString &  aMessage = wxT("A commit"),
bool  aCreateUndoEntry = true,
bool  aSetDirtyBit = true 
)
pure virtual

Revert the commit by restoring the modified items state.

Implemented in BOARD_COMMIT.

Referenced by DIALOG_TRACK_VIA_PROPERTIES::TransferDataFromWindow(), and DIALOG_UNUSED_PAD_LAYERS::TransferDataFromWindow().

◆ Remove()

◆ Removed()

COMMIT& COMMIT::Removed ( EDA_ITEM aItem)
inline

◆ Revert()

virtual void COMMIT::Revert ( )
pure virtual

Implemented in BOARD_COMMIT.

◆ Stage() [1/3]

COMMIT & COMMIT::Stage ( EDA_ITEM aItem,
CHANGE_TYPE  aChangeType 
)
virtual

Reimplemented in BOARD_COMMIT.

Definition at line 47 of file commit.cpp.

48 {
49  // CHT_MODIFY and CHT_DONE are not compatible
50  assert( ( 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  assert( m_changedItems.find( aItem ) == m_changedItems.end() );
58  makeEntry( aItem, CHT_ADD | flag );
59  return *this;
60 
61  case CHT_REMOVE:
62  makeEntry( aItem, CHT_REMOVE | flag );
63  return *this;
64 
65  case CHT_MODIFY:
66  {
67  EDA_ITEM* parent = parentObject( aItem );
68  EDA_ITEM* clone = nullptr;
69 
70  assert( parent );
71 
72  if( parent )
73  clone = parent->Clone();
74 
75  assert( clone );
76 
77  if( clone )
78  return createModified( parent, clone, flag );
79 
80  break;
81  }
82 
83  default:
84  assert( false );
85  }
86 
87  return *this;
88 }
Flag to indicate the change is already applied, just notify observers (not compatible with CHT_MODIFY...
Definition: commit.h:43
Definition: commit.h:40
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr)
Definition: commit.cpp:165
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:83
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:178
COMMIT & createModified(EDA_ITEM *aItem, EDA_ITEM *aCopy, int aExtraFlags=0)
Definition: commit.cpp:148
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100
virtual EDA_ITEM * parentObject(EDA_ITEM *aItem) const =0

References CHT_ADD, CHT_DONE, CHT_FLAGS, CHT_MODIFY, CHT_REMOVE, CHT_TYPE, EDA_ITEM::Clone(), createModified(), flag, m_changedItems, makeEntry(), and parentObject().

Referenced by Add(), Added(), Modify(), Remove(), Removed(), BOARD_COMMIT::Stage(), Stage(), and StageItems().

◆ Stage() [2/3]

COMMIT & COMMIT::Stage ( std::vector< EDA_ITEM * > &  container,
CHANGE_TYPE  aChangeType 
)
virtual

Reimplemented in BOARD_COMMIT.

Definition at line 91 of file commit.cpp.

92 {
93  for( EDA_ITEM* item : container )
94  {
95  Stage( item, aChangeType );
96  }
97 
98  return *this;
99 }
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Definition: commit.cpp:47
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:100

References Stage().

◆ Stage() [3/3]

COMMIT & COMMIT::Stage ( const PICKED_ITEMS_LIST aItems,
UNDO_REDO  aModFlag = UNDO_REDO::UNSPECIFIED 
)
virtual

Execute the changes.

Reimplemented in BOARD_COMMIT.

Definition at line 102 of file commit.cpp.

103 {
104  for( unsigned int i = 0; i < aItems.GetCount(); i++ )
105  {
106  UNDO_REDO change_type = aItems.GetPickedItemStatus( i );
107  EDA_ITEM* item = aItems.GetPickedItem( i );
108  EDA_ITEM* copy = NULL;
109 
110  if( change_type == UNDO_REDO::UNSPECIFIED )
111  change_type = aModFlag;
112 
113  if( ( copy = aItems.GetPickedItemLink( i ) ) )
114  {
115  assert( change_type == UNDO_REDO::CHANGED );
116 
117  // There was already a copy created, so use it
118  Modified( item, copy );
119  }
120  else
121  {
122  Stage( item, convert( change_type ) );
123  }
124  }
125 
126  return *this;
127 }
EDA_ITEM * GetPickedItemLink(unsigned int aIdx) const
virtual COMMIT & Stage(EDA_ITEM *aItem, CHANGE_TYPE aChangeType)
Definition: commit.cpp:47
UNDO_REDO
Undo Redo considerations: Basically we have 3 cases New item Deleted item Modified item there is also...
unsigned GetCount() const
#define NULL
EDA_ITEM * GetPickedItem(unsigned int aIdx) const
COMMIT & Modified(EDA_ITEM *aItem, EDA_ITEM *aCopy)
Definition: commit.h:110
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
CHANGE_TYPE convert(UNDO_REDO aType) const
Definition: commit.cpp:200

References CHANGED, convert(), copy, PICKED_ITEMS_LIST::GetCount(), PICKED_ITEMS_LIST::GetPickedItem(), PICKED_ITEMS_LIST::GetPickedItemLink(), PICKED_ITEMS_LIST::GetPickedItemStatus(), Modified(), NULL, Stage(), and UNSPECIFIED.

◆ StageItems()

template<class Range >
COMMIT& COMMIT::StageItems ( const Range &  aRange,
CHANGE_TYPE  aChangeType 
)
inline

Add a change of the item aItem of type aChangeType to the change list.

Definition at line 117 of file commit.h.

References Stage().

Referenced by PCB_POINT_EDITOR::OnSelectionChange().

Member Data Documentation

◆ m_changedItems

std::set<EDA_ITEM*> COMMIT::m_changedItems
protected

Definition at line 178 of file commit.h.

Referenced by clear(), createModified(), makeEntry(), and Stage().

◆ m_changes

std::vector<COMMIT_LINE> COMMIT::m_changes
protected

The documentation for this class was generated from the following files: