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
|
|
|
|
m_fields.clear();
|
|
|
|
|
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 ) )
|
|
|
|
m_fields.insert( m_fieldsBox->GetString( i ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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 )
|
|
|
|
{
|
|
|
|
const auto part = component->GetPartRef().lock();
|
|
|
|
|
|
|
|
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 )
|
|
|
|
m_fields.insert( field->GetName( false ) );
|
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 )
|
|
|
|
{
|
|
|
|
m_fieldsBox->Append( m_components.front()->GetField( i )->GetName( false ) );
|
|
|
|
|
|
|
|
if( i != REFERENCE && i != VALUE )
|
|
|
|
m_fieldsBox->Check( i, true );
|
|
|
|
}
|
|
|
|
|
2017-10-02 12:53:48 +00:00
|
|
|
for( const auto& field : m_fields )
|
|
|
|
{
|
|
|
|
int idx = m_fieldsBox->Append( field );
|
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 )
|
|
|
|
{
|
|
|
|
std::vector<SCH_FIELD*> oldFields;
|
|
|
|
SCH_FIELDS newFields;
|
|
|
|
|
|
|
|
PART_SPTR libPart = aComponent->GetPartRef().lock();
|
2018-07-02 19:02:27 +00:00
|
|
|
|
|
|
|
if( libPart == nullptr ) // the symbol is not found in lib: cannot update fields
|
|
|
|
return;
|
|
|
|
|
2018-06-26 17:50:58 +00:00
|
|
|
LIB_ALIAS* alias = m_frame->GetLibAlias( aComponent->GetLibId() );
|
|
|
|
|
2017-10-02 12:53:48 +00:00
|
|
|
aComponent->GetFields( oldFields, false );
|
|
|
|
|
|
|
|
for( auto compField : oldFields )
|
|
|
|
{
|
|
|
|
// If requested, transfer only fields that occur also in the original library part
|
|
|
|
if( !m_removeExtraBox->IsChecked() || libPart->FindField( compField->GetName() ) )
|
|
|
|
newFields.push_back( *compField );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the requested field values
|
|
|
|
for( const auto& partField : m_fields )
|
|
|
|
{
|
|
|
|
LIB_FIELD* libField = libPart->FindField( partField );
|
|
|
|
|
|
|
|
if( !libField )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SCH_FIELD* field = nullptr;
|
|
|
|
auto it = std::find_if( newFields.begin(), newFields.end(), [&] ( const SCH_FIELD& f )
|
|
|
|
{ return f.GetName() == partField; } );
|
|
|
|
|
|
|
|
if( it != newFields.end() )
|
|
|
|
{
|
|
|
|
field = &*it;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Missing field, it has to be added to the component
|
|
|
|
SCH_FIELD f( wxPoint( 0, 0 ), newFields.size(), aComponent, partField );
|
|
|
|
newFields.push_back( f );
|
|
|
|
field = &newFields.back();
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:50:58 +00:00
|
|
|
wxString fieldValue = libField->GetText();
|
|
|
|
|
|
|
|
if( alias )
|
|
|
|
{
|
|
|
|
if( partField == TEMPLATE_FIELDNAME::GetDefaultFieldName( VALUE ) )
|
|
|
|
fieldValue = alias->GetName();
|
|
|
|
else if( partField == TEMPLATE_FIELDNAME::GetDefaultFieldName( DATASHEET ) )
|
|
|
|
fieldValue = alias->GetDocFileName();
|
|
|
|
}
|
|
|
|
|
2018-02-04 11:27:51 +00:00
|
|
|
// If the library field is empty an update would clear an existing entry.
|
|
|
|
// Check if this is the desired behavior.
|
2019-01-30 16:28:40 +00:00
|
|
|
if( !fieldValue.empty() || m_resetEmpty->IsChecked() )
|
2018-06-26 17:50:58 +00:00
|
|
|
field->SetText( fieldValue );
|
2018-03-12 11:08:31 +00:00
|
|
|
|
|
|
|
if( m_resetVisibility->IsChecked() )
|
|
|
|
field->SetVisible( libField->IsVisible() );
|
|
|
|
|
|
|
|
if( m_resetPosition->IsChecked() )
|
|
|
|
{
|
|
|
|
field->SetTextAngle( libField->GetTextAngle() );
|
|
|
|
|
|
|
|
// Board fields are board-relative; symbol editor fields are component-relative
|
|
|
|
if( m_createUndo )
|
|
|
|
field->SetTextPos( libField->GetTextPos() + aComponent->GetPosition() );
|
|
|
|
else
|
|
|
|
field->SetTextPos( libField->GetTextPos() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_resetSizeAndStyle->IsChecked() )
|
2018-02-04 11:27:51 +00:00
|
|
|
{
|
2018-03-12 11:08:31 +00:00
|
|
|
field->SetHorizJustify( libField->GetHorizJustify() );
|
|
|
|
field->SetVertJustify( libField->GetVertJustify() );
|
|
|
|
field->SetTextSize( libField->GetTextSize() );
|
|
|
|
field->SetItalic( libField->IsItalic() );
|
|
|
|
field->SetBold( libField->IsBold() );
|
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 );
|
|
|
|
}
|