2016-05-10 15:57:21 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
|
|
|
* @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>
|
|
|
|
|
2016-06-09 12:28:54 +00:00
|
|
|
#include <class_undoredo_container.h>
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2016-06-09 12:28:54 +00:00
|
|
|
class EDA_ITEM;
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2016-08-18 14:28:04 +00:00
|
|
|
///> Types of changes
|
|
|
|
enum CHANGE_TYPE {
|
|
|
|
CHT_ADD = 1,
|
|
|
|
CHT_REMOVE = 2,
|
|
|
|
CHT_MODIFY = 4,
|
|
|
|
CHT_TYPE = CHT_ADD | CHT_REMOVE | CHT_MODIFY,
|
|
|
|
|
|
|
|
///> Flag to indicate the change is already applied,
|
|
|
|
///> just notify observers (not compatible with CHT_MODIFY)
|
|
|
|
CHT_DONE = 8,
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Class COMMIT
|
|
|
|
*
|
|
|
|
* Represents 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.
|
|
|
|
*/
|
|
|
|
class COMMIT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
COMMIT();
|
|
|
|
virtual ~COMMIT();
|
|
|
|
|
|
|
|
///> Adds a new item to the model
|
|
|
|
COMMIT& Add( EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
return Stage( aItem, CHT_ADD );
|
|
|
|
}
|
|
|
|
|
2016-08-18 14:28:04 +00:00
|
|
|
///> Notifies observers that aItem has been added
|
|
|
|
COMMIT& Added( EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
return Stage( aItem, CHT_ADD | CHT_DONE );
|
|
|
|
}
|
|
|
|
|
2016-05-10 15:57:21 +00:00
|
|
|
///> Removes a new item from the model
|
|
|
|
COMMIT& Remove( EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
return Stage( aItem, CHT_REMOVE );
|
|
|
|
}
|
|
|
|
|
2016-08-18 14:28:04 +00:00
|
|
|
///> Notifies observers that aItem has been removed
|
|
|
|
COMMIT& Removed( EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
return Stage( aItem, CHT_REMOVE | CHT_DONE );
|
|
|
|
}
|
|
|
|
|
2016-05-10 15:57:21 +00:00
|
|
|
///> Modifies a given item in the model.
|
|
|
|
///> Must be called before modification is performed.
|
|
|
|
COMMIT& Modify( EDA_ITEM* aItem )
|
|
|
|
{
|
|
|
|
return Stage( aItem, CHT_MODIFY );
|
|
|
|
}
|
|
|
|
|
2016-09-02 14:38:40 +00:00
|
|
|
///> Creates an undo entry for an item that has been already modified. Requires a copy done
|
|
|
|
///> before the modification.
|
|
|
|
COMMIT& Modified( EDA_ITEM* aItem, EDA_ITEM* aCopy );
|
|
|
|
|
2016-11-04 21:29:47 +00:00
|
|
|
template<class Range>
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-10 15:57:21 +00:00
|
|
|
///> Adds a change of the item aItem of type aChangeType to the change list.
|
2016-06-09 12:28:54 +00:00
|
|
|
COMMIT& Stage( EDA_ITEM* aItem, CHANGE_TYPE aChangeType );
|
2016-05-10 15:57:21 +00:00
|
|
|
|
2016-06-09 12:28:54 +00:00
|
|
|
COMMIT& Stage( std::vector<EDA_ITEM*>& container, CHANGE_TYPE aChangeType );
|
|
|
|
|
|
|
|
COMMIT& Stage( const PICKED_ITEMS_LIST& aItems, UNDO_REDO_T aModFlag = UR_UNSPECIFIED );
|
2016-05-10 15:57:21 +00:00
|
|
|
|
|
|
|
///> Executes the changes.
|
2017-03-03 12:41:41 +00:00
|
|
|
virtual void Push( const wxString& aMessage = wxT( "A commit" ), bool aCreateUndoEntry = true ) = 0;
|
2016-05-10 15:57:21 +00:00
|
|
|
|
|
|
|
///> Revertes the commit by restoring the modifed items state.
|
|
|
|
virtual void Revert() = 0;
|
|
|
|
|
2016-06-15 08:15:10 +00:00
|
|
|
bool Empty() const
|
|
|
|
{
|
|
|
|
return m_changes.empty();
|
|
|
|
}
|
|
|
|
|
2016-05-10 15:57:21 +00:00
|
|
|
protected:
|
|
|
|
struct COMMIT_LINE
|
|
|
|
{
|
2016-06-20 13:19:45 +00:00
|
|
|
///> Main item that is added/deleted/modified
|
2016-06-09 12:28:54 +00:00
|
|
|
EDA_ITEM* m_item;
|
2016-06-20 13:19:45 +00:00
|
|
|
|
|
|
|
///> Optional copy of the item
|
2016-06-09 12:28:54 +00:00
|
|
|
EDA_ITEM* m_copy;
|
2016-06-20 13:19:45 +00:00
|
|
|
|
|
|
|
///> Modification type
|
2016-05-10 15:57:21 +00:00
|
|
|
CHANGE_TYPE m_type;
|
|
|
|
};
|
|
|
|
|
2016-06-15 08:15:10 +00:00
|
|
|
// Should be called in Push() & Revert() methods
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_changedItems.clear();
|
|
|
|
m_changes.clear();
|
|
|
|
}
|
|
|
|
|
2016-06-09 12:28:54 +00:00
|
|
|
virtual void makeEntry( EDA_ITEM* aItem, CHANGE_TYPE aType, EDA_ITEM* aCopy = NULL );
|
|
|
|
|
2016-05-10 15:57:21 +00:00
|
|
|
virtual EDA_ITEM* parentObject( EDA_ITEM* aItem ) const = 0;
|
|
|
|
|
2016-06-09 12:28:54 +00:00
|
|
|
CHANGE_TYPE convert( UNDO_REDO_T aType ) const;
|
|
|
|
|
2016-05-10 15:57:21 +00:00
|
|
|
std::set<EDA_ITEM*> m_changedItems;
|
|
|
|
std::vector<COMMIT_LINE> m_changes;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|