/* * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2014 Henner Zeller * Copyright (C) 2016-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 #include // For SYMBOL_LIBRARY_FILTER #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include wxString PANEL_SYMBOL_CHOOSER::g_symbolSearchString; wxString PANEL_SYMBOL_CHOOSER::g_powerSearchString; PANEL_SYMBOL_CHOOSER::PANEL_SYMBOL_CHOOSER( SCH_BASE_FRAME* aFrame, wxWindow* aParent, const SYMBOL_LIBRARY_FILTER* aFilter, std::vector& aHistoryList, bool aAllowFieldEdits, bool aShowFootprints, std::function aCloseHandler ) : wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ), m_symbol_preview( nullptr ), m_hsplitter( nullptr ), m_vsplitter( nullptr ), m_fp_sel_ctrl( nullptr ), m_fp_preview( nullptr ), m_tree( nullptr ), m_details( nullptr ), m_frame( aFrame ), m_closeHandler( std::move( aCloseHandler ) ), m_showPower( false ), m_allow_field_edits( aAllowFieldEdits ), m_show_footprints( aShowFootprints ) { SYMBOL_LIB_TABLE* libs = PROJECT_SCH::SchSymbolLibTable( &m_frame->Prj() ); COMMON_SETTINGS::SESSION& session = Pgm().GetCommonSettings()->m_Session; PROJECT_FILE& project = m_frame->Prj().GetProjectFile(); // Make sure settings are loaded before we start running multi-threaded symbol loaders Pgm().GetSettingsManager().GetAppSettings(); Pgm().GetSettingsManager().GetAppSettings(); m_adapter = SYMBOL_TREE_MODEL_ADAPTER::Create( m_frame, libs ); SYMBOL_TREE_MODEL_ADAPTER* adapter = static_cast( m_adapter.get() ); bool loaded = false; if( aFilter ) { const wxArrayString& liblist = aFilter->GetAllowedLibList(); for( const wxString& nickname : liblist ) { if( libs->HasLibrary( nickname, true ) ) { loaded = true; bool pinned = alg::contains( session.pinned_symbol_libs, nickname ) || alg::contains( project.m_PinnedSymbolLibs, nickname ); if( libs->FindRow( nickname )->GetIsVisible() ) adapter->AddLibrary( nickname, pinned ); } } adapter->AssignIntrinsicRanks(); if( aFilter->GetFilterPowerSymbols() ) { adapter->SetFilter( SYMBOL_TREE_MODEL_ADAPTER::SYM_FILTER_POWER ); m_showPower = true; m_show_footprints = false; } } std::vector history_list_storage; std::vector history_list; history_list_storage.reserve( aHistoryList.size() ); for( const PICKED_SYMBOL& i : aHistoryList ) { LIB_SYMBOL* symbol = m_frame->GetLibSymbol( i.LibId ); // This can be null, for example when a symbol has been deleted from a library if( symbol ) { history_list_storage.emplace_back( *symbol ); for( const std::pair& fieldDef : i.Fields ) { LIB_FIELD* field = history_list_storage.back().GetFieldById( fieldDef.first ); if( field ) field->SetText( fieldDef.second ); } history_list.push_back( &history_list_storage.back() ); } } adapter->DoAddLibrary( wxT( "-- " ) + _( "Recently Used" ) + wxT( " --" ), wxEmptyString, history_list, false, true ); if( !aHistoryList.empty() ) adapter->SetPreselectNode( aHistoryList[0].LibId, aHistoryList[0].Unit ); const std::vector< wxString > libNicknames = libs->GetLogicalLibs(); if( !loaded ) { if( !adapter->AddLibraries( libNicknames, m_frame ) ) { // loading cancelled by user m_closeHandler(); } } // ------------------------------------------------------------------------------------- // Construct the actual panel // wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL ); // Use a slightly different layout, with a details pane spanning the entire window, // if we're not showing footprints. if( m_show_footprints ) { m_hsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH ); //Avoid the splitter window being assigned as the Parent to additional windows m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT ); sizer->Add( m_hsplitter, 1, wxEXPAND, 5 ); } else { m_vsplitter = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH ); m_hsplitter = new wxSplitterWindow( m_vsplitter, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_LIVE_UPDATE | wxSP_NOBORDER | wxSP_3DSASH ); // Avoid the splitter window being assigned as the parent to additional windows. m_vsplitter->SetExtraStyle( wxWS_EX_TRANSIENT ); m_hsplitter->SetExtraStyle( wxWS_EX_TRANSIENT ); wxPanel* detailsPanel = new wxPanel( m_vsplitter ); wxBoxSizer* detailsSizer = new wxBoxSizer( wxVERTICAL ); detailsPanel->SetSizer( detailsSizer ); m_details = new HTML_WINDOW( detailsPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize ); detailsSizer->Add( m_details, 1, wxEXPAND, 5 ); detailsPanel->Layout(); detailsSizer->Fit( detailsPanel ); m_vsplitter->SetSashGravity( 0.5 ); m_vsplitter->SetMinimumPaneSize( 20 ); m_vsplitter->SplitHorizontally( m_hsplitter, detailsPanel ); sizer->Add( m_vsplitter, 1, wxEXPAND, 5 ); } wxPanel* treePanel = new wxPanel( m_hsplitter ); wxBoxSizer* treeSizer = new wxBoxSizer( wxVERTICAL ); treePanel->SetSizer( treeSizer ); m_tree = new LIB_TREE( treePanel, m_showPower ? wxT( "power" ) : wxT( "symbols" ), libs, m_adapter, LIB_TREE::FLAGS::ALL_WIDGETS, m_details ); treeSizer->Add( m_tree, 1, wxEXPAND, 5 ); treePanel->Layout(); treeSizer->Fit( treePanel ); m_adapter->FinishTreeInitialization(); if( m_showPower ) m_tree->SetSearchString( g_powerSearchString ); else m_tree->SetSearchString( g_symbolSearchString ); m_hsplitter->SetSashGravity( 0.8 ); m_hsplitter->SetMinimumPaneSize( 20 ); m_hsplitter->SplitVertically( treePanel, constructRightPanel( m_hsplitter ) ); m_dbl_click_timer = new wxTimer( this ); SetSizer( sizer ); Layout(); Bind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onCloseTimer, this, m_dbl_click_timer->GetId() ); Bind( EVT_LIBITEM_SELECTED, &PANEL_SYMBOL_CHOOSER::onSymbolSelected, this ); Bind( EVT_LIBITEM_CHOSEN, &PANEL_SYMBOL_CHOOSER::onSymbolChosen, this ); if( m_fp_sel_ctrl ) { m_fp_sel_ctrl->Bind( EVT_FOOTPRINT_SELECTED, &PANEL_SYMBOL_CHOOSER::onFootprintSelected, this ); } if( m_details ) { m_details->Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( PANEL_SYMBOL_CHOOSER::OnCharHook ), nullptr, this ); } } PANEL_SYMBOL_CHOOSER::~PANEL_SYMBOL_CHOOSER() { Unbind( wxEVT_TIMER, &PANEL_SYMBOL_CHOOSER::onCloseTimer, this ); Unbind( EVT_LIBITEM_SELECTED, &PANEL_SYMBOL_CHOOSER::onSymbolSelected, this ); Unbind( EVT_LIBITEM_CHOSEN, &PANEL_SYMBOL_CHOOSER::onSymbolChosen, this ); // Stop the timer during destruction early to avoid potential race conditions (that do happen) m_dbl_click_timer->Stop(); delete m_dbl_click_timer; if( m_showPower ) g_powerSearchString = m_tree->GetSearchString(); else g_symbolSearchString = m_tree->GetSearchString(); if( m_fp_sel_ctrl ) { m_fp_sel_ctrl->Unbind( EVT_FOOTPRINT_SELECTED, &PANEL_SYMBOL_CHOOSER::onFootprintSelected, this ); } if( m_details ) { m_details->Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( PANEL_SYMBOL_CHOOSER::OnCharHook ), nullptr, this ); } if( EESCHEMA_SETTINGS* cfg = dynamic_cast( Kiface().KifaceSettings() ) ) { cfg->m_SymChooserPanel.width = GetParent()->GetSize().x; cfg->m_SymChooserPanel.height = GetParent()->GetSize().y; cfg->m_SymChooserPanel.sash_pos_h = m_hsplitter->GetSashPosition(); if( m_vsplitter ) cfg->m_SymChooserPanel.sash_pos_v = m_vsplitter->GetSashPosition(); cfg->m_SymChooserPanel.sort_mode = m_tree->GetSortMode(); } } wxPanel* PANEL_SYMBOL_CHOOSER::constructRightPanel( wxWindow* aParent ) { EDA_DRAW_PANEL_GAL::GAL_TYPE backend; if( m_frame->GetCanvas() ) { backend = m_frame->GetCanvas()->GetBackend(); } else { EESCHEMA_SETTINGS* cfg = Pgm().GetSettingsManager().GetAppSettings(); backend = (EDA_DRAW_PANEL_GAL::GAL_TYPE) cfg->m_Graphics.canvas_type; } wxPanel* panel = new wxPanel( aParent ); wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL ); m_symbol_preview = new SYMBOL_PREVIEW_WIDGET( panel, &m_frame->Kiway(), true, backend ); m_symbol_preview->SetLayoutDirection( wxLayout_LeftToRight ); if( m_show_footprints ) { FOOTPRINT_LIST* fp_list = FOOTPRINT_LIST::GetInstance( m_frame->Kiway() ); sizer->Add( m_symbol_preview, 11, wxEXPAND | wxBOTTOM, 5 ); if ( fp_list ) { if( m_allow_field_edits ) m_fp_sel_ctrl = new FOOTPRINT_SELECT_WIDGET( m_frame, panel, fp_list, true ); m_fp_preview = new FOOTPRINT_PREVIEW_WIDGET( panel, m_frame->Kiway() ); m_fp_preview->SetUserUnits( m_frame->GetUserUnits() ); } if( m_fp_sel_ctrl ) sizer->Add( m_fp_sel_ctrl, 0, wxEXPAND | wxTOP | wxBOTTOM, 4 ); if( m_fp_preview ) sizer->Add( m_fp_preview, 10, wxEXPAND, 5 ); } else { sizer->Add( m_symbol_preview, 1, wxEXPAND, 5 ); } panel->SetSizer( sizer ); panel->Layout(); sizer->Fit( panel ); return panel; } void PANEL_SYMBOL_CHOOSER::FinishSetup() { if( EESCHEMA_SETTINGS* cfg = dynamic_cast( Kiface().KifaceSettings() ) ) { auto horizPixelsFromDU = [&]( int x ) -> int { wxSize sz( x, 0 ); return GetParent()->ConvertDialogToPixels( sz ).x; }; EESCHEMA_SETTINGS::PANEL_SYM_CHOOSER& panelCfg = cfg->m_SymChooserPanel; int w = panelCfg.width > 40 ? panelCfg.width : horizPixelsFromDU( 440 ); int h = panelCfg.height > 40 ? panelCfg.height : horizPixelsFromDU( 340 ); GetParent()->SetSize( wxSize( w, h ) ); GetParent()->Layout(); // We specify the width of the right window (m_symbol_view_panel), because specify // the width of the left window does not work as expected when SetSashGravity() is called if( panelCfg.sash_pos_h < 0 ) panelCfg.sash_pos_h = horizPixelsFromDU( 220 ); if( panelCfg.sash_pos_v < 0 ) panelCfg.sash_pos_v = horizPixelsFromDU( 230 ); m_hsplitter->SetSashPosition( panelCfg.sash_pos_h ); if( m_vsplitter ) m_vsplitter->SetSashPosition( panelCfg.sash_pos_v ); m_adapter->SetSortMode( (LIB_TREE_MODEL_ADAPTER::SORT_MODE) panelCfg.sort_mode ); } if( m_fp_preview && m_fp_preview->IsInitialized() ) { // This hides the GAL panel and shows the status label m_fp_preview->SetStatusText( wxEmptyString ); } if( m_fp_sel_ctrl ) m_fp_sel_ctrl->Load( m_frame->Kiway(), m_frame->Prj() ); } void PANEL_SYMBOL_CHOOSER::OnCharHook( wxKeyEvent& e ) { if( m_details && e.GetKeyCode() == 'C' && e.ControlDown() && !e.AltDown() && !e.ShiftDown() && !e.MetaDown() ) { wxString txt = m_details->SelectionToText(); wxLogNull doNotLog; // disable logging of failed clipboard actions if( wxTheClipboard->Open() ) { wxTheClipboard->SetData( new wxTextDataObject( txt ) ); wxTheClipboard->Flush(); // Allow data to be available after closing KiCad wxTheClipboard->Close(); } } else { e.Skip(); } } void PANEL_SYMBOL_CHOOSER::SetPreselect( const LIB_ID& aPreselect ) { m_adapter->SetPreselectNode( aPreselect, 0 ); } LIB_ID PANEL_SYMBOL_CHOOSER::GetSelectedLibId( int* aUnit ) const { return m_tree->GetSelectedLibId( aUnit ); } void PANEL_SYMBOL_CHOOSER::onCloseTimer( wxTimerEvent& aEvent ) { // Hack because of eaten MouseUp event. See PANEL_SYMBOL_CHOOSER::onSymbolChosen // for the beginning of this spaghetti noodle. wxMouseState state = wxGetMouseState(); if( state.LeftIsDown() ) { // Mouse hasn't been raised yet, so fire the timer again. Otherwise the // purpose of this timer is defeated. m_dbl_click_timer->StartOnce( PANEL_SYMBOL_CHOOSER::DBLCLICK_DELAY ); } else { m_closeHandler(); } } void PANEL_SYMBOL_CHOOSER::showFootprintFor( LIB_ID const& aLibId ) { if( !m_fp_preview || !m_fp_preview->IsInitialized() ) return; LIB_SYMBOL* symbol = nullptr; try { symbol = PROJECT_SCH::SchSymbolLibTable( &m_frame->Prj() )->LoadSymbol( aLibId ); } catch( const IO_ERROR& ioe ) { wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ), aLibId.GetLibItemName().wx_str(), aLibId.GetLibNickname().wx_str(), ioe.What() ); } if( !symbol ) return; LIB_FIELD* fp_field = symbol->GetFieldById( FOOTPRINT_FIELD ); wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" ); showFootprint( fp_name ); } void PANEL_SYMBOL_CHOOSER::showFootprint( wxString const& aFootprint ) { if( !m_fp_preview || !m_fp_preview->IsInitialized() ) return; if( aFootprint == wxEmptyString ) { m_fp_preview->SetStatusText( _( "No footprint specified" ) ); } else { LIB_ID lib_id; if( lib_id.Parse( aFootprint ) == -1 && lib_id.IsValid() ) { m_fp_preview->ClearStatus(); m_fp_preview->DisplayFootprint( lib_id ); } else { m_fp_preview->SetStatusText( _( "Invalid footprint specified" ) ); } } } void PANEL_SYMBOL_CHOOSER::populateFootprintSelector( LIB_ID const& aLibId ) { if( !m_fp_sel_ctrl ) return; m_fp_sel_ctrl->ClearFilters(); LIB_SYMBOL* symbol = nullptr; if( aLibId.IsValid() ) { try { symbol = PROJECT_SCH::SchSymbolLibTable( &m_frame->Prj() )->LoadSymbol( aLibId ); } catch( const IO_ERROR& ioe ) { wxLogError( _( "Error loading symbol %s from library '%s'." ) + wxS( "\n%s" ), aLibId.GetLibItemName().wx_str(), aLibId.GetLibNickname().wx_str(), ioe.What() ); } } if( symbol != nullptr ) { LIB_PINS temp_pins; LIB_FIELD* fp_field = symbol->GetFieldById( FOOTPRINT_FIELD ); wxString fp_name = fp_field ? fp_field->GetFullText() : wxString( "" ); // All units, but only a single De Morgan variant. if( symbol->HasConversion() ) symbol->GetPins( temp_pins, 0, 1 ); else symbol->GetPins( temp_pins ); m_fp_sel_ctrl->FilterByPinCount( temp_pins.size() ); m_fp_sel_ctrl->FilterByFootprintFilters( symbol->GetFPFilters(), true ); m_fp_sel_ctrl->SetDefaultFootprint( fp_name ); m_fp_sel_ctrl->UpdateList(); m_fp_sel_ctrl->Enable(); } else { m_fp_sel_ctrl->UpdateList(); m_fp_sel_ctrl->Disable(); } } void PANEL_SYMBOL_CHOOSER::onFootprintSelected( wxCommandEvent& aEvent ) { m_fp_override = aEvent.GetString(); alg::delete_if( m_field_edits, []( std::pair const& i ) { return i.first == FOOTPRINT_FIELD; } ); m_field_edits.emplace_back( std::make_pair( FOOTPRINT_FIELD, m_fp_override ) ); showFootprint( m_fp_override ); } void PANEL_SYMBOL_CHOOSER::onSymbolSelected( wxCommandEvent& aEvent ) { LIB_TREE_NODE* node = m_tree->GetCurrentTreeNode(); if( node && node->m_LibId.IsValid() ) { m_symbol_preview->DisplaySymbol( node->m_LibId, node->m_Unit ); if( !node->m_Footprint.IsEmpty() ) showFootprint( node->m_Footprint ); else showFootprintFor( node->m_LibId ); populateFootprintSelector( node->m_LibId ); } else { m_symbol_preview->SetStatusText( _( "No symbol selected" ) ); if( m_fp_preview && m_fp_preview->IsInitialized() ) m_fp_preview->SetStatusText( wxEmptyString ); populateFootprintSelector( LIB_ID() ); } } void PANEL_SYMBOL_CHOOSER::onSymbolChosen( wxCommandEvent& aEvent ) { if( m_tree->GetSelectedLibId().IsValid() ) { // Got a selection. We can't just end the modal dialog here, because wx leaks some events // back to the parent window (in particular, the MouseUp following a double click). // // NOW, here's where it gets really fun. wxTreeListCtrl eats MouseUp. This isn't really // feasible to bypass without a fully custom wxDataViewCtrl implementation, and even then // might not be fully possible (docs are vague). To get around this, we use a one-shot // timer to schedule the dialog close. // // See PANEL_SYMBOL_CHOOSER::onCloseTimer for the other end of this spaghetti noodle. m_dbl_click_timer->StartOnce( PANEL_SYMBOL_CHOOSER::DBLCLICK_DELAY ); } }