Only expand lib tree when filtering.

This commit is contained in:
Jeff Young 2023-10-02 16:24:31 +01:00
parent 7c8e74c156
commit 06446bb80e
9 changed files with 59 additions and 46 deletions

View File

@ -31,10 +31,10 @@
// increase this score. This way, an empty search string will result in all components being // 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 // displayed as they have the minimum score. However, in that case, we avoid expanding all the
// nodes asd the result is very unspecific. // nodes asd the result is very unspecific.
static const unsigned kLowestDefaultScore = 1; static const int kLowestDefaultScore = 1;
void LIB_TREE_NODE::ResetScore( std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) void LIB_TREE_NODE::ResetScore( std::function<int( LIB_TREE_NODE& aNode )>* aFilter )
{ {
for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children ) for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
child->ResetScore( aFilter ); 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<bool( LIB_TREE_NODE& aNode )>* aFilter ) void LIB_TREE_NODE_LIB_ID::ResetScore( std::function<int( LIB_TREE_NODE& aNode )>* aFilter )
{ {
for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children ) for( std::unique_ptr<LIB_TREE_NODE>& child: m_Children )
child->ResetScore( aFilter ); child->ResetScore( aFilter );
if( !aFilter ) if( aFilter )
m_Score = kLowestDefaultScore; m_Score = kLowestDefaultScore + (*aFilter)(*this);
else if( (*aFilter)(*this) )
m_Score = kLowestDefaultScore + 1;
else else
m_Score = 0; m_Score = kLowestDefaultScore;
} }

View File

@ -102,13 +102,12 @@ PANEL_SYMBOL_CHOOSER::PANEL_SYMBOL_CHOOSER( SCH_BASE_FRAME* aFrame, wxWindow* aP
if( aFilter->GetFilterPowerSymbols() ) if( aFilter->GetFilterPowerSymbols() )
{ {
// Note: there is only a single filter ever used for symbols (the power filter), // HACK ALERT: the only filter ever used for symbols is the power filter, so we
// so the code simply sets a flag based on the filter being non-null. The filter // sometimes just look for the function pointer being non-null.
// is not (at present) actually called. static std::function<int( LIB_TREE_NODE& )> powerFilter =
static std::function<bool( LIB_TREE_NODE& )> powerFilter = []( LIB_TREE_NODE& ) -> int
[]( LIB_TREE_NODE& ) -> bool
{ {
return true; return 0;
}; };
adapter->SetFilter( &powerFilter ); adapter->SetFilter( &powerFilter );

View File

@ -85,7 +85,7 @@ public:
/** /**
* Initialize score to kLowestDefaultScore, recursively. * Initialize score to kLowestDefaultScore, recursively.
*/ */
virtual void ResetScore( std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ); virtual void ResetScore( std::function<int( LIB_TREE_NODE& aNode )>* aFilter );
/** /**
* Store intrinsic ranks on all children of this node. See m_IntrinsicRank * Store intrinsic ranks on all children of this node. See m_IntrinsicRank
@ -211,7 +211,7 @@ public:
*/ */
void Update( LIB_TREE_ITEM* aItem ); void Update( LIB_TREE_ITEM* aItem );
void ResetScore( std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) override; void ResetScore( std::function<int( LIB_TREE_NODE& aNode )>* aFilter ) override;
/** /**
* Perform the actual search. * Perform the actual search.

View File

@ -141,12 +141,12 @@ public:
* *
* @param aFilter if SYM_FILTER_POWER, only power parts are loaded * @param aFilter if SYM_FILTER_POWER, only power parts are loaded
*/ */
void SetFilter( std::function<bool( LIB_TREE_NODE& aNode )>* aFilter ) { m_filter = aFilter; } void SetFilter( std::function<int( LIB_TREE_NODE& aNode )>* aFilter ) { m_filter = aFilter; }
/** /**
* Return the active filter. * Return the active filter.
*/ */
std::function<bool( LIB_TREE_NODE& aNode )>* GetFilter() const { return m_filter; } std::function<int( LIB_TREE_NODE& aNode )>* GetFilter() const { return m_filter; }
void SetSortMode( SORT_MODE aMode ) { m_sort_mode = aMode; } void SetSortMode( SORT_MODE aMode ) { m_sort_mode = aMode; }
SORT_MODE GetSortMode() const { return m_sort_mode; } SORT_MODE GetSortMode() const { return m_sort_mode; }
@ -415,7 +415,7 @@ private:
wxDataViewCtrl* m_widget; wxDataViewCtrl* m_widget;
std::function<bool( LIB_TREE_NODE& aNode )>* m_filter; std::function<int( LIB_TREE_NODE& aNode )>* m_filter;
std::vector<wxDataViewColumn*> m_columns; std::vector<wxDataViewColumn*> m_columns;
std::map<wxString, wxDataViewColumn*> m_colNameMap; std::map<wxString, wxDataViewColumn*> m_colNameMap;

View File

@ -37,9 +37,9 @@ DIALOG_FOOTPRINT_CHOOSER::DIALOG_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aParent,
wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL ); wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
m_chooserPanel = new PANEL_FOOTPRINT_CHOOSER( aParent, this, aFootprintHistoryList, m_chooserPanel = new PANEL_FOOTPRINT_CHOOSER( aParent, this, aFootprintHistoryList,
// Filter // Filter
[]( LIB_TREE_NODE& aNode ) -> bool []( LIB_TREE_NODE& aNode ) -> int
{ {
return true; return 0;
}, },
// Close handler // Close handler
[this]() [this]()

View File

@ -83,7 +83,7 @@ FOOTPRINT_CHOOSER_FRAME::FOOTPRINT_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aPare
wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL ); wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
m_chooserPanel = new PANEL_FOOTPRINT_CHOOSER( this, this, s_FootprintHistoryList, m_chooserPanel = new PANEL_FOOTPRINT_CHOOSER( this, this, s_FootprintHistoryList,
// Filter // Filter
[this]( LIB_TREE_NODE& aNode ) -> bool [this]( LIB_TREE_NODE& aNode ) -> int
{ {
return filterFootprint( aNode ); 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<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;
};
if( m_pinCount > 0 && m_filterByPinCount->GetValue() ) if( m_pinCount > 0 && m_filterByPinCount->GetValue() )
{ {
filtering = true;
if( aNode.m_PinCount != m_pinCount ) if( aNode.m_PinCount != m_pinCount )
return false; return -1;
} }
if( !m_fpFilters.empty() && m_filterByFPFilters->GetValue() ) if( !m_fpFilters.empty() && m_filterByFPFilters->GetValue() )
{ {
// The matching is case insensitive filtering = true;
wxString name;
for( const std::unique_ptr<EDA_PATTERN_MATCH>& each_filter : m_fpFilters ) if( !patternMatch( aNode.m_LibId, m_fpFilters ) )
{ return -1;
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;
} }
return true; if( filtering )
return 1;
else
return 0;
} }

View File

@ -56,7 +56,7 @@ protected:
FOOTPRINT_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent ); FOOTPRINT_CHOOSER_FRAME( KIWAY* aKiway, wxWindow* aParent );
private: private:
bool filterFootprint( LIB_TREE_NODE& aNode ); int filterFootprint( LIB_TREE_NODE& aNode );
void OnPaint( wxPaintEvent& aEvent ); void OnPaint( wxPaintEvent& aEvent );
void OnOK( wxCommandEvent& aEvent ); void OnOK( wxCommandEvent& aEvent );

View File

@ -43,7 +43,7 @@
PANEL_FOOTPRINT_CHOOSER::PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopLevelWindow* aParent, PANEL_FOOTPRINT_CHOOSER::PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopLevelWindow* aParent,
const wxArrayString& aFootprintHistoryList, const wxArrayString& aFootprintHistoryList,
std::function<bool( LIB_TREE_NODE& )> aFilter, std::function<int( LIB_TREE_NODE& )> aFilter,
std::function<void()> aCloseHandler ) : std::function<void()> aCloseHandler ) :
wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ), wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize ),
m_hsplitter( nullptr ), m_hsplitter( nullptr ),

View File

@ -46,7 +46,7 @@ public:
*/ */
PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopLevelWindow* aParent, PANEL_FOOTPRINT_CHOOSER( PCB_BASE_FRAME* aFrame, wxTopLevelWindow* aParent,
const wxArrayString& aFootprintHistoryList, const wxArrayString& aFootprintHistoryList,
std::function<bool( LIB_TREE_NODE& )> aFilter, std::function<int( LIB_TREE_NODE& )> aFilter,
std::function<void()> aCloseHandler ); std::function<void()> aCloseHandler );
~PANEL_FOOTPRINT_CHOOSER(); ~PANEL_FOOTPRINT_CHOOSER();
@ -94,7 +94,7 @@ protected:
LIB_TREE* m_tree; LIB_TREE* m_tree;
PCB_BASE_FRAME* m_frame; PCB_BASE_FRAME* m_frame;
std::function<bool( LIB_TREE_NODE& )> m_filter; std::function<int( LIB_TREE_NODE& )> m_filter;
std::function<void()> m_closeHandler; std::function<void()> m_closeHandler;
}; };