Fix a few minor symbol library inheritance bugs.

Don't clobber value file when load aliases in legacy symbol library cache
parser.

Use actual symbol library LIB_PART pointer rather than a flattened copy
of the symbol.  This fixed a bug when displaying the parent field in the
message panel for derived symbols.

Simplify the flatten code by copying the parent and updating the lesser
information from the inherited symbol.
This commit is contained in:
Wayne Stambaugh 2019-11-07 09:09:24 -05:00
parent 54f066fed7
commit 8e150521a2
10 changed files with 64 additions and 73 deletions

View File

@ -277,39 +277,17 @@ std::unique_ptr< LIB_PART > LIB_PART::Flatten() const
wxCHECK_MSG( parent, retv,
wxString::Format( "Parent of derived symbol '%s' undefined", m_name ) );
retv.reset( new LIB_PART( *const_cast< LIB_PART* >( this ) ) );
// Copy the parent.
retv.reset( new LIB_PART( *const_cast< LIB_PART* >( parent.get() ) ) );
// Flattened symbols have no inheritance.
retv->SetParent( nullptr );
// Now add the inherited part (this) information.
retv->SetName( m_name );
// Flatten parent information into the derived symbol.
retv->SetUnitCount( parent->GetUnitCount() );
LIB_ITEM* newItem;
for( LIB_ITEM& item : parent->GetDrawItems() )
{
// Only add fields from the parent that are not present in the child. The child
// symbol fields are already set.
if( item.Type() == LIB_FIELD_T )
{
LIB_FIELD* field = (LIB_FIELD*) &item;
if( retv->GetField( field->GetId() ) )
continue;
}
newItem = (LIB_ITEM*) item.Clone();
newItem->SetParent( retv.get() );
retv->GetDrawItems().push_back( newItem );
}
if( parent->IsPower() )
retv->SetPower();
else
retv->SetNormal();
retv->LockUnits( parent->UnitsLocked() );
const LIB_FIELD* datasheetField = GetField( DATASHEET );
retv->GetField( DATASHEET )->SetText( datasheetField->GetText() );
retv->SetDocFileName( m_docFileName );
retv->SetKeyWords( m_keyWords );
retv->SetDescription( m_description );
}
else
{
@ -803,9 +781,9 @@ void LIB_PART::GetFields( LIB_FIELDS& aList )
}
LIB_FIELD* LIB_PART::GetField( int aId )
LIB_FIELD* LIB_PART::GetField( int aId ) const
{
for( LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
for( const LIB_ITEM& item : m_drawings[ LIB_FIELD_T ] )
{
LIB_FIELD* field = ( LIB_FIELD* ) &item;
@ -876,6 +854,15 @@ bool LIB_PART::HasConversion() const
return true;
}
if( PART_SPTR parent = m_parent.lock() )
{
for( const LIB_ITEM& item : parent->GetDrawItems() )
{
if( item.m_Convert > LIB_ITEM::LIB_CONVERT::BASE )
return true;
}
}
return false;
}

View File

@ -271,7 +271,7 @@ public:
* @param aId - Id of field to return.
* @return The field if found, otherwise NULL.
*/
LIB_FIELD* GetField( int aId );
LIB_FIELD* GetField( int aId ) const;
/** Return reference to the value field. */
LIB_FIELD& GetValueField();

View File

@ -2896,9 +2896,13 @@ void SCH_LEGACY_PLUGIN_CACHE::loadAliases( std::unique_ptr<LIB_PART>& aPart,
if( field->GetId() < MANDATORY_FIELDS )
{
// Get all of the parent field information except for the string.
*newPart->GetField( field->GetId() ) = *field;
if( field->GetId() == REFERENCE || field->GetId() == FOOTPRINT )
// Restore the alias strings that are defined in this file format.
if( field->GetId() == REFERENCE
|| field->GetId() == FOOTPRINT
|| field->GetId() == VALUE )
newPart->GetField( field->GetId() )->SetText( tmp );
}
}

View File

@ -135,9 +135,7 @@ void SCH_VIEW::DisplayComponent( LIB_PART* aPart )
std::shared_ptr< LIB_PART > parent;
LIB_PART* drawnPart = aPart;
if( aPart->IsAlias() )
{
// Draw the alias mandatory fields.
// Draw the mandatory fields for aliases and parent symbols.
for( auto& item : aPart->GetDrawItems() )
{
if( item.Type() != LIB_FIELD_T )
@ -149,6 +147,9 @@ void SCH_VIEW::DisplayComponent( LIB_PART* aPart )
Add( &item );
}
// Draw the parent items if the symbol is inherited from another symbol.
if( aPart->IsAlias() )
{
parent = aPart->GetParent().lock();
wxCHECK( parent, /* void */ );
@ -158,12 +159,12 @@ void SCH_VIEW::DisplayComponent( LIB_PART* aPart )
for( auto& item : drawnPart->GetDrawItems() )
{
// Do not overwrite the alias mandatory fields with the parent mandatory fields.
// The mandatory fields are already in place so we only add user defined fields.
if( item.Type() == LIB_FIELD_T )
{
LIB_FIELD* field = (LIB_FIELD*) &item;
if( aPart->IsAlias() && field->GetId() < MANDATORY_FIELDS )
if( field->GetId() < MANDATORY_FIELDS )
continue;
}

View File

@ -146,7 +146,7 @@ void LIB_VIEW_FRAME::ReCreateMenuBar()
void LIB_VIEW_FRAME::SyncToolbars()
{
std::unique_ptr< LIB_PART > symbol( GetSelectedSymbol() );
LIB_PART* symbol = GetSelectedSymbol();
m_mainToolBar->Toggle( EE_ACTIONS::showDatasheet,
symbol && !symbol->GetDocFileName().IsEmpty() );

View File

@ -252,8 +252,7 @@ int EE_INSPECTION_TOOL::ShowDatasheet( const TOOL_EVENT& aEvent )
}
else if( m_frame->IsType( FRAME_SCH_VIEWER ) || m_frame->IsType( FRAME_SCH_VIEWER_MODAL ) )
{
std::unique_ptr< LIB_PART > entry =
static_cast<LIB_VIEW_FRAME*>( m_frame )->GetSelectedSymbol();
LIB_PART* entry = static_cast<LIB_VIEW_FRAME*>( m_frame )->GetSelectedSymbol();
if( !entry )
return 0;

View File

@ -373,7 +373,7 @@ int LIB_CONTROL::AddSymbolToSchematic( const TOOL_EVENT& aEvent )
}
else
{
part = viewFrame->GetSelectedSymbol().get();
part = viewFrame->GetSelectedSymbol();
libId = part->GetLibId();
unit = viewFrame->GetUnit();
convert = viewFrame->GetConvert();

View File

@ -207,7 +207,7 @@ LIB_VIEW_FRAME::LIB_VIEW_FRAME( KIWAY* aKiway, wxWindow* aParent, FRAME_T aFrame
LIB_VIEW_FRAME::~LIB_VIEW_FRAME()
{
if( m_previewItem )
GetCanvas()->GetView()->Remove( m_previewItem.get() );
GetCanvas()->GetView()->Remove( m_previewItem );
}
@ -248,17 +248,12 @@ void LIB_VIEW_FRAME::SetUnitAndConvert( int aUnit, int aConvert )
}
std::unique_ptr< LIB_PART > LIB_VIEW_FRAME::GetSelectedSymbol() const
LIB_PART* LIB_VIEW_FRAME::GetSelectedSymbol() const
{
std::unique_ptr< LIB_PART > symbol;
LIB_PART* symbol = nullptr;
if( !m_libraryName.IsEmpty() && !m_entryName.IsEmpty() )
{
LIB_PART* tmp = Prj().SchSymbolLibTable()->LoadSymbol( m_libraryName, m_entryName );
if( tmp )
symbol = tmp->Flatten();
}
symbol = Prj().SchSymbolLibTable()->LoadSymbol( m_libraryName, m_entryName );
return symbol;
}
@ -266,12 +261,12 @@ std::unique_ptr< LIB_PART > LIB_VIEW_FRAME::GetSelectedSymbol() const
void LIB_VIEW_FRAME::updatePreviewSymbol()
{
std::unique_ptr< LIB_PART > symbol = GetSelectedSymbol();
LIB_PART* symbol = GetSelectedSymbol();
KIGFX::SCH_VIEW* view = GetCanvas()->GetView();
if( m_previewItem )
{
view->Remove( m_previewItem.get() );
view->Remove( m_previewItem );
m_previewItem = nullptr;
}
@ -282,8 +277,8 @@ void LIB_VIEW_FRAME::updatePreviewSymbol()
GetRenderSettings()->m_ShowUnit = m_unit;
GetRenderSettings()->m_ShowConvert = m_convert;
m_previewItem.reset( symbol.release() );
view->Add( m_previewItem.get() );
m_previewItem = symbol;
view->Add( m_previewItem );
wxString parentName = _( "<none>" );
std::shared_ptr< LIB_PART > parent = m_previewItem->GetParent().lock();
@ -370,7 +365,7 @@ void LIB_VIEW_FRAME::OnSize( wxSizeEvent& SizeEv )
void LIB_VIEW_FRAME::onUpdateUnitChoice( wxUpdateUIEvent& aEvent )
{
std::unique_ptr< LIB_PART > part = GetSelectedSymbol();
LIB_PART* part = GetSelectedSymbol();
int unit_count = 1;
@ -701,7 +696,7 @@ void LIB_VIEW_FRAME::SetFilter( const SCHLIB_FILTER* aFilter )
const BOX2I LIB_VIEW_FRAME::GetDocumentExtents() const
{
std::unique_ptr< LIB_PART > part = GetSelectedSymbol();
LIB_PART* part = GetSelectedSymbol();
if( !part )
{
@ -709,9 +704,14 @@ const BOX2I LIB_VIEW_FRAME::GetDocumentExtents() const
}
else
{
EDA_RECT bbox = part->GetUnitBoundingBox( m_unit, m_convert );
return BOX2I( bbox.GetOrigin(), VECTOR2I( bbox.GetWidth(), bbox.GetHeight() ) );
std::shared_ptr< LIB_PART > tmp;
tmp = ( part->IsAlias() ) ? part->GetParent().lock() : part->SharedPtr();
wxCHECK( tmp, BOX2I( VECTOR2I(-200, -200), VECTOR2I( 400, 400 ) ) );
EDA_RECT bbox = tmp->GetUnitBoundingBox( m_unit, m_convert );
return BOX2I( bbox.GetOrigin(), VECTOR2I( bbox.GetWidth(), bbox.GetHeight() ) );
}
}

View File

@ -137,7 +137,7 @@ public:
int GetUnit() const { return m_unit; }
int GetConvert() const { return m_convert; }
std::unique_ptr< LIB_PART > GetSelectedSymbol() const;
LIB_PART* GetSelectedSymbol() const;
const BOX2I GetDocumentExtents() const override;
@ -190,7 +190,7 @@ private:
*/
bool m_selection_changed;
std::unique_ptr< LIB_PART > m_previewItem;
LIB_PART* m_previewItem;
DECLARE_EVENT_TABLE()
};

View File

@ -55,7 +55,7 @@ void LIB_VIEW_FRAME::OnSelectSymbol( wxCommandEvent& aEvent )
const auto libNicknames = libs->GetLogicalLibs();
adapter->AddLibraries( libNicknames, this );
std::unique_ptr< LIB_PART > current( GetSelectedSymbol() );
LIB_PART* current = GetSelectedSymbol();
LIB_ID id;
int unit = 0;