From 06446bb80e08f37db9fafdc231d4bc3935dba81e Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Mon, 2 Oct 2023 16:24:31 +0100 Subject: [PATCH] Only expand lib tree when filtering. --- common/lib_tree_model.cpp | 14 +++-- eeschema/widgets/panel_symbol_chooser.cpp | 11 ++-- include/lib_tree_model.h | 4 +- include/lib_tree_model_adapter.h | 6 +-- pcbnew/dialogs/dialog_footprint_chooser.cpp | 4 +- pcbnew/footprint_chooser_frame.cpp | 58 +++++++++++++-------- pcbnew/footprint_chooser_frame.h | 2 +- pcbnew/widgets/panel_footprint_chooser.cpp | 2 +- pcbnew/widgets/panel_footprint_chooser.h | 4 +- 9 files changed, 59 insertions(+), 46 deletions(-) diff --git a/common/lib_tree_model.cpp b/common/lib_tree_model.cpp index 15b92ddc46..5170913eba 100644 --- a/common/lib_tree_model.cpp +++ b/common/lib_tree_model.cpp @@ -31,10 +31,10 @@ // increase this score. This way, an empty search string will result in all components being // displayed as they have the minimum score. However, in that case, we avoid expanding all the // nodes asd the result is very unspecific. -static const unsigned kLowestDefaultScore = 1; +static const int kLowestDefaultScore = 1; -void LIB_TREE_NODE::ResetScore( std::function* aFilter ) +void LIB_TREE_NODE::ResetScore( std::function* aFilter ) { for( std::unique_ptr& child: m_Children ) child->ResetScore( aFilter ); @@ -219,17 +219,15 @@ void LIB_TREE_NODE_LIB_ID::Update( LIB_TREE_ITEM* aItem ) } -void LIB_TREE_NODE_LIB_ID::ResetScore( std::function* aFilter ) +void LIB_TREE_NODE_LIB_ID::ResetScore( std::function* aFilter ) { for( std::unique_ptr& child: m_Children ) child->ResetScore( aFilter ); - if( !aFilter ) - m_Score = kLowestDefaultScore; - else if( (*aFilter)(*this) ) - m_Score = kLowestDefaultScore + 1; + if( aFilter ) + m_Score = kLowestDefaultScore + (*aFilter)(*this); else - m_Score = 0; + m_Score = kLowestDefaultScore; } diff --git a/eeschema/widgets/panel_symbol_chooser.cpp b/eeschema/widgets/panel_symbol_chooser.cpp index d2a56cca11..f297048262 100644 --- a/eeschema/widgets/panel_symbol_chooser.cpp +++ b/eeschema/widgets/panel_symbol_chooser.cpp @@ -102,13 +102,12 @@ PANEL_SYMBOL_CHOOSER::PANEL_SYMBOL_CHOOSER( SCH_BASE_FRAME* aFrame, wxWindow* aP if( aFilter->GetFilterPowerSymbols() ) { - // Note: there is only a single filter ever used for symbols (the power filter), - // so the code simply sets a flag based on the filter being non-null. The filter - // is not (at present) actually called. - static std::function powerFilter = - []( LIB_TREE_NODE& ) -> bool + // HACK ALERT: the only filter ever used for symbols is the power filter, so we + // sometimes just look for the function pointer being non-null. + static std::function powerFilter = + []( LIB_TREE_NODE& ) -> int { - return true; + return 0; }; adapter->SetFilter( &powerFilter ); diff --git a/include/lib_tree_model.h b/include/lib_tree_model.h index 9e1cfb2472..d50218e689 100644 --- a/include/lib_tree_model.h +++ b/include/lib_tree_model.h @@ -85,7 +85,7 @@ public: /** * Initialize score to kLowestDefaultScore, recursively. */ - virtual void ResetScore( std::function* aFilter ); + virtual void ResetScore( std::function* aFilter ); /** * Store intrinsic ranks on all children of this node. See m_IntrinsicRank @@ -211,7 +211,7 @@ public: */ void Update( LIB_TREE_ITEM* aItem ); - void ResetScore( std::function* aFilter ) override; + void ResetScore( std::function* aFilter ) override; /** * Perform the actual search. diff --git a/include/lib_tree_model_adapter.h b/include/lib_tree_model_adapter.h index ac8dbedfec..572ea9af5f 100644 --- a/include/lib_tree_model_adapter.h +++ b/include/lib_tree_model_adapter.h @@ -141,12 +141,12 @@ public: * * @param aFilter if SYM_FILTER_POWER, only power parts are loaded */ - void SetFilter( std::function* aFilter ) { m_filter = aFilter; } + void SetFilter( std::function* aFilter ) { m_filter = aFilter; } /** * Return the active filter. */ - std::function* GetFilter() const { return m_filter; } + std::function* GetFilter() const { return m_filter; } void SetSortMode( SORT_MODE aMode ) { m_sort_mode = aMode; } SORT_MODE GetSortMode() const { return m_sort_mode; } @@ -415,7 +415,7 @@ private: wxDataViewCtrl* m_widget; - std::function* m_filter; + std::function* m_filter; std::vector m_columns; std::map m_colNameMap; diff --git a/pcbnew/dialogs/dialog_footprint_chooser.cpp b/pcbnew/dialogs/dialog_footprint_chooser.cpp index ee92cb6c49..bb76145c27 100644 --- a/pcbnew/dialogs/dialog_footprint_chooser.cpp +++ b/pcbnew/dialogs/dialog_footprint_chooser.cpp @@ -37,9 +37,9 @@ DIALOG_FOOTPRINT_CHOOSER::DIALOG_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aParent, wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL ); m_chooserPanel = new PANEL_FOOTPRINT_CHOOSER( aParent, this, aFootprintHistoryList, // Filter - []( LIB_TREE_NODE& aNode ) -> bool + []( LIB_TREE_NODE& aNode ) -> int { - return true; + return 0; }, // Close handler [this]() diff --git a/pcbnew/footprint_chooser_frame.cpp b/pcbnew/footprint_chooser_frame.cpp index d860bf6763..18af4da6c4 100644 --- a/pcbnew/footprint_chooser_frame.cpp +++ b/pcbnew/footprint_chooser_frame.cpp @@ -83,7 +83,7 @@ FOOTPRINT_CHOOSER_FRAME::FOOTPRINT_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aPare wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL ); m_chooserPanel = new PANEL_FOOTPRINT_CHOOSER( this, this, s_FootprintHistoryList, // Filter - [this]( LIB_TREE_NODE& aNode ) -> bool + [this]( LIB_TREE_NODE& aNode ) -> int { return filterFootprint( aNode ); }, @@ -158,37 +158,53 @@ FOOTPRINT_CHOOSER_FRAME::~FOOTPRINT_CHOOSER_FRAME() } } -bool FOOTPRINT_CHOOSER_FRAME::filterFootprint( LIB_TREE_NODE& aNode ) +int FOOTPRINT_CHOOSER_FRAME::filterFootprint( LIB_TREE_NODE& aNode ) { + bool filtering = false; + + auto patternMatch = + []( LIB_ID& id, std::vector>& filters ) -> bool + { + // The matching is case insensitive + wxString name; + + for( const std::unique_ptr& 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; + }; + if( m_pinCount > 0 && m_filterByPinCount->GetValue() ) { + filtering = true; + if( aNode.m_PinCount != m_pinCount ) - return false; + return -1; } if( !m_fpFilters.empty() && m_filterByFPFilters->GetValue() ) { - // The matching is case insensitive - wxString name; + filtering = true; - for( const std::unique_ptr& each_filter : m_fpFilters ) - { - name.Empty(); - - // If the filter contains a ':' character, include the library name in the pattern - if( each_filter->GetPattern().Contains( wxS( ":" ) ) ) - name = aNode.m_LibId.GetUniStringLibNickname().Lower() + wxS( ":" ); - - name += aNode.m_LibId.GetUniStringLibItemName().Lower(); - - if( each_filter->Find( name ) ) - return true; - } - - return false; + if( !patternMatch( aNode.m_LibId, m_fpFilters ) ) + return -1; } - return true; + if( filtering ) + return 1; + else + return 0; } diff --git a/pcbnew/footprint_chooser_frame.h b/pcbnew/footprint_chooser_frame.h index da5ae3e2ed..c7344aaa6e 100644 --- a/pcbnew/footprint_chooser_frame.h +++ b/pcbnew/footprint_chooser_frame.h @@ -56,7 +56,7 @@ protected: FOOTPRINT_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent ); private: - bool filterFootprint( LIB_TREE_NODE& aNode ); + int filterFootprint( LIB_TREE_NODE& aNode ); void OnPaint( wxPaintEvent& aEvent ); void OnOK( wxCommandEvent& aEvent ); diff --git a/pcbnew/widgets/panel_footprint_chooser.cpp b/pcbnew/widgets/panel_footprint_chooser.cpp index ac9eeedaf5..13ad1be378 100644 --- a/pcbnew/widgets/panel_footprint_chooser.cpp +++ b/pcbnew/widgets/panel_footprint_chooser.cpp @@ -43,7 +43,7 @@ PANEL_FOOTPRINT_CHOOSER::PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopLevelWindow* aParent, const wxArrayString& aFootprintHistoryList, - std::function aFilter, + std::function aFilter, std::function aCloseHandler ) : wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ), m_hsplitter( nullptr ), diff --git a/pcbnew/widgets/panel_footprint_chooser.h b/pcbnew/widgets/panel_footprint_chooser.h index 412ca5f4db..c665cfe7db 100644 --- a/pcbnew/widgets/panel_footprint_chooser.h +++ b/pcbnew/widgets/panel_footprint_chooser.h @@ -46,7 +46,7 @@ public: */ PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopLevelWindow* aParent, const wxArrayString& aFootprintHistoryList, - std::function aFilter, + std::function aFilter, std::function aCloseHandler ); ~PANEL_FOOTPRINT_CHOOSER(); @@ -94,7 +94,7 @@ protected: LIB_TREE* m_tree; PCB_BASE_FRAME* m_frame; - std::function m_filter; + std::function m_filter; std::function m_closeHandler; };