2021-02-13 21:28:06 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 CERN
|
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <core/kicad_algo.h>
|
|
|
|
#include <dialog_update_symbol_fields.h>
|
2021-02-24 13:48:02 +00:00
|
|
|
#include <lib_symbol.h>
|
2021-02-13 21:28:06 +00:00
|
|
|
#include <symbol_edit_frame.h>
|
2023-06-09 21:41:33 +00:00
|
|
|
#include <sch_commit.h>
|
2021-02-13 21:28:06 +00:00
|
|
|
#include <template_fieldnames.h>
|
2021-07-29 09:56:22 +00:00
|
|
|
#include <string_utils.h>
|
2021-02-13 21:28:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool g_removeExtraLibFields = false;
|
|
|
|
bool g_resetEmptyLibFields = false;
|
2021-02-22 00:35:37 +00:00
|
|
|
bool g_resetLibFieldText = true;
|
2021-02-13 21:28:06 +00:00
|
|
|
bool g_resetLibFieldVisibilities = true;
|
|
|
|
bool g_resetLibFieldEffects = true;
|
|
|
|
bool g_resetLibFieldPositions = true;
|
|
|
|
|
|
|
|
|
|
|
|
DIALOG_UPDATE_SYMBOL_FIELDS::DIALOG_UPDATE_SYMBOL_FIELDS( SYMBOL_EDIT_FRAME* aParent,
|
2021-06-10 18:51:46 +00:00
|
|
|
LIB_SYMBOL* aSymbol ) :
|
2021-02-13 21:28:06 +00:00
|
|
|
DIALOG_UPDATE_SYMBOL_FIELDS_BASE( aParent ),
|
|
|
|
m_editFrame( aParent ),
|
|
|
|
m_symbol( aSymbol)
|
|
|
|
{
|
|
|
|
wxASSERT( aParent );
|
|
|
|
wxASSERT( aSymbol );
|
|
|
|
|
2021-06-30 10:53:04 +00:00
|
|
|
m_parentSymbolReadOnly->SetValue( UnescapeString( m_symbol->GetParent().lock()->GetName() ) );
|
2021-02-13 21:28:06 +00:00
|
|
|
|
|
|
|
for( int i = 0; i < MANDATORY_FIELDS; ++i )
|
|
|
|
{
|
2022-05-12 11:05:06 +00:00
|
|
|
m_fieldsBox->Append( TEMPLATE_FIELDNAME::GetDefaultFieldName( i, DO_TRANSLATE ) );
|
2021-02-13 21:28:06 +00:00
|
|
|
m_fieldsBox->Check( i, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFieldsList();
|
|
|
|
|
|
|
|
m_removeExtraBox->SetValue( g_removeExtraLibFields );
|
|
|
|
m_resetEmptyFields->SetValue( g_resetEmptyLibFields );
|
2021-02-22 00:35:37 +00:00
|
|
|
m_resetFieldText->SetValue( g_resetLibFieldText );
|
2021-02-13 21:28:06 +00:00
|
|
|
m_resetFieldVisibilities->SetValue( g_resetLibFieldVisibilities );
|
|
|
|
m_resetFieldEffects->SetValue( g_resetLibFieldEffects );
|
|
|
|
m_resetFieldPositions->SetValue( g_resetLibFieldPositions );
|
|
|
|
|
2021-11-16 19:39:58 +00:00
|
|
|
SetupStandardButtons();
|
2021-02-13 21:28:06 +00:00
|
|
|
|
|
|
|
// Now all widgets have the size fixed, call FinishDialogSettings
|
|
|
|
finishDialogSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DIALOG_UPDATE_SYMBOL_FIELDS::~DIALOG_UPDATE_SYMBOL_FIELDS()
|
|
|
|
{
|
|
|
|
g_removeExtraLibFields = m_removeExtraBox->GetValue();
|
|
|
|
g_resetEmptyLibFields = m_resetEmptyFields->GetValue();
|
2021-02-22 00:35:37 +00:00
|
|
|
g_resetLibFieldText = m_resetFieldText->GetValue();
|
2021-02-13 21:28:06 +00:00
|
|
|
g_resetLibFieldVisibilities = m_resetFieldVisibilities->GetValue();
|
|
|
|
g_resetLibFieldEffects = m_resetFieldEffects->GetValue();
|
|
|
|
g_resetLibFieldPositions = m_resetFieldPositions->GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_UPDATE_SYMBOL_FIELDS::updateFieldsList()
|
|
|
|
{
|
|
|
|
// Load non-mandatory fields from the parent part
|
2021-06-10 18:51:46 +00:00
|
|
|
std::vector<LIB_FIELD*> libFields;
|
|
|
|
std::set<wxString> fieldNames;
|
|
|
|
std::unique_ptr<LIB_SYMBOL> flattenedParent = m_symbol->GetParent().lock()->Flatten();
|
2021-02-13 21:28:06 +00:00
|
|
|
|
|
|
|
flattenedParent->GetFields( libFields );
|
|
|
|
|
|
|
|
for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
|
|
|
|
fieldNames.insert( libFields[i]->GetName() );
|
|
|
|
|
|
|
|
libFields.clear(); // flattenedPart is about to go out of scope...
|
|
|
|
|
|
|
|
// Load non-mandatory fields from the editor symbol
|
|
|
|
m_symbol->GetFields( libFields );
|
|
|
|
|
|
|
|
for( unsigned i = MANDATORY_FIELDS; i < libFields.size(); ++i )
|
|
|
|
fieldNames.insert( libFields[i]->GetName() );
|
|
|
|
|
|
|
|
libFields.clear();
|
|
|
|
|
|
|
|
// Update the listbox widget
|
|
|
|
for( unsigned i = m_fieldsBox->GetCount() - 1; i >= MANDATORY_FIELDS; --i )
|
|
|
|
m_fieldsBox->Delete( i );
|
|
|
|
|
|
|
|
for( const wxString& fieldName : fieldNames )
|
|
|
|
m_fieldsBox->Append( fieldName );
|
|
|
|
|
|
|
|
for( unsigned i = MANDATORY_FIELDS; i < m_fieldsBox->GetCount(); ++i )
|
|
|
|
m_fieldsBox->Check( i, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_UPDATE_SYMBOL_FIELDS::checkAll( bool aCheck )
|
|
|
|
{
|
|
|
|
for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
|
|
|
|
m_fieldsBox->Check( i, aCheck );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_UPDATE_SYMBOL_FIELDS::onOkButtonClicked( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
wxBusyCursor dummy;
|
2023-06-09 21:41:33 +00:00
|
|
|
SCH_COMMIT commit( m_editFrame );
|
2021-02-13 21:28:06 +00:00
|
|
|
|
2023-06-09 21:41:33 +00:00
|
|
|
commit.Modify( m_symbol );
|
2021-02-13 21:28:06 +00:00
|
|
|
|
|
|
|
// Create the set of fields to be updated
|
|
|
|
m_updateFields.clear();
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
|
|
|
|
{
|
|
|
|
if( m_fieldsBox->IsChecked( i ) )
|
|
|
|
m_updateFields.insert( m_fieldsBox->GetString( i ) );
|
|
|
|
}
|
|
|
|
|
2021-06-10 18:51:46 +00:00
|
|
|
std::unique_ptr<LIB_SYMBOL> flattenedParent = m_symbol->GetParent().lock()->Flatten();
|
2021-02-13 21:28:06 +00:00
|
|
|
|
|
|
|
bool removeExtras = m_removeExtraBox->GetValue();
|
|
|
|
bool resetVis = m_resetFieldVisibilities->GetValue();
|
|
|
|
bool resetEffects = m_resetFieldEffects->GetValue();
|
|
|
|
bool resetPositions = m_resetFieldPositions->GetValue();
|
|
|
|
|
|
|
|
std::vector<LIB_FIELD> fields;
|
|
|
|
std::vector<LIB_FIELD> result;
|
|
|
|
m_symbol->GetFields( fields );
|
|
|
|
|
|
|
|
for( LIB_FIELD& field : fields )
|
|
|
|
{
|
|
|
|
bool copy = true;
|
|
|
|
LIB_FIELD* parentField = nullptr;
|
|
|
|
|
|
|
|
if( alg::contains( m_updateFields, field.GetName() ) )
|
|
|
|
{
|
|
|
|
parentField = flattenedParent->FindField( field.GetName() );
|
|
|
|
|
|
|
|
if( parentField )
|
|
|
|
{
|
2021-02-22 00:35:37 +00:00
|
|
|
bool resetText = parentField->GetText().IsEmpty() ? m_resetEmptyFields->GetValue()
|
|
|
|
: m_resetFieldText->GetValue();
|
|
|
|
|
|
|
|
if( resetText )
|
2021-02-13 21:28:06 +00:00
|
|
|
field.SetText( parentField->GetText() );
|
|
|
|
|
|
|
|
if( resetVis )
|
2022-09-03 20:54:05 +00:00
|
|
|
{
|
2021-02-13 21:28:06 +00:00
|
|
|
field.SetVisible( parentField->IsVisible() );
|
2022-09-03 20:54:05 +00:00
|
|
|
field.SetNameShown( parentField->IsNameShown() );
|
|
|
|
}
|
2021-02-13 21:28:06 +00:00
|
|
|
|
|
|
|
if( resetEffects )
|
|
|
|
{
|
2021-12-28 22:13:54 +00:00
|
|
|
// Careful: the visible bit and position are also set by SetAttributes()
|
2021-02-14 18:29:27 +00:00
|
|
|
bool visible = field.IsVisible();
|
2022-01-01 06:04:08 +00:00
|
|
|
VECTOR2I pos = field.GetPosition();
|
2021-02-14 18:29:27 +00:00
|
|
|
|
2021-12-28 22:13:54 +00:00
|
|
|
field.SetAttributes( *parentField );
|
2021-02-14 18:29:27 +00:00
|
|
|
|
2021-02-13 21:28:06 +00:00
|
|
|
field.SetVisible( visible );
|
2021-02-14 18:29:27 +00:00
|
|
|
field.SetPosition( pos );
|
2021-02-13 21:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( resetPositions )
|
|
|
|
field.SetTextPos( parentField->GetTextPos() );
|
|
|
|
}
|
|
|
|
else if( removeExtras )
|
|
|
|
{
|
|
|
|
copy = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( copy )
|
|
|
|
result.emplace_back( std::move( field ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<LIB_FIELD*> parentFields;
|
|
|
|
int idx = result.size();
|
|
|
|
|
|
|
|
flattenedParent->GetFields( parentFields );
|
|
|
|
|
|
|
|
for( LIB_FIELD* parentField : parentFields )
|
|
|
|
{
|
|
|
|
if( !alg::contains( m_updateFields, parentField->GetName() ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( !m_symbol->FindField( parentField->GetName() ) )
|
|
|
|
{
|
|
|
|
result.emplace_back( m_symbol, idx++ );
|
|
|
|
LIB_FIELD* newField = &result.back();
|
|
|
|
|
|
|
|
newField->SetName( parentField->GetCanonicalName() );
|
|
|
|
newField->SetText( parentField->GetText() );
|
2021-12-28 22:13:54 +00:00
|
|
|
newField->SetAttributes( *parentField ); // Includes visible bit and position
|
2021-02-13 21:28:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_symbol->SetFields( result );
|
|
|
|
|
2023-06-09 21:41:33 +00:00
|
|
|
commit.Push( _( "Update Symbol Fields" ) );
|
|
|
|
|
2021-02-13 21:28:06 +00:00
|
|
|
m_editFrame->RebuildView();
|
2021-07-21 21:37:49 +00:00
|
|
|
wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
|
2021-02-13 21:28:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|