-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
net_navigator.cpp
585 lines (461 loc) · 19.7 KB
/
net_navigator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2023 Rivos
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* @author Wayne Stambaugh <[email protected]>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <wx/log.h>
#include <core/profile.h>
#include <tool/tool_manager.h>
#include <kiface_base.h>
#include <sch_edit_frame.h>
#include <sch_bus_entry.h>
#include <sch_line.h>
#include <sch_junction.h>
#include <sch_no_connect.h>
#include <sch_sheet_pin.h>
#include <schematic.h>
#include <string_utils.h>
#include <trace_helpers.h>
#include <connection_graph.h>
#include <widgets/wx_aui_utils.h>
#include <tools/ee_actions.h>
static wxString GetNetNavigatorItemText( const SCH_ITEM* aItem,
const SCH_SHEET_PATH& aSheetPath,
UNITS_PROVIDER* aUnitsProvider )
{
wxString retv;
wxCHECK( aItem && aUnitsProvider, retv );
switch( aItem->Type() )
{
case SCH_LINE_T:
{
const SCH_LINE* line = static_cast<const SCH_LINE*>( aItem );
if( aItem->GetLayer() == LAYER_WIRE )
{
retv.Printf( _( "Wire from %s, %s to %s, %s" ),
aUnitsProvider->MessageTextFromValue( line->GetStartPoint().x ),
aUnitsProvider->MessageTextFromValue( line->GetStartPoint().y ),
aUnitsProvider->MessageTextFromValue( line->GetEndPoint().x ),
aUnitsProvider->MessageTextFromValue( line->GetEndPoint().y ) );
}
else if( aItem->GetLayer() == LAYER_BUS )
{
retv.Printf( _( "Bus from %s, %s to %s, %s" ),
aUnitsProvider->MessageTextFromValue( line->GetStartPoint().x ),
aUnitsProvider->MessageTextFromValue( line->GetStartPoint().y ),
aUnitsProvider->MessageTextFromValue( line->GetEndPoint().x ),
aUnitsProvider->MessageTextFromValue( line->GetEndPoint().y ) );
}
else
{
retv = _( "Graphic line not connectable" );
}
break;
}
case SCH_PIN_T:
{
const SCH_PIN* pin = static_cast<const SCH_PIN*>( aItem );
if( const SYMBOL* symbol = pin->GetParentSymbol() )
{
retv.Printf( _( "Symbol '%s' pin '%s'" ),
symbol->GetRef( &aSheetPath, true ),
UnescapeString( pin->GetNumber() ) );
if( wxString pinName = UnescapeString( pin->GetShownName() ); !pinName.IsEmpty() )
{
retv += wxString::Format( " (%s)", pinName );
}
}
break;
}
case SCH_SHEET_PIN_T:
{
const SCH_SHEET_PIN* pin = static_cast<const SCH_SHEET_PIN*>( aItem );
if( SCH_SHEET* sheet = pin->GetParent() )
{
retv.Printf( _( "Sheet '%s' pin '%s'" ),
sheet->GetName(),
UnescapeString( pin->GetText() ) );
}
break;
}
case SCH_LABEL_T:
{
const SCH_LABEL* label = static_cast<const SCH_LABEL*>( aItem );
retv.Printf( _( "Label '%s' at %s, %s" ),
UnescapeString( label->GetText() ),
aUnitsProvider->MessageTextFromValue( label->GetPosition().x ),
aUnitsProvider->MessageTextFromValue( label->GetPosition().y ) );
break;
}
case SCH_GLOBAL_LABEL_T:
{
const SCH_GLOBALLABEL* label = static_cast<const SCH_GLOBALLABEL*>( aItem );
retv.Printf( _( "Global label '%s' at %s, %s" ), UnescapeString( label->GetText() ),
aUnitsProvider->MessageTextFromValue( label->GetPosition().x ),
aUnitsProvider->MessageTextFromValue( label->GetPosition().y ) );
break;
}
case SCH_HIER_LABEL_T:
{
const SCH_HIERLABEL* label = static_cast<const SCH_HIERLABEL*>( aItem );
retv.Printf( _( "Hierarchical label '%s' at %s, %s" ), UnescapeString( label->GetText() ),
aUnitsProvider->MessageTextFromValue( label->GetPosition().x ),
aUnitsProvider->MessageTextFromValue( label->GetPosition().y ) );
break;
}
case SCH_JUNCTION_T:
{
const SCH_JUNCTION* junction = static_cast<const SCH_JUNCTION*>( aItem );
retv.Printf( _( "Junction at %s, %s" ),
aUnitsProvider->MessageTextFromValue( junction->GetPosition().x ),
aUnitsProvider->MessageTextFromValue( junction->GetPosition().y ) );
break;
}
case SCH_NO_CONNECT_T:
{
const SCH_NO_CONNECT* nc = static_cast<const SCH_NO_CONNECT*>( aItem );
retv.Printf( _( "No-Connect at %s, %s" ),
aUnitsProvider->MessageTextFromValue( nc->GetPosition().x ),
aUnitsProvider->MessageTextFromValue( nc->GetPosition().y ) );
break;
}
case SCH_BUS_WIRE_ENTRY_T:
{
const SCH_BUS_WIRE_ENTRY* entry = static_cast<const SCH_BUS_WIRE_ENTRY*>( aItem );
retv.Printf( _( "Bus to wire entry from %s, %s to %s, %s" ),
aUnitsProvider->MessageTextFromValue( entry->GetPosition().x ),
aUnitsProvider->MessageTextFromValue( entry->GetPosition().y ),
aUnitsProvider->MessageTextFromValue( entry->GetEnd().x ),
aUnitsProvider->MessageTextFromValue( entry->GetEnd().y ) );
break;
}
case SCH_BUS_BUS_ENTRY_T:
{
const SCH_BUS_BUS_ENTRY* entry = static_cast<const SCH_BUS_BUS_ENTRY*>( aItem );
retv.Printf( _( "Bus to bus entry from %s, %s to %s, %s" ),
aUnitsProvider->MessageTextFromValue( entry->GetPosition().x ),
aUnitsProvider->MessageTextFromValue( entry->GetPosition().y ),
aUnitsProvider->MessageTextFromValue( entry->GetEnd().x ),
aUnitsProvider->MessageTextFromValue( entry->GetEnd().y ) );
break;
}
case SCH_DIRECTIVE_LABEL_T:
{
const SCH_DIRECTIVE_LABEL* entry = static_cast<const SCH_DIRECTIVE_LABEL*>( aItem );
retv.Printf( _( "Netclass label '%s' at %s, %s" ), UnescapeString( entry->GetText() ),
aUnitsProvider->MessageTextFromValue( entry->GetPosition().x ),
aUnitsProvider->MessageTextFromValue( entry->GetPosition().y ) );
break;
}
default:
retv.Printf( _( "Unhandled item type %d" ), aItem->Type() );
}
return retv;
}
void SCH_EDIT_FRAME::MakeNetNavigatorNode( const wxString& aNetName, wxTreeItemId aParentId,
const NET_NAVIGATOR_ITEM_DATA* aSelection,
bool aSingleSheetSchematic )
{
wxCHECK( !aNetName.IsEmpty(), /* void */ );
wxCHECK( m_schematic, /* void */ );
wxCHECK( m_netNavigator, /* void */ );
wxTreeItemId expandId = aParentId;
CONNECTION_GRAPH* connectionGraph = m_schematic->ConnectionGraph();
wxCHECK( connectionGraph, /* void */ );
wxString sheetPathPrefix;
std::set<CONNECTION_SUBGRAPH*> subgraphs;
{
const std::vector<CONNECTION_SUBGRAPH*>& tmp = connectionGraph->GetAllSubgraphs( aNetName );
subgraphs.insert( tmp.begin(), tmp.end() );
}
for( CONNECTION_SUBGRAPH* sg : subgraphs )
{
for( const auto& [_, bus_sgs] : sg->GetBusParents() )
{
for( const CONNECTION_SUBGRAPH* bus_sg : bus_sgs )
{
const std::vector<CONNECTION_SUBGRAPH*>& tmp =
connectionGraph->GetAllSubgraphs( bus_sg->GetNetName() );
subgraphs.insert( tmp.begin(), tmp.end() );
}
}
}
std::map<wxString, wxTreeItemId> sheetIds;
for( const CONNECTION_SUBGRAPH* subGraph : subgraphs )
{
NET_NAVIGATOR_ITEM_DATA* itemData = nullptr;
SCH_SHEET_PATH sheetPath = subGraph->GetSheet();
wxCHECK2( subGraph && sheetPath.Last(), continue );
if( subGraph->GetItems().empty() )
continue;
itemData = new NET_NAVIGATOR_ITEM_DATA( sheetPath, nullptr );
bool stripTrailingSeparator = !sheetPath.Last()->IsRootSheet();
wxString txt = sheetPath.PathHumanReadable( true, stripTrailingSeparator );
wxTreeItemId sheetId;
if( auto sheetIdIt = sheetIds.find( txt ); sheetIdIt != sheetIds.end() )
{
sheetId = sheetIdIt->second;
}
else
{
sheetIds[txt] = m_netNavigator->AppendItem( aParentId, txt, -1, -1, itemData );
sheetId = sheetIds[txt];
}
if( aSelection && *aSelection == *itemData )
m_netNavigator->SelectItem( sheetId );
// If there is only one sheet in the schematic, always expand the sheet tree.
if( aSingleSheetSchematic )
expandId = sheetId;
for( const SCH_ITEM* item : subGraph->GetItems() )
{
KICAD_T type = item->Type();
if( ( type == SCH_LINE_T ) || ( type == SCH_JUNCTION_T )
|| ( type == SCH_BUS_WIRE_ENTRY_T ) || ( type == SCH_BUS_BUS_ENTRY_T ) )
continue;
itemData = new NET_NAVIGATOR_ITEM_DATA( sheetPath, item );
wxTreeItemId id = m_netNavigator->AppendItem( sheetId,
GetNetNavigatorItemText( item, sheetPath,
this ),
-1, -1, itemData );
if( aSelection && *aSelection == *itemData )
{
expandId = sheetId;
m_netNavigator->EnsureVisible( id );
m_netNavigator->SelectItem( id );
}
}
m_netNavigator->SortChildren( sheetId );
}
// Sort the items in the tree control alphabetically
m_netNavigator->SortChildren( aParentId );
m_netNavigator->Expand( aParentId );
}
void SCH_EDIT_FRAME::RefreshNetNavigator( const NET_NAVIGATOR_ITEM_DATA* aSelection )
{
wxCHECK( m_netNavigator, /* void */ );
if( !m_netNavigator->IsShown() )
return;
bool singleSheetSchematic = m_schematic->Hierarchy().size() == 1;
size_t nodeCnt = 0;
m_netNavigator->Freeze();
PROF_TIMER timer;
if( m_highlightedConn.IsEmpty() )
{
m_netNavigator->DeleteAllItems();
// Create a tree of all nets in the schematic.
wxTreeItemId rootId = m_netNavigator->AddRoot( _( "Nets" ), 0 );
const NET_MAP& netMap = m_schematic->ConnectionGraph()->GetNetMap();
for( const auto& net : netMap )
{
// Skip bus member subgraphs for the moment.
if( net.first.Name.IsEmpty() )
continue;
nodeCnt++;
wxTreeItemId netId = m_netNavigator->AppendItem( rootId,
UnescapeString( net.first.Name ) );
MakeNetNavigatorNode( net.first.Name, netId, aSelection, singleSheetSchematic );
}
m_netNavigator->Expand( rootId );
}
else if( !m_netNavigator->IsEmpty() )
{
const wxString shownNetName = m_netNavigator->GetItemText( m_netNavigator->GetRootItem() );
if( shownNetName != m_highlightedConn )
{
m_netNavigator->DeleteAllItems();
nodeCnt++;
wxTreeItemId rootId = m_netNavigator->AddRoot( UnescapeString( m_highlightedConn ), 0 );
MakeNetNavigatorNode( m_highlightedConn, rootId, aSelection, singleSheetSchematic );
}
else
{
NET_NAVIGATOR_ITEM_DATA* itemData = nullptr;
wxTreeItemId selection = m_netNavigator->GetSelection();
if( selection.IsOk() )
itemData = dynamic_cast<NET_NAVIGATOR_ITEM_DATA*>( m_netNavigator->GetItemData( selection ) );
m_netNavigator->DeleteAllItems();
nodeCnt++;
wxTreeItemId rootId = m_netNavigator->AddRoot( UnescapeString( m_highlightedConn ), 0 );
MakeNetNavigatorNode( m_highlightedConn, rootId, itemData, singleSheetSchematic );
}
}
else
{
nodeCnt++;
wxTreeItemId rootId = m_netNavigator->AddRoot( UnescapeString( m_highlightedConn ), 0 );
MakeNetNavigatorNode( m_highlightedConn, rootId, aSelection, singleSheetSchematic );
}
timer.Stop();
wxLogTrace( traceUiProfile, wxS( "Adding %zu nodes to net navigator took %s." ),
nodeCnt, timer.to_string() );
m_netNavigator->Thaw();
}
void SCH_EDIT_FRAME::SelectNetNavigatorItem( const NET_NAVIGATOR_ITEM_DATA* aSelection )
{
wxCHECK( m_netNavigator, /* void */ );
// Maybe in the future we can do something like collapse the tree for an empty selection.
// For now, leave the tree selection in its current state.
if( !aSelection )
return;
wxTreeItemIdValue sheetCookie;
NET_NAVIGATOR_ITEM_DATA* itemData = nullptr;
wxTreeItemId rootId = m_netNavigator->GetRootItem();
wxTreeItemId sheetId = m_netNavigator->GetFirstChild( rootId, sheetCookie );
while( sheetId.IsOk() )
{
if( m_netNavigator->ItemHasChildren( sheetId ) )
{
wxTreeItemIdValue itemCookie;
wxTreeItemId itemId = m_netNavigator->GetFirstChild( sheetId, itemCookie );
while( itemId.IsOk() )
{
itemData = dynamic_cast<NET_NAVIGATOR_ITEM_DATA*>( m_netNavigator->GetItemData( itemId ) );
wxCHECK2( itemData, continue );
if( *itemData == *aSelection )
{
if( !m_netNavigator->IsVisible( itemId ) )
{
m_netNavigator->CollapseAll();
m_netNavigator->EnsureVisible( itemId );
}
m_netNavigator->SelectItem( itemId );
return;
}
itemId = m_netNavigator->GetNextSibling( itemId );
}
sheetId = m_netNavigator->GetNextSibling( sheetId );
}
}
}
const SCH_ITEM* SCH_EDIT_FRAME::GetSelectedNetNavigatorItem() const
{
if( !m_netNavigator )
return nullptr;
wxTreeItemId id = m_netNavigator->GetSelection();
if( !id.IsOk() || ( id == m_netNavigator->GetRootItem() ) )
return nullptr;
NET_NAVIGATOR_ITEM_DATA* itemData =
dynamic_cast<NET_NAVIGATOR_ITEM_DATA*>( m_netNavigator->GetItemData( id ) );
wxCHECK( itemData, nullptr );
return itemData->GetItem();
}
void SCH_EDIT_FRAME::onNetNavigatorSelection( wxTreeEvent& aEvent )
{
wxCHECK( m_netNavigator, /* void */ );
wxTreeItemId id = aEvent.GetItem();
// Clicking on the root item (net name ) does nothing.
if( id == m_netNavigator->GetRootItem() )
return;
NET_NAVIGATOR_ITEM_DATA* itemData =
dynamic_cast<NET_NAVIGATOR_ITEM_DATA*>( m_netNavigator->GetItemData( id ) );
// Just a net name when we have all nets displayed.
if( !itemData )
return;
if( GetCurrentSheet() != itemData->GetSheetPath() )
{
GetToolManager()->RunAction<SCH_SHEET_PATH*>( EE_ACTIONS::changeSheet,
&itemData->GetSheetPath() );
}
// Do not focus on item when a sheet tree node is selected.
if( m_netNavigator->GetItemParent( id ) != m_netNavigator->GetRootItem()
&& itemData->GetItem() )
{
// Make sure we didn't remove the item and/or the screen it resides on before we access it.
const SCH_ITEM* item = itemData->GetItem();
// Don't search for child items in screen r-tree.
item = ( ( item->Type() == SCH_SHEET_PIN_T ) || ( item->Type() == SCH_PIN_T ) ) ?
static_cast<const SCH_ITEM*>( item->GetParent() ) : item;
const SCH_SCREEN* screen = itemData->GetSheetPath().LastScreen();
wxCHECK( screen, /* void */ );
wxCHECK( screen->Items().contains( item, true ), /* void */ );
FocusOnLocation( itemData->GetItem()->GetBoundingBox().Centre() );
}
GetCanvas()->Refresh();
}
void SCH_EDIT_FRAME::onNetNavigatorSelChanging( wxTreeEvent& aEvent )
{
wxCHECK( m_netNavigator, /* void */ );
aEvent.Skip();
}
void SCH_EDIT_FRAME::ToggleNetNavigator()
{
EESCHEMA_SETTINGS* cfg = eeconfig();
wxCHECK( cfg, /* void */ );
wxAuiPaneInfo& netNavigatorPane = m_auimgr.GetPane( NetNavigatorPaneName() );
netNavigatorPane.Show( !netNavigatorPane.IsShown() );
updateSelectionFilterVisbility();
cfg->m_AuiPanels.show_net_nav_panel = netNavigatorPane.IsShown();
if( netNavigatorPane.IsShown() )
{
if( netNavigatorPane.IsFloating() )
{
netNavigatorPane.FloatingSize( cfg->m_AuiPanels.net_nav_panel_float_size );
m_auimgr.Update();
}
else if( cfg->m_AuiPanels.net_nav_panel_docked_size.GetWidth() > 0 )
{
// SetAuiPaneSize also updates m_auimgr
SetAuiPaneSize( m_auimgr, netNavigatorPane,
cfg->m_AuiPanels.net_nav_panel_docked_size.GetWidth(), -1 );
}
}
else
{
NET_NAVIGATOR_ITEM_DATA* itemData = nullptr;
wxTreeItemId selection = m_netNavigator->GetSelection();
if( selection.IsOk() )
itemData = dynamic_cast<NET_NAVIGATOR_ITEM_DATA*>( m_netNavigator->GetItemData( selection ) );
RefreshNetNavigator( itemData );
if( netNavigatorPane.IsFloating() )
{
cfg->m_AuiPanels.net_nav_panel_float_size = netNavigatorPane.floating_size;
}
else
{
cfg->m_AuiPanels.net_nav_panel_docked_size = m_netNavigator->GetSize();
}
m_auimgr.Update();
}
}
void SCH_EDIT_FRAME::onResizeNetNavigator( wxSizeEvent& aEvent )
{
aEvent.Skip();
// Called when resizing the Hierarchy Navigator panel
// Store the current pane size
// It allows to retrieve the last defined pane size when switching between
// docked and floating pane state
// Note: *DO NOT* call m_auimgr.Update() here: it crashes Kicad at least on Windows
EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
wxCHECK( cfg, /* void */ );
wxAuiPaneInfo& netNavigatorPane = m_auimgr.GetPane( NetNavigatorPaneName() );
if( m_netNavigator->IsShownOnScreen() )
{
cfg->m_AuiPanels.net_nav_panel_float_size = netNavigatorPane.floating_size;
// initialize net_nav_panel_docked_width and best size only if the netNavigatorPane
// width is > 0 (i.e. if its size is already set and has meaning)
// if it is floating, its size is not initialized (only floating_size is initialized)
// initializing netNavigatorPane.best_size is useful when switching to float pane and
// after switching to the docked pane, to retrieve the last docked pane width
if( netNavigatorPane.rect.width > 50 ) // 50 is a good margin
{
cfg->m_AuiPanels.net_nav_panel_docked_size.SetWidth( netNavigatorPane.rect.width );
netNavigatorPane.best_size.x = netNavigatorPane.rect.width;
}
}
}