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