refactoring: split BOARD_ITEM::SwapData to use polymorphic per-item methods

This commit is contained in:
Tomasz Włostowski 2017-10-31 14:59:03 +01:00
parent 26f862a9d0
commit 0245912c34
20 changed files with 116 additions and 74 deletions

View File

@ -67,11 +67,6 @@ enum STROKE_T
*/
class BOARD_ITEM : public EDA_ITEM
{
// These are made private here so they may not be used.
// Instead everything derived from BOARD_ITEM is handled via DLIST<>'s
// use of DHEAD's member functions.
void SetNext( EDA_ITEM* aNext ) { Pnext = aNext; }
void SetBack( EDA_ITEM* aBack ) { Pback = aBack; }
protected:
PCB_LAYER_ID m_Layer;
@ -166,7 +161,7 @@ public:
* Pnext and Pback because aItem is not changed in the linked list
* @param aImage = the item image which contains data to swap
*/
void SwapData( BOARD_ITEM* aImage );
virtual void SwapData( BOARD_ITEM* aImage );
/**
* Function IsOnLayer

View File

@ -314,6 +314,7 @@ set( PCBNEW_CLASS_SRCS
tools/tools_common.cpp
tools/tool_event_utils.cpp
tools/size_menu.cpp
tools/selection.cpp
footprint_preview_panel.cpp
)

View File

@ -258,3 +258,9 @@ void BOARD_ITEM::DeleteStructure()
delete this;
}
void BOARD_ITEM::SwapData( BOARD_ITEM* aImage )
{
}

View File

@ -505,3 +505,10 @@ EDA_ITEM* DIMENSION::Clone() const
{
return new DIMENSION( *this );
}
void DIMENSION::SwapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_DIMENSION_T );
std::swap( *((DIMENSION*) this), *((DIMENSION*) aImage) );
}

View File

@ -227,6 +227,8 @@ public:
virtual const BOX2I ViewBBox() const override;
virtual void SwapData( BOARD_ITEM* aImage ) override;
#if defined(DEBUG)
virtual void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
#endif

View File

@ -759,3 +759,10 @@ const std::vector<wxPoint> DRAWSEGMENT::GetPolyPoints() const
return rv;
}
void DRAWSEGMENT::SwapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_LINE_T );
std::swap( *((DRAWSEGMENT*) this), *((DRAWSEGMENT*) aImage) );
}

View File

@ -240,6 +240,8 @@ public:
virtual const BOX2I ViewBBox() const override;
virtual void SwapData( BOARD_ITEM* aImage ) override;
#if defined(DEBUG)
void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
#endif

View File

@ -206,3 +206,10 @@ EDA_ITEM* PCB_TARGET::Clone() const
{
return new PCB_TARGET( *this );
}
void PCB_TARGET::SwapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_TARGET_T );
std::swap( *((PCB_TARGET*) this), *((PCB_TARGET*) aImage) );
}

View File

@ -104,6 +104,8 @@ public:
EDA_ITEM* Clone() const override;
virtual void SwapData( BOARD_ITEM* aImage ) override;
#if defined(DEBUG)
void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
#endif

View File

@ -1304,3 +1304,10 @@ bool MODULE::BuildPolyCourtyard()
return success;
}
void MODULE::SwapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_MODULE_T );
std::swap( *((MODULE*) this), *((MODULE*) aImage) );
}

View File

@ -674,6 +674,8 @@ public:
*/
bool BuildPolyCourtyard();
virtual void SwapData( BOARD_ITEM* aImage ) override;
#if defined(DEBUG)
virtual void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
#endif

View File

@ -1392,3 +1392,10 @@ void D_PAD::ImportSettingsFromMaster( const D_PAD& aMasterPad )
SetAnchorPadShape( aMasterPad.GetAnchorPadShape() );
MergePrimitivesAsPolygon();
}
void D_PAD::SwapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_PAD_T );
std::swap( *((MODULE*) this), *((MODULE*) aImage) );
}

View File

@ -731,6 +731,8 @@ public:
*/
void CopyNetlistSettings( D_PAD* aPad, bool aCopyLocalSettings );
virtual void SwapData( BOARD_ITEM* aImage ) override;
#if defined(DEBUG)
virtual void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
#endif

View File

@ -206,3 +206,10 @@ EDA_ITEM* TEXTE_PCB::Clone() const
{
return new TEXTE_PCB( *this );
}
void TEXTE_PCB::SwapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_TEXT_T );
std::swap( *((TEXTE_PCB*) this), *((TEXTE_PCB*) aImage) );
}

View File

@ -140,6 +140,8 @@ public:
EDA_ITEM* Clone() const override;
virtual void SwapData( BOARD_ITEM* aImage ) override;
#if defined(DEBUG)
virtual void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
#endif

View File

@ -1608,11 +1608,24 @@ wxString TRACK::GetSelectMenuText() const
}
BITMAP_DEF TRACK:: GetMenuImage() const
BITMAP_DEF TRACK::GetMenuImage() const
{
return showtrack_xpm;
}
void TRACK::SwapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_TRACE_T );
std::swap( *((TRACK*) this), *((TRACK*) aImage) );
}
void VIA::SwapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_VIA_T );
std::swap( *((VIA*) this), *((VIA*) aImage) );
}
#if defined(DEBUG)

View File

@ -302,6 +302,8 @@ public:
virtual unsigned int ViewGetLOD( int aLayer, KIGFX::VIEW* aView ) const override;
virtual void SwapData( BOARD_ITEM* aImage ) override;
#if defined (DEBUG)
virtual void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
@ -475,6 +477,8 @@ public:
*/
bool IsDrillDefault() const { return m_Drill <= 0; }
virtual void SwapData( BOARD_ITEM* aImage ) override;
protected:
virtual void GetMsgPanelInfoBase( std::vector< MSG_PANEL_ITEM >& aList ) override;

View File

@ -1305,3 +1305,10 @@ BITMAP_DEF ZONE_CONTAINER::GetMenuImage() const
{
return add_zone_xpm;
}
void ZONE_CONTAINER::SwapData( BOARD_ITEM* aImage )
{
assert( aImage->Type() == PCB_ZONE_AREA_T );
std::swap( *((ZONE_CONTAINER*) this), *((ZONE_CONTAINER*) aImage) );
}

View File

@ -733,7 +733,7 @@ public:
virtual void Show( int nestLevel, std::ostream& os ) const override { ShowDummy( os ); }
#endif
virtual void SwapData( BOARD_ITEM* aImage ) override;
private:
void buildFeatureHoleList( BOARD* aPcb, SHAPE_POLY_SET& aFeatures );

View File

@ -168,6 +168,33 @@ static bool TestForExistingItem( BOARD* aPcb, BOARD_ITEM* aItem )
return std::binary_search( itemsList.begin(), itemsList.end(), aItem );
}
static void SwapItemData( BOARD_ITEM* aItem, BOARD_ITEM* aImage )
{
if( aImage == NULL )
return;
wxASSERT( aItem->Type() == aImage->Type() );
// Remark: to create images of edited items to undo, we are using Clone method
// which can duplication of items foe copy, but does not clone all members
// mainly pointers in chain and time stamp, which is set to new, unique value.
// So we have to use the current values of these parameters.
EDA_ITEM* pnext = aItem->Next();
EDA_ITEM* pback = aItem->Back();
DHEAD* mylist = aItem->GetList();
time_t timestamp = aItem->GetTimeStamp();
EDA_ITEM* parent = aItem->GetParent();
aItem->SwapData( aImage );
// Restore pointers and time stamp, to be sure they are not broken
aItem->SetNext( pnext );
aItem->SetBack( pback );
aItem->SetList( mylist );
aItem->SetTimeStamp( timestamp );
aItem->SetParent( parent );
}
void PCB_BASE_EDIT_FRAME::SaveCopyInUndoList( BOARD_ITEM* aItem, UNDO_REDO_T aCommandType,
const wxPoint& aTransformPoint )
@ -470,7 +497,7 @@ void PCB_BASE_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList, bool
view->Remove( item );
connectivity->Remove( item );
item->SwapData( image );
SwapItemData( item, image );
// Update all pads/drawings/texts, as they become invalid
// for the VIEW after SwapData() called for modules
@ -564,71 +591,6 @@ void PCB_BASE_EDIT_FRAME::PutDataInPreviousState( PICKED_ITEMS_LIST* aList, bool
}
void BOARD_ITEM::SwapData( BOARD_ITEM* aImage )
{
if( aImage == NULL )
return;
wxASSERT( Type() == aImage->Type() );
// Remark: to create images of edited items to undo, we are using Clone method
// which can duplication of items foe copy, but does not clone all members
// mainly pointers in chain and time stamp, which is set to new, unique value.
// So we have to use the current values of these parameters.
EDA_ITEM* pnext = Next();
EDA_ITEM* pback = Back();
DHEAD* mylist = m_List;
time_t timestamp = GetTimeStamp();
EDA_ITEM* parent = GetParent();
switch( Type() )
{
case PCB_MODULE_T:
std::swap( *((MODULE*) this), *((MODULE*) aImage) );
break;
case PCB_ZONE_AREA_T:
std::swap( *((ZONE_CONTAINER*) this), *((ZONE_CONTAINER*) aImage) );
break;
case PCB_LINE_T:
std::swap( *((DRAWSEGMENT*) this), *((DRAWSEGMENT*) aImage) );
break;
case PCB_TRACE_T:
std::swap( *((TRACK*) this), *((TRACK*) aImage) );
break;
case PCB_VIA_T:
std::swap( *((VIA*) this), *((VIA*) aImage) );
break;
case PCB_TEXT_T:
std::swap( *((TEXTE_PCB*)this), *((TEXTE_PCB*)aImage) );
break;
case PCB_TARGET_T:
std::swap( *((PCB_TARGET*)this), *((PCB_TARGET*)aImage) );
break;
case PCB_DIMENSION_T:
std::swap( *((DIMENSION*)this), *((DIMENSION*)aImage) );
break;
case PCB_ZONE_T:
default:
wxLogMessage( wxT( "SwapData() error: unexpected type %d" ), Type() );
break;
}
// Restore pointers and time stamp, to be sure they are not broken
Pnext = pnext;
Pback = pback;
m_List = mylist;
SetTimeStamp( timestamp );
SetParent( parent );
}
void PCB_SCREEN::ClearUndoORRedoList( UNDO_REDO_CONTAINER& aList, int aItemCount )