From b8350f037bd44ddf9c269f0a6fac3118234d9a73 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Thu, 9 Jun 2016 14:28:54 +0200 Subject: [PATCH] COMMIT: Added an interface to store items that already have a copy created. --- common/commit.cpp | 108 +++++++++++++++++++++++++++++++++++++--------- common/commit.h | 21 ++++++--- 2 files changed, 102 insertions(+), 27 deletions(-) diff --git a/common/commit.cpp b/common/commit.cpp index 938f773ed1..689d852e10 100644 --- a/common/commit.cpp +++ b/common/commit.cpp @@ -23,8 +23,7 @@ */ #include -#include - +#include COMMIT::COMMIT() { @@ -45,30 +44,17 @@ COMMIT::~COMMIT() } -void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE type, bool saveCopy ) -{ - COMMIT_LINE ent; - - ent.m_item = aItem; - ent.m_type = type; - ent.m_copy = saveCopy ? (EDA_ITEM*) aItem->Clone() : NULL; - - m_changedItems.insert( aItem ); - m_changes.push_back( ent ); -} - - COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType ) { switch( aChangeType ) { case CHT_ADD: assert( m_changedItems.find( aItem ) == m_changedItems.end() ); - makeEntry( aItem, CHT_ADD, false ); + makeEntry( aItem, CHT_ADD ); return *this; case CHT_REMOVE: - makeEntry( aItem, CHT_REMOVE, false ); + makeEntry( aItem, CHT_REMOVE ); return *this; case CHT_MODIFY: @@ -78,13 +64,29 @@ COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType ) if( m_changedItems.find( parent ) != m_changedItems.end() ) return *this; // item already modifed once - makeEntry( parent, CHT_MODIFY, true ); + makeEntry( parent, CHT_MODIFY, parent->Clone() ); + return *this; } default: assert( false ); } + + return *this; +} + + +COMMIT& COMMIT::Stage( EDA_ITEM* aItem, EDA_ITEM* aCopy ) +{ + EDA_ITEM* parent = parentObject( aItem ); + + if( m_changedItems.find( parent ) != m_changedItems.end() ) + return *this; // item already modifed once + + makeEntry( parent, CHT_MODIFY, aCopy ); + + return *this; } @@ -99,9 +101,75 @@ COMMIT& COMMIT::Stage( std::vector& container, CHANGE_TYPE aChangeTyp } -void COMMIT::Stage( const PICKED_ITEMS_LIST& aItems, CHANGE_TYPE aChangeType ) +COMMIT& COMMIT::Stage( const PICKED_ITEMS_LIST& aItems, UNDO_REDO_T aModFlag ) { for( unsigned int i = 0; i < aItems.GetCount(); i++ ) - Stage( aItems.GetPickedItem( i ), aChangeType ); + { + UNDO_REDO_T change_type = aItems.GetPickedItemStatus( i ); + EDA_ITEM* item = aItems.GetPickedItem( i ); + EDA_ITEM* copy = NULL; + + if( change_type == UR_UNSPECIFIED ) + change_type = aItems.m_Status; + + if( change_type == UR_UNSPECIFIED ) + change_type = aModFlag; + + if( ( copy = aItems.GetPickedItemLink( i ) ) ) + { + assert( change_type == UR_CHANGED ); + + // There was already a copy created, so use it + Stage( item, copy ); + } + else + { + Stage( item, convert( change_type ) ); + } + } + + return *this; +} + + +void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy ) +{ + // Expect an item copy if it is going to be modified + assert( !!aCopy == ( aType == CHT_MODIFY ) ); + + COMMIT_LINE ent; + + ent.m_item = aItem; + ent.m_type = aType; + ent.m_copy = aCopy; + + m_changedItems.insert( aItem ); + m_changes.push_back( ent ); +} + + +COMMIT::CHANGE_TYPE COMMIT::convert( UNDO_REDO_T aType ) const +{ + switch( aType ) + { + case UR_NEW: + return CHT_ADD; + + case UR_DELETED: + return CHT_REMOVE; + + default: + assert( false ); + // fall through + + case UR_CHANGED: + case UR_MOVED: + case UR_MIRRORED_X: + case UR_MIRRORED_Y: + case UR_ROTATED: + case UR_ROTATED_CLOCKWISE: + case UR_FLIPPED: + return CHT_MODIFY; + } } diff --git a/common/commit.h b/common/commit.h index 3e0b11557f..9743b3dd03 100644 --- a/common/commit.h +++ b/common/commit.h @@ -27,10 +27,10 @@ #include #include -#include +#include -class PICKED_ITEMS_LIST; +class EDA_ITEM; /** * Class COMMIT @@ -74,10 +74,14 @@ public: } ///> Adds a change of the item aItem of type aChangeType to the change list. - virtual COMMIT& Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType ); + COMMIT& Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType ); + ///> Adds a change of an item with its copy done before the change has occurred. + COMMIT& Stage( EDA_ITEM* aItem, EDA_ITEM* aCopy ); - void Stage( const PICKED_ITEMS_LIST& aItems, CHANGE_TYPE aChangeType ); + COMMIT& Stage( std::vector& container, CHANGE_TYPE aChangeType ); + + COMMIT& Stage( const PICKED_ITEMS_LIST& aItems, UNDO_REDO_T aModFlag = UR_UNSPECIFIED ); ///> Executes the changes. virtual void Push( const wxString& aMessage ) = 0; @@ -88,14 +92,17 @@ public: protected: struct COMMIT_LINE { - EDA_ITEM *m_item; - EDA_ITEM *m_copy; + EDA_ITEM* m_item; + EDA_ITEM* m_copy; CHANGE_TYPE m_type; }; - virtual void makeEntry( EDA_ITEM* aItem, CHANGE_TYPE type, bool saveCopy ); + virtual void makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy = NULL ); + virtual EDA_ITEM* parentObject( EDA_ITEM* aItem ) const = 0; + CHANGE_TYPE convert( UNDO_REDO_T aType ) const; + bool m_committed; std::set m_changedItems;