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-01-25 12:42:36 +00:00
|
|
|
* Copyright (C) 2020-2021 KiCad Developers, see change_log.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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __COMMIT_H
|
|
|
|
#define __COMMIT_H
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
2021-06-08 02:29:33 +00:00
|
|
|
#include <wx/string.h>
|
2018-01-31 08:23:20 +00:00
|
|
|
#include <undo_redo_container.h>
|
2023-07-15 16:37:17 +00:00
|
|
|
#include <kiid.h>
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2016-06-09 12:28:54 +00:00
|
|
|
class EDA_ITEM;
|
2023-04-29 00:02:42 +00:00
|
|
|
class BASE_SCREEN;
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Types of changes
|
2016-08-18 14:28:04 +00:00
|
|
|
enum CHANGE_TYPE {
|
|
|
|
CHT_ADD = 1,
|
|
|
|
CHT_REMOVE = 2,
|
|
|
|
CHT_MODIFY = 4,
|
2023-12-16 14:14:47 +00:00
|
|
|
CHT_GROUP = 8,
|
|
|
|
CHT_UNGROUP = 16,
|
|
|
|
CHT_TYPE = CHT_ADD | CHT_REMOVE | CHT_MODIFY | CHT_GROUP | CHT_UNGROUP,
|
2016-08-18 14:28:04 +00:00
|
|
|
|
2023-12-16 14:14:47 +00:00
|
|
|
CHT_DONE = 32, ///< Flag to indicate the change is already applied
|
2016-08-18 14:28:04 +00:00
|
|
|
CHT_FLAGS = CHT_DONE
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
CHANGE_TYPE operator|( CHANGE_TYPE aTypeA, T aTypeB )
|
|
|
|
{
|
|
|
|
return CHANGE_TYPE( (int) aTypeA | (int) aTypeB );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
CHANGE_TYPE operator&( CHANGE_TYPE aTypeA, T aTypeB )
|
|
|
|
{
|
|
|
|
return CHANGE_TYPE( (int) aTypeA & (int) aTypeB );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-10 15:57:21 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Represent a set of changes (additions, deletions or modifications) of a data model
|
|
|
|
* (e.g. the BOARD) class.
|
2016-05-10 15:57:21 +00:00
|
|
|
*
|
|
|
|
* The class can be used to propagate changes to subscribed objects (e.g. views, ratsnest),
|
|
|
|
* and automatically create undo/redo points.
|
|
|
|
*/
|
|
|
|
class COMMIT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
COMMIT();
|
|
|
|
virtual ~COMMIT();
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Add a new item to the model
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& Add( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
|
2016-05-10 15:57:21 +00:00
|
|
|
{
|
2023-04-29 00:02:42 +00:00
|
|
|
return Stage( aItem, CHT_ADD, aScreen );
|
2016-05-10 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Notify observers that aItem has been added
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& Added( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
|
2016-08-18 14:28:04 +00:00
|
|
|
{
|
2023-04-29 00:02:42 +00:00
|
|
|
return Stage( aItem, CHT_ADD | CHT_DONE, aScreen );
|
2016-08-18 14:28:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Remove a new item from the model
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& Remove( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
|
2016-05-10 15:57:21 +00:00
|
|
|
{
|
2023-04-29 00:02:42 +00:00
|
|
|
return Stage( aItem, CHT_REMOVE, aScreen );
|
2016-05-10 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Notify observers that aItem has been removed
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& Removed( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
|
2016-08-18 14:28:04 +00:00
|
|
|
{
|
2023-04-29 00:02:42 +00:00
|
|
|
return Stage( aItem, CHT_REMOVE | CHT_DONE, aScreen );
|
2016-08-18 14:28:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Modify a given item in the model.
|
|
|
|
///< Must be called before modification is performed.
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& Modify( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr )
|
2016-05-10 15:57:21 +00:00
|
|
|
{
|
2023-04-29 00:02:42 +00:00
|
|
|
return Stage( aItem, CHT_MODIFY, aScreen );
|
2016-05-10 15:57:21 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Create an undo entry for an item that has been already modified. Requires a copy done
|
|
|
|
///< before the modification.
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& Modified( EDA_ITEM* aItem, EDA_ITEM* aCopy, BASE_SCREEN *aScreen = nullptr )
|
2017-07-19 08:45:14 +00:00
|
|
|
{
|
2024-04-24 19:44:32 +00:00
|
|
|
return createModified( aItem, aCopy, 0, aScreen );
|
2017-07-19 08:45:14 +00:00
|
|
|
}
|
2016-09-02 14:38:40 +00:00
|
|
|
|
2016-11-04 21:29:47 +00:00
|
|
|
template<class Range>
|
2020-12-18 14:04:03 +00:00
|
|
|
|
2016-11-04 21:29:47 +00:00
|
|
|
COMMIT& StageItems( const Range& aRange, CHANGE_TYPE aChangeType )
|
|
|
|
{
|
2016-12-09 11:04:32 +00:00
|
|
|
for( const auto& item : aRange )
|
|
|
|
Stage( item, aChangeType );
|
|
|
|
|
2016-11-04 21:29:47 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Add a change of the item aItem of type aChangeType to the change list.
|
2023-04-29 00:02:42 +00:00
|
|
|
virtual COMMIT& Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType,
|
|
|
|
BASE_SCREEN *aScreen = nullptr );
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
virtual COMMIT& Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType,
|
|
|
|
BASE_SCREEN *aScreen = nullptr );
|
2016-06-09 12:28:54 +00:00
|
|
|
|
2020-12-18 14:04:03 +00:00
|
|
|
virtual COMMIT& Stage( const PICKED_ITEMS_LIST& aItems,
|
2023-04-29 00:02:42 +00:00
|
|
|
UNDO_REDO aModFlag = UNDO_REDO::UNSPECIFIED,
|
|
|
|
BASE_SCREEN *aScreen = nullptr );
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Execute the changes.
|
2022-02-25 13:05:25 +00:00
|
|
|
virtual void Push( const wxString& aMessage = wxT( "A commit" ), int aFlags = 0 ) = 0;
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Revert the commit by restoring the modified items state.
|
2016-05-10 15:57:21 +00:00
|
|
|
virtual void Revert() = 0;
|
|
|
|
|
2016-06-15 08:15:10 +00:00
|
|
|
bool Empty() const
|
|
|
|
{
|
|
|
|
return m_changes.empty();
|
|
|
|
}
|
|
|
|
|
2021-01-25 12:42:36 +00:00
|
|
|
///< Returns status of an item.
|
2023-04-29 00:02:42 +00:00
|
|
|
int GetStatus( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr );
|
2017-07-19 08:45:14 +00:00
|
|
|
|
2023-10-22 16:34:32 +00:00
|
|
|
EDA_ITEM* GetFirst() const { return m_changes.empty() ? nullptr : m_changes[0].m_item; }
|
|
|
|
|
2016-05-10 15:57:21 +00:00
|
|
|
protected:
|
|
|
|
struct COMMIT_LINE
|
|
|
|
{
|
2023-07-15 16:37:17 +00:00
|
|
|
EDA_ITEM* m_item; ///< Main item that is added/deleted/modified
|
|
|
|
EDA_ITEM* m_copy; ///< Optional copy of the item
|
|
|
|
CHANGE_TYPE m_type; ///< Modification type
|
|
|
|
KIID m_parent = NilUuid(); ///< Parent item (primarily for undo of deleted items)
|
2023-04-29 00:02:42 +00:00
|
|
|
BASE_SCREEN* m_screen;
|
2016-05-10 15:57:21 +00:00
|
|
|
};
|
|
|
|
|
2016-06-15 08:15:10 +00:00
|
|
|
// Should be called in Push() & Revert() methods
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_changedItems.clear();
|
2023-10-31 10:59:11 +00:00
|
|
|
m_deletedItems.clear();
|
2016-06-15 08:15:10 +00:00
|
|
|
m_changes.clear();
|
|
|
|
}
|
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT& createModified( EDA_ITEM* aItem, EDA_ITEM* aCopy, int aExtraFlags = 0,
|
|
|
|
BASE_SCREEN *aScreen = nullptr );
|
2017-07-19 08:45:14 +00:00
|
|
|
|
2023-04-29 00:02:42 +00:00
|
|
|
virtual void makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy = nullptr,
|
|
|
|
BASE_SCREEN *aScreen = nullptr );
|
2016-06-09 12:28:54 +00:00
|
|
|
|
2017-07-19 08:45:14 +00:00
|
|
|
/**
|
2020-12-18 14:04:03 +00:00
|
|
|
* Search for an entry describing change for a particular item.
|
|
|
|
*
|
2017-07-19 08:45:14 +00:00
|
|
|
* @return null if there is no related entry.
|
|
|
|
*/
|
2023-04-29 00:02:42 +00:00
|
|
|
COMMIT_LINE* findEntry( EDA_ITEM* aItem, BASE_SCREEN *aScreen = nullptr );
|
2017-07-19 08:45:14 +00:00
|
|
|
|
2016-05-10 15:57:21 +00:00
|
|
|
virtual EDA_ITEM* parentObject( EDA_ITEM* aItem ) const = 0;
|
|
|
|
|
2022-11-15 23:51:42 +00:00
|
|
|
virtual EDA_ITEM* makeImage( EDA_ITEM* aItem ) const = 0;
|
|
|
|
|
2020-08-26 18:04:32 +00:00
|
|
|
CHANGE_TYPE convert( UNDO_REDO aType ) const;
|
2023-05-12 21:03:54 +00:00
|
|
|
UNDO_REDO convert( CHANGE_TYPE aType ) const;
|
2016-06-09 12:28:54 +00:00
|
|
|
|
2023-05-12 21:03:54 +00:00
|
|
|
protected:
|
|
|
|
std::set<EDA_ITEM*> m_changedItems;
|
2023-10-31 10:59:11 +00:00
|
|
|
std::set<EDA_ITEM*> m_deletedItems;
|
2016-05-10 15:57:21 +00:00
|
|
|
std::vector<COMMIT_LINE> m_changes;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|