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
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,
83 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 ),
89 m_lastCheckedTicker( 0 ),
90 m_hoveredCol( -1 ),
91 m_lastNetclassGridWidth( -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
99 g_lineStyleIcons.push_back( BITMAPS::stroke_none );
100 g_lineStyleNames.push_back( _( "<Not defined>" ) );
101 g_lineStyleIcons.push_back( BITMAPS::stroke_solid );
102 g_lineStyleNames.push_back( _( "Solid" ) );
103 g_lineStyleIcons.push_back( BITMAPS::stroke_dash );
104 g_lineStyleNames.push_back( _( "Dashed" ) );
105 g_lineStyleIcons.push_back( BITMAPS::stroke_dot );
106 g_lineStyleNames.push_back( _( "Dotted" ) );
107 g_lineStyleIcons.push_back( BITMAPS::stroke_dashdot );
108 g_lineStyleNames.push_back( _( "Dash-Dot" ) );
109 g_lineStyleIcons.push_back( BITMAPS::stroke_dashdotdot );
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 Freeze();
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 )
149 else
151 }
152
154
155 if( m_isEEschema )
157 else
159
161
162 wxGridCellAttr* attr = new wxGridCellAttr;
163 attr->SetRenderer( new GRID_CELL_COLOR_RENDERER( PAGED_DIALOG::GetDialog( this ) ) );
164 attr->SetEditor( new GRID_CELL_COLOR_SELECTOR( PAGED_DIALOG::GetDialog( this ), m_netclassGrid ) );
165 m_netclassGrid->SetColAttr( GRID_SCHEMATIC_COLOR, attr );
166
167 attr = new wxGridCellAttr;
168 attr->SetRenderer( new GRID_CELL_COLOR_RENDERER( PAGED_DIALOG::GetDialog( this ) ) );
169 attr->SetEditor( new GRID_CELL_COLOR_SELECTOR( PAGED_DIALOG::GetDialog( this ), m_netclassGrid ) );
170 m_netclassGrid->SetColAttr( GRID_PCB_COLOR, attr );
171
172 attr = new wxGridCellAttr;
175 m_netclassGrid->SetColAttr( GRID_LINESTYLE, attr );
176
177 if( m_isEEschema )
178 {
179 m_importColorsButton->Hide();
180 }
181 else
182 {
183 m_colorDefaultHelpText->SetLabel( _( "Set color to transparent to use layer default color." ) );
184 m_colorDefaultHelpText->GetParent()->Layout();
185 }
186
187 m_colorDefaultHelpText->SetFont( KIUI::GetInfoFont( this ).Italic() );
188
199
200
201 // Be sure the column labels are readable
203
204 m_netclassGrid->PushEventHandler( new GRID_TRICKS( m_netclassGrid ) );
205 m_assignmentGrid->PushEventHandler( new GRID_TRICKS( m_assignmentGrid ) );
206
207 m_netclassGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
208 m_assignmentGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
209
210 m_splitter->SetSashPosition( cfg->m_NetclassPanel.sash_pos );
211
212 m_addButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
213 m_removeButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
214
215 m_addAssignmentButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
216 m_removeAssignmentButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
217
218 m_moveUpButton->SetBitmap( KiBitmapBundle( BITMAPS::small_up ) );
219 m_moveDownButton->SetBitmap( KiBitmapBundle( BITMAPS::small_down ) );
220
221 // wxFormBuilder doesn't include this event...
222 m_netclassGrid->Connect( wxEVT_GRID_CELL_CHANGING,
224 nullptr, this );
225
226 // Handle tooltips for grid
227 m_netclassGrid->GetGridColLabelWindow()->Bind( wxEVT_MOTION, &PANEL_SETUP_NETCLASSES::OnNetclassGridMouseEvent,
228 this );
229
230 // Allow sorting assignments by column
231 m_assignmentGrid->Connect( wxEVT_GRID_LABEL_LEFT_CLICK,
233 nullptr, this );
234
235 m_frame->Bind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_NETCLASSES::onUnitsChanged, this );
236
237 m_netclassGrid->EndBatch();
238 m_assignmentGrid->EndBatch();
239 Thaw();
240
241 Bind( wxEVT_IDLE,
242 [this]( wxIdleEvent& aEvent )
243 {
244 // Careful of consuming CPU in an idle event handler. Check the ticker first to
245 // see if there's even a possibility of the netclasses having changed.
247 {
248 wxWindow* dialog = wxGetTopLevelParent( this );
249 wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
250
251 if( topLevelFocus == dialog && m_lastLoaded != m_netSettings->GetNetclasses() )
252 checkReload();
253 }
254 } );
255
256 m_matchingNets->SetFont( KIUI::GetInfoFont( this ) );
257}
258
259
261{
263 cfg->m_NetclassPanel.sash_pos = m_splitter->GetSashPosition();
264
265 if( m_isEEschema )
267 else
269
270 // Delete the GRID_TRICKS.
271 m_netclassGrid->PopEventHandler( true );
272 m_assignmentGrid->PopEventHandler( true );
273
274 m_netclassGrid->Disconnect( wxEVT_GRID_CELL_CHANGING,
276 nullptr, this );
277
278 m_assignmentGrid->Disconnect( wxEVT_GRID_LABEL_LEFT_CLICK,
280 nullptr, this );
281
282 m_frame->Unbind( EDA_EVT_UNITS_CHANGED, &PANEL_SETUP_NETCLASSES::onUnitsChanged, this );
283}
284
285
287{
288 auto netclassToGridRow =
289 [&]( int aRow, const NETCLASS* nc )
290 {
291 m_netclassGrid->SetCellValue( aRow, GRID_NAME, nc->GetName() );
292 m_netclassGrid->SetCellValue( aRow, GRID_DELAY_PROFILE, nc->GetDelayProfile() );
293
294 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_WIREWIDTH, nc->GetWireWidthOpt() );
295 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_BUSWIDTH, nc->GetBusWidthOpt() );
296
297 wxString colorAsString = nc->GetSchematicColor().ToCSSString();
298 m_netclassGrid->SetCellValue( aRow, GRID_SCHEMATIC_COLOR, colorAsString );
299
300 if( nc->HasLineStyle() )
301 {
302 int lineStyleIdx = std::max( 0, nc->GetLineStyle() );
303
304 if( lineStyleIdx >= (int) g_lineStyleNames.size() + 1 )
305 lineStyleIdx = 0;
306
307 m_netclassGrid->SetCellValue( aRow, GRID_LINESTYLE, g_lineStyleNames[lineStyleIdx + 1] );
308 }
309 else
310 {
311 // <Not defined> line style in list.
312 m_netclassGrid->SetCellValue( aRow, GRID_LINESTYLE, g_lineStyleNames[0] );
313 }
314
315 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_CLEARANCE, nc->GetClearanceOpt() );
316 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_TRACKSIZE, nc->GetTrackWidthOpt() );
317 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_VIASIZE, nc->GetViaDiameterOpt() );
318 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_VIADRILL, nc->GetViaDrillOpt() );
319 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_uVIASIZE, nc->GetuViaDiameterOpt() );
320 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_uVIADRILL, nc->GetuViaDrillOpt() );
321 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_DIFF_PAIR_WIDTH, nc->GetDiffPairWidthOpt() );
322 m_netclassGrid->SetOptionalUnitValue( aRow, GRID_DIFF_PAIR_GAP, nc->GetDiffPairGapOpt() );
323
324 colorAsString = nc->GetPcbColor().ToCSSString();
325 m_netclassGrid->SetCellValue( aRow, GRID_PCB_COLOR, colorAsString );
326
327 if( nc->IsDefault() )
328 {
329 m_netclassGrid->SetReadOnly( aRow, GRID_NAME );
330 m_netclassGrid->SetReadOnly( aRow, GRID_PCB_COLOR );
331 m_netclassGrid->SetReadOnly( aRow, GRID_SCHEMATIC_COLOR );
332 m_netclassGrid->SetReadOnly( aRow, GRID_LINESTYLE );
333 }
334
335 setNetclassRowNullableEditors( aRow, nc->IsDefault() );
336 };
337
338 // Get the netclasses sorted by priority
339 std::vector<const NETCLASS*> netclasses;
340 netclasses.reserve( m_netSettings->GetNetclasses().size() );
341
342 for( const auto& [name, netclass] : m_netSettings->GetNetclasses() )
343 netclasses.push_back( netclass.get() );
344
345 std::sort( netclasses.begin(), netclasses.end(),
346 []( const NETCLASS* nc1, const NETCLASS* nc2 )
347 {
348 return nc1->GetPriority() < nc2->GetPriority();
349 } );
350
351 // Enter user-defined netclasses
353 m_netclassGrid->AppendRows( static_cast<int>( netclasses.size() ) );
354
355 int row = 0;
356
357 for( const NETCLASS* nc : netclasses )
358 netclassToGridRow( row++, nc );
359
360 // Enter the Default netclass.
361 m_netclassGrid->AppendRows( 1 );
362 netclassToGridRow( row, m_netSettings->GetDefaultNetclass().get() );
363
365 m_assignmentGrid->AppendRows( m_netSettings->GetNetclassPatternAssignments().size() );
366
367 row = 0;
368
369 for( const auto& [matcher, netclassName] : m_netSettings->GetNetclassPatternAssignments() )
370 {
371 m_assignmentGrid->SetCellValue( row, 0, matcher->GetPattern() );
372 m_assignmentGrid->SetCellValue( row, 1, netclassName );
373 row++;
374 }
375}
376
377
379{
380 // Set nullable editors
381 auto setCellEditor = [this, aRowId, aIsDefault]( int aCol )
382 {
383 GRID_CELL_MARK_AS_NULLABLE* cellEditor;
384
385 if( aIsDefault )
386 cellEditor = new GRID_CELL_MARK_AS_NULLABLE( false );
387 else
388 cellEditor = new GRID_CELL_MARK_AS_NULLABLE( true );
389
390 wxGridCellAttr* attr = m_netclassGrid->GetOrCreateCellAttr( aRowId, aCol );
391 attr->SetEditor( cellEditor );
392 attr->DecRef();
393 };
394
395 setCellEditor( GRID_WIREWIDTH );
396 setCellEditor( GRID_BUSWIDTH );
397 setCellEditor( GRID_CLEARANCE );
398 setCellEditor( GRID_TRACKSIZE );
399 setCellEditor( GRID_VIASIZE );
400 setCellEditor( GRID_VIADRILL );
401 setCellEditor( GRID_VIADRILL );
402 setCellEditor( GRID_uVIASIZE );
403 setCellEditor( GRID_uVIADRILL );
404 setCellEditor( GRID_DIFF_PAIR_WIDTH );
405 setCellEditor( GRID_DIFF_PAIR_GAP );
406}
407
408
410{
411 // MUST update the ticker before calling IsOK (or we'll end up re-entering through the idle
412 // event until we crash the stack).
414
415 if( IsOK( m_parent, _( "The netclasses have been changed outside the Setup dialog.\n"
416 "Do you wish to reload them?" ) ) )
417 {
418 m_lastLoaded = m_netSettings->GetNetclasses();
420 }
421}
422
423
424void PANEL_SETUP_NETCLASSES::onUnitsChanged( wxCommandEvent& aEvent )
425{
426 std::shared_ptr<NET_SETTINGS> tempNetSettings = std::make_shared<NET_SETTINGS>( nullptr, "" );
427 std::shared_ptr<NET_SETTINGS> saveNetSettings = m_netSettings;
428
429 m_netSettings = tempNetSettings;
430
432
433 m_schUnitsProvider->SetUserUnits( m_frame->GetUserUnits() );
434 m_pcbUnitsProvider->SetUserUnits( m_frame->GetUserUnits() );
435
437
438 m_netSettings = saveNetSettings;
439
440 aEvent.Skip();
441}
442
443
445{
446 m_lastLoaded = m_netSettings->GetNetclasses();
448
450 AdjustAssignmentGridColumns( GetSize().x * 3 / 5 );
451
452 return true;
453}
454
455
457{
459
460 wxArrayString netclassNames;
461
462 for( int ii = 0; ii < m_netclassGrid->GetNumberRows(); ii++ )
463 {
464 wxString netclassName = m_netclassGrid->GetCellValue( ii, GRID_NAME );
465
466 if( !netclassName.IsEmpty() )
467 netclassNames.push_back( netclassName );
468 }
469
470 wxGridCellAttr* attr = new wxGridCellAttr;
471 attr->SetEditor( new wxGridCellChoiceEditor( netclassNames ) );
472 m_assignmentGrid->SetColAttr( 1, attr );
473}
474
475
477{
478 if( !Validate() )
479 return false;
480
481 auto gridRowToNetclass =
482 [&]( int aRow, const std::shared_ptr<NETCLASS>& nc )
483 {
484 if( nc->IsDefault() )
485 nc->SetPriority( std::numeric_limits<int>::max() );
486 else
487 nc->SetPriority( aRow );
488
489 nc->SetName( m_netclassGrid->GetCellValue( aRow, GRID_NAME ) );
490 nc->SetDelayProfile( m_netclassGrid->GetCellValue( aRow, GRID_DELAY_PROFILE ) );
491
492 nc->SetWireWidth( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_WIREWIDTH ) );
493 nc->SetBusWidth( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_BUSWIDTH ) );
494
495 wxString lineStyle = m_netclassGrid->GetCellValue( aRow, GRID_LINESTYLE );
496 int lineIdx = g_lineStyleNames.Index( lineStyle );
497
498 if( lineIdx == 0 )
499 nc->SetLineStyle( std::optional<int>() );
500 else
501 nc->SetLineStyle( lineIdx - 1 );
502
503 wxASSERT_MSG( lineIdx >= 0, "Line style name not found." );
504
505 // clang-format off
506 nc->SetClearance( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_CLEARANCE ) );
507 nc->SetTrackWidth( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_TRACKSIZE ) );
508 nc->SetViaDiameter( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_VIASIZE ) );
509 nc->SetViaDrill( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_VIADRILL ) );
510 nc->SetuViaDiameter( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_uVIASIZE ) );
511 nc->SetuViaDrill( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_uVIADRILL ) );
512 nc->SetDiffPairWidth( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_DIFF_PAIR_WIDTH ) );
513 nc->SetDiffPairGap( m_netclassGrid->GetOptionalUnitValue( aRow, GRID_DIFF_PAIR_GAP ) );
514 // clang-format on
515
516 if( !nc->IsDefault() )
517 {
518 wxString color = m_netclassGrid->GetCellValue( aRow, GRID_PCB_COLOR );
519 KIGFX::COLOR4D newPcbColor( color );
520
521 if( newPcbColor != KIGFX::COLOR4D::UNSPECIFIED )
522 nc->SetPcbColor( newPcbColor );
523
524 color = m_netclassGrid->GetCellValue( aRow, GRID_SCHEMATIC_COLOR );
525 KIGFX::COLOR4D newSchematicColor( color );
526
527 if( newSchematicColor != KIGFX::COLOR4D::UNSPECIFIED )
528 nc->SetSchematicColor( newSchematicColor );
529 }
530 };
531
532 m_netSettings->ClearNetclasses();
533
534 // Copy the default NetClass:
535 gridRowToNetclass( m_netclassGrid->GetNumberRows() - 1, m_netSettings->GetDefaultNetclass() );
536
537 // Copy other NetClasses:
538 for( int row = 0; row < m_netclassGrid->GetNumberRows() - 1; ++row )
539 {
540 auto nc = std::make_shared<NETCLASS>( m_netclassGrid->GetCellValue( row, GRID_NAME ), false );
541 gridRowToNetclass( row, nc );
542 m_netSettings->SetNetclass( nc->GetName(), nc );
543 }
544
545 m_netSettings->ClearNetclassPatternAssignments();
546 m_netSettings->ClearAllCaches();
547
548 for( int row = 0; row < m_assignmentGrid->GetNumberRows(); ++row )
549 {
550 wxString pattern = m_assignmentGrid->GetCellValue( row, 0 );
551 wxString netclass = m_assignmentGrid->GetCellValue( row, 1 );
552
553 m_netSettings->SetNetclassPatternAssignment( pattern, netclass );
554 }
555
556 return true;
557}
558
559
560bool PANEL_SETUP_NETCLASSES::validateNetclassName( int aRow, const wxString& aName,
561 bool focusFirst )
562{
563 wxString tmp = aName;
564
565 tmp.Trim( true );
566 tmp.Trim( false );
567
568 if( tmp.IsEmpty() )
569 {
570 wxString msg = _( "Netclass must have a name." );
571 PAGED_DIALOG::GetDialog( this )->SetError( msg, this, m_netclassGrid, aRow, GRID_NAME );
572 return false;
573 }
574
575 for( int ii = 0; ii < m_netclassGrid->GetNumberRows(); ii++ )
576 {
577 if( ii != aRow && m_netclassGrid->GetCellValue( ii, GRID_NAME ).CmpNoCase( tmp ) == 0 )
578 {
579 wxString msg = _( "Netclass name already in use." );
581 focusFirst ? aRow : ii, GRID_NAME );
582 return false;
583 }
584 }
585
586 return true;
587}
588
589
591{
592 // Clip clearance. This is not a nag; we can end up with overflow errors and very poor
593 // performance if the clearance is too large.
594
595 std::optional<int> clearance = m_netclassGrid->GetOptionalUnitValue( aRow, GRID_CLEARANCE );
596
597 if( clearance.has_value() && clearance.value() > MAXIMUM_CLEARANCE )
598 {
599 wxString msg = wxString::Format( _( "Clearance was too large. It has been clipped to %s." ),
603 return false;
604 }
605
606 return true;
607}
608
609
611{
612 if( event.GetCol() == GRID_NAME )
613 {
614 if( validateNetclassName( event.GetRow(), event.GetString() ) )
615 {
616 wxString oldName = m_netclassGrid->GetCellValue( event.GetRow(), GRID_NAME );
617 wxString newName = event.GetString();
618
619 if( !oldName.IsEmpty() )
620 {
621 for( int row = 0; row < m_assignmentGrid->GetNumberRows(); ++row )
622 {
623 if( m_assignmentGrid->GetCellValue( row, 1 ) == oldName )
624 m_assignmentGrid->SetCellValue( row, 1, newName );
625 }
626 }
627
628 m_netclassesDirty = true;
629 }
630 else
631 {
632 event.Veto();
633 }
634 }
635 else if( event.GetCol() == GRID_CLEARANCE )
636 {
637 validateNetclassClearance( event.GetRow() );
638 }
639}
640
642{
643 event.Skip();
644
646 return;
647
648 if( ( event.GetCol() < 0 ) || ( event.GetCol() >= m_assignmentGrid->GetNumberCols() ) )
649 return;
650
651 // Toggle sort order if the same column is clicked
652 if( event.GetCol() != m_sortCol )
653 {
654 m_sortCol = event.GetCol();
655 m_sortAsc = true;
656 }
657 else
658 {
660 }
661
662 std::vector<std::pair<wxString, wxString>> netclassesassignments;
663 netclassesassignments.reserve( m_assignmentGrid->GetNumberRows() );
664
665 for( int row = 0; row < m_assignmentGrid->GetNumberRows(); ++row )
666 {
667 netclassesassignments.emplace_back( m_assignmentGrid->GetCellValue( row, 0 ),
668 m_assignmentGrid->GetCellValue( row, 1 ) );
669 }
670
671 std::sort( netclassesassignments.begin(), netclassesassignments.end(),
672 [this]( const std::pair<wxString, wxString>& assign1,
673 const std::pair<wxString, wxString>& assign2 )
674 {
675 const wxString& str1 = ( m_sortCol == 0 ) ? assign1.first : assign1.second;
676 const wxString& str2 = ( m_sortCol == 0 ) ? assign2.first : assign2.second;
677 return m_sortAsc ? ( str1 < str2 ) : ( str1 > str2 );
678 } );
679
681 m_assignmentGrid->AppendRows( netclassesassignments.size() );
682
683 int row = 0;
684
685 for( const auto& [pattern, netclassName] : netclassesassignments )
686 {
687 m_assignmentGrid->SetCellValue( row, 0, pattern );
688 m_assignmentGrid->SetCellValue( row, 1, netclassName );
689 row++;
690 }
691}
692
694{
695 int col = m_netclassGrid->XToCol( aEvent.GetPosition().x );
696
697 if( aEvent.Moving() || aEvent.Entering() )
698 {
699 aEvent.Skip();
700
701 if( col == wxNOT_FOUND )
702 {
703 m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
704 return;
705 }
706
707 if( col == m_hoveredCol )
708 return;
709
710 m_hoveredCol = col;
711
712 wxString tip;
713
714 switch( col )
715 {
716 case GRID_CLEARANCE: tip = _( "Minimum copper clearance" ); break;
717 case GRID_TRACKSIZE: tip = _( "Minimum track width" ); break;
718 case GRID_VIASIZE: tip = _( "Via pad diameter" ); break;
719 case GRID_VIADRILL: tip = _( "Via plated hole diameter" ); break;
720 case GRID_uVIASIZE: tip = _( "Microvia pad diameter" ); break;
721 case GRID_uVIADRILL: tip = _( "Microvia plated hole diameter" ); break;
722 case GRID_DIFF_PAIR_WIDTH: tip = _( "Differential pair track width" ); break;
723 case GRID_DIFF_PAIR_GAP: tip = _( "Differential pair gap" ); break;
724 case GRID_WIREWIDTH: tip = _( "Schematic wire thickness" ); break;
725 case GRID_BUSWIDTH: tip = _( "Bus wire thickness" ); break;
726 case GRID_SCHEMATIC_COLOR: tip = _( "Schematic wire color" ); break;
727 case GRID_LINESTYLE: tip = _( "Schematic wire line style" ); break;
728 case GRID_PCB_COLOR: tip = _( "PCB netclass color" ); break;
729 }
730
731 m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
732 m_netclassGrid->GetGridColLabelWindow()->SetToolTip( tip );
733 }
734 else if( aEvent.Leaving() )
735 {
736 m_netclassGrid->GetGridColLabelWindow()->UnsetToolTip();
737 aEvent.Skip();
738 }
739
740 aEvent.Skip();
741}
742
743
745{
747 return;
748
749 m_netclassGrid->InsertRows();
750
751 // Set defaults where required
752 wxString colorAsString = KIGFX::COLOR4D::UNSPECIFIED.ToCSSString();
753 m_netclassGrid->SetCellValue( 0, GRID_PCB_COLOR, colorAsString );
754 m_netclassGrid->SetCellValue( 0, GRID_SCHEMATIC_COLOR, colorAsString );
755 m_netclassGrid->SetCellValue( 0, GRID_LINESTYLE, g_lineStyleNames[0] );
756
757 // Set the row nullable editors
759
760 m_netclassGrid->MakeCellVisible( 0, 0 );
761 m_netclassGrid->SetGridCursor( 0, 0 );
762
763 m_netclassGrid->EnableCellEditControl( true );
764 m_netclassGrid->ShowCellEditControl();
765
766 m_netclassesDirty = true;
767}
768
769
771{
773 return;
774
775 int curRow = m_netclassGrid->GetGridCursorRow();
776
777 if( curRow < 0 )
778 {
779 return;
780 }
781 else if( curRow == m_netclassGrid->GetNumberRows() - 1 )
782 {
783 wxWindow* topLevelParent = wxGetTopLevelParent( this );
784
785 DisplayErrorMessage( topLevelParent, _( "The default net class is required." ) );
786 return;
787 }
788
789 // reset the net class to default for members of the removed class
790 wxString classname = m_netclassGrid->GetCellValue( curRow, GRID_NAME );
791
792 for( int row = 0; row < m_assignmentGrid->GetNumberRows(); ++row )
793 {
794 if( m_assignmentGrid->GetCellValue( row, 1 ) == classname )
795 m_assignmentGrid->SetCellValue( row, 1, NETCLASS::Default );
796 }
797
798 m_netclassGrid->DeleteRows( curRow, 1 );
799
800 m_netclassGrid->MakeCellVisible( std::max( 0, curRow-1 ), m_netclassGrid->GetGridCursorCol() );
801 m_netclassGrid->SetGridCursor( std::max( 0, curRow-1 ), m_netclassGrid->GetGridCursorCol() );
802
803 m_netclassesDirty = true;
804}
805
806
808{
809 if( aWidth != m_lastNetclassGridWidth )
810 {
812
813 // Account for scroll bars
814 aWidth -= ( m_netclassGrid->GetSize().x - m_netclassGrid->GetClientSize().x );
815
816 for( int i = 1; i < m_netclassGrid->GetNumberCols(); i++ )
817 {
818 if( m_netclassGrid->GetColSize( i ) > 0 )
819 {
820 m_netclassGrid->SetColSize( i, m_originalColWidths[ i ] );
821 aWidth -= m_originalColWidths[ i ];
822 }
823 }
824
825 m_netclassGrid->SetColSize( 0, std::max( aWidth - 2, m_originalColWidths[ 0 ] ) );
826 }
827}
828
829
831{
832 AdjustNetclassGridColumns( event.GetSize().GetX() );
833
834 event.Skip();
835}
836
837
839{
841 return;
842
843 int row = m_assignmentGrid->GetNumberRows();
844 m_assignmentGrid->AppendRows();
845
846 m_assignmentGrid->SetCellValue( row, 1, m_netSettings->GetDefaultNetclass()->GetName() );
847
848 m_assignmentGrid->MakeCellVisible( row, 0 );
849 m_assignmentGrid->SetGridCursor( row, 0 );
850
851 m_assignmentGrid->EnableCellEditControl( true );
852 m_assignmentGrid->ShowCellEditControl();
853}
854
855
857{
859 return;
860
861 int curRow = m_assignmentGrid->GetGridCursorRow();
862
863 if( curRow < 0 )
864 return;
865
866 m_assignmentGrid->DeleteRows( curRow, 1 );
867
868 if( m_assignmentGrid->GetNumberRows() > 0 )
869 {
870 m_assignmentGrid->MakeCellVisible( std::max( 0, curRow-1 ), 0 );
871 m_assignmentGrid->SetGridCursor( std::max( 0, curRow-1 ), 0 );
872 }
873}
874
875
877{
878 const std::map<wxString, std::shared_ptr<NETCLASS>>& netclasses =
879 m_netSettings->GetNetclasses();
880
881 for( int row = 0; row < m_netclassGrid->GetNumberRows() - 1; ++row )
882 {
883 wxString netclassName = m_netclassGrid->GetCellValue( row, GRID_NAME );
884
885 if( netclasses.find( netclassName ) != netclasses.end() )
886 {
887 const KIGFX::COLOR4D ncColor = netclasses.at( netclassName )->GetSchematicColor();
888 m_netclassGrid->SetCellValue( row, GRID_PCB_COLOR, ncColor.ToCSSString() );
889 }
890 }
891}
892
893
895{
896 // Account for scroll bars
897 aWidth -= ( m_assignmentGrid->GetSize().x - m_assignmentGrid->GetClientSize().x );
898
899 int classNameWidth = 160;
900 m_assignmentGrid->SetColSize( 1, classNameWidth );
901 m_assignmentGrid->SetColSize( 0, std::max( aWidth - classNameWidth, classNameWidth ) );
902}
903
904
906{
907 AdjustAssignmentGridColumns( event.GetSize().GetX() );
908
909 event.Skip();
910}
911
912
913void PANEL_SETUP_NETCLASSES::OnUpdateUI( wxUpdateUIEvent& event )
914{
916 {
918 m_netclassesDirty = false;
919 }
920
922 {
923 AdjustNetclassGridColumns( GetSize().x - 1 );
925 }
926
927 if( m_assignmentGrid->GetNumberRows() == 0 )
928 return;
929
930 wxString pattern;
931 int row = m_assignmentGrid->GetGridCursorRow();
932 int col = m_assignmentGrid->GetGridCursorCol();
933
934 if( row >= 0 )
935 pattern = m_assignmentGrid->GetCellValue( row, 0 );
936
937 if( col == 0 && m_assignmentGrid->IsCellEditControlShown() )
938 {
939 wxGridCellEditor* cellEditor = m_assignmentGrid->GetCellEditor( row, 0 );
940
941 if( wxTextEntry* txt = dynamic_cast<wxTextEntry*>( cellEditor->GetControl() ) )
942 pattern = txt->GetValue();
943
944 cellEditor->DecRef();
945 }
946
947 if( pattern != m_lastPattern )
948 {
950
951 if( !pattern.IsEmpty() )
952 {
953 EDA_COMBINED_MATCHER matcher( pattern, CTX_NETCLASS );
954
955 m_matchingNets->Report( wxString::Format( _( "<b>Nets matching '%s':</b>" ), pattern ) );
956
957 for( const wxString& net : m_netNames )
958 {
959 if( matcher.StartsWith( net ) )
960 m_matchingNets->Report( net );
961 }
962 }
963
965 m_lastPattern = pattern;
966 }
967}
968
969
971{
973 return false;
974
975 // Test net class parameters.
976 for( int row = 0; row < m_netclassGrid->GetNumberRows(); row++ )
977 {
978 wxString netclassName = m_netclassGrid->GetCellValue( row, GRID_NAME );
979 netclassName.Trim( true );
980 netclassName.Trim( false );
981
982 if( !validateNetclassName( row, netclassName, false ) )
983 return false;
984
985 if( !validateNetclassClearance( row ) )
986 return false;
987 }
988
989 return true;
990}
991
992
993void PANEL_SETUP_NETCLASSES::ImportSettingsFrom( const std::shared_ptr<NET_SETTINGS>& aNetSettings )
994{
995 std::shared_ptr<NET_SETTINGS> savedSettings = m_netSettings;
996
997 m_netSettings = aNetSettings;
999
1001
1002 m_netclassGrid->ForceRefresh();
1003 m_assignmentGrid->ForceRefresh();
1004
1005 m_netSettings = std::move( savedSettings );
1006}
1007
1008
1010{
1012 return;
1013
1014 // Work out which rows are selected
1015 std::vector<int> selectedRows;
1016
1017 for( int i = 0; i < m_netclassGrid->GetNumberRows(); ++i )
1018 {
1019 if( m_netclassGrid->IsInSelection( i, 0 ) )
1020 selectedRows.push_back( i );
1021 }
1022
1023 // Only move one row at a time
1024 if( selectedRows.size() != 1 )
1025 return;
1026
1027 // Can't move the first netclass, nor move the Default netclass
1028 if( selectedRows[0] == 0 || selectedRows[0] == ( m_netclassGrid->GetNumberRows() - 1 ) )
1029 return;
1030
1031 int newRowId = selectedRows[0] - 1;
1032 m_netclassGrid->InsertRows( newRowId );
1033
1034 for( int col = 0; col < m_netclassGrid->GetNumberCols(); col++ )
1035 m_netclassGrid->SetCellValue( newRowId, col, m_netclassGrid->GetCellValue( newRowId + 2, col ) );
1036
1037 // Set the row nullable editors
1038 setNetclassRowNullableEditors( newRowId, false );
1039
1040 m_netclassGrid->DeleteRows( newRowId + 2, 1 );
1041 m_netclassGrid->MakeCellVisible( newRowId, 0 );
1042 m_netclassGrid->SetGridCursor( newRowId, 0 );
1043
1044 m_netclassesDirty = true;
1045}
1046
1047
1049{
1051 return;
1052
1053 // Work out which rows are selected
1054 std::vector<int> selectedRows;
1055
1056 for( int i = 0; i < m_netclassGrid->GetNumberRows(); ++i )
1057 {
1058 if( m_netclassGrid->IsInSelection( i, 0 ) )
1059 selectedRows.push_back( i );
1060 }
1061
1062 // Only move one row at a time
1063 if( selectedRows.size() != 1 )
1064 return;
1065
1066 // Can't move the last row down, nor move the Default netclass
1067 if( selectedRows[0] == ( m_netclassGrid->GetNumberRows() - 2 )
1068 || selectedRows[0] == ( m_netclassGrid->GetNumberRows() - 1 ) )
1069 {
1070 return;
1071 }
1072
1073 int newRowId = selectedRows[0] + 2;
1074 m_netclassGrid->InsertRows( newRowId );
1075
1076 for( int col = 0; col < m_netclassGrid->GetNumberCols(); col++ )
1077 m_netclassGrid->SetCellValue( newRowId, col, m_netclassGrid->GetCellValue( newRowId - 2, col ) );
1078
1079 m_netclassGrid->DeleteRows( newRowId - 2, 1 );
1080 m_netclassGrid->MakeCellVisible( newRowId - 1, 0 );
1081 m_netclassGrid->SetGridCursor( newRowId - 1, 0 );
1082
1083 m_netclassesDirty = true;
1084}
1085
1086
1087void PANEL_SETUP_NETCLASSES::UpdateDelayProfileNames( const std::vector<wxString>& aNames ) const
1088{
1089 wxArrayString profileNames;
1090 profileNames.push_back( wxEmptyString );
1091 std::ranges::for_each( aNames,
1092 [&]( const wxString& aName )
1093 {
1094 profileNames.push_back( aName );
1095 } );
1096
1097 wxGridCellAttr* attr = new wxGridCellAttr;
1098 attr->SetEditor( new wxGridCellChoiceEditor( profileNames, false ) );
1099 m_netclassGrid->SetColAttr( GRID_DELAY_PROFILE, attr );
1100}
int color
Definition: DXF_plotter.cpp:63
const char * name
Definition: DXF_plotter.cpp:62
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
#define MAXIMUM_CLEARANCE
NETCLASS_PANEL m_NetclassPanel
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:104
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:398
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
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)
Class PANEL_SETUP_NETCLASSES_BASE.
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
std::bitset< 64 > m_shownColumns
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
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:556
int GetNetclassesTicker() const
Definition: project.h:121
int GetTextVarsTicker() const
Definition: project.h:118
void SetBitmap(const wxBitmapBundle &aBmp)
EDA_UNITS GetUserUnits() const
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
int GetVisibleWidth(int aCol, bool aHeader=true, bool aContents=true, bool aKeep=false)
Calculate the specified column based on the actual size of the text on screen.
Definition: wx_grid.cpp:776
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition: wx_grid.cpp:490
void SetUnitValue(int aRow, int aCol, int aValue)
Set a unitized cell's value.
Definition: wx_grid.cpp:750
void SetOptionalUnitValue(int aRow, int aCol, std::optional< int > aValue)
Set a unitized cell's optional value.
Definition: wx_grid.cpp:763
void EnsureColLabelsVisible()
Ensure the height of the row displaying the column labels is enough, even if labels are multiline tex...
Definition: wx_grid.cpp:816
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:460
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:479
std::optional< int > GetOptionalUnitValue(int aRow, int aCol)
Apply standard KiCad unit and eval services to a numeric cell.
Definition: wx_grid.cpp:732
void SetAutoEvalCols(const std::vector< int > &aCols)
Definition: wx_grid.h:128
void SetUnitsProvider(UNITS_PROVIDER *aProvider, int aCol=0)
Set a EUNITS_PROVIDER to enable use of unit- and eval-based Getters.
Definition: wx_grid.cpp:692
void ClearRows()
wxWidgets recently added an ASSERT which fires if the position is greater than or equal to the number...
Definition: wx_grid.h:203
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:640
void Clear() override
Delete the stored messages.
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
void Flush()
Build the HTML messages page.
void SetBorders(bool aLeft, bool aRight, bool aTop, bool aBottom)
Definition: wx_panel.h:39
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:260
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:203
This file is part of the common library.
#define _(s)
@ CTX_NETCLASS
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
Definition: ui_common.cpp:156
STL namespace.
std::vector< BITMAPS > g_lineStyleIcons
@ GRID_FIRST_PCBNEW
@ GRID_DIFF_PAIR_GAP
@ GRID_SCHEMATIC_COLOR
@ GRID_DIFF_PAIR_WIDTH
@ GRID_FIRST_EESCHEMA
@ GRID_DELAY_PROFILE
wxArrayString g_lineStyleNames
PGM_BASE & Pgm()
The global program "get" accessor.
Definition: pgm_base.cpp:893
see class PGM_BASE
int clearance