KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_footprint_users.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <algorithm>
23
24#include <wx/clipbrd.h>
25#include <wx/menu.h>
26#include <wx/types.h>
27#include <wx/wupdlock.h>
28
29#include <kiway.h>
30#include <kiway_mail.h>
31#include <string_utils.h>
32
33
34namespace
35{
36// A footprint is normally used by at most a few dozen symbols, which is all the list needs
37// to show.
38static constexpr int MAX_USER_RESULTS = 400;
39
40
42enum FOOTPRINT_USERS_MATCH_CATEGORY
43{
44 MATCH_FILTERS = 1 << 0,
45 MATCH_FOOTPRINT_FIELD = 1 << 1,
46 MATCH_PIN_MAPS = 1 << 2
47};
48
49
51enum FOOTPRINT_USERS_COLUMN
52{
53 COL_FOOTPRINT_USERS_LIBRARY = 0,
54 COL_FOOTPRINT_USERS_SYMBOL,
55 COL_FOOTPRINT_USERS_PINS,
56 COL_FOOTPRINT_USERS_FILTER,
57 COL_FOOTPRINT_USERS_FOOTPRINT_FIELD_MATCH,
58 COL_FOOTPRINT_USERS_PIN_MAP_MATCH
59};
60
61
62enum ROW_DATA_FLAGS
63{
64 ROW_DATA_FLAG_PIN_COUNT_MATCHES = 0x01,
65 ROW_DATA_FLAG_DERIVED_SYMBOL = 0x02
66};
67
68
73class FOOTPRINT_USERS_LIST_STORE : public wxDataViewListStore
74{
75public:
76 bool GetAttrByRow( unsigned int aRow, unsigned int aCol, wxDataViewItemAttr& aAttr ) const override
77 {
78 switch( aCol )
79 {
80 case COL_FOOTPRINT_USERS_SYMBOL:
81 if( GetItemData( GetItem( aRow ) ) & ROW_DATA_FLAG_DERIVED_SYMBOL )
82 {
83 aAttr.SetItalic( true );
84 return true;
85 }
86
87 break;
88
89 case COL_FOOTPRINT_USERS_PINS:
90 if( GetItemData( GetItem( aRow ) ) & ROW_DATA_FLAG_PIN_COUNT_MATCHES )
91 {
92 aAttr.SetBold( true );
93 return true;
94 }
95
96 break;
97
98 default:
99 break;
100 }
101
102 return false;
103 }
104};
105
106} // namespace
107
108
109DIALOG_FOOTPRINT_USERS::DIALOG_FOOTPRINT_USERS( wxWindow* aParent, KIWAY& aKiway, const LIB_ID& aFootprint,
110 int aFootprintPinCount ) :
112 m_kiway( aKiway ),
113 m_footprint( aFootprint ),
114 m_footprintPinCount( aFootprintPinCount ),
115 m_loadTimer( this ),
116 m_librariesReady( false )
117{
118 // Replace the default store with the pin count bolding variant before adding columns
119 FOOTPRINT_USERS_LIST_STORE* store = new FOOTPRINT_USERS_LIST_STORE();
120 m_footprintUserList->AssociateModel( store );
121 store->DecRef();
122
123 // Each row is one matching symbol; the columns say where it comes from and what makes it
124 // a match.
125 m_footprintUserList->AppendTextColumn( _( "Library" ), wxDATAVIEW_CELL_INERT, 180 );
126 m_footprintUserList->AppendTextColumn( _( "Symbol" ), wxDATAVIEW_CELL_INERT, 200 );
127 m_footprintUserList->AppendTextColumn( _( "Pins" ), wxDATAVIEW_CELL_INERT, 60 );
128 m_footprintUserList->AppendTextColumn( _( "Footprint filter" ), wxDATAVIEW_CELL_INERT, 160 );
129 m_footprintUserList->AppendToggleColumn( _( "Footprint field match" ), wxDATAVIEW_CELL_INERT, 120 );
130 m_footprintUserList->AppendTextColumn( _( "Pin map match" ), wxDATAVIEW_CELL_INERT, 160 );
131
132
133 // Double-clicking a user opens that symbol in the symbol editor.
134 m_footprintUserList->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &DIALOG_FOOTPRINT_USERS::onUserActivated, this );
135 m_footprintUserList->Bind( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &DIALOG_FOOTPRINT_USERS::onUserRightClick, this );
136 m_footprintUserList->Bind( wxEVT_KEY_DOWN, &DIALOG_FOOTPRINT_USERS::onUserKeyDown, this );
137
138 // Every way of using the footprint is shown by default, and switched independently.
139 m_cbShowFilterMatches->SetValue( true );
140 m_cbShowFootprintFieldMatches->SetValue( true );
141 m_cbShowPinMapMatches->SetValue( true );
142
146
147 // The libraries the users come from are loaded in the background. Rather than blocking
148 // the dialog, the status text shows the load progress and the list is greyed out until
149 // they arrive.
150 Bind( wxEVT_TIMER, &DIALOG_FOOTPRINT_USERS::onLoadTimer, this, m_loadTimer.GetId() );
151
152 m_footprintUserList->SetFocus();
153
154 // We'll fill this status text as needed
155 m_statusText->SetLabel( wxEmptyString );
156
157 // Most likely the symbol libraries are already loaded/ing (eeschema preloads them when a
158 // project is open) but this is basically free in that case.
160
161 // Wait until the dialog has been painted before showing the results or the loading
162 // indication.
163 CallAfter(
164 [this]()
165 {
166 updateUsers();
167 } );
168
169 // Now all widgets have the size fixed, call FinishDialogSettings
171}
172
173
175{
176 if( !symbolLibrariesReady() )
177 return;
178
179 const int categories = matchCategories();
180
182 query.m_Footprint = m_footprint;
183 query.m_MatchFilters = ( categories & MATCH_FILTERS ) != 0;
184 query.m_MatchFootprintField = ( categories & MATCH_FOOTPRINT_FIELD ) != 0;
185 query.m_MatchPinMaps = ( categories & MATCH_PIN_MAPS ) != 0;
186 query.m_MaxResults = MAX_USER_RESULTS;
187
189}
190
191
193{
194 int categories = 0;
195
196 if( m_cbShowFilterMatches->GetValue() )
197 categories |= MATCH_FILTERS;
198
199 if( m_cbShowFootprintFieldMatches->GetValue() )
200 categories |= MATCH_FOOTPRINT_FIELD;
201
202 if( m_cbShowPinMapMatches->GetValue() )
203 categories |= MATCH_PIN_MAPS;
204
205 return categories;
206}
207
208
210{
211 if( m_librariesReady )
212 return true;
213
214 float progress = GetSymbolLibrariesLoadProgress( m_kiway );
215
216 if( progress >= 1.0f )
217 {
218 m_librariesReady = true;
219 showLoadIndication( false );
220 return true;
221 }
222
223 showLoadIndication( true, progress );
224 // Go and poll in #onLoadTimer
225 m_loadTimer.StartOnce( 250 );
226 return false;
227}
228
229
231{
232 std::vector<FOOTPRINT_USER_MATCH> matches = aResult.m_Matches;
233
234 const auto libThenSymbolNameCmp =
235 []( const FOOTPRINT_USER_MATCH& aLeft, const FOOTPRINT_USER_MATCH& aRight )
236 {
237 const int libCmp = StrNumCmp( aLeft.m_Symbol.GetUniStringLibNickname(),
238 aRight.m_Symbol.GetUniStringLibNickname(), CMP_T::IGNORE_CASE );
239
240 if( libCmp != 0 )
241 return libCmp < 0;
242
244 aRight.m_Symbol.GetUniStringLibItemName(), CMP_T::IGNORE_CASE ) < 0;
245 };
246
247 std::sort( matches.begin(), matches.end(), libThenSymbolNameCmp );
248
249 wxWindowUpdateLocker lock( m_footprintUserList );
250 m_footprintUserList->DeleteAllItems();
251
252 for( const FOOTPRINT_USER_MATCH& match : matches )
253 {
254 wxVector<wxVariant> row;
255
256 row.push_back( wxVariant( match.m_Symbol.GetUniStringLibNickname() ) );
257 row.push_back( wxVariant( match.m_Symbol.GetUniStringLibItemName() ) );
258 row.push_back( wxVariant( wxString::Format( wxS( "%d" ), match.m_PinCount ) ) );
259 row.push_back( wxVariant( match.m_MatchedFilter ) );
260 row.push_back( wxVariant( match.m_MatchesFootprintField ) );
261
262 // The pin map column names the maps bound to the footprint rather than ticking a box.
263 wxString pinMapNames;
264
265 for( const wxString& mapName : match.m_MatchedPinMaps )
266 {
267 if( !pinMapNames.IsEmpty() )
268 pinMapNames += wxS( ", " );
269
270 pinMapNames += mapName;
271 }
272
273 row.push_back( wxVariant( pinMapNames ) );
274
275 wxUIntPtr rowData = 0;
276
277 if( match.m_PinCount == m_footprintPinCount )
278 rowData |= ROW_DATA_FLAG_PIN_COUNT_MATCHES;
279
280 if( match.m_IsDerived )
281 rowData |= ROW_DATA_FLAG_DERIVED_SYMBOL;
282
283 m_footprintUserList->AppendItem( row, rowData );
284 }
285
286 if( !aResult.m_Success )
287 {
288 m_statusText->SetLabel( _( "Failed to query matching symbols." ) );
289 }
290 else if( aResult.m_IsLimited )
291 {
292 m_statusText->SetLabel( wxString::Format( _( "This footprint is referenced by more than %zu symbols" ),
293 matches.size() ) );
294 }
295 else
296 {
297 m_statusText->SetLabel( wxString::Format( _( "This footprint is referenced by %zu symbols" ),
298 matches.size() ) );
299 }
300
301 Layout();
302}
303
304
305void DIALOG_FOOTPRINT_USERS::showLoadIndication( bool aShow, float aProgress )
306{
307 m_footprintUserList->Enable( !aShow );
308
309 if( aShow )
310 {
311 m_statusText->SetLabel( wxString::Format( _( "Loading symbol libraries (%d%%)..." ),
312 static_cast<int>( aProgress * 100.0f ) ) );
313 }
314 else
315 {
316 m_footprintUserList->DeleteAllItems();
317 m_statusText->SetLabel( wxEmptyString );
318 }
319}
320
321
322void DIALOG_FOOTPRINT_USERS::onMatchTypeChanged( wxCommandEvent& aEvent )
323{
324 updateUsers();
325}
326
327
328void DIALOG_FOOTPRINT_USERS::onLoadTimer( wxTimerEvent& aEvent )
329{
330 if( !symbolLibrariesReady() )
331 return;
332
333 // Inputs may have changed while the libraries were loading.
334 updateUsers();
335}
336
337
339{
340 // Events can have wxNOT_FOUND as the row.
341 if( aRow < 0 || aRow >= static_cast<int>( m_footprintUserList->GetItemCount() ) )
342 return wxEmptyString;
343
344 const wxString library = m_footprintUserList->GetTextValue( aRow, COL_FOOTPRINT_USERS_LIBRARY );
345 const wxString symbol = m_footprintUserList->GetTextValue( aRow, COL_FOOTPRINT_USERS_SYMBOL );
346
347 // A row without a library and symbol is not a match.
348 if( library.IsEmpty() || symbol.IsEmpty() )
349 return wxEmptyString;
350
351 LIB_ID symbolId;
352
353 if( symbolId.Parse( library + wxS( ":" ) + symbol ) != -1 || !symbolId.IsValid() )
354 return wxEmptyString;
355
356 return symbolId.Format().wx_str();
357}
358
359
360void DIALOG_FOOTPRINT_USERS::openSymbolInEditor( const wxString& aSymbolName )
361{
362 m_kiway.Player( FRAME_SCH_SYMBOL_EDITOR, true );
363
364 std::string packet = aSymbolName.utf8_string();
365 m_kiway.ExpressMail( FRAME_SCH_SYMBOL_EDITOR, MAIL_SCH_EDIT_LIBID, packet, this );
366
367 // Close the dialog to avoid a modal focus fight
368 EndModal( wxID_OK );
369}
370
371
372void DIALOG_FOOTPRINT_USERS::copySymbolName( const wxString& aSymbolName )
373{
374 if( wxTheClipboard->Open() )
375 {
376 wxTheClipboard->SetData( new wxTextDataObject( aSymbolName ) );
377 wxTheClipboard->Close();
378 }
379}
380
381
382void DIALOG_FOOTPRINT_USERS::onUserActivated( wxDataViewEvent& aEvent )
383{
384 wxString symbolName = userSymbolName( m_footprintUserList->ItemToRow( aEvent.GetItem() ) );
385
386 if( !symbolName.IsEmpty() )
387 openSymbolInEditor( symbolName );
388}
389
390
391void DIALOG_FOOTPRINT_USERS::onUserRightClick( wxDataViewEvent& aEvent )
392{
393 const wxDataViewItem item = aEvent.GetItem();
394
395 // The context menu can be asked for from the empty area below the rows.
396 if( !item.IsOk() )
397 return;
398
399 wxString symbolName = userSymbolName( m_footprintUserList->ItemToRow( item ) );
400
401 if( symbolName.IsEmpty() )
402 return;
403
404 enum
405 {
406 OPEN_IN_SYMBOL_EDITOR = 1,
407 COPY_SYMBOL_NAME
408 };
409
410 wxMenu menu;
411
412 menu.Append( OPEN_IN_SYMBOL_EDITOR, _( "Open in Symbol Editor" ) );
413 menu.Append( COPY_SYMBOL_NAME, _( "Copy Symbol Name" ) + "\tCtrl+C" );
414
415 switch( GetPopupMenuSelectionFromUser( menu ) )
416 {
417 case OPEN_IN_SYMBOL_EDITOR:
418 openSymbolInEditor( symbolName );
419 break;
420 case COPY_SYMBOL_NAME:
421 copySymbolName( symbolName );
422 break;
423 default:
424 break;
425 }
426}
427
428
430{
431 if( aEvent.ControlDown() && aEvent.GetKeyCode() == 'C' )
432 {
433 wxString symbolName = userSymbolName( m_footprintUserList->GetSelectedRow() );
434
435 if( !symbolName.IsEmpty() )
436 {
437 copySymbolName( symbolName );
438 return;
439 }
440 }
441
442 aEvent.Skip();
443}
DIALOG_FOOTPRINT_USERS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Footprint users"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(750, 450), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void onMatchTypeChanged(wxCommandEvent &aEvent)
void showLoadIndication(bool aShow, float aProgress=0.0f)
void onUserActivated(wxDataViewEvent &aEvent)
void showUsers(const FOOTPRINT_USERS_RESULT &aResult)
void onUserRightClick(wxDataViewEvent &aEvent)
wxString userSymbolName(int aRow) const
Return the "library:symbol" name of a table row, or an empty string if there is none.
int matchCategories() const
Return the enabled match categories, as the bit set of FOOTPRINT_USERS_MATCH_CATEGORY.
void copySymbolName(const wxString &aSymbolName)
bool symbolLibrariesReady()
Return true once the symbol libraries are loaded, showing the progress when they are not.
void openSymbolInEditor(const wxString &aSymbolName)
void onLoadTimer(wxTimerEvent &aEvent)
void updateUsers()
Show the users for the enabled match categories, querying them when they are not cached yet.
DIALOG_FOOTPRINT_USERS(wxWindow *aParent, KIWAY &aKiway, const LIB_ID &aFootprint, int aFootprintPinCount)
void onUserKeyDown(wxKeyEvent &aEvent)
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
A minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the same KiCad...
Definition kiway.h:348
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:45
int Parse(const UTF8 &aId, bool aFix=false)
Parse LIB_ID with the information from aId.
Definition lib_id.cpp:65
bool IsValid() const
Check if this LID_ID is valid.
Definition lib_id.h:168
UTF8 Format() const
Definition lib_id.cpp:132
const wxString GetUniStringLibItemName() const
Get strings for display messages in dialogs.
Definition lib_id.h:108
const wxString GetUniStringLibNickname() const
Definition lib_id.h:84
wxString wx_str() const
Definition utf8.cpp:41
#define _(s)
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
@ MAIL_SCH_EDIT_LIBID
Definition mail_type.h:54
int StrNumCmp(const wxString &aString1, const wxString &aString2, CMP_T aCaseSensitivity)
Compare two strings with alphanumerical content.
@ IGNORE_CASE
All the information needed to query the symbol libraries for the symbols using a footprint.
bool m_MatchPinMaps
When true, symbols whose pin maps are bound to the footprint are returned.
int m_MaxResults
Limits the number of returned symbols; zero means no limit.
bool m_MatchFilters
When true, symbols whose footprint filters select the footprint are returned.
bool m_MatchFootprintField
When true, symbols whose footprint field names the footprint are returned.
LIB_ID m_Footprint
The footprint to find the users of.
The outcome of a footprint users query, and the JSON encoding used to move it across the Eeschema kif...
bool m_IsLimited
True if the result set is limited by the maximum number of results.
std::vector< FOOTPRINT_USER_MATCH > m_Matches
The matching symbols, in the order the libraries were searched.
bool m_Success
False if the query could not be performed, in which case Matches is empty.
One symbol that uses a footprint, and the reason it matched.
LIB_ID m_Symbol
The "library:symbol" name of the matching symbol.
FOOTPRINT_USERS_RESULT QueryFootprintUsers(KIWAY &aKiway, const FOOTPRINT_USERS_QUERY &aQuery)
Ask the symbol libraries, for the symbols that use a given footprint (over the Eeschema kiface).
bool StartSymbolLibrariesLoad(KIWAY &aKiway)
Start loading the symbol libraries in the background.
float GetSymbolLibrariesLoadProgress(KIWAY &aKiway)
Return how far the background load of the symbol libraries has got.