KiCad PCB EDA Suite
Loading...
Searching...
No Matches
footprint_filter.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <footprint_filter.h>
22#include <stdexcept>
23#include <wx/tokenzr.h>
24
26
27
29 m_pos( 0 ),
30 m_filter( nullptr )
31{
32}
33
34
36 m_pos( aOther.m_pos ),
37 m_filter( aOther.m_filter )
38{
39}
40
41
43 m_pos( (size_t) -1 ),
44 m_filter( &aFilter )
45{
46 increment();
47}
48
49
51{
52 if( !m_filter || !m_filter->m_list || m_filter->m_list->GetCount() == 0 )
53 {
54 m_pos = 0;
55 return;
56 }
57
58 int filter_type = m_filter->m_filter_type;
59 FOOTPRINT_LIST* list = m_filter->m_list;
60 wxString& lib_name = m_filter->m_lib_name;
61
62 for( ++m_pos; m_pos < list->GetCount(); ++m_pos )
63 {
64 FOOTPRINT_INFO& candidate = list->GetItem( m_pos );
65
66 if( filter_type == FOOTPRINT_FILTER::UNFILTERED_FP_LIST )
67 break;
68
70 {
71 if( !PinCountMatch( candidate ) )
72 continue;
73 }
74
76 {
77 if( !lib_name.IsEmpty() && !candidate.InLibrary( lib_name ) )
78 continue;
79 }
80
82 {
83 if( !FootprintFilterMatch( candidate ) )
84 continue;
85 }
86
88 {
89 bool exclude = false;
90
91 for( std::unique_ptr<EDA_COMBINED_MATCHER>& matcher : m_filter->m_pattern_filters )
92 {
93 if( !matcher->ScoreTerms( candidate.GetSearchTerms() ) )
94 {
95 exclude = true;
96 break;
97 }
98 }
99
100 if( exclude )
101 continue;
102 }
103
104 // Candidate passed all filters; exit loop
105 break;
106 }
107}
108
109
111{
112 // Invalid iterators are always equal
113 return ( m_pos == aOther.m_pos ) && ( m_filter == aOther.m_filter || m_pos == (size_t) -1 );
114}
115
116
118{
119 if( m_filter && m_filter->m_list && m_pos < m_filter->m_list->GetCount() )
120 return m_filter->m_list->GetItem( m_pos );
121 else
122 throw std::out_of_range( "Attempt to dereference past FOOTPRINT_FILTER::end()" );
123}
124
125
127{
128 if( m_filter->m_footprint_filters.empty() )
129 return true;
130
131 // The matching is case insensitive
132 wxString name;
133
134 for( const std::unique_ptr<EDA_PATTERN_MATCH>& each_filter : m_filter->m_footprint_filters )
135 {
136 name.Empty();
137
138 // If the filter contains a ':' character, include the library name in the pattern
139 if( each_filter->GetPattern().Contains( wxS( ":" ) ) )
140 name = aItem.GetLibNickname().Lower() + wxS( ":" );
141
142 name += aItem.GetFootprintName().Lower();
143
144 if( each_filter->Find( name ) )
145 return true;
146 }
147
148 return false;
149}
150
151
153{
154 return m_filter->m_pin_count >= 0
155 && (unsigned) m_filter->m_pin_count == aItem.GetUniquePadCount();
156}
157
158
164
165
172
173
175{
176 m_list = &aList;
177}
178
179
184
185
186void FOOTPRINT_FILTER::FilterByLibrary( const wxString& aLibName )
187{
188 m_lib_name = aLibName;
190}
191
192
194{
195 m_pin_count = aPinCount;
197}
198
199
200void FOOTPRINT_FILTER::FilterByFootprintFilters( const wxArrayString& aFilters )
201{
202 m_footprint_filters.clear();
203
204 for( const wxString& each_pattern : aFilters )
205 {
206 m_footprint_filters.push_back( std::make_unique<EDA_PATTERN_MATCH_WILDCARD_ANCHORED>() );
207 m_footprint_filters.back()->SetPattern( each_pattern.Lower() );
208 }
209
211}
212
213
214void FOOTPRINT_FILTER::FilterByTextPattern( wxString const& aPattern )
215{
216 m_filter_pattern = aPattern;
217
218 wxStringTokenizer tokenizer( aPattern.Lower(), " \t\r\n", wxTOKEN_STRTOK );
219
220 while( tokenizer.HasMoreTokens() )
221 {
222 const wxString term = tokenizer.GetNextToken().Lower();
223 m_pattern_filters.push_back( std::make_unique<EDA_COMBINED_MATCHER>( term, CTX_LIBITEM ) );
224 }
225
227}
228
229
234
235
237{
238 FOOTPRINT_FILTER_IT end_it( *this );
239 end_it.m_pos = m_list ? m_list->GetCount() : 0;
240 return end_it;
241}
const char * name
Inner iterator class returned by begin() and end().
bool PinCountMatch(FOOTPRINT_INFO &aItem)
Check if the stored component matches an item by pin count.
FOOTPRINT_INFO & dereference() const
bool equal(const ITERATOR &aOther) const
bool FootprintFilterMatch(FOOTPRINT_INFO &aItem)
Check if the stored component matches an item by footprint filter.
FOOTPRINT_FILTER(FOOTPRINT_LIST &aList)
Construct a filter.
FOOTPRINT_FILTER()
Construct a filter without assigning a footprint list.
FOOTPRINT_LIST * m_list
void SetList(FOOTPRINT_LIST &aList)
Set the list to filter.
void FilterByPinCount(int aPinCount)
Set a pin count to filter by.
ITERATOR begin()
Get an iterator to the beginning of the filtered view.
void FilterByTextPattern(const wxString &aPattern)
Add a pattern to filter by name, including wildcards and optionally a colon-delimited library name.
void ClearFilters()
Clear all filter criteria.
void FilterByLibrary(const wxString &aLibName)
Add library name to filter criteria.
ITERATOR end()
Get an iterator to the end of the filtered view.
std::vector< std::unique_ptr< EDA_COMBINED_MATCHER > > m_pattern_filters
std::vector< std::unique_ptr< EDA_PATTERN_MATCH > > m_footprint_filters
void FilterByFootprintFilters(const wxArrayString &aFilters)
Set a list of footprint filters to filter by.
bool InLibrary(const wxString &aLibrary) const
Test if the FOOTPRINT_INFO object was loaded from aLibrary.
wxString GetLibNickname() const override
const wxString & GetFootprintName() const
unsigned GetUniquePadCount()
std::vector< SEARCH_TERM > & GetSearchTerms() override
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
@ CTX_LIBITEM
FOOTPRINT_FILTER::ITERATOR FOOTPRINT_FILTER_IT