2023-09-26 17:31:45 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2023 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 2
|
|
|
|
* 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:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pgm_base.h>
|
2023-09-29 16:02:57 +00:00
|
|
|
#include <kiface_base.h>
|
2023-09-26 17:31:45 +00:00
|
|
|
#include <kiway.h>
|
|
|
|
#include <kiway_express.h>
|
|
|
|
#include <wx/button.h>
|
2023-09-29 16:02:57 +00:00
|
|
|
#include <wx/checkbox.h>
|
2023-09-26 17:31:45 +00:00
|
|
|
#include <kiplatform/ui.h>
|
|
|
|
#include <widgets/panel_footprint_chooser.h>
|
|
|
|
#include <settings/settings_manager.h>
|
|
|
|
#include <footprint_editor_settings.h>
|
|
|
|
#include <footprint_chooser_frame.h>
|
|
|
|
|
|
|
|
|
|
|
|
static wxArrayString s_FootprintHistoryList;
|
|
|
|
static unsigned s_FootprintHistoryMaxCount = 8;
|
|
|
|
|
|
|
|
static void AddFootprintToHistory( const wxString& aName )
|
|
|
|
{
|
|
|
|
// Remove duplicates
|
2023-09-29 16:02:57 +00:00
|
|
|
for( int ii = (int) s_FootprintHistoryList.GetCount() - 1; ii >= 0; --ii )
|
2023-09-26 17:31:45 +00:00
|
|
|
{
|
|
|
|
if( s_FootprintHistoryList[ ii ] == aName )
|
2023-09-28 13:09:45 +00:00
|
|
|
s_FootprintHistoryList.RemoveAt( (size_t) ii );
|
2023-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new name at the beginning of the history list
|
|
|
|
s_FootprintHistoryList.Insert( aName, 0 );
|
|
|
|
|
|
|
|
// Remove extra names
|
|
|
|
while( s_FootprintHistoryList.GetCount() >= s_FootprintHistoryMaxCount )
|
|
|
|
s_FootprintHistoryList.RemoveAt( s_FootprintHistoryList.GetCount() - 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_EVENT_TABLE( FOOTPRINT_CHOOSER_FRAME, PCB_BASE_FRAME )
|
2023-09-29 16:02:57 +00:00
|
|
|
EVT_MENU( wxID_CLOSE, FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser )
|
2023-09-26 17:31:45 +00:00
|
|
|
EVT_BUTTON( wxID_OK, FOOTPRINT_CHOOSER_FRAME::OnOK )
|
2023-09-29 16:02:57 +00:00
|
|
|
EVT_BUTTON( wxID_CANCEL, FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser )
|
2023-09-26 17:31:45 +00:00
|
|
|
EVT_PAINT( FOOTPRINT_CHOOSER_FRAME::OnPaint )
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
|
2023-10-03 13:48:27 +00:00
|
|
|
#define MODAL_FRAME ( wxRESIZE_BORDER | wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxCLIP_CHILDREN \
|
2023-09-26 17:31:45 +00:00
|
|
|
| wxWANTS_CHARS | wxFRAME_NO_TASKBAR | wxSTAY_ON_TOP )
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_CHOOSER_FRAME::FOOTPRINT_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent ) :
|
2023-09-29 16:02:57 +00:00
|
|
|
PCB_BASE_FRAME( aKiway, aParent, FRAME_FOOTPRINT_CHOOSER, _( "Footprint Chooser" ),
|
2023-10-03 13:48:27 +00:00
|
|
|
wxDefaultPosition, wxDefaultSize, MODAL_FRAME,
|
2023-09-29 16:02:57 +00:00
|
|
|
FOOTPRINT_CHOOSER_FRAME_NAME ),
|
|
|
|
m_filterByPinCount( nullptr ),
|
|
|
|
m_filterByFPFilters( nullptr ),
|
2023-09-30 16:17:09 +00:00
|
|
|
m_pinCount( 0 ),
|
|
|
|
m_firstPaintEvent( true )
|
2023-09-26 17:31:45 +00:00
|
|
|
{
|
|
|
|
SetModal( true );
|
|
|
|
|
|
|
|
wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
|
|
|
|
m_chooserPanel = new PANEL_FOOTPRINT_CHOOSER( this, this, s_FootprintHistoryList,
|
2023-09-29 16:02:57 +00:00
|
|
|
// Filter
|
2023-10-02 15:24:31 +00:00
|
|
|
[this]( LIB_TREE_NODE& aNode ) -> int
|
2023-09-29 16:02:57 +00:00
|
|
|
{
|
|
|
|
return filterFootprint( aNode );
|
|
|
|
},
|
|
|
|
// Close handler
|
|
|
|
[this]()
|
|
|
|
{
|
|
|
|
wxCommandEvent dummy;
|
|
|
|
OnOK( dummy );
|
|
|
|
} );
|
2023-09-26 17:31:45 +00:00
|
|
|
|
2023-10-02 15:33:26 +00:00
|
|
|
sizer->Add( m_chooserPanel, 1, wxEXPAND | wxBOTTOM, 2 );
|
2023-09-26 17:31:45 +00:00
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
wxBoxSizer* fpFilterSizer = new wxBoxSizer( wxVERTICAL );
|
|
|
|
|
|
|
|
m_filterByFPFilters = new wxCheckBox( this, wxID_ANY, _( "Apply footprint filters" ) );
|
2023-10-02 15:33:26 +00:00
|
|
|
fpFilterSizer->Add( m_filterByFPFilters, 0, wxTOP | wxEXPAND, 5 );
|
2023-09-29 16:02:57 +00:00
|
|
|
m_filterByFPFilters->Show( false );
|
|
|
|
|
|
|
|
sizer->Add( fpFilterSizer, 0, wxEXPAND | wxLEFT, 10 );
|
|
|
|
|
|
|
|
wxBoxSizer* buttonsSizer = new wxBoxSizer( wxHORIZONTAL );
|
|
|
|
|
|
|
|
m_filterByPinCount = new wxCheckBox( this, wxID_ANY, _( "Filter by pin count" ) );
|
|
|
|
buttonsSizer->Add( m_filterByPinCount, 0, wxLEFT | wxTOP | wxALIGN_TOP, 5 );
|
|
|
|
m_filterByPinCount->Show( false );
|
|
|
|
|
|
|
|
if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
|
|
|
|
{
|
|
|
|
m_filterByFPFilters->SetValue( cfg->m_FootprintChooser.use_fp_filters );
|
|
|
|
m_filterByPinCount->SetValue( cfg->m_FootprintChooser.filter_on_pin_count );
|
|
|
|
}
|
|
|
|
|
2023-09-26 17:31:45 +00:00
|
|
|
wxStdDialogButtonSizer* sdbSizer = new wxStdDialogButtonSizer();
|
|
|
|
wxButton* okButton = new wxButton( this, wxID_OK );
|
|
|
|
wxButton* cancelButton = new wxButton( this, wxID_CANCEL );
|
2023-09-29 16:02:57 +00:00
|
|
|
|
2023-09-26 17:31:45 +00:00
|
|
|
sdbSizer->AddButton( okButton );
|
|
|
|
sdbSizer->AddButton( cancelButton );
|
|
|
|
sdbSizer->Realize();
|
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
buttonsSizer->Add( sdbSizer, 1, wxALL, 5 );
|
|
|
|
|
|
|
|
sizer->Add( buttonsSizer, 0, wxEXPAND | wxLEFT, 5 );
|
2023-09-26 17:31:45 +00:00
|
|
|
SetSizer( sizer );
|
|
|
|
|
|
|
|
SetTitle( GetTitle() + wxString::Format( _( " (%d items loaded)" ),
|
|
|
|
m_chooserPanel->GetItemCount() ) );
|
|
|
|
|
|
|
|
Layout();
|
|
|
|
m_chooserPanel->FinishSetup();
|
2023-09-29 16:02:57 +00:00
|
|
|
|
|
|
|
m_filterByPinCount->Bind( wxEVT_CHECKBOX,
|
|
|
|
[&]( wxCommandEvent& evt )
|
|
|
|
{
|
|
|
|
m_chooserPanel->Regenerate();
|
|
|
|
} );
|
|
|
|
|
|
|
|
m_filterByFPFilters->Bind( wxEVT_CHECKBOX,
|
|
|
|
[&]( wxCommandEvent& evt )
|
|
|
|
{
|
|
|
|
m_chooserPanel->Regenerate();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FOOTPRINT_CHOOSER_FRAME::~FOOTPRINT_CHOOSER_FRAME()
|
|
|
|
{
|
|
|
|
if( PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( Kiface().KifaceSettings() ) )
|
|
|
|
{
|
|
|
|
cfg->m_FootprintChooser.use_fp_filters = m_filterByFPFilters->GetValue();
|
|
|
|
cfg->m_FootprintChooser.filter_on_pin_count = m_filterByPinCount->GetValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 15:24:31 +00:00
|
|
|
int FOOTPRINT_CHOOSER_FRAME::filterFootprint( LIB_TREE_NODE& aNode )
|
2023-09-29 16:02:57 +00:00
|
|
|
{
|
2023-10-02 15:24:31 +00:00
|
|
|
bool filtering = false;
|
|
|
|
|
|
|
|
auto patternMatch =
|
|
|
|
[]( LIB_ID& id, std::vector<std::unique_ptr<EDA_PATTERN_MATCH>>& filters ) -> bool
|
|
|
|
{
|
|
|
|
// The matching is case insensitive
|
|
|
|
wxString name;
|
|
|
|
|
|
|
|
for( const std::unique_ptr<EDA_PATTERN_MATCH>& filter : filters )
|
|
|
|
{
|
|
|
|
name.Empty();
|
|
|
|
|
|
|
|
// If the filter contains a ':' then include the library name in the pattern
|
|
|
|
if( filter->GetPattern().Contains( wxS( ":" ) ) )
|
|
|
|
name = id.GetUniStringLibNickname().Lower() + wxS( ":" );
|
|
|
|
|
|
|
|
name += id.GetUniStringLibItemName().Lower();
|
|
|
|
|
|
|
|
if( filter->Find( name ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
if( m_pinCount > 0 && m_filterByPinCount->GetValue() )
|
|
|
|
{
|
2023-10-02 15:24:31 +00:00
|
|
|
filtering = true;
|
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
if( aNode.m_PinCount != m_pinCount )
|
2023-10-02 15:24:31 +00:00
|
|
|
return -1;
|
2023-09-29 16:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !m_fpFilters.empty() && m_filterByFPFilters->GetValue() )
|
|
|
|
{
|
2023-10-02 15:24:31 +00:00
|
|
|
filtering = true;
|
2023-09-29 16:02:57 +00:00
|
|
|
|
2023-10-02 15:24:31 +00:00
|
|
|
if( !patternMatch( aNode.m_LibId, m_fpFilters ) )
|
|
|
|
return -1;
|
2023-09-29 16:02:57 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 15:24:31 +00:00
|
|
|
if( filtering )
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
2023-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FOOTPRINT_CHOOSER_FRAME::doCloseWindow()
|
|
|
|
{
|
|
|
|
// Only dismiss a modal frame once, so that the return values set by
|
|
|
|
// the prior DismissModal() are not bashed for ShowModal().
|
|
|
|
if( !IsDismissed() )
|
|
|
|
DismissModal( false );
|
|
|
|
|
|
|
|
// window to be destroyed by the caller of KIWAY_PLAYER::ShowModal()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
WINDOW_SETTINGS* FOOTPRINT_CHOOSER_FRAME::GetWindowSettings( APP_SETTINGS_BASE* aCfg )
|
|
|
|
{
|
|
|
|
PCBNEW_SETTINGS* cfg = dynamic_cast<PCBNEW_SETTINGS*>( aCfg );
|
|
|
|
wxCHECK_MSG( cfg, nullptr, wxT( "config not existing" ) );
|
|
|
|
|
|
|
|
return &cfg->m_FootprintViewer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
COLOR_SETTINGS* FOOTPRINT_CHOOSER_FRAME::GetColorSettings( bool aForceRefresh ) const
|
|
|
|
{
|
|
|
|
auto* settings = Pgm().GetSettingsManager().GetAppSettings<FOOTPRINT_EDITOR_SETTINGS>();
|
|
|
|
|
|
|
|
if( settings )
|
|
|
|
return Pgm().GetSettingsManager().GetColorSettings( settings->m_ColorTheme );
|
|
|
|
else
|
|
|
|
return Pgm().GetSettingsManager().GetColorSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FOOTPRINT_CHOOSER_FRAME::KiwayMailIn( KIWAY_EXPRESS& mail )
|
|
|
|
{
|
|
|
|
const std::string& payload = mail.GetPayload();
|
|
|
|
|
|
|
|
switch( mail.Command() )
|
|
|
|
{
|
|
|
|
case MAIL_SYMBOL_NETLIST:
|
|
|
|
{
|
2023-09-29 16:02:57 +00:00
|
|
|
m_pinCount = 0;
|
|
|
|
m_fpFilters.clear();
|
|
|
|
|
2023-09-26 17:31:45 +00:00
|
|
|
/*
|
|
|
|
* Symbol netlist format:
|
2023-09-29 16:02:57 +00:00
|
|
|
* pinCount
|
|
|
|
* fpFilters
|
2023-09-26 17:31:45 +00:00
|
|
|
*/
|
|
|
|
std::vector<std::string> strings = split( payload, "\r" );
|
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
if( strings.size() >= 1 )
|
2023-09-26 17:31:45 +00:00
|
|
|
{
|
2023-09-29 16:02:57 +00:00
|
|
|
wxString pinCountStr( strings[0] );
|
|
|
|
pinCountStr.ToInt( &m_pinCount );
|
2023-09-26 17:31:45 +00:00
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
if( m_pinCount > 0 )
|
|
|
|
{
|
|
|
|
m_filterByPinCount->SetLabel( m_filterByPinCount->GetLabel()
|
|
|
|
+ wxString::Format( wxS( " (%d)" ), m_pinCount ) );
|
|
|
|
m_filterByPinCount->Show( true );
|
|
|
|
}
|
|
|
|
}
|
2023-09-26 17:31:45 +00:00
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
if( strings.size() >= 2 && !strings[1].empty() )
|
|
|
|
{
|
|
|
|
for( const wxString& filter : wxSplit( strings[1], ' ' ) )
|
2023-09-26 17:31:45 +00:00
|
|
|
{
|
2023-09-29 16:02:57 +00:00
|
|
|
m_fpFilters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
|
|
|
|
m_fpFilters.back()->SetPattern( filter.Lower() );
|
2023-09-26 17:31:45 +00:00
|
|
|
}
|
2023-09-29 16:02:57 +00:00
|
|
|
|
|
|
|
m_filterByFPFilters->SetLabel( m_filterByFPFilters->GetLabel()
|
|
|
|
+ wxString::Format( wxS( " (%s)" ), strings[1] ) );
|
|
|
|
m_filterByFPFilters->Show( true );
|
2023-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FOOTPRINT_CHOOSER_FRAME::ShowModal( wxString* aFootprint, wxWindow* aParent )
|
|
|
|
{
|
|
|
|
if( aFootprint && !aFootprint->IsEmpty() )
|
|
|
|
{
|
|
|
|
LIB_ID fpid;
|
|
|
|
|
|
|
|
fpid.Parse( *aFootprint, true );
|
|
|
|
|
|
|
|
if( fpid.IsValid() )
|
|
|
|
m_chooserPanel->SetPreselect( fpid );
|
|
|
|
}
|
|
|
|
|
|
|
|
return KIWAY_PLAYER::ShowModal( aFootprint, aParent );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FOOTPRINT_CHOOSER_FRAME::OnPaint( wxPaintEvent& aEvent )
|
|
|
|
{
|
|
|
|
if( m_firstPaintEvent )
|
|
|
|
{
|
|
|
|
KIPLATFORM::UI::FixupCancelButtonCmdKeyCollision( this );
|
|
|
|
KIPLATFORM::UI::ForceFocus( m_chooserPanel->GetFocusTarget() );
|
|
|
|
|
|
|
|
m_firstPaintEvent = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aEvent.Skip();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FOOTPRINT_CHOOSER_FRAME::OnOK( wxCommandEvent& aEvent )
|
|
|
|
{
|
|
|
|
LIB_ID fpID = m_chooserPanel->GetSelectedLibId();
|
|
|
|
|
|
|
|
if( fpID.IsValid() )
|
|
|
|
{
|
|
|
|
wxString footprint = fpID.Format();
|
|
|
|
|
|
|
|
AddFootprintToHistory( footprint );
|
|
|
|
DismissModal( true, footprint );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DismissModal( false );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-29 16:02:57 +00:00
|
|
|
void FOOTPRINT_CHOOSER_FRAME::closeFootprintChooser( wxCommandEvent& aEvent )
|
2023-09-26 17:31:45 +00:00
|
|
|
{
|
|
|
|
Close( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|