2016-05-10 15:57:21 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-07-19 08:45:14 +00:00
|
|
|
* Copyright 2016-2017 CERN
|
2021-07-15 19:26:35 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2016-05-10 15:57:21 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2016-09-02 14:38:40 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
2016-05-10 15:57:21 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2021-10-01 10:52:34 +00:00
|
|
|
#include <core/kicad_algo.h>
|
2016-05-10 15:57:21 +00:00
|
|
|
#include <commit.h>
|
2020-10-14 01:06:53 +00:00
|
|
|
#include <eda_item.h>
|
2021-06-14 14:52:37 +00:00
|
|
|
#include <macros.h>
|
2016-05-10 15:57:21 +00:00
|
|
|
|
|
|
|
COMMIT::COMMIT()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
COMMIT::~COMMIT()
|
|
|
|
{
|
2016-06-15 08:15:10 +00:00
|
|
|
for( COMMIT_LINE& ent : m_changes )
|
2016-05-10 15:57:21 +00:00
|
|
|
{
|
2016-06-15 08:15:10 +00:00
|
|
|
if( ent.m_copy )
|
|
|
|
delete ent.m_copy;
|
2016-05-10 15:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& COMMIT::Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType, BASE_SCREEN* aScreen )
|
2016-05-10 15:57:21 +00:00
|
|
|
{
|
2016-10-20 15:15:27 +00:00
|
|
|
// CHT_MODIFY and CHT_DONE are not compatible
|
2022-11-15 23:51:42 +00:00
|
|
|
wxASSERT( ( aChangeType & ( CHT_MODIFY | CHT_DONE ) ) != ( CHT_MODIFY | CHT_DONE ) );
|
2016-08-18 14:28:04 +00:00
|
|
|
|
|
|
|
int flag = aChangeType & CHT_FLAGS;
|
|
|
|
|
|
|
|
switch( aChangeType & CHT_TYPE )
|
2016-05-10 15:57:21 +00:00
|
|
|
{
|
2021-07-15 19:26:35 +00:00
|
|
|
case CHT_ADD:
|
2023-04-29 00:02:42 +00:00
|
|
|
makeEntry( aItem, CHT_ADD | flag, nullptr, aScreen );
|
2021-07-15 19:26:35 +00:00
|
|
|
return *this;
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
case CHT_REMOVE:
|
2023-10-31 10:59:11 +00:00
|
|
|
m_deletedItems.insert( aItem );
|
2023-04-29 00:02:42 +00:00
|
|
|
makeEntry( aItem, CHT_REMOVE | flag, nullptr, aScreen );
|
2021-07-15 19:26:35 +00:00
|
|
|
return *this;
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
case CHT_MODIFY:
|
|
|
|
{
|
|
|
|
EDA_ITEM* parent = parentObject( aItem );
|
2022-11-15 23:51:42 +00:00
|
|
|
EDA_ITEM* clone = makeImage( parent );
|
2019-02-16 21:28:45 +00:00
|
|
|
|
2022-11-15 23:51:42 +00:00
|
|
|
wxASSERT( clone );
|
2019-02-16 21:28:45 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
if( clone )
|
2023-04-29 00:02:42 +00:00
|
|
|
return createModified( parent, clone, flag, aScreen );
|
2019-02-16 21:28:45 +00:00
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2023-12-16 14:14:47 +00:00
|
|
|
case CHT_GROUP:
|
|
|
|
case CHT_UNGROUP:
|
|
|
|
makeEntry( aItem, aChangeType, nullptr, aScreen );
|
|
|
|
return *this;
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
default:
|
2022-11-15 23:51:42 +00:00
|
|
|
wxFAIL;
|
2016-05-10 15:57:21 +00:00
|
|
|
}
|
2016-06-09 12:28:54 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& COMMIT::Stage( std::vector<EDA_ITEM*> &container, CHANGE_TYPE aChangeType,
|
|
|
|
BASE_SCREEN *aScreen )
|
2016-06-09 12:28:06 +00:00
|
|
|
{
|
|
|
|
for( EDA_ITEM* item : container )
|
2023-04-29 00:02:42 +00:00
|
|
|
Stage( item, aChangeType, aScreen);
|
2016-06-09 12:28:06 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& COMMIT::Stage( const PICKED_ITEMS_LIST &aItems, UNDO_REDO aModFlag, BASE_SCREEN *aScreen )
|
2016-05-10 15:57:21 +00:00
|
|
|
{
|
2016-05-12 11:55:58 +00:00
|
|
|
for( unsigned int i = 0; i < aItems.GetCount(); i++ )
|
2016-06-09 12:28:54 +00:00
|
|
|
{
|
2020-08-26 18:04:32 +00:00
|
|
|
UNDO_REDO change_type = aItems.GetPickedItemStatus( i );
|
2016-06-09 12:28:54 +00:00
|
|
|
EDA_ITEM* item = aItems.GetPickedItem( i );
|
|
|
|
|
2020-08-26 18:04:32 +00:00
|
|
|
if( change_type == UNDO_REDO::UNSPECIFIED )
|
2016-06-09 12:28:54 +00:00
|
|
|
change_type = aModFlag;
|
|
|
|
|
2022-11-15 23:51:42 +00:00
|
|
|
if( EDA_ITEM* copy = aItems.GetPickedItemLink( i ) )
|
2016-06-09 12:28:54 +00:00
|
|
|
{
|
2020-08-26 18:04:32 +00:00
|
|
|
assert( change_type == UNDO_REDO::CHANGED );
|
2016-06-09 12:28:54 +00:00
|
|
|
|
|
|
|
// There was already a copy created, so use it
|
2023-04-29 00:02:42 +00:00
|
|
|
Modified( item, copy, aScreen );
|
2016-06-09 12:28:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-29 00:02:42 +00:00
|
|
|
Stage( item, convert( change_type ), aScreen );
|
2016-06-09 12:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-07-19 08:45:14 +00:00
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
int COMMIT::GetStatus( EDA_ITEM* aItem, BASE_SCREEN *aScreen )
|
2017-07-19 08:45:14 +00:00
|
|
|
{
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT_LINE* entry = findEntry( parentObject( aItem ), aScreen );
|
2017-07-19 08:45:14 +00:00
|
|
|
|
|
|
|
return entry ? entry->m_type : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& COMMIT::createModified( EDA_ITEM* aItem, EDA_ITEM* aCopy, int aExtraFlags, BASE_SCREEN* aScreen )
|
2017-07-19 08:45:14 +00:00
|
|
|
{
|
|
|
|
EDA_ITEM* parent = parentObject( aItem );
|
|
|
|
|
2023-10-31 10:59:11 +00:00
|
|
|
if( m_changedItems.find( parent ) != m_changedItems.end() )
|
2018-06-27 23:15:27 +00:00
|
|
|
{
|
2018-06-27 23:40:57 +00:00
|
|
|
delete aCopy;
|
2017-07-19 08:45:14 +00:00
|
|
|
return *this; // item has been already modified once
|
2018-06-27 23:15:27 +00:00
|
|
|
}
|
2017-07-19 08:45:14 +00:00
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
makeEntry( parent, CHT_MODIFY | aExtraFlags, aCopy, aScreen );
|
2017-07-19 08:45:14 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
void COMMIT::makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy, BASE_SCREEN *aScreen )
|
2016-06-09 12:28:54 +00:00
|
|
|
{
|
|
|
|
// Expect an item copy if it is going to be modified
|
2019-10-26 15:49:29 +00:00
|
|
|
wxASSERT( !!aCopy == ( ( aType & CHT_TYPE ) == CHT_MODIFY ) );
|
2016-06-09 12:28:54 +00:00
|
|
|
|
|
|
|
COMMIT_LINE ent;
|
|
|
|
|
|
|
|
ent.m_item = aItem;
|
|
|
|
ent.m_type = aType;
|
|
|
|
ent.m_copy = aCopy;
|
2023-04-29 00:02:42 +00:00
|
|
|
ent.m_screen = aScreen;
|
2016-06-09 12:28:54 +00:00
|
|
|
|
|
|
|
m_changedItems.insert( aItem );
|
|
|
|
m_changes.push_back( ent );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT::COMMIT_LINE* COMMIT::findEntry( EDA_ITEM* aItem, BASE_SCREEN *aScreen )
|
2017-07-19 08:45:14 +00:00
|
|
|
{
|
|
|
|
for( COMMIT_LINE& change : m_changes )
|
|
|
|
{
|
2023-04-29 00:02:42 +00:00
|
|
|
if( change.m_item == aItem && change.m_screen == aScreen )
|
2017-07-19 08:45:14 +00:00
|
|
|
return &change;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-26 18:04:32 +00:00
|
|
|
CHANGE_TYPE COMMIT::convert( UNDO_REDO aType ) const
|
2016-06-09 12:28:54 +00:00
|
|
|
{
|
|
|
|
switch( aType )
|
|
|
|
{
|
2023-12-16 14:14:47 +00:00
|
|
|
case UNDO_REDO::NEWITEM: return CHT_ADD;
|
|
|
|
case UNDO_REDO::DELETED: return CHT_REMOVE;
|
2023-12-16 16:17:24 +00:00
|
|
|
case UNDO_REDO::REGROUP: return CHT_GROUP;
|
2023-12-16 14:14:47 +00:00
|
|
|
case UNDO_REDO::UNGROUP: return CHT_UNGROUP;
|
|
|
|
case UNDO_REDO::CHANGED: return CHT_MODIFY;
|
|
|
|
default: wxASSERT( false ); return CHT_MODIFY;
|
2016-06-09 12:28:54 +00:00
|
|
|
}
|
2016-05-10 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 21:03:54 +00:00
|
|
|
|
|
|
|
UNDO_REDO COMMIT::convert( CHANGE_TYPE aType ) const
|
|
|
|
{
|
|
|
|
switch( aType )
|
|
|
|
{
|
2023-12-16 14:14:47 +00:00
|
|
|
case CHT_ADD: return UNDO_REDO::NEWITEM;
|
|
|
|
case CHT_REMOVE: return UNDO_REDO::DELETED;
|
2023-12-16 16:17:24 +00:00
|
|
|
case CHT_GROUP: return UNDO_REDO::REGROUP;
|
2023-12-16 14:14:47 +00:00
|
|
|
case CHT_UNGROUP: return UNDO_REDO::UNGROUP;
|
|
|
|
case CHT_MODIFY: return UNDO_REDO::CHANGED;
|
|
|
|
default: wxASSERT( false ); return UNDO_REDO::CHANGED;
|
2023-05-12 21:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|