KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_fp_edit_pad_table.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/display.h>
23#include <wx/dcclient.h>
24#include <wx/filedlg.h>
25#include <wx/msgdlg.h>
26#include <wx/settings.h>
27
28#include <column_formatter.h>
29#include <pcb_shape.h>
30#include <widgets/wx_grid.h>
34#include <base_units.h>
35#include <bitmaps.h>
36#include <units_provider.h>
38#include <board.h>
39#include <footprint.h>
41#include <grid_tricks.h>
42#include <kiplatform/ui.h>
43#include <pin_numbers.h>
44#include <board_commit.h>
45#include <pad_utils.h>
46#include <reporter.h>
47#include <table_io.h>
49
50
52
53// Helper to map shape string to PAD_SHAPE
54static PAD_SHAPE ShapeFromString( const wxString& shape )
55{
56 if( shape == _( "Oval" ) ) return PAD_SHAPE::OVAL;
57 if( shape == _( "Rectangle" ) ) return PAD_SHAPE::RECTANGLE;
58 if( shape == _( "Trapezoid" ) ) return PAD_SHAPE::TRAPEZOID;
59 if( shape == _( "Rounded rectangle" ) ) return PAD_SHAPE::ROUNDRECT;
60 if( shape == _( "Chamfered rectangle" ) ) return PAD_SHAPE::CHAMFERED_RECT;
61 if( shape == _( "Custom shape" ) ) return PAD_SHAPE::CUSTOM;
62
63 return PAD_SHAPE::CIRCLE;
64}
65
66
67static wxString ShapeToString( PAD_SHAPE shape )
68{
69 switch( shape )
70 {
71 case PAD_SHAPE::CIRCLE: return _( "Circle" );
72 case PAD_SHAPE::OVAL: return _( "Oval" );
73 case PAD_SHAPE::RECTANGLE: return _( "Rectangle" );
74 case PAD_SHAPE::TRAPEZOID: return _( "Trapezoid" );
75 case PAD_SHAPE::ROUNDRECT: return _( "Rounded rectangle" );
76 case PAD_SHAPE::CHAMFERED_RECT: return _( "Chamfered rectangle" );
77 case PAD_SHAPE::CUSTOM: return _( "Custom shape" );
78 default:
79 wxFAIL_MSG( wxT( "Invalid pad shape" ) );
80 return wxEmptyString;
81 }
82}
83
84
85static wxString GetPadTypeString( const PAD& aPad )
86{
87 if( aPad.IsAperturePad() )
88 return _( "Aperture" );
89
90 const PAD_ATTRIB attrib = aPad.GetAttribute();
91 return PAD_UTILS::GetPadAttributeString( attrib );
92}
93
94
95static void SetPadTypeFromString( PAD& aPad, const wxString& aType )
96{
97 // Heuristic for detecting pads that look like they mean to be
98 // back-only pads.
99 const auto isBackOnlyPad =
100 []( const PAD& pad ) -> bool
101 {
102 const LSET layers = pad.GetLayerSet();
103 return ( layers & LSET::BackMask() ).any() && ( layers & LSET::FrontMask() ).none();
104 };
105
106 if( MatchTranslationOrNative( aType, _HKI( "Through-hole" ), false ) )
107 {
109 }
110 else if( MatchTranslationOrNative( aType, _HKI( "SMD" ), false ) )
111 {
112 // If the pad was already SMD, don't mess with the layers, but if it
113 // is _becoming_ SMD (including if it was an aperture pad), set
114 // default layerset
115 if( aPad.GetAttribute() != PAD_ATTRIB::SMD || aPad.IsAperturePad() )
116 {
117 LSET newLayers = PAD::SMDMask();
118
119 if( isBackOnlyPad( aPad ) )
120 newLayers = newLayers.FlipStandardLayers();
121
122 aPad.SetLayerSet( newLayers );
123 }
124
126 }
127 else if( MatchTranslationOrNative( aType, _HKI( "Connector" ), false ) )
128 {
130 }
131 else if( MatchTranslationOrNative( aType, _HKI( "Non-component" ), false ) )
132 {
134 {
135 // Take the first set copper layer as the default for non-component pads.
136 const LSET currCu = aPad.GetLayerSet() & LSET::AllCuMask();
137
138 if( !currCu.any() )
139 aPad.SetLayerSet( LSET{ F_Cu } );
140 else
141 aPad.SetLayerSet( LSET{ currCu.CuStack().front() } );
142
144 }
145 }
146 else if( MatchTranslationOrNative( aType, _HKI( "NPTH" ), false ) )
147 {
149 }
150 else if( MatchTranslationOrNative( aType, _HKI( "Aperture" ), false ) )
151 {
152 if( !aPad.IsAperturePad() )
153 {
154 // Aperture pads are SMD pads with no copper
156
157 // Unset layers except F.Paste
158 LSET apertureLayers{ F_Paste };
159
160 if( isBackOnlyPad( aPad ) )
161 apertureLayers = apertureLayers.FlipStandardLayers();
162
163 aPad.SetLayerSet( apertureLayers );
164 }
165 }
166
167 // Note, bad strings can sneak in here, e.g. via pasting into a dropdown cell.
168 // So don't assert or crash, it's not necessarily a programming error.
169}
170
171
172static bool DrillsAreEditable( const PAD& aPad )
173{
174 return aPad.GetAttribute() == PAD_ATTRIB::PTH || aPad.GetAttribute() == PAD_ATTRIB::NPTH;
175}
176
177
188static void UpdateDrillCells( WX_GRID& aGrid, UNITS_PROVIDER& aUnitsProvider, int aRowId, const PAD& aPad,
189 bool aPreserveValues )
190{
191 const bool drillIsEditable = DrillsAreEditable( aPad );
192 const wxColour drillTextColour = drillIsEditable ? aGrid.GetDefaultCellTextColour()
193 : wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT );
194
195 aGrid.SetReadOnly( aRowId, COLS::COL_DRILL_X, !drillIsEditable );
196 aGrid.SetReadOnly( aRowId, COLS::COL_DRILL_Y, !drillIsEditable );
197 aGrid.SetCellTextColour( aRowId, COLS::COL_DRILL_X, drillTextColour );
198 aGrid.SetCellTextColour( aRowId, COLS::COL_DRILL_Y, drillTextColour );
199
200 if( aPreserveValues )
201 return;
202
203 const VECTOR2I drill = aPad.GetDrillSize();
204 aGrid.SetCellValue( aRowId, COLS::COL_DRILL_X, drill.x > 0 ? aUnitsProvider.StringFromValue( drill.x, true )
205 : wxString{} );
206 aGrid.SetCellValue( aRowId, COLS::COL_DRILL_Y, drill.y > 0 ? aUnitsProvider.StringFromValue( drill.y, true )
207 : wxString{} );
208}
209
210
216static wxString GetPadTableColLabel( COLS aCol )
217{
218 switch( aCol )
219 {
220 case COLS::COL_NUMBER: return wxT( "Number" );
221 case COLS::COL_TYPE: return wxT( "Type" );
222 case COLS::COL_SHAPE: return wxT( "Shape" );
223 case COLS::COL_POS_X: return wxT( "Pos X" );
224 case COLS::COL_POS_Y: return wxT( "Pos Y" );
225 case COLS::COL_SIZE_X: return wxT( "Size X" );
226 case COLS::COL_SIZE_Y: return wxT( "Size Y" );
227 case COLS::COL_DRILL_X: return wxT( "Drill X" );
228 case COLS::COL_DRILL_Y: return wxT( "Drill Y" );
229 case COLS::COL_P2D_LENGTH: return wxT( "Pad to die length" );
230 case COLS::COL_P2D_DELAY: return wxT( "Pad to die delay" );
231 default:
232 wxFAIL_MSG( wxT( "Invalid column index" ) );
233 return wxEmptyString;
234 }
235}
236
237
238static COLS GetColTypeForString( const wxString& aStr )
239{
240 for( int i = 0; i < static_cast<int>( COLS::COL_COUNT ); i++ )
241 {
242 COLS col = static_cast<COLS>( i );
243
244 if( MatchTranslationOrNative( aStr, GetPadTableColLabel( col ), false ) )
245 return col;
246 }
247 return COLS::COL_COUNT;
248}
249
250
258{
259public:
260 PAD_INFO_FORMATTER( UNITS_PROVIDER& aUnitsProvider, bool aIncludeUnits, BOOL_FORMAT aBoolFormat,
261 REPORTER& aReporter ) :
262 COLUMN_FORMATTER( aUnitsProvider, aIncludeUnits, aBoolFormat, aReporter )
263 {
264 }
265
266 wxString Format( const PAD& aPin, int aFieldId ) const
267 {
268 wxCHECK_MSG( aFieldId >= 0 && aFieldId < static_cast<int>( COLS::COL_COUNT ), wxEmptyString,
269 wxT( "Invalid column index" ) );
270
271 const DIALOG_FP_EDIT_PAD_TABLE::COLS col = static_cast<DIALOG_FP_EDIT_PAD_TABLE::COLS>( aFieldId );
272
273 switch( col )
274 {
276 return aPin.GetNumber();
277
279 return GetPadTypeString( aPin );
280
283
285 return m_unitsProvider.StringFromValue( aPin.GetCenter().x, m_includeUnits );
286
288 return m_unitsProvider.StringFromValue( aPin.GetCenter().y, m_includeUnits );
289
291 return m_unitsProvider.StringFromValue( aPin.GetSize( PADSTACK::ALL_LAYERS ).x, m_includeUnits );
292
294 return m_unitsProvider.StringFromValue( aPin.GetSize( PADSTACK::ALL_LAYERS ).y, m_includeUnits );
295
297 return m_unitsProvider.StringFromValue( aPin.GetDrillSize().x, m_includeUnits );
298
300 return m_unitsProvider.StringFromValue( aPin.GetDrillSize().y, m_includeUnits );
301
303 if( aPin.GetPadToDieLength() )
304 return m_unitsProvider.StringFromValue( aPin.GetPadToDieLength(), m_includeUnits );
305
306 return wxEmptyString;
307
309 if( aPin.GetPadToDieDelay() )
310 return m_unitsProvider.StringFromValue( aPin.GetPadToDieDelay(), m_includeUnits, EDA_DATA_TYPE::TIME );
311
312 return wxEmptyString;
313
315 return wxEmptyString;
316 }
317
318 wxFAIL_MSG( "Invalid column index" );
319 return wxEmptyString;
320 }
321
328 void UpdatePad( PAD& aPad, const wxString& aValue, int aFieldId ) const
329 {
330 switch( aFieldId )
331 {
333 aPad.SetNumber( aValue );
334 break;
335
337 SetPadTypeFromString( aPad, aValue );
338 break;
339
341 // Note that this will blow away any layer-specific shapes, but this process
342 // doesn't support layer-specific editing.
344 [&]( PCB_LAYER_ID aLayer )
345 {
346 aPad.SetShape( aLayer, ShapeFromString( aValue ) );
347 } );
348 break;
349
351 {
352 VECTOR2I pos = aPad.GetPosition();
353 pos.x = m_unitsProvider.ValueFromString( aValue );
354 aPad.SetPosition( pos );
355 break;
356 }
357
359 {
360 VECTOR2I pos = aPad.GetPosition();
361 pos.y = m_unitsProvider.ValueFromString( aValue );
362 aPad.SetPosition( pos );
363 break;
364 }
365
367 // Same as shape: overwrite all layers
369 [&]( PCB_LAYER_ID aLayer )
370 {
371 VECTOR2I size = aPad.GetSize( aLayer );
372 size.x = m_unitsProvider.ValueFromString( aValue );
373 aPad.SetSize( aLayer, size );
374 } );
375 break;
376
378 // Same as shape: overwrite all layers
380 [&]( PCB_LAYER_ID aLayer )
381 {
382 VECTOR2I size = aPad.GetSize( aLayer );
383 size.y = m_unitsProvider.ValueFromString( aValue );
384 aPad.SetSize( aLayer, size );
385 } );
386 break;
387
389 {
390 VECTOR2I drill = aPad.GetDrillSize();
391 drill.x = m_unitsProvider.ValueFromString( aValue );
392 aPad.SetDrillSize( drill );
393 break;
394 }
395
397 {
398 VECTOR2I drill = aPad.GetDrillSize();
399 drill.y = m_unitsProvider.ValueFromString( aValue );
400 aPad.SetDrillSize( drill );
401 break;
402 }
403
405 if( aValue.empty() )
406 aPad.SetPadToDieLength( 0 );
407 else
408 aPad.SetPadToDieLength( m_unitsProvider.ValueFromString( aValue ) );
409
410 break;
411
413 if( aValue.empty() )
414 aPad.SetPadToDieDelay( 0 );
415 else
416 aPad.SetPadToDieDelay( m_unitsProvider.ValueFromString( aValue, EDA_DATA_TYPE::TIME ) );
417
418 break;
419
420 default:
421 wxFAIL_MSG( "Invalid column index" );
422 break;
423 }
424 }
425};
426
427
429 DIALOG_FP_EDIT_PAD_TABLE_BASE( (wxWindow*) aParent ),
430 m_frame( aParent ),
431 m_footprint( aFootprint ),
433 m_summaryDirty( true )
434{
436
437 // The base class created a single placeholder row; resize the grid to fit the pads.
438 if( m_grid->GetNumberRows() > 0 )
439 m_grid->DeleteRows( 0, m_grid->GetNumberRows() );
440
441 if( !m_originalPads.empty() )
442 m_grid->AppendRows( static_cast<int>( m_originalPads.size() ) );
443
444 // Constrain summary label widths so they ellipsize rather than push the layout around
445 // when long pin-number summaries (or duplicate lists) are produced.
446 const int summaryW = m_pin_numbers_summary->GetCharWidth() * 30;
447
448 m_duplicate_pins->SetWindowStyleFlag( m_duplicate_pins->GetWindowStyleFlag() | wxST_ELLIPSIZE_END );
449 m_pin_numbers_summary->SetMaxSize( wxSize( summaryW, -1 ) );
450
451 m_duplicate_pins->SetWindowStyleFlag( m_duplicate_pins->GetWindowStyleFlag() | wxST_ELLIPSIZE_END );
452 m_duplicate_pins->SetMaxSize( wxSize( summaryW, -1 ) );
453
454 wxGridCellAttr* attr = nullptr;
455
456 // Type column editor (attribute)
457 attr = new wxGridCellAttr;
458 wxArrayString typeNames;
459 typeNames.push_back( _( "Through-hole" ) ); // PTH
460 typeNames.push_back( _( "SMD" ) ); // SMD
461 typeNames.push_back( _( "Connector" ) ); // CONN SMD? (use CONN?)
462 typeNames.push_back( _( "NPTH" ) ); // NPTH
463 typeNames.push_back( _( "Non-component" ) ); // NON_COMPONENT
464 typeNames.push_back( _( "Aperture" ) ); // inferred copper-less
465 attr->SetEditor( new GRID_CELL_COMBOBOX( typeNames ) );
466 m_grid->SetColAttr( COL_TYPE, attr );
467
468 attr = new wxGridCellAttr;
469 wxArrayString shapeNames;
477 attr->SetEditor( new GRID_CELL_COMBOBOX( shapeNames ) );
478 m_grid->SetColAttr( COL_SHAPE, attr );
479
480 attr = new wxGridCellAttr;
481 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
482 m_grid->SetColAttr( COL_POS_X, attr );
483
484 attr = new wxGridCellAttr;
485 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
486 m_grid->SetColAttr( COL_POS_Y, attr );
487
488 attr = new wxGridCellAttr;
489 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
490 m_grid->SetColAttr( COL_SIZE_X, attr );
491
492 attr = new wxGridCellAttr;
493 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
494 m_grid->SetColAttr( COL_SIZE_Y, attr );
495
496 // Drill X
497 attr = new wxGridCellAttr;
498 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
499 m_grid->SetColAttr( COL_DRILL_X, attr );
500
501 // Drill Y
502 attr = new wxGridCellAttr;
503 attr->SetEditor( new GRID_CELL_TEXT_EDITOR() );
504 m_grid->SetColAttr( COL_DRILL_Y, attr );
505
506 // Pad->Die Length
507 m_grid->SetAutoEvalColUnits( COL_P2D_LENGTH, m_unitsProvider->GetUnitsFromType( EDA_DATA_TYPE::DISTANCE ) );
508
509 // Pad->Die Delay
510 m_grid->SetAutoEvalColUnits( COL_P2D_DELAY, m_unitsProvider->GetUnitsFromType( EDA_DATA_TYPE::TIME ) );
511
512 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_POS_X );
513 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_POS_Y );
514 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_SIZE_X );
515 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_SIZE_Y );
516 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_DRILL_X );
517 m_grid->SetUnitsProvider( m_unitsProvider.get(), COL_DRILL_Y );
518 m_grid->SetAutoEvalCols( { COL_POS_X, COL_POS_Y,
522 COL_P2D_DELAY } );
523
524 // add Cut, Copy, and Paste to wxGrid
525 GRID_TRICKS* gridTricks = new GRID_TRICKS( m_grid );
526 gridTricks->EnableColumnBulkEdit();
527 m_grid->PushEventHandler( gridTricks );
528 m_grid->SetSelectionMode( wxGrid::wxGridSelectCells );
529
532
534
535 Layout();
537
538 // Cap the initial height so the dialog does not grow off-screen for footprints
539 // with many pads. The grid grows to fill the available space via wxEXPAND.
540 // Use the parent window to find the display since this dialog isn't shown yet.
541 int displayIdx = wxDisplay::GetFromWindow( aParent );
542
543 if( displayIdx == wxNOT_FOUND )
544 displayIdx = 0;
545
546 wxRect displayArea = wxDisplay( (unsigned int) displayIdx ).GetClientArea();
547 wxSize dlgSize = GetSize();
548 int maxH = ( displayArea.height * 4 ) / 5;
549
550 if( dlgSize.y > maxH )
551 {
552 dlgSize.y = maxH;
553 SetSize( dlgSize );
554
555 // Reset minimum height so the user can resize the capped dialog freely.
556 // The minimum width from finishDialogSettings() is still honoured.
557 wxSize minSz = GetMinSize();
558 minSz.y = -1;
559 SetMinSize( minSz );
560
561 Centre();
562 }
563}
564
565
567{
568 // Roll back any session changes unless the dialog was accepted. This means
569 // rollback happens on both title-bar 'X'/Esc and Cancel.
570 if( !m_accepted )
572
573 // destroy GRID_TRICKS before m_grid.
574 m_grid->PopEventHandler( true );
575}
576
577
579{
580 m_grid->SetCellValue( aRowId, COL_NUMBER, aPad->GetNumber() );
581
582 wxString attrStr = GetPadTypeString( *aPad );
583 int size_x = aPad->GetSize( F_Cu ).x;
584 int size_y = aPad->GetSize( F_Cu ).y;
585 wxString padShape = aPad->ShowPadShape( F_Cu );
586
588 [&]( PCB_LAYER_ID aLayer )
589 {
590 if( aPad->GetSize( aLayer ).x != size_x )
591 size_x = -1;
592
593 if( aPad->GetSize( aLayer ).y != size_y )
594 size_y = -1;
595
596 if( aPad->ShowPadShape( aLayer ) != padShape )
597 padShape = INDETERMINATE_STATE;
598 } );
599
600 m_grid->SetCellValue( aRowId, COL_TYPE, attrStr );
601 m_grid->SetCellValue( aRowId, COL_SHAPE, padShape );
602 m_grid->SetCellValue( aRowId, COL_POS_X, m_unitsProvider->StringFromValue( aPad->GetPosition().x, true ) );
603 m_grid->SetCellValue( aRowId, COL_POS_Y, m_unitsProvider->StringFromValue( aPad->GetPosition().y, true ) );
604 m_grid->SetCellValue( aRowId, COL_SIZE_X, size_x >= 0 ? m_unitsProvider->StringFromValue( size_x, true )
606 m_grid->SetCellValue( aRowId, COL_SIZE_Y, size_y >= 0 ? m_unitsProvider->StringFromValue( size_y, true )
608
609 UpdateDrillCells( *m_grid, *m_unitsProvider, aRowId, *aPad, false );
610
611 // Pad to die metrics
612 if( aPad->GetPadToDieLength() )
613 m_grid->SetUnitValue( aRowId, COL_P2D_LENGTH, aPad->GetPadToDieLength() );
614 else
615 m_grid->SetCellValue( aRowId, COL_P2D_LENGTH, wxEmptyString );
616
617 if( aPad->GetPadToDieDelay() )
618 m_grid->SetUnitValue( aRowId, COL_P2D_DELAY, aPad->GetPadToDieDelay() );
619 else
620 m_grid->SetCellValue( aRowId, COL_P2D_DELAY, wxEmptyString );
621
622 setRowNullableEditors( aRowId );
623}
624
625
627{
628 if( !m_footprint )
629 return false;
630
631 int row = 0;
632
633 for( PAD* pad : m_rowPads )
634 {
635 fillGridRow( row, pad );
636 row++;
637 }
638
639 // Auto size the data columns first to get reasonable initial widths
640 m_grid->AutoSizeColumns();
641
642 // Ensure the Shape column (index 1) is wide enough for the longest translated
643 // shape text plus the dropdown arrow / padding. We compute a max text width
644 // using a device context and add a platform neutral padding.
645 {
646 wxClientDC dc( m_grid );
647 dc.SetFont( m_grid->GetFont() );
648
649 wxArrayString shapeNames;
657
658 int maxWidth = 0;
659
660 for( const wxString& str : shapeNames )
661 {
662 int w, h;
663 dc.GetTextExtent( str, &w, &h );
664 maxWidth = std::max( maxWidth, w );
665 }
666
667 // Add padding for internal cell margins + dropdown control.
668 int padding = FromDIP( 30 ); // heuristic: 2*margin + arrow button
669 m_grid->SetColSize( COL_SHAPE, maxWidth + padding );
670 }
671
672 // Record initial proportions for proportional resizing later.
674
675 // Run an initial proportional resize using current client size so columns
676 // respect proportions immediately.
677 wxSizeEvent sizeEvt( GetSize(), GetId() );
678 CallAfter(
679 [this, sizeEvt]
680 {
681 wxSizeEvent evt( sizeEvt );
682 this->OnSize( evt );
683 } );
684
685 // If pads exist, select the first row to show initial highlight
686 if( m_grid->GetNumberRows() > 0 )
687 {
688 m_grid->SetGridCursor( 0, 0 );
689
690 // Construct event with required parameters (id, type, obj, row, col,...)
691 wxGridEvent ev( m_grid->GetId(), wxEVT_GRID_SELECT_CELL, m_grid, 0, 0, -1, -1, true );
692 OnSelectCell( ev );
693 }
694
695 return true;
696}
697
698
700{
701 // Set nullable editors
702 auto setCellEditor =
703 [this, aRowId]( int aCol )
704 {
706 wxGridCellAttr* attr = m_grid->GetOrCreateCellAttr( aRowId, aCol );
707 attr->SetEditor( cellEditor );
708 attr->DecRef();
709 };
710
711 setCellEditor( COL_P2D_LENGTH );
712 setCellEditor( COL_P2D_DELAY );
713}
714
715
717{
718 m_originalPads.clear();
719 m_rowPads.clear();
720 m_removedPads.clear();
721
722 if( !m_footprint )
723 return;
724
725 for( PAD* pad : m_footprint->Pads() )
726 {
727 PAD_SNAPSHOT snap( pad );
728 snap.number = pad->GetNumber();
729 snap.position = pad->GetPosition();
730 snap.padstack = pad->Padstack();
731 snap.attribute = pad->GetAttribute();
732 snap.padToDieLength= pad->GetPadToDieLength();
733 snap.padToDieDelay = pad->GetPadToDieDelay();
734
735 m_originalPads.try_emplace( pad, std::move( snap ) );
736 m_rowPads.push_back( pad );
737 }
738
739 std::sort( m_rowPads.begin(), m_rowPads.end(), PAD_SNAPSHOT_COMPARE() );
740}
741
742
744{
745 aPad.SetNumber( aSnap.number );
746 aPad.SetPosition( aSnap.position );
747 aPad.SetPadstack( aSnap.padstack );
748 aPad.SetAttribute( aSnap.attribute );
749 aPad.SetPadToDieLength( aSnap.padToDieLength );
750 aPad.SetPadToDieDelay( aSnap.padToDieDelay );
751}
752
753
755{
756 if( !m_footprint )
757 return;
758
759 const PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
760 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
761
762 for( PAD* pad : m_footprint->Pads() )
763 {
764 // Clear brighted even if the pad isn't original
765 pad->ClearBrightened();
766
767 if( !m_originalPads.contains( pad ) )
768 continue;
769
771
772 if( canvas )
773 canvas->GetView()->Update( pad, KIGFX::REPAINT );
774 }
775}
776
777
779{
780 if( !m_footprint )
781 return;
782
784
785 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
786 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
787 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
788
789 // Remove pads added during the session; they have no snapshot entry.
790 std::vector<PAD*> livePads( m_footprint->Pads().begin(), m_footprint->Pads().end() );
791
792 for( PAD* pad : livePads )
793 {
794 if( m_originalPads.contains( pad ) )
795 continue;
796
797 m_footprint->Remove( pad );
798
799 if( view )
800 view->Remove( pad );
801
802 delete pad;
803 }
804
805 // Re-add pads removed during the session and restore their original data.
806 // Pads with no snapshot were added and removed again, so just drop them.
807 for( PAD* pad : m_removedPads )
808 {
809 auto snapIt = m_originalPads.find( pad );
810
811 if( snapIt == m_originalPads.end() )
812 {
813 delete pad;
814 continue;
815 }
816
817 restorePadFromSnapshot( *pad, snapIt->second );
818 m_footprint->Add( pad );
819
820 if( view )
821 view->Add( pad );
822 }
823
824 m_removedPads.clear();
825
826 // Rebuild the row mapping so no pointers to freed pads survive for any UI
827 // events still pending while the dialog closes.
828 m_rowPads.clear();
829
830 for( PAD* pad : m_footprint->Pads() )
831 m_rowPads.push_back( pad );
832
833 std::sort( m_rowPads.begin(), m_rowPads.end(), PAD_SNAPSHOT_COMPARE() );
834
835 if( canvas )
836 {
838 canvas->ForceRefresh();
839 }
840
841 m_summaryDirty = true;
842}
843
844
846{
847 switch( aCol )
848 {
849 case COL_NUMBER:
850 aPad.SetNumber( m_grid->GetCellValue( aRowId, aCol ) );
851 break;
852
853 case COL_TYPE:
854 SetPadTypeFromString( aPad, m_grid->GetCellValue( aRowId, aCol ) );
855 break;
856
857 case COL_SHAPE:
858 {
859 const wxString shape = m_grid->GetCellValue( aRowId, aCol );
860
861 if( shape == INDETERMINATE_STATE )
862 break;
863
864 const PAD_SHAPE newShape = ShapeFromString( shape );
865
867 [&]( PCB_LAYER_ID aLayer )
868 {
869 aPad.SetShape( aLayer, newShape );
870 } );
871 break;
872 }
873
874 case COL_POS_X:
875 case COL_POS_Y:
876 {
877 VECTOR2I pos = aPad.GetPosition();
878
879 if( aCol == COL_POS_X )
880 pos.x = m_grid->GetUnitValue( aRowId, aCol );
881 else
882 pos.y = m_grid->GetUnitValue( aRowId, aCol );
883
884 aPad.SetPosition( pos );
885 break;
886 }
887
888 case COL_SIZE_X:
889 case COL_SIZE_Y:
890 {
891 const wxString sizeValue = m_grid->GetCellValue( aRowId, aCol );
892
893 if( sizeValue == INDETERMINATE_STATE )
894 break;
895
896 const int size = m_grid->GetUnitValue( aRowId, aCol );
897
899 [&]( PCB_LAYER_ID aLayer )
900 {
901 VECTOR2I layerSize = aPad.GetSize( aLayer );
902
903 if( aCol == COL_SIZE_X )
904 layerSize.x = size;
905 else
906 layerSize.y = size;
907
908 aPad.SetSize( aLayer, layerSize );
909 } );
910 break;
911 }
912
913 case COL_DRILL_X:
914 case COL_DRILL_Y:
915 {
916 // Drill sizes (only if attribute allows)
917 if( DrillsAreEditable( aPad ) )
918 {
919 int drillX = m_grid->GetUnitValue( aRowId, COL_DRILL_X );
920 int drillY = m_grid->GetUnitValue( aRowId, COL_DRILL_Y );
921
922 if( drillX > 0 || drillY > 0 )
923 {
924 if( drillX <= 0 )
925 drillX = drillY;
926
927 if( drillY <= 0 )
928 drillY = drillX;
929
930 aPad.SetDrillSize( { drillX, drillY } );
931 }
932 }
933
934 break;
935 }
936
937 case COL_P2D_LENGTH:
938 {
939 const wxString lenStr = m_grid->GetCellValue( aRowId, aCol );
940
941 if( lenStr.IsEmpty() )
942 aPad.SetPadToDieLength( 0 );
943 else
944 aPad.SetPadToDieLength( m_grid->GetUnitValue( aRowId, aCol ) );
945
946 break;
947 }
948
949 case COL_P2D_DELAY:
950 {
951 const wxString delayStr = m_grid->GetCellValue( aRowId, aCol );
952
953 if( delayStr.IsEmpty() )
954 aPad.SetPadToDieDelay( 0 );
955 else
956 aPad.SetPadToDieDelay( m_grid->GetUnitValue( aRowId, aCol ) );
957
958 break;
959 }
960
961 default:
962 wxFAIL_MSG( wxT( "Invalid column index" ) );
963 break;
964 }
965}
966
967
969{
970 if( !m_grid->CommitPendingChanges() )
971 return false;
972
973 if( !m_footprint )
974 return true;
975
976 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
977 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
978 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
979
981
982 BOARD_COMMIT commit( m_frame );
983
984 // Pads removed during the session are re-inserted momentarily so their
985 // removal can be staged in this commit (Push performs the actual removal).
986 for( PAD* pad : m_removedPads )
987 {
988 auto snapIt = m_originalPads.find( pad );
989
990 if( snapIt == m_originalPads.end() )
991 {
992 // A pad added and removed again during the session: it never needs
993 // to appear in the commit.
994 delete pad;
995 continue;
996 }
997
998 restorePadFromSnapshot( *pad, snapIt->second );
999 m_footprint->Add( pad );
1000
1001 if( view )
1002 view->Add( pad );
1003
1004 commit.Remove( pad );
1005 }
1006
1007 m_removedPads.clear();
1008
1009 int row = 0;
1010
1011 const auto applyRowDataToPad =
1012 [&]( PAD& aPad, int aRowId )
1013 {
1014 for( int col = 0; col < m_grid->GetNumberCols(); ++col )
1015 setPadFromGridCell( aPad, aRowId, static_cast<COLS>( col ) );
1016 };
1017
1018 for( PAD* pad : m_rowPads )
1019 {
1020 if( m_originalPads.contains( pad ) )
1021 {
1022 // Existing pad: its data was already restored to the dialog-open
1023 // state above, so the commit's undo image is correct.
1024 commit.Modify( pad );
1025 applyRowDataToPad( *pad, row );
1026 }
1027 else
1028 {
1029 // Imported pad: it was added to the footprint at import time for the
1030 // canvas preview. Take it out again so the commit can stage a clean
1031 // addition.
1032 applyRowDataToPad( *pad, row );
1033
1034 m_footprint->Remove( pad );
1035
1036 if( view )
1037 view->Remove( pad );
1038
1039 commit.Add( pad );
1040 }
1041
1042 row++;
1043 }
1044
1045 commit.Push( _( "Edit Pads" ) );
1046 m_frame->Refresh();
1047
1048 m_accepted = true;
1049
1050 return true;
1051}
1052
1053
1055{
1056 m_colProportions.clear();
1057 m_minColWidths.clear();
1058
1059 if( !m_grid )
1060 return;
1061
1062 // Only consider the actual data columns (all of them since row labels are hidden)
1063 int cols = m_grid->GetNumberCols();
1064 int total = 0;
1065 std::vector<int> widths;
1066 widths.reserve( cols );
1067
1068 for( int c = 0; c < cols; ++c )
1069 {
1070 int w = m_grid->GetColSize( c );
1071 widths.push_back( w );
1072 total += w;
1073 }
1074
1075 if( total <= 0 )
1076 return;
1077
1078 for( int w : widths )
1079 {
1080 m_colProportions.push_back( (double) w / (double) total );
1081 m_minColWidths.push_back( w );
1082 }
1083}
1084
1085
1086void DIALOG_FP_EDIT_PAD_TABLE::OnSize( wxSizeEvent& aEvent )
1087{
1088 if( m_colProportions.empty() )
1089 {
1090 aEvent.Skip();
1091 return;
1092 }
1093
1094 // Compute available total width for columns and resize keeping proportions.
1095 int cols = m_grid->GetNumberCols();
1096 int available = 0;
1097
1098 for( int c = 0; c < cols; ++c )
1099 available += m_grid->GetColSize( c );
1100
1101 // Use client size of grid minus scrollbar estimate to better distribute.
1102 int clientW = m_grid->GetClientSize().x;
1103
1104 if( clientW > 0 )
1105 available = clientW; // prefer actual client width
1106
1107 int used = 0;
1108
1109 for( int c = 0; c < cols; ++c )
1110 {
1111 int target = (int) std::round( m_colProportions[c] * available );
1112 target = std::max( target, m_minColWidths[c] );
1113
1114 // Defer last column to absorb rounding diff.
1115 if( c == cols - 1 )
1116 target = std::max( available - used, m_minColWidths[c] );
1117
1118 m_grid->SetColSize( c, target );
1119 used += target;
1120 }
1121
1122 aEvent.Skip();
1123}
1124
1125
1127{
1128 if( m_grid->IsCellEditControlShown() && m_grid->GetGridCursorCol() == COL_NUMBER )
1129 m_summaryDirty = true;
1130
1131 DIALOG_SHIM::OnCharHook( aEvent );
1132}
1133
1134
1136{
1137 int row = aEvent.GetRow();
1138 int col = aEvent.GetCol();
1139
1140 if( !m_footprint )
1141 return;
1142
1143 PAD* target = getPadForRow( row );
1144
1145 if( !target )
1146 return;
1147
1148 const bool drillsWereEditable = DrillsAreEditable( *target );
1149 bool needCanvasRefresh = true;
1150
1151 setPadFromGridCell( *target, row, static_cast<COLS>( col ) );
1152
1153 if( col == COL_TYPE )
1154 {
1155 const bool drillsAreEditable = DrillsAreEditable( *target );
1156
1157 if( drillsAreEditable )
1158 {
1159 // PAD::SetAttribute() removes drills for SMD pads. So if we roundtrip
1160 // from PTH -> SMD -> PTH, the drill size is lost. In that case, restore
1161 // from the "ghost" value in the grid if there is one.
1162 const int drillX = m_grid->GetUnitValue( row, COL_DRILL_X );
1163 const int drillY = m_grid->GetUnitValue( row, COL_DRILL_Y );
1164
1165 if( drillX > 0 || drillY > 0 )
1166 {
1167 setPadFromGridCell( *target, row, COL_DRILL_X );
1168 }
1169 else if( !drillsWereEditable )
1170 {
1171 // Use a default drill size the pad is becoming editable
1172 // And there was no "ghost" value in the grid to restore from.
1173 const int defaultDrill = pcbIUScale.mmToIU( 1.0 );
1174 target->SetDrillSize( { defaultDrill, defaultDrill } );
1175 }
1176 }
1177
1178 UpdateDrillCells( *m_grid, *m_unitsProvider, row, *target, !drillsAreEditable );
1179 }
1180 else if( col == COL_P2D_LENGTH || col == COL_P2D_DELAY )
1181 {
1182 // Pad-to-die values are not drawn on the canvas.
1183 needCanvasRefresh = false;
1184 }
1185
1186 if( col == COL_NUMBER )
1187 m_summaryDirty = true;
1188
1189 // Request redraw (simple approach)
1190 target->SetDirty();
1191
1192 if( needCanvasRefresh )
1193 {
1194 if( PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() ) )
1195 {
1196 if( KIGFX::PCB_VIEW* view = base->GetCanvas()->GetView() )
1197 {
1198 // Some changes, e.g. type change, can change the layers
1199 view->Update( target, KIGFX::REPAINT | KIGFX::LAYERS );
1200 }
1201
1202 // A bulk edit sends one event per row; Refresh() coalesces their repaints
1203 base->GetCanvas()->Refresh();
1204 }
1205 }
1206}
1207
1208
1210{
1211 int row = aEvent.GetRow();
1212
1213 if( !m_footprint )
1214 return;
1215
1216 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
1217 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
1218
1219 // Clear existing pad selections
1220 for( PAD* pad : m_footprint->Pads() )
1221 {
1222 if( pad->IsBrightened() )
1223 {
1224 pad->ClearBrightened();
1225
1226 if( canvas )
1227 canvas->GetView()->Update( pad, KIGFX::REPAINT );
1228 }
1229 }
1230
1231 PAD* pad = getPadForRow( row );
1232
1233 if( !pad )
1234 return;
1235
1236 pad->SetBrightened();
1237
1238 if( canvas )
1239 {
1240 canvas->GetView()->Update( pad, KIGFX::REPAINT );
1241 canvas->ForceRefresh();
1242 }
1243}
1244
1245
1246void DIALOG_FP_EDIT_PAD_TABLE::OnUpdateUI( wxUpdateUIEvent& aEvent )
1247{
1248 if( m_summaryDirty )
1249 {
1250 if( m_grid->IsCellEditControlShown() && m_grid->GetGridCursorCol() == COL_NUMBER )
1251 {
1252 int row = m_grid->GetGridCursorRow();
1253 int col = m_grid->GetGridCursorCol();
1254
1255 PAD* target = getPadForRow( row );
1256
1257 if( !target )
1258 return;
1259
1260 wxGridCellEditor* editor = m_grid->GetCellEditor( row, col );
1261
1262 if( editor )
1263 {
1264 target->SetNumber( editor->GetValue() );
1265 editor->DecRef();
1266 }
1267 }
1268
1269 updateSummary();
1270 m_summaryDirty = false;
1271 }
1272}
1273
1274
1275void DIALOG_FP_EDIT_PAD_TABLE::OnCancel( wxCommandEvent& aEvent )
1276{
1277 // The destructor rolls back everything that was not accepted.
1278 m_accepted = false;
1279 aEvent.Skip();
1280}
1281
1282
1284{
1285 PIN_NUMBERS pinNumbers;
1286
1287 for( PAD* pad : m_footprint->Pads() )
1288 {
1289 if( pad->GetNumber().Length() )
1290 pinNumbers.insert( pad->GetNumber() );
1291 }
1292
1293 const wxString summary = pinNumbers.GetSummary();
1294 const wxString duplicates = pinNumbers.GetDuplicates();
1295
1296 m_pin_numbers_summary->SetLabel( summary );
1297 m_pin_numbers_summary->SetToolTip( summary );
1298 m_pin_count->SetLabel( wxString::Format( wxT( "%u" ), (unsigned) m_footprint->Pads().size() ) );
1299 m_duplicate_pins->SetLabel( duplicates );
1300 m_duplicate_pins->SetToolTip( duplicates );
1301
1302 Layout();
1303}
1304
1305
1307{
1308 if( aRowId < 0 || static_cast<size_t>( aRowId ) >= m_rowPads.size() )
1309 return nullptr;
1310
1311 return m_rowPads[aRowId];
1312}
1313
1314
1316{
1317 bool toFile = aEvent.GetEventObject() == m_btnExportToFile;
1318
1319 wxString filePath;
1320
1321 if( toFile )
1322 {
1323 wxFileName fn( m_footprint->GetFPID().GetLibItemName() );
1324 fn.SetExt( FILEEXT::CsvFileExtension );
1325
1326 wxFileDialog dlg( this, _( "Select pad data file" ), "", fn.GetFullName(), FILEEXT::CsvTsvFileWildcard(),
1327 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
1328
1330
1331 if( dlg.ShowModal() == wxID_CANCEL )
1332 return;
1333
1334 filePath = dlg.GetPath();
1335 }
1336
1337 std::vector<PAD*> padsToExport;
1338 bool complexPadstacks = false;
1339
1340 for( PAD* pad : m_rowPads )
1341 {
1342 if( pad->GetPadstackMode() != PADSTACK::MODE::NORMAL )
1343 {
1344 complexPadstacks = true;
1345 continue;
1346 }
1347
1348 padsToExport.push_back( pad );
1349 }
1350
1351 if( complexPadstacks )
1352 {
1353 if( wxMessageBox( _( "Complex padstacks cannot be exported via CSV.\n\nThey will be skipped." ),
1354 _( "Export Pad Table" ), wxOK | wxCANCEL | wxICON_WARNING, this ) != wxOK )
1355 {
1356 return;
1357 }
1358 }
1359
1360 static const std::vector<COLS> exportCols {
1361 COLS::COL_NUMBER,
1362 COLS::COL_TYPE,
1363 COLS::COL_SHAPE,
1364 COLS::COL_POS_X,
1365 COLS::COL_POS_Y,
1366 COLS::COL_SIZE_X,
1367 COLS::COL_SIZE_Y,
1368 COLS::COL_DRILL_X,
1369 COLS::COL_DRILL_Y,
1370 COLS::COL_P2D_LENGTH,
1371 COLS::COL_P2D_DELAY,
1372 };
1373
1376
1377 std::vector<std::vector<wxString>> table;
1378 table.reserve( padsToExport.size() + 1 );
1379
1380 std::vector<wxString> header;
1381 header.reserve( exportCols.size() );
1382
1383 for( COLS col : exportCols )
1384 header.emplace_back( wxGetTranslation( GetPadTableColLabel( col ) ) );
1385
1386 table.emplace_back( std::move( header ) );
1387
1388 for( PAD* pad : padsToExport )
1389 {
1390 std::vector<wxString>& row = table.emplace_back();
1391 row.reserve( exportCols.size() );
1392
1393 for( COLS col : exportCols )
1394 row.emplace_back( formatter.Format( *pad, col ) );
1395 }
1396
1398}
1399
1400
1402{
1403 bool fromFile = aEvent.GetEventObject() == m_btnImportFromFile;
1404 bool replaceAll = m_rbReplaceExisting->GetValue();
1405
1407
1409
1410 std::optional<std::vector<std::vector<wxString>>> csvData = ReadTableFromFileOrClipboard( *m_frame, fromFile );
1411
1412 // The pad table does not and cannot capture the full glory of a PAD object
1413 // (for example a custom pad's custom shape). So, when we are re-importing
1414 // pads and "replacing existing", we attempt to rematch the imported pads
1415 // to the existing ones by pad number. If a match is found, we update the
1416 // existing pad with the imported data. If no match is found, we add the
1417 // imported pad as a new pad. If "replace existing" is not selected, we
1418 // simply add the imported pads as new pads.
1419
1420 // Group the current pads by case-insensitive pad number so each imported
1421 // row can be rematched with a single map lookup rather than a linear scan.
1422 // Buckets keep the grid order, so this is stable as we'll match in grid order.
1423 std::map<wxString, std::vector<PAD*>> padsByNumber;
1424
1425 for( PAD* pad : m_rowPads )
1426 padsByNumber[pad->GetNumber()].push_back( pad );
1427
1428 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
1429 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
1430 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
1431
1432 std::vector<std::unique_ptr<PAD>> createdPads;
1433
1434 if( csvData && csvData->size() >= 2 )
1435 {
1436 std::vector<COLS> headerCols;
1437 wxArrayString unknownHeaders;
1438 std::optional<size_t> numberColIdx;
1439
1440 for( const wxString& label : ( *csvData )[0] )
1441 {
1442 COLS col = GetColTypeForString( label );
1443
1444 if( col >= COLS::COL_COUNT )
1445 unknownHeaders.push_back( label );
1446
1447 if( col == COLS::COL_NUMBER )
1448 numberColIdx = headerCols.size();
1449
1450 headerCols.push_back( col );
1451 }
1452
1453 if( replaceAll && !numberColIdx.has_value() )
1454 {
1455 wxString msg = _( "Imported pad data must include pad numbers." );
1456 wxMessageBox( msg, _( "Import error" ), wxOK | wxICON_ERROR, this );
1457 return;
1458 }
1459
1460 if( !unknownHeaders.IsEmpty() )
1461 {
1462 wxString msg = wxString::Format( _( "Unknown columns in data: %s. These columns will be ignored." ),
1463 AccumulateDescriptions( unknownHeaders ) );
1464 reporter.Report( msg, RPT_SEVERITY_WARNING );
1465 }
1466
1467 if( reporter.HasMessage() )
1468 {
1469 int ret = wxMessageBox( reporter.GetMessages(), _( "Errors" ), wxOK | wxCANCEL | wxICON_ERROR, this );
1470
1471 if( ret == wxCANCEL )
1472 return;
1473 }
1474
1475 for( size_t i = 1; i < csvData->size(); ++i )
1476 {
1477 const std::vector<wxString>& cols = ( *csvData )[i];
1478
1479 // Decide whether to create a new pad or update an existing one based on the
1480 // "replace existing" option and the pad number.
1481 std::unique_ptr<PAD> newPad;
1482 PAD* padToUpdate = nullptr;
1483
1484 const size_t numberCol = numberColIdx.value_or( cols.size() );
1485
1486 if( replaceAll && numberCol < cols.size() && !cols[numberCol].IsEmpty() )
1487 {
1488 auto bucketIt = padsByNumber.find( cols[numberCol] );
1489
1490 if( bucketIt != padsByNumber.end() && !bucketIt->second.empty() )
1491 {
1492 padToUpdate = bucketIt->second.front();
1493 bucketIt->second.erase( bucketIt->second.begin() );
1494 }
1495 }
1496
1497 if( !padToUpdate )
1498 {
1499 // We are adding a new pad, not updating an existing one.
1500 newPad = std::make_unique<PAD>( m_footprint );
1501 padToUpdate = newPad.get();
1502 }
1503
1504 size_t maxCol = std::min( headerCols.size(), cols.size() );
1505
1506 for( size_t j = 0; j < maxCol; ++j )
1507 {
1508 if( headerCols[j] == COLS::COL_COUNT )
1509 continue;
1510
1511 fmt.UpdatePad( *padToUpdate, cols[j], headerCols[j] );
1512 }
1513
1514 // Invalidate the pad's cached draw data and mark it for re-render
1515 // so the canvas refresh below shows the imported values.
1516 padToUpdate->SetDirty();
1517
1518 // Type changes can change the pad's layers
1519 if( view )
1520 view->Update( padToUpdate, KIGFX::REPAINT | KIGFX::LAYERS );
1521
1522 if( newPad )
1523 createdPads.push_back( std::move( newPad ) );
1524 }
1525
1526 // Bulk-load the imported pads into the view.
1527 if( !createdPads.empty() )
1528 {
1529 if( view )
1530 {
1531 std::vector<KIGFX::VIEW_ITEM*> viewPads;
1532
1533 for( const auto& pad : createdPads )
1534 viewPads.push_back( pad.get() );
1535
1536 view->AddBatch( viewPads );
1537 }
1538
1539 for( std::unique_ptr<PAD>& pad : createdPads )
1540 m_footprint->Add( pad.release(), ADD_MODE::BULK_APPEND, true );
1541 }
1542
1543 // Any pads not matched to an imported pad are removed from the footprint.
1544 // They are kept alive so they can be restored on cancel and staged as
1545 // removals when the dialog is accepted.
1546 if( replaceAll )
1547 {
1548 for( const auto& [number, pads] : padsByNumber )
1549 {
1550 for( PAD* pad : pads )
1551 {
1552 m_footprint->Remove( pad );
1553
1554 if( view )
1555 view->Remove( pad );
1556
1557 m_removedPads.push_back( pad );
1558 }
1559 }
1560 }
1561 }
1562
1563 // Commit any in-progress cell edits so the grid rebuild below does not
1564 // discard them.
1565 m_grid->CommitPendingChanges();
1566
1567 // Rebuild the row mapping from the pads now in the footprint.
1568 m_rowPads.clear();
1569
1570 for( PAD* pad : m_footprint->Pads() )
1571 m_rowPads.push_back( pad );
1572
1573 std::sort( m_rowPads.begin(), m_rowPads.end(), PAD_SNAPSHOT_COMPARE() );
1574
1575 // Rebuild the grid to match the new pad count.
1576 int currentRows = m_grid->GetNumberRows();
1577 int neededRows = static_cast<int>( m_rowPads.size() );
1578
1579 if( neededRows > currentRows )
1580 m_grid->AppendRows( neededRows - currentRows );
1581 else if( neededRows < currentRows )
1582 m_grid->DeleteRows( neededRows, currentRows - neededRows );
1583
1585 updateSummary();
1586
1587 if( canvas )
1588 canvas->ForceRefresh();
1589}
1590
1591
1592void DIALOG_FP_EDIT_PAD_TABLE::OnAddRow( wxCommandEvent& aEvent )
1593{
1594 if( !m_footprint )
1595 return;
1596
1597 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
1598 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
1599 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
1600
1601 m_grid->OnAddRow(
1602 [&]() -> std::pair<int, int>
1603 {
1604 PAD* newPad = nullptr;
1605
1606 // Copy the settings of the last pad onto the new pad and offset its position
1607 // by the current grid so the copy is easy to find.
1608 if( !m_rowPads.empty() )
1609 {
1610 newPad = static_cast<PAD*>( m_rowPads.back()->Duplicate( false, nullptr ) );
1611 newPad->Move( VECTOR2I( 0, KiROUND( canvas->GetGAL()->GetGridSize().y ) ) );
1612 }
1613 else
1614 {
1615 newPad = new PAD( m_footprint );
1616 }
1617
1618 if( view )
1619 view->Add( newPad );
1620
1621 m_rowPads.push_back( newPad );
1622 m_footprint->Add( newPad );
1623
1624 int row = m_grid->GetNumberRows();
1625 m_grid->AppendRows( 1 );
1626 fillGridRow( row, newPad );
1627 updateSummary();
1628
1629 if( canvas )
1630 canvas->ForceRefresh();
1631
1632 return { row, COL_NUMBER };
1633 } );
1634}
1635
1636
1637void DIALOG_FP_EDIT_PAD_TABLE::OnDeleteRow( wxCommandEvent& aEvent )
1638{
1639 if( !m_footprint )
1640 return;
1641
1642 PCB_BASE_FRAME* base = dynamic_cast<PCB_BASE_FRAME*>( GetParent() );
1643 PCB_DRAW_PANEL_GAL* canvas = base ? base->GetCanvas() : nullptr;
1644 KIGFX::PCB_VIEW* view = canvas ? canvas->GetView() : nullptr;
1645
1646 m_grid->OnDeleteRows(
1647 [&]( int row )
1648 {
1649 if( row < 0 || static_cast<size_t>( row ) >= m_rowPads.size() )
1650 return;
1651
1652 PAD* pad = m_rowPads[row];
1653
1654 pad->ClearBrightened();
1655 m_footprint->Remove( pad );
1656
1657 if( view )
1658 view->Remove( pad );
1659
1660 // Keep the pad alive: on OK it is staged as a removal, on cancel
1661 // it is re-added to the footprint.
1662 m_removedPads.push_back( pad );
1663
1664 m_rowPads.erase( m_rowPads.begin() + row );
1665 m_grid->DeleteRows( row, 1 );
1666 } );
1667
1668 updateSummary();
1669
1670 if( canvas )
1671 canvas->ForceRefresh();
1672}
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
COLUMN_FORMATTER(UNITS_PROVIDER &aUnitsProvider, bool aIncludeUnits, BOOL_FORMAT aBoolFormat, REPORTER &aReporter)
UNITS_PROVIDER & m_unitsProvider
COMMIT & Remove(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Remove a new item from the model.
Definition commit.h:86
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:102
COMMIT & Add(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr)
Add a new item to the model.
Definition commit.h:74
DIALOG_FP_EDIT_PAD_TABLE_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Pad Table"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
void setPadFromGridCell(PAD &aPad, int aRowId, COLS aCol)
void OnCellChanged(wxGridEvent &aEvent) override
DIALOG_FP_EDIT_PAD_TABLE(PCB_BASE_FRAME *aParent, FOOTPRINT *aFootprint)
void OnCharHook(wxKeyEvent &aEvent) override
void fillGridRow(int aRowId, PAD *aPad)
void OnSelectCell(wxGridEvent &aEvent) override
void setRowNullableEditors(int aRowId) const
void OnImportButtonClick(wxCommandEvent &aEvent) override
bool m_accepted
Set when the changes are committed on OK.
void OnExportButtonClick(wxCommandEvent &aEvent) override
std::map< PAD *, PAD_SNAPSHOT > m_originalPads
void OnDeleteRow(wxCommandEvent &aEvent) override
void OnSize(wxSizeEvent &aEvent) override
void restorePadFromSnapshot(PAD &aPad, const PAD_SNAPSHOT &aSnap) const
void OnCancel(wxCommandEvent &aEvent) override
std::vector< double > m_colProportions
std::unique_ptr< UNITS_PROVIDER > m_unitsProvider
void OnUpdateUI(wxUpdateUIEvent &aEvent) override
void OnAddRow(wxCommandEvent &aEvent) override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
virtual void OnCharHook(wxKeyEvent &aEvt)
EDA_UNITS GetUserUnits() const
void ForceRefresh()
Force a redraw.
KIGFX::GAL * GetGAL() const
Return a pointer to the GAL instance used in the panel.
This class works around a bug in wxGrid where the first keystroke doesn't get sent through the valida...
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:61
void EnableColumnBulkEdit(bool aEnable=true)
Apply an edit to every selected cell in the edited cell's column.
Definition grid_tricks.h:72
const VECTOR2D & GetGridSize() const
Return the grid size.
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const override
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition pcb_view.cpp:87
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1) override
Add a VIEW_ITEM to the view.
Definition pcb_view.cpp:53
virtual void Remove(VIEW_ITEM *aItem) override
Remove a VIEW_ITEM from the view.
Definition pcb_view.cpp:70
void AddBatch(const std::vector< VIEW_ITEM * > &aItems)
Add a batch of items to the view, using bulk-loaded R-trees for initial population.
Definition view.cpp:414
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:729
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
static const LSET & FrontMask()
Return a mask holding all technical layers and the external CU layer on front side.
Definition lset.cpp:718
static const LSET & AllCuMask()
return AllCuMask( MAX_CU_LAYERS );
Definition lset.cpp:604
static const LSET & BackMask()
Return a mask holding all technical layers and the external CU layer on back side.
Definition lset.cpp:725
LSEQ CuStack() const
Return a sequence of copper layers in starting from the front/top and extending to the back/bottom.
Definition lset.cpp:259
LSET & FlipStandardLayers(int aCopperLayersCount=0)
Flip the layers in this set.
Definition lset.cpp:481
A singleton reporter that reports to nowhere.
Definition reporter.h:267
void ForEachUniqueLayer(const std::function< void(PCB_LAYER_ID)> &aMethod) const
Runs the given callable for each active unique copper layer in this padstack, meaning F_Cu for MODE::...
@ NORMAL
Shape is the same on all layers.
Definition padstack.h:175
static constexpr PCB_LAYER_ID ALL_LAYERS
! The layer identifier to use for the single defintion on normal padstacks
Definition padstack.h:184
Class that handles conversion of various pad data fields into strings for display in the UI or serial...
void UpdatePad(PAD &aPad, const wxString &aValue, int aFieldId) const
Update the pad from the given col/string.
PAD_INFO_FORMATTER(UNITS_PROVIDER &aUnitsProvider, bool aIncludeUnits, BOOL_FORMAT aBoolFormat, REPORTER &aReporter)
wxString Format(const PAD &aPin, int aFieldId) const
Definition pad.h:61
bool IsAperturePad() const
Definition pad.h:589
void SetAttribute(PAD_ATTRIB aAttribute)
Definition pad.cpp:1563
static wxString ShowPadShape(PAD_SHAPE aShape)
Definition pad.cpp:2524
LSET GetLayerSet() const override
Return a std::bitset of all layers on which the item physically resides.
Definition pad.h:579
PAD_ATTRIB GetAttribute() const
Definition pad.h:582
const wxString & GetNumber() const
Definition pad.h:143
void SetShape(PCB_LAYER_ID aLayer, PAD_SHAPE aShape)
Set the new shape of this pad.
Definition pad.h:196
VECTOR2I GetCenter() const override
This defaults to the center of the bounding box if not overridden.
Definition pad.h:329
VECTOR2I GetPosition() const override
Definition pad.cpp:255
void Move(const VECTOR2I &aMoveVector) override
Move this object.
Definition pad.h:1023
void SetDirty()
Definition pad.h:571
VECTOR2I GetDrillSize() const
Definition pad.h:320
void SetPadToDieDelay(int aDelay)
Definition pad.h:602
PAD_SHAPE GetShape(PCB_LAYER_ID aLayer) const
Definition pad.h:205
void SetNumber(const wxString &aNumber)
Set the pad number (note that it can be alphanumeric, such as the array reference "AA12").
Definition pad.h:142
VECTOR2I GetSize(PCB_LAYER_ID aLayer) const
Definition pad.cpp:297
int GetPadToDieDelay() const
Definition pad.h:603
void SetPadstack(const PADSTACK &aPadstack)
Definition pad.h:333
void SetPosition(const VECTOR2I &aPos) override
Definition pad.cpp:244
const PADSTACK & Padstack() const
Definition pad.h:331
void SetDrillSize(const VECTOR2I &aSize)
Definition pad.h:319
void SetSize(PCB_LAYER_ID aLayer, const VECTOR2I &aSize)
Definition pad.cpp:264
static LSET SMDMask()
layer set for a SMD pad on Front layer
Definition pad.cpp:627
void SetLayerSet(const LSET &aLayers) override
Definition pad.cpp:1930
void SetPadToDieLength(int aLength)
Definition pad.h:599
int GetPadToDieLength() const
Definition pad.h:600
Base PCB main window class for Pcbnew, Gerbview, and CvPcb footprint viewer.
PCB_DRAW_PANEL_GAL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
virtual KIGFX::PCB_VIEW * GetView() const override
Return a pointer to the KIGFX::VIEW instance used in the panel.
wxString GetDuplicates() const
Gets a formatted string of all the pins that have duplicate numbers.
void insert(value_type const &v)
Definition pin_numbers.h:58
wxString GetSummary() const
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
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.
A wrapper for reporting to a wxString object.
Definition reporter.h:242
bool MatchTranslationOrNative(const wxString &aStr, const wxString &aNativeLabel, bool aCaseSensitive)
Return true if the given string matches either the translated or native version of the given label.
static void UpdateDrillCells(WX_GRID &aGrid, UNITS_PROVIDER &aUnitsProvider, int aRowId, const PAD &aPad, bool aPreserveValues)
Update the drill size cells in the pad table for a given pad.
static COLS GetColTypeForString(const wxString &aStr)
static wxString GetPadTableColLabel(COLS aCol)
Get the label for a given column in the pin table.
static wxString GetPadTypeString(const PAD &aPad)
static PAD_SHAPE ShapeFromString(const wxString &shape)
static bool DrillsAreEditable(const PAD &aPad)
static wxString ShapeToString(PAD_SHAPE shape)
static void SetPadTypeFromString(PAD &aPad, const wxString &aType)
DIALOG_FP_EDIT_PAD_TABLE::COLS COLS
static COL_ORDER GetColTypeForString(const wxString &aStr)
#define _(s)
static std::map< int, wxString > shapeNames
static const std::string CsvFileExtension
static wxString CsvTsvFileWildcard()
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
@ F_Paste
Definition layer_ids.h:100
@ F_Cu
Definition layer_ids.h:60
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:54
@ LAYERS
Layers have changed.
Definition view_item.h:52
@ TARGET_OVERLAY
Items that may change while the view stays the same (noncached)
Definition definitions.h:35
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:741
wxString GetPadAttributeString(PAD_ATTRIB aAttrib)
Get the human-readable string for a pad attribute.
Definition pad_utils.cpp:68
STL namespace.
PAD_ATTRIB
The set of pad shapes, used with PAD::{Set,Get}Attribute().
Definition padstack.h:98
@ NPTH
like PAD_PTH, but not plated mechanical use only, no connection allowed
Definition padstack.h:104
@ NON_COMPONENT
Pad with no hole that does not connect to a physical component, but rather provides a connection poin...
Definition padstack.h:106
@ SMD
Smd pad, appears on the solder paste layer (default)
Definition padstack.h:100
@ PTH
Plated through hole pad.
Definition padstack.h:99
@ CONN
Like smd, does not appear on the solder paste layer (default) Note: also has a special attribute in G...
Definition padstack.h:101
PAD_SHAPE
The set of pad shapes, used with PAD::{Set,Get}Shape()
Definition padstack.h:53
@ CHAMFERED_RECT
Definition padstack.h:61
@ ROUNDRECT
Definition padstack.h:58
@ TRAPEZOID
Definition padstack.h:57
@ RECTANGLE
Definition padstack.h:55
#define _HKI(x)
Definition page_info.cpp:40
@ RPT_SEVERITY_WARNING
void AccumulateDescriptions(wxString &aDesc, const T &aItemCollection)
Build a comma-separated list from a collection of wxStrings.
void WriteTableToFileOrClipboard(const wxString &aToFile, const std::vector< std::vector< wxString > > &aTable)
Write aTable as CSV to aToFile (if non-empty) or to the system clipboard.
Definition table_io.cpp:66
std::optional< std::vector< std::vector< wxString > > > ReadTableFromFileOrClipboard(EDA_BASE_FRAME &aFrame, bool aFromFile)
Read a CSV/TSV table from a file selected via a dialog (aFromFile) or from the system clipboard.
Definition table_io.cpp:34
const std::string table
DATABASE_CONNECTION dc(odbcBackend(cs))
IbisParser parser & reporter
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition ui_common.h:48
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
Definition of file extensions used in Kicad.