KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_setup_netclasses.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) 2004-2009 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2009 Dick Hollenbeck, [email protected]
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include <algorithm>
23#include <limits>
24#include <wx/wupdlock.h>
25#include <pgm_base.h>
26#include <eda_draw_frame.h>
27#include <bitmaps.h>
28#include <netclass.h>
29#include <gal/painter.h>
30#include <grid_tricks.h>
32#include <tool/tool_manager.h>
33#include <pcb_painter.h>
35#include <string_utils.h>
36#include <view/view.h>
41#include <widgets/wx_panel.h>
44#include <confirm.h>
45
46
47// columns of netclasses grid
48enum
49{
51
63
69
71};
72
73std::vector<BITMAPS> g_lineStyleIcons;
74wxArrayString g_lineStyleNames;
75
76
78 std::shared_ptr<NET_SETTINGS> aNetSettings,
79 const std::set<wxString>& aNetNames, bool aIsEEschema ) :
80 PANEL_SETUP_NETCLASSES_BASE( aParentWindow ),
81 m_frame( aFrame ),
82 m_isEEschema( aIsEEschema ),
83 m_netSettings( std::move( aNetSettings ) ),
84 m_netNames( aNetNames ),
86 m_hoveredCol( -1 ),
88 m_sortAsc( false ),
89 m_sortCol( 0 )
90{
91 // Clear and re-load each time. Language (or darkmode) might have changed.
92 g_lineStyleIcons.clear();
93 g_lineStyleNames.clear();
94
96 g_lineStyleNames.push_back( _( "<Not defined>" ) );
98 g_lineStyleNames.push_back( _( "Solid" ) );
100 g_lineStyleNames.push_back( _( "Dashed" ) );
102 g_lineStyleNames.push_back( _( "Dotted" ) );
104 g_lineStyleNames.push_back( _( "Dash-Dot" ) );
106 g_lineStyleNames.push_back( _( "Dash-Dot-Dot" ) );
107
108 m_netclassesDirty = true;
109
110 m_schUnitsProvider = std::make_unique<UNITS_PROVIDER>( schIUScale, m_frame->GetUserUnits() );
111 m_pcbUnitsProvider = std::make_unique<UNITS_PROVIDER>( pcbIUScale, m_frame->GetUserUnits() );
112
113 m_netclassesPane->SetBorders( true, false, false, false );
114 m_membershipPane->SetBorders( true, false, false, false );
115
116 // Prevent Size events from firing before we are ready
117 wxWindowUpdateLocker updateLock( this );
118
119 m_netclassGrid->BeginBatch();
120 m_netclassGrid->SetUseNativeColLabels();
121 m_assignmentGrid->BeginBatch();
122 m_assignmentGrid->SetUseNativeColLabels();
123
124 m_splitter->SetMinimumPaneSize( FromDIP( m_splitter->GetMinimumPaneSize() ) );
125
126 wxASSERT( m_netclassGrid->GetNumberCols() == GRID_END );
127
128 // Calculate a min best size to handle longest usual numeric values:
129 int const min_best_width = m_netclassGrid->GetTextExtent( "555,555555 mils" ).x;
130
131 for( int i = 0; i < m_netclassGrid->GetNumberCols(); ++i )
132 {
133 // We calculate the column min size only from texts sizes, not using the initial col width
134 // as this initial width is sometimes strange depending on the language (wxGrid bug?)
135 int const min_width = m_netclassGrid->GetVisibleWidth( i, true, true );
136
137 int const weighted_min_best_width = ( i == GRID_LINESTYLE ) ? min_best_width * 3 / 2
138 : min_best_width;
139
140 // We use a "best size" >= min_best_width
141 m_originalColWidths[ i ] = std::max( min_width, weighted_min_best_width );
142 m_netclassGrid->SetColSize( i, m_originalColWidths[ i ] );
143
144 if( i >= GRID_FIRST_EESCHEMA )
145 m_netclassGrid->SetUnitsProvider( m_schUnitsProvider.get(), i );
146 else
147 m_netclassGrid->SetUnitsProvider( m_pcbUnitsProvider.get(), i );
148 }
149
150 if( m_isEEschema )
151 m_netclassGrid->ShowHideColumns( "0 11 12 13 14" );
152 else
153 m_netclassGrid->ShowHideColumns( "0 1 2 3 4 5 6 7 8 9 10" );
154
155 m_shownColumns = m_netclassGrid->GetShownColumns();
156
157 wxGridCellAttr* attr = new wxGridCellAttr;
158 attr->SetRenderer( new GRID_CELL_COLOR_RENDERER( PAGED_DIALOG::GetDialog( this ) ) );
159 attr->SetEditor( new GRID_CELL_COLOR_SELECTOR( PAGED_DIALOG::GetDialog( this ), m_netclassGrid ) );
160 m_netclassGrid->SetColAttr( GRID_SCHEMATIC_COLOR, attr );
161
162 attr = new wxGridCellAttr;
163 attr->SetRenderer( new GRID_CELL_COLOR_RENDERER( PAGED_DIALOG::GetDialog( this ) ) );
164 attr->SetEditor( new GRID_CELL_COLOR_SELECTOR( PAGED_DIALOG::GetDialog( this ), m_netclassGrid ) );
165 m_netclassGrid->SetColAttr( GRID_PCB_COLOR, attr );
166
167 attr = new wxGridCellAttr;
170 m_netclassGrid->SetColAttr( GRID_LINESTYLE, attr );
171
172 if( m_isEEschema )
173 {
174 m_importColorsButton->Hide();
175 }
176 else
177 {
178 m_colorDefaultHelpText->SetLabel( _( "Set color to transparent to use layer default color." ) );
179 m_colorDefaultHelpText->GetParent()->Layout();
180 }
181
182 m_colorDefaultHelpText->SetFont( KIUI::GetInfoFont( this ).Italic() );
183
184 m_netclassGrid->SetAutoEvalCols( { GRID_WIREWIDTH,
194
195 // Be sure the column labels are readable
196 m_netclassGrid->EnsureColLabelsVisible();
197
198 m_netclassGrid->PushEventHandler( new GRID_TRICKS( m_netclassGrid ) );
199 m_assignmentGrid->PushEventHandler( new GRID_TRICKS( m_assignmentGrid ) );
200
201 m_netclassGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
202 m_assignmentGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
203
206
209
212
213 // wxFormBuilder doesn't include this event...
214 m_netclassGrid->Connect( wxEVT_GRID_CELL_CHANGING,
216 nullptr, this );
217
218 // Handle tooltips for grid
219 m_netclassGrid->GetGridColLabelWindow()->Bind( wxEVT_MOTION, &PANEL_SETUP_NETCLASSES::OnNetclassGridMouseEvent,
220 this );
221
222 // Allow sorting assignments by column
223 m_assignmentGrid->Connect( wxEVT_GRID_LABEL_LEFT_CLICK,
225 nullptr, this );
226
227 m_frame->Bind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_NETCLASSES::onUnitsChanged, this );
228
229 m_netclassGrid->EndBatch();
230 m_assignmentGrid->EndBatch();
231
232 Bind( wxEVT_IDLE,
233 [this]( wxIdleEvent& aEvent )
234 {
235 // Careful of consuming CPU in an idle event handler. Check the ticker first to
236 // see if there's even a possibility of the netclasses having changed.
237 if( m_frame->Prj().GetNetclassesTicker() > m_lastCheckedTicker )
238 {
239 wxWindow* dialog = wxGetTopLevelParent( this );
240 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
241
242 if( topLevelFocus == dialog && m_lastLoaded != m_netSettings->GetNetclasses() )
243 checkReload();
244 }
245 } );
246
247 m_matchingNets->SetFont( KIUI::GetInfoFont( this ) );
248}
249
250
252{
253 // Delete the GRID_TRICKS.
254 m_netclassGrid->PopEventHandler( true );
255 m_assignmentGrid->PopEventHandler( true );
256
257 m_netclassGrid->Disconnect( wxEVT_GRID_CELL_CHANGING,
259 nullptr, this );
260
261 m_assignmentGrid->Disconnect( wxEVT_GRID_LABEL_LEFT_CLICK,
263 nullptr, this );
264
265 m_frame->Unbind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_NETCLASSES::onUnitsChanged, this );
266}
267
268
270{
271 auto netclassToGridRow =
272 [&]( int aRow, const NETCLASS* nc )
273 {
274 m_netclassGrid->SetCellValue( aRow, GRID_NAME, nc->GetName() );
275 m_netclassGrid->SetCellValue( aRow, GRID_DELAY_PROFILE, nc->GetTuningProfile() );
276
277 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_WIREWIDTH, nc->GetWireWidthOpt() );
278 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_BUSWIDTH, nc->GetBusWidthOpt() );
279
280 wxString colorAsString = nc->GetSchematicColor().ToCSSString();
281 m_netclassGrid->SetCellValue( aRow, GRID_SCHEMATIC_COLOR, colorAsString );
282
283 if( nc->HasLineStyle() )
284 {
285 int lineStyleIdx = std::max( 0, nc->GetLineStyle() );
286
287 if( lineStyleIdx >= (int) g_lineStyleNames.size() + 1 )
288 lineStyleIdx = 0;
289
290 m_netclassGrid->SetCellValue( aRow, GRID_LINESTYLE, g_lineStyleNames[lineStyleIdx + 1] );
291 }
292 else
293 {
294 // <Not defined> line style in list.
295 m_netclassGrid->SetCellValue( aRow, GRID_LINESTYLE, g_lineStyleNames[0] );
296 }
297
298 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_CLEARANCE, nc->GetClearanceOpt() );
299 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_TRACKSIZE, nc->GetTrackWidthOpt() );
300 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_VIASIZE, nc->GetViaDiameterOpt() );
301 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_VIADRILL, nc->GetViaDrillOpt() );
302 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_uVIASIZE, nc->GetuViaDiameterOpt() );
303 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_uVIADRILL, nc->GetuViaDrillOpt() );
304 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_DIFF_PAIR_WIDTH, nc->GetDiffPairWidthOpt() );
305 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_DIFF_PAIR_GAP, nc->GetDiffPairGapOpt() );
306
307 colorAsString = nc->GetPcbColor().ToCSSString();
308 m_netclassGrid->SetCellValue( aRow, GRID_PCB_COLOR, colorAsString );
309
310 if( nc->IsDefault() )
311 {
312 m_netclassGrid->SetReadOnly( aRow, GRID_NAME );
313 m_netclassGrid->SetReadOnly( aRow, GRID_PCB_COLOR );
314 m_netclassGrid->SetReadOnly( aRow, GRID_SCHEMATIC_COLOR );
315 m_netclassGrid->SetReadOnly( aRow, GRID_LINESTYLE );
316 }
317
318 setNetclassRowNullableEditors( aRow, nc->IsDefault() );
319 };
320
321 // Get the netclasses sorted by priority
322 std::vector<const NETCLASS*> netclasses;
323 netclasses.reserve( m_netSettings->GetNetclasses().size() );
324
325 for( const auto& [name, netclass] : m_netSettings->GetNetclasses() )
326 netclasses.push_back( netclass.get() );
327
328 std::sort( netclasses.begin(), netclasses.end(),
329 []( const NETCLASS* nc1, const NETCLASS* nc2 )
330 {
331 return nc1->GetPriority() < nc2->GetPriority();
332 } );
333
334 // Enter user-defined netclasses
335 m_netclassGrid->ClearRows();
336 m_netclassGrid->AppendRows( static_cast<int>( netclasses.size() ) );
337
338 int row = 0;
339
340 for( const NETCLASS* nc : netclasses )
341 netclassToGridRow( row++, nc );
342
343 // Enter the Default netclass.
344 m_netclassGrid->AppendRows( 1 );
345 netclassToGridRow( row, m_netSettings->GetDefaultNetclass().get() );
346
347 m_assignmentGrid->ClearRows();
348 m_assignmentGrid->AppendRows( m_netSettings->GetNetclassPatternAssignments().size() );
349
350 row = 0;
351
352 for( const auto& [matcher, netclassName] : m_netSettings->GetNetclassPatternAssignments() )
353 {
354 m_assignmentGrid->SetCellValue( row, 0, matcher->GetPattern() );
355 m_assignmentGrid->SetCellValue( row, 1, netclassName );
356 row++;
357 }
358}
359
360
362{
363 // Set nullable editors
364 auto setCellEditor =
365 [this, aRowId, aIsDefault]( int aCol )
366 {
367 GRID_CELL_MARK_AS_NULLABLE* cellEditor;
368
369 if( aIsDefault )
370 cellEditor = new GRID_CELL_MARK_AS_NULLABLE( false );
371 else
372 cellEditor = new GRID_CELL_MARK_AS_NULLABLE( true );
373
374 wxGridCellAttr* attr = m_netclassGrid->GetOrCreateCellAttr( aRowId, aCol );
375 attr->SetEditor( cellEditor );
376 attr->DecRef();
377 };
378
379 setCellEditor( GRID_WIREWIDTH );
380 setCellEditor( GRID_BUSWIDTH );
381 setCellEditor( GRID_CLEARANCE );
382 setCellEditor( GRID_TRACKSIZE );
383 setCellEditor( GRID_VIASIZE );
384 setCellEditor( GRID_VIADRILL );
385 setCellEditor( GRID_VIADRILL );
386 setCellEditor( GRID_uVIASIZE );
387 setCellEditor( GRID_uVIADRILL );
388 setCellEditor( GRID_DIFF_PAIR_WIDTH );
389 setCellEditor( GRID_DIFF_PAIR_GAP );
390}
391
392
394{
395 // MUST update the ticker before calling IsOK (or we'll end up re-entering through the idle
396 // event until we crash the stack).
397 m_lastCheckedTicker = m_frame->Prj().GetTextVarsTicker();
398
399 if( IsOK( m_parent, _( "The netclasses have been changed outside the Setup dialog.\n"
400 "Do you wish to reload them?" ) ) )
401 {
402 m_lastLoaded = m_netSettings->GetNetclasses();
404 }
405}
406
407
408void PANEL_SETUP_NETCLASSES::onUnitsChanged( wxCommandEvent& aEvent )
409{
410 std::shared_ptr<NET_SETTINGS> tempNetSettings = std::make_shared<NET_SETTINGS>( nullptr, "" );
411 std::shared_ptr<NET_SETTINGS> saveNetSettings = m_netSettings;
412
413 m_netSettings = tempNetSettings;
414
416
417 m_schUnitsProvider->SetUserUnits( m_frame->GetUserUnits() );
418 m_pcbUnitsProvider->SetUserUnits( m_frame->GetUserUnits() );
419
421
422 m_netSettings = saveNetSettings;
423
424 aEvent.Skip();
425}
426
427
429{
430 m_lastLoaded = m_netSettings->GetNetclasses();
431 m_lastCheckedTicker = m_frame->Prj().GetNetclassesTicker();
432
434 AdjustAssignmentGridColumns( GetSize().x * 3 / 5 );
435
436 return true;
437}
438
439
441{
442 m_assignmentGrid->CommitPendingChanges( true );
443
444 wxArrayString netclassNames;
445
446 for( int ii = 0; ii < m_netclassGrid->GetNumberRows(); ii++ )
447 {
448 wxString netclassName = m_netclassGrid->GetCellValue( ii, GRID_NAME );
449
450 if( !netclassName.IsEmpty() )
451 netclassNames.push_back( netclassName );
452 }
453
454 wxGridCellAttr* attr = new wxGridCellAttr;
455 attr->SetEditor( new wxGridCellChoiceEditor( netclassNames ) );
456 m_assignmentGrid->SetColAttr( 1, attr );
457}
458
459
461{
462 if( !Validate() )
463 return false;
464
465 auto gridRowToNetclass =
466 [&]( int aRow, const std::shared_ptr<NETCLASS>& nc )
467 {
468 if( nc->IsDefault() )
469 nc->SetPriority( std::numeric_limits<int>::max() );
470 else
471 nc->SetPriority( aRow );
472
473 nc->SetName( m_netclassGrid->GetCellValue( aRow, GRID_NAME ) );
474 nc->SetTuningProfile( m_netclassGrid->GetCellValue( aRow, GRID_DELAY_PROFILE ) );
475
476 nc->SetWireWidth( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_WIREWIDTH ) );
477 nc->SetBusWidth( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_BUSWIDTH ) );
478
479 wxString lineStyle = m_netclassGrid->GetCellValue( aRow, GRID_LINESTYLE );
480 int lineIdx = g_lineStyleNames.Index( lineStyle );
481
482 if( lineIdx == 0 )
483 nc->SetLineStyle( std::optional<int>() );
484 else
485 nc->SetLineStyle( lineIdx - 1 );
486
487 wxASSERT_MSG( lineIdx >= 0, "Line style name not found." );
488
489 // clang-format off
490 nc->SetClearance( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_CLEARANCE ) );
491 nc->SetTrackWidth( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_TRACKSIZE ) );
492 nc->SetViaDiameter( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_VIASIZE ) );
493 nc->SetViaDrill( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_VIADRILL ) );
494 nc->SetuViaDiameter( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_uVIASIZE ) );
495 nc->SetuViaDrill( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_uVIADRILL ) );
496 nc->SetDiffPairWidth( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_DIFF_PAIR_WIDTH ) );
497 nc->SetDiffPairGap( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_DIFF_PAIR_GAP ) );
498 // clang-format on
499
500 if( !nc->IsDefault() )
501 {
502 wxString color = m_netclassGrid->GetCellValue( aRow, GRID_PCB_COLOR );
503 KIGFX::COLOR4D newPcbColor( color );
504
505 if( newPcbColor != KIGFX::COLOR4D::UNSPECIFIED )
506 nc->SetPcbColor( newPcbColor );
507
508 color = m_netclassGrid->GetCellValue( aRow, GRID_SCHEMATIC_COLOR );
509 KIGFX::COLOR4D newSchematicColor( color );
510
511 if( newSchematicColor != KIGFX::COLOR4D::UNSPECIFIED )
512 nc->SetSchematicColor( newSchematicColor );
513 }
514 };
515
516 m_netSettings->ClearNetclasses();
517
518 // Copy the default NetClass:
519 gridRowToNetclass( m_netclassGrid->GetNumberRows() - 1, m_netSettings->GetDefaultNetclass() );
520
521 // Copy other NetClasses:
522 for( int row = 0; row < m_netclassGrid->GetNumberRows() - 1; ++row )
523 {
524 auto nc = std::make_shared<NETCLASS>( m_netclassGrid->GetCellValue( row, GRID_NAME ), false );
525 gridRowToNetclass( row, nc );
526 m_netSettings->SetNetclass( nc->GetName(), nc );
527 }
528
529 m_netSettings->ClearNetclassPatternAssignments();
530 m_netSettings->ClearAllCaches();
531
532 for( int row = 0; row < m_assignmentGrid->GetNumberRows(); ++row )
533 {
534 wxString pattern = m_assignmentGrid->GetCellValue( row, 0 );
535 wxString netclass = m_assignmentGrid->GetCellValue( row, 1 );
536
537 m_netSettings->SetNetclassPatternAssignment( pattern, netclass );
538 }
539
540 return true;
541}
542
543
544bool PANEL_SETUP_NETCLASSES::validateNetclassName( int aRow, const wxString& aName, bool focusFirst )
545{
546 wxString tmp = aName;
547
548 tmp.Trim( true );
549 tmp.Trim( false );
550
551 if( tmp.IsEmpty() )
552 {
553 wxString msg = _( "Netclass must have a name." );
554 PAGED_DIALOG::GetDialog( this )->SetError( msg, this, m_netclassGrid, aRow, GRID_NAME );
555 return false;
556 }
557
558 for( int ii = 0; ii < m_netclassGrid->GetNumberRows(); ii++ )
559 {
560 if( ii != aRow && m_netclassGrid->GetCellValue( ii, GRID_NAME ).CmpNoCase( tmp ) == 0 )
561 {
562 wxString msg = _( "Netclass name already in use." );
563 PAGED_DIALOG::GetDialog( this )->SetError( msg, this, m_netclassGrid, focusFirst ? aRow : ii,
564 GRID_NAME );
565 return false;
566 }
567 }
568
569 return true;
570}
571
572
574{
575 // Clip clearance. This is not a nag; we can end up with overflow errors and very poor
576 // performance if the clearance is too large.
577
578 std::optional<int> clearance = m_netclassGrid->GetOptionalUnitValue( aRow, GRID_CLEARANCE );
579
580 if( clearance.has_value() && clearance.value() > MAXIMUM_CLEARANCE )
581 {
582 wxString msg = wxString::Format( _( "Clearance was too large. It has been clipped to %s." ),
583 m_frame->StringFromValue( MAXIMUM_CLEARANCE, true ) );
585 m_netclassGrid->SetUnitValue( aRow, GRID_CLEARANCE, MAXIMUM_CLEARANCE );
586 return false;
587 }
588
589 return true;
590}
591
592
594{
595 if( event.GetCol() == GRID_NAME )
596 {
597 if( validateNetclassName( event.GetRow(), event.GetString() ) )
598 {
599 wxString oldName = m_netclassGrid->GetCellValue( event.GetRow(), GRID_NAME );
600 wxString newName = event.GetString();
601
602 if( !oldName.IsEmpty() )
603 {
604 for( int row = 0; row < m_assignmentGrid->GetNumberRows(); ++row )
605 {
606 if( m_assignmentGrid->GetCellValue( row, 1 ) == oldName )
607 m_assignmentGrid->SetCellValue( row, 1, newName );
608 }
609 }
610
611 m_netclassesDirty = true;
612 }
613 else
614 {
615 event.Veto();
616 }
617 }
618 else if( event.GetCol() == GRID_CLEARANCE )
619 {
620 validateNetclassClearance( event.GetRow() );
621 }
622}
623
625{
626 event.Skip();
627
628 if( !m_assignmentGrid->CommitPendingChanges() )
629 return;
630
631 if( ( event.GetCol() < 0 ) || ( event.GetCol() >= m_assignmentGrid->GetNumberCols() ) )
632 return;
633
634 // Toggle sort order if the same column is clicked
635 if( event.GetCol() != m_sortCol )
636 {
637 m_sortCol = event.GetCol();
638 m_sortAsc = true;
639 }
640 else
641 {
643 }
644
645 std::vector<std::pair<wxString, wxString>> netclassesassignments;
646 netclassesassignments.reserve( m_assignmentGrid->GetNumberRows() );
647
648 for( int row = 0; row < m_assignmentGrid->GetNumberRows(); ++row )
649 {
650 netclassesassignments.emplace_back( m_assignmentGrid->GetCellValue( row, 0 ),
651 m_assignmentGrid->GetCellValue( row, 1 ) );
652 }
653
654 std::sort( netclassesassignments.begin(), netclassesassignments.end(),
655 [this]( const std::pair<wxString, wxString>& assign1,
656 const std::pair<wxString, wxString>& assign2 )
657 {
658 const wxString& str1 = ( m_sortCol == 0 ) ? assign1.first : assign1.second;
659 const wxString& str2 = ( m_sortCol == 0 ) ? assign2.first : assign2.second;
660 return m_sortAsc ? ( str1 < str2 ) : ( str1 > str2 );
661 } );
662
663 m_assignmentGrid->ClearRows();
664 m_assignmentGrid->AppendRows( netclassesassignments.size() );
665
666 int row = 0;
667
668 for( const auto& [pattern, netclassName] : netclassesassignments )
669 {
670 m_assignmentGrid->SetCellValue( row, 0, pattern );
671 m_assignmentGrid->SetCellValue( row, 1, netclassName );
672 row++;
673 }
674}
675
677{
678 int col = m_netclassGrid->XToCol( aEvent.GetPosition().x );
679
680 if( aEvent.Moving() || aEvent.Entering() )
681 {
682 aEvent.Skip();
683
684 if( col == wxNOT_FOUND )
685 {
686 m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
687 return;
688 }
689
690 if( col == m_hoveredCol )
691 return;
692
693 m_hoveredCol = col;
694
695 wxString tip;
696
697 switch( col )
698 {
699 case GRID_CLEARANCE: tip = _( "Minimum copper clearance" ); break;
700 case GRID_TRACKSIZE: tip = _( "Optimum track width" ); break;
701 case GRID_VIASIZE: tip = _( "Via pad diameter" ); break;
702 case GRID_VIADRILL: tip = _( "Via plated hole diameter" ); break;
703 case GRID_uVIASIZE: tip = _( "Microvia pad diameter" ); break;
704 case GRID_uVIADRILL: tip = _( "Microvia plated hole diameter" ); break;
705 case GRID_DIFF_PAIR_WIDTH: tip = _( "Differential pair track width" ); break;
706 case GRID_DIFF_PAIR_GAP: tip = _( "Differential pair gap" ); break;
707 case GRID_WIREWIDTH: tip = _( "Schematic wire thickness" ); break;
708 case GRID_BUSWIDTH: tip = _( "Bus wire thickness" ); break;
709 case GRID_SCHEMATIC_COLOR: tip = _( "Schematic wire color" ); break;
710 case GRID_LINESTYLE: tip = _( "Schematic wire line style" ); break;
711 case GRID_PCB_COLOR: tip = _( "PCB netclass color" ); break;
712 }
713
714 m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
715 m_netclassGrid->GetGridColLabelWindow()->SetToolTip( tip );
716 }
717 else if( aEvent.Leaving() )
718 {
719 m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
720 aEvent.Skip();
721 }
722
723 aEvent.Skip();
724}
725
726
728{
729 m_netclassGrid->OnAddRow(
730 [&]() -> std::pair<int, int>
731 {
732 m_netclassGrid->InsertRows();
733
734 // Set defaults where required
735 wxString colorAsString = KIGFX::COLOR4D::UNSPECIFIED.ToCSSString();
736 m_netclassGrid->SetCellValue( 0, GRID_PCB_COLOR, colorAsString );
737 m_netclassGrid->SetCellValue( 0, GRID_SCHEMATIC_COLOR, colorAsString );
738 m_netclassGrid->SetCellValue( 0, GRID_LINESTYLE, g_lineStyleNames[0] );
739
740 // Set the row nullable editors
742
743 m_netclassesDirty = true;
744 return { 0, GRID_NAME };
745 } );
746}
747
748
750{
751 m_netclassGrid->OnDeleteRows(
752 [&]( int row )
753 {
754 if( row == m_netclassGrid->GetNumberRows() - 1 )
755 {
756 DisplayErrorMessage( wxGetTopLevelParent( this ), _( "The default net class is required." ) );
757 return false;
758 }
759
760 return true;
761 },
762 [&]( int row )
763 {
764 // reset the net class to default for members of the removed class
765 wxString classname = m_netclassGrid->GetCellValue( row, GRID_NAME );
766
767 for( int assignment = 0; assignment < m_assignmentGrid->GetNumberRows(); ++assignment )
768 {
769 if( m_assignmentGrid->GetCellValue( assignment, 1 ) == classname )
770 m_assignmentGrid->SetCellValue( assignment, 1, NETCLASS::Default );
771 }
772
773 m_netclassGrid->DeleteRows( row, 1 );
774 m_netclassesDirty = true;
775 } );
776}
777
778
780{
781 if( aWidth != m_lastNetclassGridWidth )
782 {
784
785 // Account for scroll bars
786 aWidth -= ( m_netclassGrid->GetSize().x - m_netclassGrid->GetClientSize().x );
787
788 for( int i = 1; i < m_netclassGrid->GetNumberCols(); i++ )
789 {
790 if( m_netclassGrid->GetColSize( i ) > 0 )
791 {
792 m_netclassGrid->SetColSize( i, m_originalColWidths[ i ] );
793 aWidth -= m_originalColWidths[ i ];
794 }
795 }
796
797 m_netclassGrid->SetColSize( 0, std::max( aWidth - 2, m_originalColWidths[ 0 ] ) );
798 }
799}
800
801
803{
804 AdjustNetclassGridColumns( event.GetSize().GetX() );
805
806 event.Skip();
807}
808
809
811{
812 m_assignmentGrid->OnAddRow(
813 [&]() -> std::pair<int, int>
814 {
815 int row = m_assignmentGrid->GetNumberRows();
816 m_assignmentGrid->AppendRows();
817 m_assignmentGrid->SetCellValue( row, 1, m_netSettings->GetDefaultNetclass()->GetName() );
818 return { row, 0 };
819 } );
820}
821
822
824{
825 m_assignmentGrid->OnDeleteRows(
826 [&]( int row )
827 {
828 m_assignmentGrid->DeleteRows( row, 1 );
829 } );
830}
831
832
834{
835 const std::map<wxString, std::shared_ptr<NETCLASS>>& netclasses =
836 m_netSettings->GetNetclasses();
837
838 for( int row = 0; row < m_netclassGrid->GetNumberRows() - 1; ++row )
839 {
840 wxString netclassName = m_netclassGrid->GetCellValue( row, GRID_NAME );
841
842 if( netclasses.find( netclassName ) != netclasses.end() )
843 {
844 const KIGFX::COLOR4D ncColor = netclasses.at( netclassName )->GetSchematicColor();
845 m_netclassGrid->SetCellValue( row, GRID_PCB_COLOR, ncColor.ToCSSString() );
846 }
847 }
848}
849
850
852{
853 // Account for scroll bars
854 aWidth -= ( m_assignmentGrid->GetSize().x - m_assignmentGrid->GetClientSize().x );
855
856 int classNameWidth = 160;
857 m_assignmentGrid->SetColSize( 1, classNameWidth );
858 m_assignmentGrid->SetColSize( 0, std::max( aWidth - classNameWidth, classNameWidth ) );
859}
860
861
863{
864 AdjustAssignmentGridColumns( event.GetSize().GetX() );
865
866 event.Skip();
867}
868
869
870void PANEL_SETUP_NETCLASSES::OnUpdateUI( wxUpdateUIEvent& event )
871{
873 {
875 m_netclassesDirty = false;
876 }
877
878 if( m_shownColumns != m_netclassGrid->GetShownColumns() )
879 {
880 AdjustNetclassGridColumns( GetSize().x - 1 );
881 m_shownColumns = m_netclassGrid->GetShownColumns();
882 }
883
884 if( m_assignmentGrid->GetNumberRows() == 0 )
885 return;
886
887 wxString pattern;
888 int row = m_assignmentGrid->GetGridCursorRow();
889 int col = m_assignmentGrid->GetGridCursorCol();
890
891 if( row >= 0 )
892 pattern = m_assignmentGrid->GetCellValue( row, 0 );
893
894 if( col == 0 && m_assignmentGrid->IsCellEditControlShown() )
895 {
896 wxGridCellEditor* cellEditor = m_assignmentGrid->GetCellEditor( row, 0 );
897
898 if( wxTextEntry* txt = dynamic_cast<wxTextEntry*>( cellEditor->GetControl() ) )
899 pattern = txt->GetValue();
900
901 cellEditor->DecRef();
902 }
903
904 if( pattern != m_lastPattern )
905 {
906 m_matchingNets->Clear();
907
908 if( !pattern.IsEmpty() )
909 {
910 EDA_COMBINED_MATCHER matcher( pattern, CTX_NETCLASS );
911
912 m_matchingNets->Report( wxString::Format( _( "<b>Nets matching '%s':</b>" ), pattern ) );
913
914 for( const wxString& net : m_netNames )
915 {
916 if( matcher.StartsWith( net ) )
917 m_matchingNets->Report( net );
918 }
919 }
920
921 m_matchingNets->Flush();
922 m_lastPattern = pattern;
923 }
924}
925
926
928{
929 if( !m_netclassGrid->CommitPendingChanges() || !m_assignmentGrid->CommitPendingChanges() )
930 return false;
931
932 // Test net class parameters.
933 for( int row = 0; row < m_netclassGrid->GetNumberRows(); row++ )
934 {
935 wxString netclassName = m_netclassGrid->GetCellValue( row, GRID_NAME );
936 netclassName.Trim( true );
937 netclassName.Trim( false );
938
939 if( !validateNetclassName( row, netclassName, false ) )
940 return false;
941
942 if( !validateNetclassClearance( row ) )
943 return false;
944 }
945
946 return true;
947}
948
949
950void PANEL_SETUP_NETCLASSES::ImportSettingsFrom( const std::shared_ptr<NET_SETTINGS>& aNetSettings )
951{
952 std::shared_ptr<NET_SETTINGS> savedSettings = m_netSettings;
953
954 m_netSettings = aNetSettings;
956
958
959 m_netclassGrid->ForceRefresh();
960 m_assignmentGrid->ForceRefresh();
961
962 m_netSettings = std::move( savedSettings );
963}
964
965
967{
968 m_netclassGrid->OnMoveRowUp(
969 [&]( int row )
970 {
971 // Can't move the Default netclass
972 return row != m_netclassGrid->GetNumberRows() - 1;
973 },
974 [&]( int row )
975 {
976 m_netclassGrid->SwapRows( row, row - 1 );
977 m_netclassesDirty = true;
978 } );
979}
980
981
983{
984 m_netclassGrid->OnMoveRowDown(
985 [&]( int row )
986 {
987 // Can't move the Default netclass
988 return row + 1 != m_netclassGrid->GetNumberRows() - 1;
989 },
990 [&]( int row )
991 {
992 m_netclassGrid->SwapRows( row, row + 1 );
993 m_netclassesDirty = true;
994 } );
995}
996
997
998void PANEL_SETUP_NETCLASSES::UpdateDelayProfileNames( const std::vector<wxString>& aNames ) const
999{
1000 wxArrayString profileNames;
1001 profileNames.push_back( wxEmptyString );
1002 std::ranges::for_each( aNames,
1003 [&]( const wxString& aName )
1004 {
1005 profileNames.push_back( aName );
1006 } );
1007
1008 wxGridCellAttr* attr = new wxGridCellAttr;
1009 attr->SetEditor( new wxGridCellChoiceEditor( profileNames, false ) );
1010 m_netclassGrid->SetColAttr( GRID_DELAY_PROFILE, attr );
1011}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
@ stroke_dashdotdot
#define MAXIMUM_CLEARANCE
bool StartsWith(const wxString &aTerm)
The base class for create windows for drawing purpose.
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:57
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
wxString ToCSSString() const
Definition color4d.cpp:146
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:398
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:38
static const char Default[]
the name of the default NETCLASS
Definition netclass.h:40
static PAGED_DIALOG * GetDialog(wxWindow *aWindow)
void SetError(const wxString &aMessage, const wxString &aPageName, int aCtrlId, int aRow=-1, int aCol=-1)
PANEL_SETUP_NETCLASSES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
void OnImportColorsClick(wxCommandEvent &event) override
std::map< int, int > m_originalColWidths
void OnUpdateUI(wxUpdateUIEvent &event) override
void OnSizeAssignmentGrid(wxSizeEvent &event) override
PANEL_SETUP_NETCLASSES(wxWindow *aParentWindow, EDA_DRAW_FRAME *aFrame, std::shared_ptr< NET_SETTINGS > aSettings, const std::set< wxString > &aNetNames, bool isEEschema)
void OnRemoveNetclassClick(wxCommandEvent &event) override
void OnRemoveAssignmentClick(wxCommandEvent &event) override
std::map< wxString, std::shared_ptr< NETCLASS > > m_lastLoaded
void UpdateDelayProfileNames(const std::vector< wxString > &aNames) const
void ImportSettingsFrom(const std::shared_ptr< NET_SETTINGS > &aNetSettings)
void onUnitsChanged(wxCommandEvent &aEvent)
void OnNetclassGridCellChanging(wxGridEvent &event)
void setNetclassRowNullableEditors(int aRowId, bool aIsDefault)
bool validateNetclassName(int aRow, const wxString &aName, bool focusFirst=true)
std::unique_ptr< UNITS_PROVIDER > m_pcbUnitsProvider
void OnNetclassGridMouseEvent(wxMouseEvent &event)
void OnAddAssignmentClick(wxCommandEvent &event) override
void AdjustAssignmentGridColumns(int aWidth)
void OnMoveNetclassUpClick(wxCommandEvent &event) override
std::set< wxString > m_netNames
void OnAddNetclassClick(wxCommandEvent &event) override
std::unique_ptr< UNITS_PROVIDER > m_schUnitsProvider
void OnSizeNetclassGrid(wxSizeEvent &event) override
void OnNetclassAssignmentSort(wxGridEvent &event)
void AdjustNetclassGridColumns(int aWidth)
std::shared_ptr< NET_SETTINGS > m_netSettings
void OnMoveNetclassDownClick(wxCommandEvent &event) override
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:274
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define _(s)
@ CTX_NETCLASS
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
STL namespace.
std::vector< BITMAPS > g_lineStyleIcons
@ GRID_SCHEMATIC_COLOR
@ GRID_DIFF_PAIR_WIDTH
@ GRID_FIRST_EESCHEMA
wxArrayString g_lineStyleNames
see class PGM_BASE
int clearance