kicad/common/class_undoredo_container.cpp

338 lines
7.9 KiB
C++
Raw Normal View History

2009-07-23 15:49:39 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
2009-07-23 15:49:39 +00:00
*
* Copyright (C) 2009 jean-pierre.charras@gipsa-lab.inpg.fr
* Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 2009 KiCad Developers, see change_log.txt for contributors.
2009-07-23 15:49:39 +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
*/
#include <fctsys.h>
#include <common.h>
#include <base_struct.h>
2009-07-23 15:49:39 +00:00
#include <base_struct.h>
#include <class_undoredo_container.h>
2009-07-23 15:49:39 +00:00
ITEM_PICKER::ITEM_PICKER( EDA_ITEM* aItem, UNDO_REDO_T aUndoRedoStatus )
{
2012-02-05 13:02:46 +00:00
m_undoRedoStatus = aUndoRedoStatus;
SetItem( aItem );
m_pickerFlags = 0;
m_link = NULL;
}
2009-07-23 15:49:39 +00:00
PICKED_ITEMS_LIST::PICKED_ITEMS_LIST()
{
m_Status = UR_UNSPECIFIED;
2010-10-04 12:58:07 +00:00
}
2009-07-23 15:49:39 +00:00
PICKED_ITEMS_LIST::~PICKED_ITEMS_LIST()
{
}
2009-08-06 07:11:04 +00:00
void PICKED_ITEMS_LIST::PushItem( ITEM_PICKER& aItem )
2009-07-23 15:49:39 +00:00
{
m_ItemsList.push_back( aItem );
}
ITEM_PICKER PICKED_ITEMS_LIST::PopItem()
2009-07-23 15:49:39 +00:00
{
ITEM_PICKER item;
if( m_ItemsList.size() != 0 )
{
item = m_ItemsList.back();
m_ItemsList.pop_back();
}
2009-07-23 15:49:39 +00:00
return item;
}
bool PICKED_ITEMS_LIST::ContainsItem( EDA_ITEM* aItem ) const
{
for( size_t i = 0; i < m_ItemsList.size(); i++ )
{
2012-02-05 13:02:46 +00:00
if( m_ItemsList[ i ].GetItem() == aItem )
return true;
}
return false;
}
void PICKED_ITEMS_LIST::ClearItemsList()
2009-07-23 15:49:39 +00:00
{
m_ItemsList.clear();
}
2009-08-06 07:11:04 +00:00
void PICKED_ITEMS_LIST::ClearListAndDeleteItems()
{
2009-08-06 07:11:04 +00:00
bool show_error_message = true;
// Delete items is they are not flagged UR_NEW, or if this is a block operation
while( GetCount() > 0 )
{
ITEM_PICKER wrapper = PopItem();
2012-02-05 13:02:46 +00:00
if( wrapper.GetItem() == NULL ) // No more item in list.
2009-08-06 07:11:04 +00:00
break;
2012-02-05 13:02:46 +00:00
switch( wrapper.GetStatus() )
2009-08-06 07:11:04 +00:00
{
case UR_UNSPECIFIED:
if( show_error_message )
wxMessageBox( wxT( "ClearUndoORRedoList() error: UR_UNSPECIFIED command type" ) );
2009-08-06 07:11:04 +00:00
show_error_message = false;
break;
case UR_WIRE_IMAGE:
{
// Specific to eeschema: a linked list of wires is stored. The wrapper picks only
// the first item (head of list), and is owner of all picked items.
2012-02-05 13:02:46 +00:00
EDA_ITEM* item = wrapper.GetItem();
2009-08-06 07:11:04 +00:00
while( item )
{
// Delete old copy of wires
EDA_ITEM* nextitem = item->Next();
2009-08-06 07:11:04 +00:00
delete item;
item = nextitem;
}
}
break;
case UR_MOVED:
case UR_FLIPPED:
case UR_MIRRORED_X:
case UR_MIRRORED_Y:
case UR_ROTATED:
case UR_ROTATED_CLOCKWISE:
case UR_NEW: // Do nothing, items are in use, the picker is not owner of items
break;
case UR_CHANGED:
case UR_EXCHANGE_T:
2012-02-05 13:02:46 +00:00
delete wrapper.GetLink(); // the picker is owner of this item
2009-08-06 07:11:04 +00:00
break;
case UR_DELETED: // the picker is owner of this item
case UR_LIBEDIT: /* Libedit save always a copy of the current item
* So, the picker is always owner of the picked item
*/
case UR_MODEDIT: /* Specific to the module editor (modedit creates a full
* copy of the current module when changed),
* and the picker is owner of this item
2009-08-06 07:11:04 +00:00
*/
2012-02-05 13:02:46 +00:00
delete wrapper.GetItem();
2009-08-06 07:11:04 +00:00
break;
default:
wxFAIL_MSG( wxString::Format( wxT( "Cannot clear unknown undo/redo command %d" ),
2012-02-05 13:02:46 +00:00
wrapper.GetStatus() ) );
break;
2009-08-06 07:11:04 +00:00
}
}
}
2009-07-23 15:49:39 +00:00
ITEM_PICKER PICKED_ITEMS_LIST::GetItemWrapper( unsigned int aIdx )
{
ITEM_PICKER picker;
2009-08-06 07:11:04 +00:00
2009-07-23 15:49:39 +00:00
if( aIdx < m_ItemsList.size() )
picker = m_ItemsList[aIdx];
return picker;
}
2009-08-06 07:11:04 +00:00
EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItem( unsigned int aIdx )
2009-07-23 15:49:39 +00:00
{
if( aIdx < m_ItemsList.size() )
2012-02-05 13:02:46 +00:00
return m_ItemsList[aIdx].GetItem();
2009-07-23 15:49:39 +00:00
else
return NULL;
}
EDA_ITEM* PICKED_ITEMS_LIST::GetPickedItemLink( unsigned int aIdx )
2009-07-23 15:49:39 +00:00
{
if( aIdx < m_ItemsList.size() )
2012-02-05 13:02:46 +00:00
return m_ItemsList[aIdx].GetLink();
2009-07-23 15:49:39 +00:00
else
return NULL;
}
UNDO_REDO_T PICKED_ITEMS_LIST::GetPickedItemStatus( unsigned int aIdx )
2009-07-23 15:49:39 +00:00
{
if( aIdx < m_ItemsList.size() )
2012-02-05 13:02:46 +00:00
return m_ItemsList[aIdx].GetStatus();
2009-07-23 15:49:39 +00:00
else
return UR_UNSPECIFIED;
2009-07-23 15:49:39 +00:00
}
STATUS_FLAGS PICKED_ITEMS_LIST::GetPickerFlags( unsigned aIdx )
{
if( aIdx < m_ItemsList.size() )
2012-02-05 13:02:46 +00:00
return m_ItemsList[aIdx].GetFlags();
else
return 0;
}
2009-07-23 15:49:39 +00:00
bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, unsigned aIdx )
2009-07-23 15:49:39 +00:00
{
if( aIdx < m_ItemsList.size() )
{
2012-02-05 13:02:46 +00:00
m_ItemsList[aIdx].SetItem( aItem );
2009-07-23 15:49:39 +00:00
return true;
}
else
return false;
}
bool PICKED_ITEMS_LIST::SetPickedItemLink( EDA_ITEM* aLink, unsigned aIdx )
2009-07-23 15:49:39 +00:00
{
if( aIdx < m_ItemsList.size() )
{
2012-02-05 13:02:46 +00:00
m_ItemsList[aIdx].SetLink( aLink );
2009-07-23 15:49:39 +00:00
return true;
}
else
return false;
}
bool PICKED_ITEMS_LIST::SetPickedItem( EDA_ITEM* aItem, UNDO_REDO_T aStatus, unsigned aIdx )
2009-07-23 15:49:39 +00:00
{
if( aIdx < m_ItemsList.size() )
{
2012-02-05 13:02:46 +00:00
m_ItemsList[aIdx].SetItem( aItem );
m_ItemsList[aIdx].SetStatus( aStatus );
2009-07-23 15:49:39 +00:00
return true;
}
else
return false;
}
bool PICKED_ITEMS_LIST::SetPickedItemStatus( UNDO_REDO_T aStatus, unsigned aIdx )
2009-07-23 15:49:39 +00:00
{
if( aIdx < m_ItemsList.size() )
{
2012-02-05 13:02:46 +00:00
m_ItemsList[aIdx].SetStatus( aStatus );
2009-07-23 15:49:39 +00:00
return true;
}
else
return false;
}
bool PICKED_ITEMS_LIST::SetPickerFlags( STATUS_FLAGS aFlags, unsigned aIdx )
{
if( aIdx < m_ItemsList.size() )
{
2012-02-05 13:02:46 +00:00
m_ItemsList[aIdx].SetFlags( aFlags );
return true;
2009-07-23 15:49:39 +00:00
}
else
return false;
}
2009-08-06 15:42:09 +00:00
bool PICKED_ITEMS_LIST::RemovePicker( unsigned aIdx )
2009-07-23 15:49:39 +00:00
{
if( aIdx >= m_ItemsList.size() )
return false;
m_ItemsList.erase( m_ItemsList.begin() + aIdx );
return true;
}
2009-08-06 07:11:04 +00:00
void PICKED_ITEMS_LIST::CopyList( const PICKED_ITEMS_LIST& aSource )
{
2009-08-06 15:42:09 +00:00
m_ItemsList = aSource.m_ItemsList; // Vector's copy
}
2009-08-08 06:07:08 +00:00
void PICKED_ITEMS_LIST::ReversePickersListOrder()
{
std::vector <ITEM_PICKER> tmp;
while( !m_ItemsList.empty() )
{
tmp.push_back( m_ItemsList.back() );
m_ItemsList.pop_back();
}
m_ItemsList.swap( tmp );
}
2009-07-23 15:49:39 +00:00
/**********************************************/
/********** UNDO_REDO_CONTAINER ***************/
/**********************************************/
UNDO_REDO_CONTAINER::UNDO_REDO_CONTAINER()
{
}
UNDO_REDO_CONTAINER::~UNDO_REDO_CONTAINER()
{
ClearCommandList();
}
void UNDO_REDO_CONTAINER::ClearCommandList()
{
for( unsigned ii = 0; ii < m_CommandsList.size(); ii++ )
delete m_CommandsList[ii];
m_CommandsList.clear();
}
void UNDO_REDO_CONTAINER::PushCommand( PICKED_ITEMS_LIST* aItem )
{
m_CommandsList.push_back( aItem );
}
PICKED_ITEMS_LIST* UNDO_REDO_CONTAINER::PopCommand()
{
if( m_CommandsList.size() != 0 )
{
PICKED_ITEMS_LIST* item = m_CommandsList.back();
m_CommandsList.pop_back();
return item;
}
return NULL;
}
2009-08-08 06:07:08 +00:00