2017-10-02 12:53:48 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 CERN
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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, you may find one here:
|
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dialog_update_fields.h"
|
|
|
|
|
2018-01-30 10:49:51 +00:00
|
|
|
#include <sch_edit_frame.h>
|
2017-10-02 12:53:48 +00:00
|
|
|
#include <sch_component.h>
|
|
|
|
#include <class_libentry.h>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
|
|
int InvokeDialogUpdateFields( SCH_EDIT_FRAME* aCaller,
|
|
|
|
const list<SCH_COMPONENT*> aComponents, bool aCreateUndoEntry )
|
|
|
|
{
|
|
|
|
DIALOG_UPDATE_FIELDS dlg( aCaller, aComponents, aCreateUndoEntry );
|
|
|
|
return dlg.ShowQuasiModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DIALOG_UPDATE_FIELDS::DIALOG_UPDATE_FIELDS( SCH_EDIT_FRAME* aParent,
|
|
|
|
const list<SCH_COMPONENT*>& aComponents, bool aCreateUndoEntry )
|
|
|
|
: DIALOG_UPDATE_FIELDS_BASE( aParent ), m_frame( aParent ),
|
|
|
|
m_components( aComponents ), m_createUndo( aCreateUndoEntry )
|
|
|
|
{
|
2018-03-12 11:08:31 +00:00
|
|
|
m_sdbSizerOK->SetDefault();
|
2017-10-02 12:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DIALOG_UPDATE_FIELDS::TransferDataFromWindow()
|
|
|
|
{
|
|
|
|
if( !wxDialog::TransferDataFromWindow() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( m_components.empty() )
|
|
|
|
return true; // nothing to process
|
|
|
|
|
|
|
|
|
|
|
|
// Create the set of fields to be updated
|
2020-04-01 14:00:40 +00:00
|
|
|
m_updateFields.clear();
|
2017-10-02 12:53:48 +00:00
|
|
|
|
2017-10-02 18:39:09 +00:00
|
|
|
for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
|
2017-10-02 12:53:48 +00:00
|
|
|
{
|
|
|
|
if( m_fieldsBox->IsChecked( i ) )
|
2020-04-01 14:00:40 +00:00
|
|
|
m_updateFields.insert( m_fieldsBox->GetString( i ) );
|
2017-10-02 12:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Undo buffer entry
|
|
|
|
if( m_createUndo )
|
|
|
|
{
|
|
|
|
PICKED_ITEMS_LIST itemsList;
|
|
|
|
|
|
|
|
for( auto component : m_components )
|
|
|
|
itemsList.PushItem( ITEM_PICKER( component, UR_CHANGED ) );
|
|
|
|
|
|
|
|
m_frame->SaveCopyInUndoList( itemsList, UR_CHANGED );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Do it!
|
|
|
|
for( auto component : m_components )
|
|
|
|
updateFields( component );
|
|
|
|
|
2018-10-18 09:50:43 +00:00
|
|
|
m_frame->SyncView();
|
|
|
|
m_frame->GetCanvas()->Refresh();
|
2017-10-02 12:53:48 +00:00
|
|
|
m_frame->OnModify();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DIALOG_UPDATE_FIELDS::TransferDataToWindow()
|
|
|
|
{
|
2018-03-12 11:08:31 +00:00
|
|
|
if( !wxDialog::TransferDataToWindow() || !m_components.size() )
|
2017-10-02 12:53:48 +00:00
|
|
|
return false;
|
|
|
|
|
2018-03-12 11:08:31 +00:00
|
|
|
// Collect all user field names from library parts of components that are going to be updated
|
2017-10-02 12:53:48 +00:00
|
|
|
{
|
|
|
|
for( auto component : m_components )
|
|
|
|
{
|
2019-11-06 19:15:42 +00:00
|
|
|
const std::unique_ptr< LIB_PART >& part = component->GetPartRef();
|
2017-10-02 12:53:48 +00:00
|
|
|
|
|
|
|
if( !part )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto& drawItems = part->GetDrawItems();
|
|
|
|
|
|
|
|
for( auto it = drawItems.begin( LIB_FIELD_T ); it != drawItems.end( LIB_FIELD_T ); ++it )
|
|
|
|
{
|
|
|
|
const LIB_FIELD* field = static_cast<const LIB_FIELD*>( &( *it ) );
|
2018-03-12 11:08:31 +00:00
|
|
|
|
|
|
|
if( field->GetId() >= MANDATORY_FIELDS )
|
2020-04-01 14:00:40 +00:00
|
|
|
m_updateFields.insert( field->GetName() );
|
2017-10-02 12:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the listbox widget
|
|
|
|
m_fieldsBox->Clear();
|
|
|
|
|
2018-03-12 11:08:31 +00:00
|
|
|
for( int i = 0; i < MANDATORY_FIELDS; ++i )
|
|
|
|
{
|
2020-03-26 11:02:59 +00:00
|
|
|
m_fieldsBox->Append( m_components.front()->GetField( i )->GetName() );
|
2018-03-12 11:08:31 +00:00
|
|
|
|
|
|
|
if( i != REFERENCE && i != VALUE )
|
|
|
|
m_fieldsBox->Check( i, true );
|
|
|
|
}
|
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
for( const wxString& fieldName : m_updateFields )
|
2017-10-02 12:53:48 +00:00
|
|
|
{
|
2020-04-01 14:00:40 +00:00
|
|
|
int idx = m_fieldsBox->Append( fieldName );
|
2018-03-12 11:08:31 +00:00
|
|
|
m_fieldsBox->Check( idx, true );
|
2017-10-02 12:53:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-24 09:31:18 +00:00
|
|
|
// Now all widgets have the size fixed, call FinishDialogSettings
|
|
|
|
FinishDialogSettings();
|
|
|
|
|
2017-10-02 12:53:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_UPDATE_FIELDS::updateFields( SCH_COMPONENT* aComponent )
|
|
|
|
{
|
|
|
|
SCH_FIELDS newFields;
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
std::unique_ptr< LIB_PART >& libPart = aComponent->GetPartRef();
|
2018-07-02 19:02:27 +00:00
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
if( !libPart ) // the symbol is not found in lib: cannot update fields
|
2018-07-02 19:02:27 +00:00
|
|
|
return;
|
|
|
|
|
2019-11-06 19:15:42 +00:00
|
|
|
LIB_PART* alias = m_frame->GetLibPart( aComponent->GetLibId() );
|
2018-06-26 17:50:58 +00:00
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
for( const SCH_FIELD& existingField : aComponent->GetFields() )
|
2017-10-02 12:53:48 +00:00
|
|
|
{
|
2020-04-01 14:00:40 +00:00
|
|
|
if( existingField.GetId() >= 0 && existingField.GetId() < MANDATORY_FIELDS )
|
|
|
|
{
|
|
|
|
newFields.push_back( existingField );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-02 12:53:48 +00:00
|
|
|
// If requested, transfer only fields that occur also in the original library part
|
2020-04-01 14:00:40 +00:00
|
|
|
if( m_removeExtraBox->IsChecked() && !libPart->FindField( existingField.GetName() ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
newFields.push_back( existingField );
|
2017-10-02 12:53:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
// Update the requested fields
|
|
|
|
for( const wxString& fieldName : m_updateFields )
|
2017-10-02 12:53:48 +00:00
|
|
|
{
|
2020-04-01 14:00:40 +00:00
|
|
|
LIB_FIELD* libField = libPart->FindField( fieldName );
|
2017-10-02 12:53:48 +00:00
|
|
|
|
|
|
|
if( !libField )
|
|
|
|
continue;
|
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
auto it = std::find_if( newFields.begin(), newFields.end(),
|
|
|
|
[&] ( const SCH_FIELD& f )
|
|
|
|
{
|
|
|
|
return f.GetName() == fieldName;
|
|
|
|
} );
|
2017-10-02 12:53:48 +00:00
|
|
|
|
|
|
|
if( it != newFields.end() )
|
|
|
|
{
|
2020-04-01 14:00:40 +00:00
|
|
|
SCH_FIELD* newField = &*it;
|
|
|
|
wxString fieldValue = libField->GetText();
|
2017-10-02 12:53:48 +00:00
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
if( alias )
|
|
|
|
{
|
|
|
|
if( fieldName == TEMPLATE_FIELDNAME::GetDefaultFieldName( VALUE ) )
|
|
|
|
fieldValue = alias->GetName();
|
|
|
|
else if( fieldName == TEMPLATE_FIELDNAME::GetDefaultFieldName( DATASHEET ) )
|
|
|
|
fieldValue = alias->GetDocFileName();
|
|
|
|
}
|
2018-06-26 17:50:58 +00:00
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
if( fieldValue.IsEmpty() )
|
|
|
|
{
|
|
|
|
// If the library field is empty an update would clear an existing entry.
|
|
|
|
// Check if this is the desired behavior.
|
|
|
|
if( m_resetEmpty->IsChecked() )
|
|
|
|
newField->SetText( wxEmptyString );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newField->SetText( fieldValue );
|
|
|
|
}
|
2018-06-26 17:50:58 +00:00
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
if( m_resetVisibility->IsChecked() )
|
|
|
|
{
|
|
|
|
newField->SetVisible( libField->IsVisible() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_resetPosition->IsChecked() )
|
|
|
|
{
|
|
|
|
newField->SetTextAngle( libField->GetTextAngle() );
|
2018-03-12 11:08:31 +00:00
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
// Schematic fields are schematic-relative; symbol editor fields are component-relative
|
|
|
|
if( m_createUndo )
|
|
|
|
newField->SetTextPos( libField->GetTextPos() + aComponent->GetPosition() );
|
|
|
|
else
|
|
|
|
newField->SetTextPos( libField->GetTextPos() );
|
|
|
|
}
|
2018-03-12 11:08:31 +00:00
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
if( m_resetSizeAndStyle->IsChecked() )
|
|
|
|
{
|
|
|
|
newField->SetHorizJustify( libField->GetHorizJustify() );
|
|
|
|
newField->SetVertJustify( libField->GetVertJustify() );
|
|
|
|
newField->SetTextSize( libField->GetTextSize() );
|
|
|
|
newField->SetItalic( libField->IsItalic() );
|
|
|
|
newField->SetBold( libField->IsBold() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2018-03-12 11:08:31 +00:00
|
|
|
{
|
2020-04-01 14:00:40 +00:00
|
|
|
// Missing field, it has to be added to the component
|
2020-04-18 20:04:41 +00:00
|
|
|
SCH_FIELD newField( wxPoint( 0, 0 ), newFields.size(), aComponent, fieldName );
|
2020-04-01 14:00:40 +00:00
|
|
|
|
|
|
|
newField.ImportValues( *libField );
|
|
|
|
newField.SetText( libField->GetText() );
|
2018-03-12 11:08:31 +00:00
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
// Schematic fields are schematic-relative; symbol editor fields are component-relative
|
2018-03-12 11:08:31 +00:00
|
|
|
if( m_createUndo )
|
2020-04-01 14:00:40 +00:00
|
|
|
newField.SetTextPos( libField->GetTextPos() + aComponent->GetPosition() );
|
2018-03-12 11:08:31 +00:00
|
|
|
else
|
2020-04-01 14:00:40 +00:00
|
|
|
newField.SetTextPos( libField->GetTextPos() );
|
2018-03-12 11:08:31 +00:00
|
|
|
|
2020-04-01 14:00:40 +00:00
|
|
|
newFields.push_back( newField );
|
2018-02-04 11:27:51 +00:00
|
|
|
}
|
2017-10-02 12:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply changes & clean-up
|
|
|
|
aComponent->SetFields( newFields );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_UPDATE_FIELDS::checkAll( bool aCheck )
|
|
|
|
{
|
2017-10-02 18:39:09 +00:00
|
|
|
for( unsigned i = 0; i < m_fieldsBox->GetCount(); ++i )
|
2017-10-02 12:53:48 +00:00
|
|
|
m_fieldsBox->Check( i, aCheck );
|
|
|
|
}
|