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