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>
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{
549 wxString tmp = aName;
550
551 tmp.Trim( true );
552 tmp.Trim( false );
553
554 if( tmp.IsEmpty() )
555 {
556 wxString msg = _( "Netclass must have a name." );
557 PAGED_DIALOG::GetDialog( this )->SetError( msg, this, m_netclassGrid, aRow, GRID_NAME );
558 return false;
559 }
560
561 for( int ii = 0; ii < m_netclassGrid->GetNumberRows(); ii++ )
562 {
563 if( ii != aRow && m_netclassGrid->GetCellValue( ii, GRID_NAME ).CmpNoCase( tmp ) == 0 )
564 {
565 wxString msg = _( "Netclass name already in use." );
566 PAGED_DIALOG::GetDialog( this )->SetError( msg, this, m_netclassGrid, focusFirst ? aRow : ii,
567 GRID_NAME );
568 return false;
569 }
570 }
571
572 return true;
573}
574
575
577{
578 // Clip clearance. This is not a nag; we can end up with overflow errors and very poor
579 // performance if the clearance is too large.
580
581 std::optional<int> clearance = m_netclassGrid->GetOptionalUnitValue( aRow, GRID_CLEARANCE );
582
583 if( clearance.has_value() && clearance.value() > MAXIMUM_CLEARANCE )
584 {
585 wxString msg = wxString::Format( _( "Clearance was too large. It has been clipped to %s." ),
586 m_frame->StringFromValue( MAXIMUM_CLEARANCE, true ) );
588 m_netclassGrid->SetUnitValue( aRow, GRID_CLEARANCE, MAXIMUM_CLEARANCE );
589 return false;
590 }
591
592 return true;
593}
594
595
597{
598 if( event.GetCol() == GRID_NAME )
599 {
600 if( validateNetclassName( event.GetRow(), event.GetString() ) )
601 {
602 wxString oldName = m_netclassGrid->GetCellValue( event.GetRow(), GRID_NAME );
603 wxString newName = event.GetString();
604
605 if( !oldName.IsEmpty() )
606 {
607 for( int row = 0; row < m_assignmentGrid->GetNumberRows(); ++row )
608 {
609 if( m_assignmentGrid->GetCellValue( row, 1 ) == oldName )
610 m_assignmentGrid->SetCellValue( row, 1, newName );
611 }
612 }
613
614 m_netclassesDirty = true;
615 }
616 else
617 {
618 event.Veto();
619 }
620 }
621 else if( event.GetCol() == GRID_CLEARANCE )
622 {
623 validateNetclassClearance( event.GetRow() );
624 }
625}
626
628{
629 event.Skip();
630
631 if( !m_assignmentGrid->CommitPendingChanges() )
632 return;
633
634 if( ( event.GetCol() < 0 ) || ( event.GetCol() >= m_assignmentGrid->GetNumberCols() ) )
635 return;
636
637 // Toggle sort order if the same column is clicked
638 if( event.GetCol() != m_sortCol )
639 {
640 m_sortCol = event.GetCol();
641 m_sortAsc = true;
642 }
643 else
644 {
646 }
647
648 std::vector<std::pair<wxString, wxString>> netclassesassignments;
649 netclassesassignments.reserve( m_assignmentGrid->GetNumberRows() );
650
651 for( int row = 0; row < m_assignmentGrid->GetNumberRows(); ++row )
652 {
653 netclassesassignments.emplace_back( m_assignmentGrid->GetCellValue( row, 0 ),
654 m_assignmentGrid->GetCellValue( row, 1 ) );
655 }
656
657 std::sort( netclassesassignments.begin(), netclassesassignments.end(),
658 [this]( const std::pair<wxString, wxString>& assign1,
659 const std::pair<wxString, wxString>& assign2 )
660 {
661 const wxString& str1 = ( m_sortCol == 0 ) ? assign1.first : assign1.second;
662 const wxString& str2 = ( m_sortCol == 0 ) ? assign2.first : assign2.second;
663 return m_sortAsc ? ( str1 < str2 ) : ( str1 > str2 );
664 } );
665
666 m_assignmentGrid->ClearRows();
667 m_assignmentGrid->AppendRows( netclassesassignments.size() );
668
669 int row = 0;
670
671 for( const auto& [pattern, netclassName] : netclassesassignments )
672 {
673 m_assignmentGrid->SetCellValue( row, 0, pattern );
674 m_assignmentGrid->SetCellValue( row, 1, netclassName );
675 row++;
676 }
677}
678
680{
681 int col = m_netclassGrid->XToCol( aEvent.GetPosition().x );
682
683 if( aEvent.Moving() || aEvent.Entering() )
684 {
685 aEvent.Skip();
686
687 if( col == wxNOT_FOUND )
688 {
689 m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
690 return;
691 }
692
693 if( col == m_hoveredCol )
694 return;
695
696 m_hoveredCol = col;
697
698 wxString tip;
699
700 switch( col )
701 {
702 case GRID_CLEARANCE: tip = _( "Minimum copper clearance" ); break;
703 case GRID_TRACKSIZE: tip = _( "Minimum track width" ); break;
704 case GRID_VIASIZE: tip = _( "Via pad diameter" ); break;
705 case GRID_VIADRILL: tip = _( "Via plated hole diameter" ); break;
706 case GRID_uVIASIZE: tip = _( "Microvia pad diameter" ); break;
707 case GRID_uVIADRILL: tip = _( "Microvia plated hole diameter" ); break;
708 case GRID_DIFF_PAIR_WIDTH: tip = _( "Differential pair track width" ); break;
709 case GRID_DIFF_PAIR_GAP: tip = _( "Differential pair gap" ); break;
710 case GRID_WIREWIDTH: tip = _( "Schematic wire thickness" ); break;
711 case GRID_BUSWIDTH: tip = _( "Bus wire thickness" ); break;
712 case GRID_SCHEMATIC_COLOR: tip = _( "Schematic wire color" ); break;
713 case GRID_LINESTYLE: tip = _( "Schematic wire line style" ); break;
714 case GRID_PCB_COLOR: tip = _( "PCB netclass color" ); break;
715 }
716
717 m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
718 m_netclassGrid->GetGridColLabelWindow()->SetToolTip( tip );
719 }
720 else if( aEvent.Leaving() )
721 {
722 m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
723 aEvent.Skip();
724 }
725
726 aEvent.Skip();
727}
728
729
731{
732 m_netclassGrid->OnAddRow(
733 [&]() -> std::pair<int, int>
734 {
735 m_netclassGrid->InsertRows();
736
737 // Set defaults where required
738 wxString colorAsString = KIGFX::COLOR4D::UNSPECIFIED.ToCSSString();
739 m_netclassGrid->SetCellValue( 0, GRID_PCB_COLOR, colorAsString );
740 m_netclassGrid->SetCellValue( 0, GRID_SCHEMATIC_COLOR, colorAsString );
741 m_netclassGrid->SetCellValue( 0, GRID_LINESTYLE, g_lineStyleNames[0] );
742
743 // Set the row nullable editors
745
746 m_netclassesDirty = true;
747 return { 0, GRID_NAME };
748 } );
749}
750
751
753{
754 m_netclassGrid->OnDeleteRows(
755 [&]( int row )
756 {
757 if( row == m_netclassGrid->GetNumberRows() - 1 )
758 {
759 DisplayErrorMessage( wxGetTopLevelParent( this ), _( "The default net class is required." ) );
760 return false;
761 }
762
763 return true;
764 },
765 [&]( int row )
766 {
767 // reset the net class to default for members of the removed class
768 wxString classname = m_netclassGrid->GetCellValue( row, GRID_NAME );
769
770 for( int assignment = 0; assignment < m_assignmentGrid->GetNumberRows(); ++assignment )
771 {
772 if( m_assignmentGrid->GetCellValue( assignment, 1 ) == classname )
773 m_assignmentGrid->SetCellValue( assignment, 1, NETCLASS::Default );
774 }
775
776 m_netclassGrid->DeleteRows( row, 1 );
777 m_netclassesDirty = true;
778 } );
779}
780
781
783{
784 if( aWidth != m_lastNetclassGridWidth )
785 {
787
788 // Account for scroll bars
789 aWidth -= ( m_netclassGrid->GetSize().x - m_netclassGrid->GetClientSize().x );
790
791 for( int i = 1; i < m_netclassGrid->GetNumberCols(); i++ )
792 {
793 if( m_netclassGrid->GetColSize( i ) > 0 )
794 {
795 m_netclassGrid->SetColSize( i, m_originalColWidths[ i ] );
796 aWidth -= m_originalColWidths[ i ];
797 }
798 }
799
800 m_netclassGrid->SetColSize( 0, std::max( aWidth - 2, m_originalColWidths[ 0 ] ) );
801 }
802}
803
804
806{
807 AdjustNetclassGridColumns( event.GetSize().GetX() );
808
809 event.Skip();
810}
811
812
814{
815 m_assignmentGrid->OnAddRow(
816 [&]() -> std::pair<int, int>
817 {
818 int row = m_assignmentGrid->GetNumberRows();
819 m_assignmentGrid->AppendRows();
820 m_assignmentGrid->SetCellValue( row, 1, m_netSettings->GetDefaultNetclass()->GetName() );
821 return { row, 0 };
822 } );
823}
824
825
827{
828 m_assignmentGrid->OnDeleteRows(
829 [&]( int row )
830 {
831 m_assignmentGrid->DeleteRows( row, 1 );
832 } );
833}
834
835
837{
838 const std::map<wxString, std::shared_ptr<NETCLASS>>& netclasses =
839 m_netSettings->GetNetclasses();
840
841 for( int row = 0; row < m_netclassGrid->GetNumberRows() - 1; ++row )
842 {
843 wxString netclassName = m_netclassGrid->GetCellValue( row, GRID_NAME );
844
845 if( netclasses.find( netclassName ) != netclasses.end() )
846 {
847 const KIGFX::COLOR4D ncColor = netclasses.at( netclassName )->GetSchematicColor();
848 m_netclassGrid->SetCellValue( row, GRID_PCB_COLOR, ncColor.ToCSSString() );
849 }
850 }
851}
852
853
855{
856 // Account for scroll bars
857 aWidth -= ( m_assignmentGrid->GetSize().x - m_assignmentGrid->GetClientSize().x );
858
859 int classNameWidth = 160;
860 m_assignmentGrid->SetColSize( 1, classNameWidth );
861 m_assignmentGrid->SetColSize( 0, std::max( aWidth - classNameWidth, classNameWidth ) );
862}
863
864
866{
867 AdjustAssignmentGridColumns( event.GetSize().GetX() );
868
869 event.Skip();
870}
871
872
873void PANEL_SETUP_NETCLASSES::OnUpdateUI( wxUpdateUIEvent& event )
874{
876 {
878 m_netclassesDirty = false;
879 }
880
881 if( m_shownColumns != m_netclassGrid->GetShownColumns() )
882 {
883 AdjustNetclassGridColumns( GetSize().x - 1 );
884 m_shownColumns = m_netclassGrid->GetShownColumns();
885 }
886
887 if( m_assignmentGrid->GetNumberRows() == 0 )
888 return;
889
890 wxString pattern;
891 int row = m_assignmentGrid->GetGridCursorRow();
892 int col = m_assignmentGrid->GetGridCursorCol();
893
894 if( row >= 0 )
895 pattern = m_assignmentGrid->GetCellValue( row, 0 );
896
897 if( col == 0 && m_assignmentGrid->IsCellEditControlShown() )
898 {
899 wxGridCellEditor* cellEditor = m_assignmentGrid->GetCellEditor( row, 0 );
900
901 if( wxTextEntry* txt = dynamic_cast<wxTextEntry*>( cellEditor->GetControl() ) )
902 pattern = txt->GetValue();
903
904 cellEditor->DecRef();
905 }
906
907 if( pattern != m_lastPattern )
908 {
909 m_matchingNets->Clear();
910
911 if( !pattern.IsEmpty() )
912 {
913 EDA_COMBINED_MATCHER matcher( pattern, CTX_NETCLASS );
914
915 m_matchingNets->Report( wxString::Format( _( "<b>Nets matching '%s':</b>" ), pattern ) );
916
917 for( const wxString& net : m_netNames )
918 {
919 if( matcher.StartsWith( net ) )
920 m_matchingNets->Report( net );
921 }
922 }
923
924 m_matchingNets->Flush();
925 m_lastPattern = pattern;
926 }
927}
928
929
931{
932 if( !m_netclassGrid->CommitPendingChanges() || !m_assignmentGrid->CommitPendingChanges() )
933 return false;
934
935 // Test net class parameters.
936 for( int row = 0; row < m_netclassGrid->GetNumberRows(); row++ )
937 {
938 wxString netclassName = m_netclassGrid->GetCellValue( row, GRID_NAME );
939 netclassName.Trim( true );
940 netclassName.Trim( false );
941
942 if( !validateNetclassName( row, netclassName, false ) )
943 return false;
944
945 if( !validateNetclassClearance( row ) )
946 return false;
947 }
948
949 return true;
950}
951
952
953void PANEL_SETUP_NETCLASSES::ImportSettingsFrom( const std::shared_ptr<NET_SETTINGS>& aNetSettings )
954{
955 std::shared_ptr<NET_SETTINGS> savedSettings = m_netSettings;
956
957 m_netSettings = aNetSettings;
959
961
962 m_netclassGrid->ForceRefresh();
963 m_assignmentGrid->ForceRefresh();
964
965 m_netSettings = std::move( savedSettings );
966}
967
968
970{
971 m_netclassGrid->OnMoveRowUp(
972 [&]( int row )
973 {
974 // Can't move the Default netclass
975 return row != m_netclassGrid->GetNumberRows() - 1;
976 },
977 [&]( int row )
978 {
979 m_netclassGrid->SwapRows( row, row - 1 );
980 m_netclassesDirty = true;
981 } );
982}
983
984
986{
987 m_netclassGrid->OnMoveRowDown(
988 [&]( int row )
989 {
990 // Can't move the Default netclass
991 return row + 1 != m_netclassGrid->GetNumberRows() - 1;
992 },
993 [&]( int row )
994 {
995 m_netclassGrid->SwapRows( row, row + 1 );
996 m_netclassesDirty = true;
997 } );
998}
999
1000
1001void PANEL_SETUP_NETCLASSES::UpdateDelayProfileNames( const std::vector<wxString>& aNames ) const
1002{
1003 wxArrayString profileNames;
1004 profileNames.push_back( wxEmptyString );
1005 std::ranges::for_each( aNames,
1006 [&]( const wxString& aName )
1007 {
1008 profileNames.push_back( aName );
1009 } );
1010
1011 wxGridCellAttr* attr = new wxGridCellAttr;
1012 attr->SetEditor( new wxGridCellChoiceEditor( profileNames, false ) );
1013 m_netclassGrid->SetColAttr( GRID_DELAY_PROFILE, attr );
1014}
const char * name
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:114
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:112
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:147
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:399
A collection of nets and the parameters used to route or test these nets.
Definition netclass.h:45
static const char Default[]
the name of the default NETCLASS
Definition netclass.h:47
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:259
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:202
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