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 33 of file commit.cpp.

34 {
35 }

◆ ~COMMIT()

COMMIT::~COMMIT ( )
virtual

Add a new item to the model.

Definition at line 38 of file commit.cpp.

39 {
40  for( COMMIT_LINE& ent : m_changes )
41  {
42  if( ent.m_copy )
43  delete ent.m_copy;
44  }
45 }
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 192 of file commit.cpp.

193 {
194  switch( aType )
195  {
196  case UNDO_REDO::NEWITEM:
197  return CHT_ADD;
198 
199  case UNDO_REDO::DELETED:
200  return CHT_REMOVE;
201 
202  default:
203  assert( false );
205 
206  case UNDO_REDO::CHANGED:
207  return CHT_MODIFY;
208  }
209 }
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 139 of file commit.cpp.

140 {
141  EDA_ITEM* parent = parentObject( aItem );
142  auto entryIt = m_changedItems.find( parent );
143 
144  if( entryIt != m_changedItems.end() )
145  {
146  delete aCopy;
147  return *this; // item has been already modified once
148  }
149 
150  makeEntry( parent, CHT_MODIFY | aExtraFlags, aCopy );
151 
152  return *this;
153 }
virtual void makeEntry(EDA_ITEM *aItem, CHANGE_TYPE aType, EDA_ITEM *aCopy=nullptr)
Definition: commit.cpp:156
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:99
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 180 of file commit.cpp.

181 {
182  for( COMMIT_LINE& change : m_changes )
183  {
184  if( change.m_item == aItem )
185  return &change;
186  }
187 
188  return nullptr;
189 }
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 131 of file commit.cpp.

132 {
133  COMMIT_LINE* entry = findEntry( parentObject( aItem ) );
134 
135  return entry ? entry->m_type : 0;
136 }
COMMIT_LINE * findEntry(EDA_ITEM *aItem)
Search for an entry describing change for a particular item.
Definition: commit.cpp:180
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 156 of file commit.cpp.

157 {
158  // Expect an item copy if it is going to be modified
159  wxASSERT( !!aCopy == ( ( aType & CHT_TYPE ) == CHT_MODIFY ) );
160 
161  if( m_changedItems.find( aItem ) != m_changedItems.end() )
162  {
163  alg::delete_if( m_changes, [aItem]( const COMMIT_LINE& aEnt )
164  {
165  return aEnt.m_item == aItem;
166  } );
167  }
168 
169  COMMIT_LINE ent;
170 
171  ent.m_item = aItem;
172  ent.m_type = aType;
173  ent.m_copy = aCopy;
174 
175  m_changedItems.insert( aItem );
176  m_changes.push_back( ent );
177 }
Flag to indicate the change is already applied, just notify observers (not compatible with CHT_MODIFY...
Definition: commit.h:43
std::set< EDA_ITEM * > m_changedItems
Definition: commit.h:178
std::vector< COMMIT_LINE > m_changes
Definition: commit.h:179
void delete_if(_Container &__c, _Function &&__f)
Deletes all values from __c for which __f returns true.
Definition: kicad_algo.h:173

References CHT_MODIFY, CHT_TYPE, alg::delete_if(), 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:139

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_UNUSED_PAD_LAYERS::TransferDataFromWindow(), DIALOG_TRACK_VIA_PROPERTIES::TransferDataFromWindow(), DIALOG_DIMENSION_PROPERTIES::TransferDataFromWindow(), DIALOG_GROUP_PROPERTIES::TransferDataFromWindow(), DIALOG_TARGET_PROPERTIES::TransferDataFromWindow(), DIALOG_FOOTPRINT_PROPERTIES_FP_EDITOR::TransferDataFromWindow(), DIALOG_TEXT_PROPERTIES::TransferDataFromWindow(), DIALOG_FOOTPRINT_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_UNUSED_PAD_LAYERS::TransferDataFromWindow(), and DIALOG_TRACK_VIA_PROPERTIES::TransferDataFromWindow().

◆ Remove()

◆ Removed()

COMMIT& COMMIT::Removed ( EDA_ITEM aItem)
inline

Modify a given item in the model.

Must be called before modification is performed.

Definition at line 96 of file commit.h.

References CHT_DONE, CHT_REMOVE, and Stage().

Referenced by TRACKS_CLEANER::deleteDanglingTracks(), TRACKS_CLEANER::mergeCollinearSegments(), TRACKS_CLEANER::removeItems(), PCB_EDIT_FRAME::RunActionPlugin(), and BOARD_NETLIST_UPDATER::UpdateNetlist().

◆ 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 48 of file commit.cpp.

49 {
50  // CHT_MODIFY and CHT_DONE are not compatible
51  assert( ( aChangeType & ( CHT_MODIFY | CHT_DONE ) ) != ( CHT_MODIFY | CHT_DONE ) );
52 
53  int flag = aChangeType & CHT_FLAGS;
54 
55  switch( aChangeType & CHT_TYPE )
56  {
57  case CHT_ADD:
58  assert( m_changedItems.find( aItem ) == m_changedItems.end() );
59  makeEntry( aItem, CHT_ADD | flag );
60  return *this;
61 
62  case CHT_REMOVE:
63  makeEntry( aItem, CHT_REMOVE | flag );
64  return *this;
65 
66  case CHT_MODIFY:
67  {
68  EDA_ITEM* parent = parentObject( aItem );
69  EDA_ITEM* clone = nullptr;
70 
71  assert( parent );
72 
73  if( parent )
74  clone = parent->Clone();
75 
76  assert( clone );
77 
78  if( clone )
79  return createModified( parent, clone, flag );
80 
81  break;
82  }
83 
84  default:
85  assert( false );
86  }
87 
88  return *this;
89 }
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:156
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:139
A base class for most all the KiCad significant classes used in schematics and boards.
Definition: eda_item.h:99
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 92 of file commit.cpp.

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

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 103 of file commit.cpp.

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

References CHANGED, convert(), copy, PICKED_ITEMS_LIST::GetCount(), PICKED_ITEMS_LIST::GetPickedItem(), PICKED_ITEMS_LIST::GetPickedItemLink(), PICKED_ITEMS_LIST::GetPickedItemStatus(), Modified(), 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: