KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_lib_edit_pin_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/filedlg.h>
23#include <wx/wfstream.h>
24#include <wx/msgdlg.h>
25#include <wx/tokenzr.h>
26
27#include <column_formatter.h>
28#include <grid_tricks.h>
29#include <richio.h>
30#include <sch_pin.h>
31#include <pin_numbers.h>
32#include <reporter.h>
33#include "pgm_base.h"
34#include <base_units.h>
35#include <bitmaps.h>
36#include <clipboard.h>
37#include <confirm.h>
38#include <symbol_edit_frame.h>
40#include <io/csv.h>
41#include <kiplatform/ui.h>
44#include <widgets/wx_grid.h>
49#include <tool/action_menu.h>
50#include <tool/tool_manager.h>
51#include <table_io.h>
53#include <string_utils.h>
54
55
61static wxString GetPinTableColLabel( int aCol )
62{
63 switch( aCol )
64 {
65 case COL_PIN_COUNT: return _HKI( "Count" );
66 case COL_NUMBER: return _HKI( "Number" );
67 case COL_NAME: return _HKI( "Name" );
68 case COL_TYPE: return _HKI( "Electrical Type" );
69 case COL_SHAPE: return _HKI( "Graphic Style" );
70 case COL_ORIENTATION: return _HKI( "Orientation" );
71 case COL_NUMBER_SIZE: return _HKI( "Number Text Size" );
72 case COL_NAME_SIZE: return _HKI( "Name Text Size" );
73 case COL_LENGTH: return _HKI( "Length" );
74 case COL_POSX: return _HKI( "X Position" );
75 case COL_POSY: return _HKI( "Y Position" );
76 case COL_VISIBLE: return _HKI( "Visible" );
77 case COL_UNIT: return _HKI( "Unit" );
78 case COL_BODY_STYLE: return _HKI( "Body Style" );
79 default: wxFAIL; return wxEmptyString;
80 }
81}
82
83
84static COL_ORDER GetColTypeForString( const wxString& aStr )
85{
86 for( int i = 0; i < COL_COUNT; i++ )
87 {
88 if( MatchTranslationOrNative( aStr, GetPinTableColLabel( i ), false ) )
89 return (COL_ORDER) i;
90 }
91 return COL_COUNT;
92}
93
99{
100public:
101 PIN_INFO_FORMATTER( UNITS_PROVIDER& aUnitsProvider, bool aIncludeUnits, BOOL_FORMAT aBoolFormat,
102 REPORTER& aReporter ) :
103 COLUMN_FORMATTER( aUnitsProvider, aIncludeUnits, aBoolFormat, aReporter )
104 {
105 }
106
107 wxString Format( const SCH_PIN& aPin, int aFieldId ) const
108 {
109 switch( aFieldId )
110 {
111 case COL_NAME:
112 return aPin.GetName();
113
114 case COL_NUMBER:
115 return aPin.GetNumber();
116
117 case COL_TYPE:
118 return PinTypeNames()[static_cast<int>( aPin.GetType() )];
119
120 case COL_SHAPE:
121 return PinShapeNames()[static_cast<int>( aPin.GetShape() )];
122
123 case COL_ORIENTATION:
124 {
125 const int index = PinOrientationIndex( aPin.GetOrientation() );
126
127 if( index >= 0)
128 return PinOrientationNames()[ index ];
129
130 return wxEmptyString;
131 }
132
133 case COL_NUMBER_SIZE:
134 return m_unitsProvider.StringFromValue( aPin.GetNumberTextSize(), m_includeUnits );
135
136 case COL_NAME_SIZE:
137 return m_unitsProvider.StringFromValue( aPin.GetNameTextSize(), m_includeUnits );
138
139 case COL_LENGTH:
140 return m_unitsProvider.StringFromValue( aPin.GetLength(), m_includeUnits );
141
142 case COL_POSX:
143 return m_unitsProvider.StringFromValue( aPin.GetPosition().x, m_includeUnits );
144
145 case COL_POSY:
146 return m_unitsProvider.StringFromValue( aPin.GetPosition().y, m_includeUnits );
147
148 case COL_VISIBLE:
149 return stringFromBool( aPin.IsVisible() );
150
151 case COL_UNIT:
152 if( const SYMBOL* parent = aPin.GetParentSymbol() )
153 {
154 if( !parent->IsMultiUnit() )
155 return wxGetTranslation( UNITS_ALL );
156 }
157
158 if( aPin.GetUnit() == ALL_UNITS )
159 return wxGetTranslation( UNITS_ALL );
160 else
161 return aPin.GetUnitDisplayName( aPin.GetUnit(), true );
162
163 case COL_BODY_STYLE:
164 if( const SYMBOL* parent = aPin.GetParentSymbol() )
165 {
166 if( !parent->IsMultiBodyStyle() )
167 return wxGetTranslation( UNITS_ALL );
168 }
169
170 if( aPin.GetBodyStyle() == ALL_BODY_STYLES )
171 return wxGetTranslation( DEMORGAN_ALL );
172 else
173 return aPin.GetBodyStyleDescription( aPin.GetBodyStyle(), true );
174
175 default:
176 wxFAIL_MSG( wxString::Format( "Invalid field id %d", aFieldId ) );
177 return wxEmptyString;
178 }
179 }
180
187 void UpdatePin( SCH_PIN& aPin, const wxString& aValue, int aFieldId, const LIB_SYMBOL& aSymbol ) const
188 {
189 switch( aFieldId )
190 {
191 case COL_NUMBER:
192 aPin.SetNumber( aValue );
193 break;
194
195 case COL_NAME:
196 aPin.SetName( aValue );
197 break;
198
199 case COL_TYPE:
200 if( PinTypeNames().Index( aValue, false ) != wxNOT_FOUND )
201 aPin.SetType( (ELECTRICAL_PINTYPE) PinTypeNames().Index( aValue, false ) );
202
203 break;
204
205 case COL_SHAPE:
206 if( PinShapeNames().Index( aValue, false ) != wxNOT_FOUND )
207 aPin.SetShape( (GRAPHIC_PINSHAPE) PinShapeNames().Index( aValue, false ) );
208
209 break;
210
211 case COL_ORIENTATION:
212 if( PinOrientationNames().Index( aValue, false ) != wxNOT_FOUND )
213 aPin.SetOrientation( (PIN_ORIENTATION) PinOrientationNames().Index( aValue, false ) );
214
215 break;
216
217 case COL_NUMBER_SIZE:
218 aPin.SetNumberTextSize( m_unitsProvider.ValueFromString( aValue ) );
219 break;
220
221 case COL_NAME_SIZE:
222 aPin.SetNameTextSize( m_unitsProvider.ValueFromString( aValue ) );
223 break;
224
225 case COL_LENGTH:
226 aPin.ChangeLength( m_unitsProvider.ValueFromString( aValue ) );
227 break;
228
229 case COL_POSX:
230 aPin.SetPosition( VECTOR2I( m_unitsProvider.ValueFromString( aValue ), aPin.GetPosition().y ) );
231 break;
232
233 case COL_POSY:
234 aPin.SetPosition( VECTOR2I( aPin.GetPosition().x, m_unitsProvider.ValueFromString( aValue ) ) );
235 break;
236
237 case COL_VISIBLE:
238 aPin.SetVisible(boolFromString( aValue, m_reporter ) );
239 break;
240
241 case COL_UNIT:
242 if( const SYMBOL* parent = aPin.GetParentSymbol() )
243 {
244 if( !parent->IsMultiUnit() )
245 break;
246 }
247
248 if( MatchTranslationOrNative( aValue, UNITS_ALL, false ) )
249 {
250 aPin.SetUnit( 0 );
251 break;
252 }
253
254 for( int i = 1; i <= aSymbol.GetUnitCount(); i++ )
255 {
256 if( aValue == aPin.GetUnitDisplayName( i, true )
257 || aValue == aPin.GetUnitDisplayName( i, false ) )
258 {
259 aPin.SetUnit( i );
260 break;
261 }
262 }
263
264 break;
265
266 case COL_BODY_STYLE:
267 if( const SYMBOL* parent = aPin.GetParentSymbol() )
268 {
269 if( !parent->IsMultiBodyStyle() )
270 break;
271 }
272
273 if( MatchTranslationOrNative( aValue, DEMORGAN_ALL, false ) )
274 {
275 aPin.SetBodyStyle( 0 );
276 break;
277 }
278
279 for( int i = 1; i <= aSymbol.GetBodyStyleCount(); i++ )
280 {
281 if( aValue == aPin.GetBodyStyleDescription( i, true )
282 || aValue == aPin.GetBodyStyleDescription( i, false ) )
283 {
284 aPin.SetBodyStyle( i );
285 break;
286 }
287 }
288
289 break;
290
291 default:
292 wxFAIL_MSG( wxString::Format( "Invalid field id %d", aFieldId ) );
293 break;
294 }
295 }
296};
297
298
300{
301public:
303 const std::vector<SCH_PIN*>& aOrigSelectedPins ) :
304 m_frame( aFrame ),
307 m_filterBySelection( false ),
308 m_edited( false ),
309 m_pinTable( aPinTable ),
310 m_symbol( aSymbol ),
311 m_origSelectedPins( aOrigSelectedPins )
312 {
313 m_eval = std::make_unique<NUMERIC_EVALUATOR>( m_frame->GetUserUnits() );
314
315 m_frame->Bind( EDA_EVT_UNITS_CHANGED, &PIN_TABLE_DATA_MODEL::onUnitsChanged, this );
316 }
317
319 {
320 m_frame->Unbind( EDA_EVT_UNITS_CHANGED, &PIN_TABLE_DATA_MODEL::onUnitsChanged, this );
321 }
322
323 void onUnitsChanged( wxCommandEvent& aEvent )
324 {
325 if( GetView() )
326 GetView()->ForceRefresh();
327
328 aEvent.Skip();
329 }
330
331 void SetUnitFilter( int aFilter ) { m_unitFilter = aFilter; }
332 void SetBodyStyleFilter( int aFilter ) { m_bodyStyleFilter = aFilter; }
333 void SetFilterBySelection( bool aFilter ) { m_filterBySelection = aFilter; }
334
335 int GetNumberRows() override { return (int) m_rows.size(); }
336 int GetNumberCols() override { return COL_COUNT; }
337
338 wxString GetColLabelValue( int aCol ) override
339 {
340 return wxGetTranslation( GetPinTableColLabel( aCol ) );
341 }
342
343 bool IsEmptyCell( int row, int col ) override
344 {
345 return false; // don't allow adjacent cell overflow, even if we are actually empty
346 }
347
348 wxString GetValue( int aRow, int aCol ) override
349 {
350 wxGrid* grid = GetView();
351
352 if( grid->GetGridCursorRow() == aRow && grid->GetGridCursorCol() == aCol && grid->IsCellEditControlShown() )
353 {
354 auto it = m_evalOriginal.find( { m_rows[ aRow ], aCol } );
355
356 if( it != m_evalOriginal.end() )
357 return it->second;
358 }
359
360 return GetValue( m_rows[ aRow ], aCol, m_frame );
361 }
362
363 static wxString GetValue( const std::vector<SCH_PIN*>& pins, int aCol, EDA_DRAW_FRAME* aParentFrame )
364 {
365 wxString fieldValue;
366
367 if( pins.empty() )
368 return fieldValue;
369
372
373 for( const SCH_PIN* pin : pins )
374 {
375 wxString val;
376 switch( aCol )
377 {
378 case COL_PIN_COUNT:
379 val << pins.size();
380 break;
381 default:
382 val << formatter.Format( *pin, aCol );
383 break;
384 }
385
386 if( aCol == COL_NUMBER )
387 {
388 if( fieldValue.length() )
389 fieldValue += wxT( ", " );
390
391 fieldValue += val;
392 }
393 else
394 {
395 if( !fieldValue.Length() )
396 fieldValue = val;
397 else if( val != fieldValue )
398 fieldValue = INDETERMINATE_STATE;
399 }
400 }
401
402 return fieldValue;
403 }
404
405 void SetValue( int aRow, int aCol, const wxString &aValue ) override
406 {
407 if( aValue == INDETERMINATE_STATE )
408 return;
409
410 wxString value = aValue;
411
412 switch( aCol )
413 {
414 case COL_NUMBER_SIZE:
415 case COL_NAME_SIZE:
416 case COL_LENGTH:
417 case COL_POSX:
418 case COL_POSY:
419 m_eval->SetDefaultUnits( m_frame->GetUserUnits() );
420
421 if( m_eval->Process( value ) )
422 {
423 m_evalOriginal[ { m_rows[ aRow ], aCol } ] = value;
424 value = m_eval->Result();
425 }
426
427 break;
428
429 default:
430 break;
431 }
432
433 std::vector<SCH_PIN*> pins = m_rows[ aRow ];
434
435 // If the NUMBER column is edited and the pins are grouped, renumber, and add or
436 // remove pins based on the comma separated list of pins.
437 if( aCol == COL_NUMBER && m_pinTable->IsDisplayGrouped() )
438 {
439 wxStringTokenizer tokenizer( value, "," );
440 size_t i = 0;
441
442 while( tokenizer.HasMoreTokens() )
443 {
444 wxString pinName = tokenizer.GetNextToken();
445
446 // Trim whitespace from both ends of the string
447 pinName.Trim( true ).Trim( false );
448
449 if( i < pins.size() )
450 {
451 // Renumber the existing pins
452 pins.at( i )->SetNumber( pinName );
453 }
454 else
455 {
456 // Create new pins
457 SCH_PIN* newPin = new SCH_PIN( this->m_symbol );
458 SCH_PIN* last = pins.back();
459
460 newPin->SetNumber( pinName );
461 newPin->SetName( last->GetName() );
462 newPin->SetOrientation( last->GetOrientation() );
463 newPin->SetType( last->GetType() );
464 newPin->SetShape( last->GetShape() );
465 newPin->SetUnit( last->GetUnit() );
466
467 VECTOR2I pos = last->GetPosition();
468
470 {
473 {
474 pos.y -= schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
475 }
476 else
477 {
478 pos.x += schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
479 }
480 }
481
482 newPin->SetPosition( pos );
483
484 pins.push_back( newPin );
485 m_pinTable->AddPin( newPin );
486 }
487
488 i++;
489 }
490
491 while( pins.size() > i )
492 {
493 m_pinTable->RemovePin( pins.back() );
494 pins.pop_back();
495 }
496
497 m_rows[aRow] = pins;
498 m_edited = true;
499
500 return;
501 }
502
505
506 for( SCH_PIN* pin : pins )
507 formatter.UpdatePin( *pin, value, aCol, *m_symbol );
508
509 m_edited = true;
510 }
511
512 static int findRow( const std::vector<std::vector<SCH_PIN*>>& aRowSet, const wxString& aName )
513 {
514 for( size_t i = 0; i < aRowSet.size(); ++i )
515 {
516 if( aRowSet[ i ][ 0 ] && aRowSet[ i ][ 0 ]->GetName() == aName )
517 return i;
518 }
519
520 return -1;
521 }
522
523 static bool compare( const std::vector<SCH_PIN*>& lhs, const std::vector<SCH_PIN*>& rhs,
524 int sortCol, bool ascending, EDA_DRAW_FRAME* parentFrame )
525 {
526 wxString lhStr = GetValue( lhs, sortCol, parentFrame );
527 wxString rhStr = GetValue( rhs, sortCol, parentFrame );
528
529 if( lhStr == rhStr )
530 {
531 // Secondary sort key is always COL_NUMBER
532 sortCol = COL_NUMBER;
533 lhStr = GetValue( lhs, sortCol, parentFrame );
534 rhStr = GetValue( rhs, sortCol, parentFrame );
535 }
536
537 bool res;
538
539 // N.B. To meet the iterator sort conditions, we cannot simply invert the truth
540 // to get the opposite sort. i.e. ~(a<b) != (a>b)
541 auto cmp =
542 [ ascending ]( const auto a, const auto b )
543 {
544 if( ascending )
545 return a < b;
546 else
547 return b < a;
548 };
549
550 switch( sortCol )
551 {
552 case COL_NUMBER:
553 case COL_NAME:
554 res = cmp( PIN_NUMBERS::Compare( lhStr, rhStr ), 0 );
555 break;
556
557 case COL_NUMBER_SIZE:
558 case COL_NAME_SIZE:
559 res = cmp( parentFrame->ValueFromString( lhStr ), parentFrame->ValueFromString( rhStr ) );
560 break;
561
562 case COL_LENGTH:
563 case COL_POSX:
564 case COL_POSY:
565 res = cmp( parentFrame->ValueFromString( lhStr ), parentFrame->ValueFromString( rhStr ) );
566 break;
567
568 case COL_VISIBLE:
569 case COL_UNIT:
570 case COL_BODY_STYLE:
571 default:
572 res = cmp( StrNumCmp( lhStr, rhStr ), 0 );
573 break;
574 }
575
576 return res;
577 }
578
579 void RebuildRows( const std::vector<SCH_PIN*>& aPins, bool groupByName, bool groupBySelection )
580 {
581 WX_GRID* grid = dynamic_cast<WX_GRID*>( GetView() );
582 std::vector<SCH_PIN*> clear_flags;
583
584 clear_flags.reserve( aPins.size() );
585
586 if( grid )
587 {
588 if( groupBySelection )
589 {
590 for( SCH_PIN* pin : aPins )
591 pin->ClearTempFlags();
592
593 for( int row : grid->GetSelectedRowIndices() )
594 {
595 for( SCH_PIN* pin : m_rows[row] )
596 {
597 pin->SetFlags( CANDIDATE );
598 clear_flags.push_back( pin );
599 }
600 }
601 }
602
603 // Commit any pending in-place edits before the row gets moved out from under
604 // the editor.
605 grid->CommitPendingChanges( true );
606
607 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0, m_rows.size() );
608 GetView()->ProcessTableMessage( msg );
609 }
610
611 m_rows.clear();
612
613 if( groupBySelection )
614 m_rows.emplace_back( std::vector<SCH_PIN*>() );
615
616 std::set<wxString> selectedNumbers;
617
619 selectedNumbers.insert( pin->GetNumber() );
620
621 const auto pinIsInEditorSelection =
622 [&]( SCH_PIN* pin )
623 {
624 // Quick check before we iterate the whole thing in N^2 time.
625 // (3000^2 = FPGAs causing issues down the road).
626 if( selectedNumbers.count( pin->GetNumber() ) == 0 )
627 return false;
628
629 for( SCH_PIN* selectedPin : m_origSelectedPins )
630 {
631 // The selected pin is in the editor, but the pins in the table
632 // are copies. We will mark the pin as selected if it's a match
633 // on the critical items.
634 if( selectedPin->GetNumber() == pin->GetNumber()
635 && selectedPin->GetName() == pin->GetName()
636 && selectedPin->GetUnit() == pin->GetUnit()
637 && selectedPin->GetBodyStyle() == pin->GetBodyStyle() )
638 {
639 return true;
640 }
641 }
642
643 return false;
644 };
645
646 for( SCH_PIN* pin : aPins )
647 {
648 const bool includedByUnit = m_unitFilter == ALL_UNITS
649 || pin->GetUnit() == ALL_UNITS
650 || pin->GetUnit() == m_unitFilter;
651 const bool includedByBodyStyle = m_bodyStyleFilter == ALL_BODY_STYLES
652 || pin->GetBodyStyle() == ALL_BODY_STYLES
653 || pin->GetBodyStyle() == m_bodyStyleFilter;
654 const bool includedBySelection = !m_filterBySelection
655 || pinIsInEditorSelection( pin );
656
657 if( includedByUnit && includedByBodyStyle && includedBySelection )
658 {
659 int rowIndex = -1;
660
661 if( groupByName )
662 rowIndex = findRow( m_rows, pin->GetName() );
663 else if( groupBySelection && ( pin->GetFlags() & CANDIDATE ) )
664 rowIndex = 0;
665
666 if( rowIndex < 0 )
667 {
668 m_rows.emplace_back( std::vector<SCH_PIN*>() );
669 rowIndex = m_rows.size() - 1;
670 }
671
672 m_rows[ rowIndex ].push_back( pin );
673 }
674 }
675
676 int sortCol = 0;
677 bool ascending = true;
678
679 if( GetView() && GetView()->GetSortingColumn() != wxNOT_FOUND )
680 {
681 sortCol = GetView()->GetSortingColumn();
682 ascending = GetView()->IsSortOrderAscending();
683 }
684
685 for( std::vector<SCH_PIN*>& row : m_rows )
686 SortPins( row );
687
688 if( !groupBySelection )
689 SortRows( sortCol, ascending );
690
691 if ( GetView() )
692 {
693 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, (int) m_rows.size() );
694 GetView()->ProcessTableMessage( msg );
695
696 if( groupBySelection )
697 GetView()->SelectRow( 0 );
698 }
699
700 for( SCH_PIN* pin : clear_flags )
701 pin->ClearFlags( CANDIDATE );
702 }
703
704 void SortRows( int aSortCol, bool ascending )
705 {
706 std::sort( m_rows.begin(), m_rows.end(),
707 [ aSortCol, ascending, this ]( const std::vector<SCH_PIN*>& lhs,
708 const std::vector<SCH_PIN*>& rhs ) -> bool
709 {
710 return compare( lhs, rhs, aSortCol, ascending, m_frame );
711 } );
712 }
713
714 void SortPins( std::vector<SCH_PIN*>& aRow )
715 {
716 std::sort( aRow.begin(), aRow.end(),
717 []( SCH_PIN* lhs, SCH_PIN* rhs ) -> bool
718 {
719 return PIN_NUMBERS::Compare( lhs->GetNumber(), rhs->GetNumber() ) < 0;
720 } );
721 }
722
723 void AppendRow( SCH_PIN* aPin )
724 {
725 std::vector<SCH_PIN*> row;
726 row.push_back( aPin );
727 m_rows.push_back( row );
728
729 if ( GetView() )
730 {
731 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
732 GetView()->ProcessTableMessage( msg );
733 }
734
735 m_edited = true;
736 }
737
738 std::vector<SCH_PIN*> RemoveRow( int aRow )
739 {
740 std::vector<SCH_PIN*> removedRow = m_rows[ aRow ];
741
742 m_rows.erase( m_rows.begin() + aRow );
743
744 if( GetView() )
745 {
746 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aRow, 1 );
747 GetView()->ProcessTableMessage( msg );
748 }
749
750 m_edited = true;
751 return removedRow;
752 }
753
754 std::vector<SCH_PIN*> GetRowPins( int aRow )
755 {
756 return m_rows[ aRow ];
757 }
758
759 bool IsEdited()
760 {
761 return m_edited;
762 }
763
764private:
766
767 // Because the rows of the grid can either be a single pin or a group of pins, the
768 // data model is a 2D vector. If we're in the single pin case, each row's SCH_PINs
769 // contains only a single pin.
770 std::vector<std::vector<SCH_PIN*>> m_rows;
771 int m_unitFilter; // 0 to show pins for all units
772 int m_bodyStyleFilter; // 0 to show all body styles
774
776
778 LIB_SYMBOL* m_symbol; // Parent symbol that the pins belong to.
779
781 const std::vector<SCH_PIN*>& m_origSelectedPins;
782
783 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
784 std::map< std::pair<std::vector<SCH_PIN*>, int>, wxString > m_evalOriginal;
785};
786
787
789 const std::vector<SCH_PIN*>& aSelectedPins ) :
791 m_editFrame( parent ),
792 m_symbol( aSymbol )
793{
794 m_dataModel = new PIN_TABLE_DATA_MODEL( m_editFrame, this, this->m_symbol, aSelectedPins );
795
796 // Save original columns widths so we can do proportional sizing.
797 for( int i = 0; i < COL_COUNT; ++i )
798 m_originalColWidths[ i ] = m_grid->GetColSize( i );
799
800 m_grid->SetTable( m_dataModel );
801 GRID_TRICKS* gridTricks = new GRID_TRICKS( m_grid,
802 [this]( wxCommandEvent& aEvent )
803 {
804 OnAddRow( aEvent );
805 } );
806 gridTricks->EnableColumnBulkEdit();
807 m_grid->PushEventHandler( gridTricks );
808 m_grid->SetSelectionMode( wxGrid::wxGridSelectCells );
809 m_grid->ShowHideColumns( "0 1 2 3 4 5 9 10" );
810 m_columnsShown = m_grid->GetShownColumns();
811
812 // Set special attributes
813 wxGridCellAttr* attr;
814
815 attr = new wxGridCellAttr;
816 attr->SetReadOnly( true );
817 m_grid->SetColAttr( COL_PIN_COUNT, attr );
818
819 attr = new wxGridCellAttr;
820 wxArrayString typeNames = PinTypeNames();
821 typeNames.push_back( INDETERMINATE_STATE );
822 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinTypeIcons(), typeNames ) );
823 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinTypeIcons(), typeNames ) );
824 m_grid->SetColAttr( COL_TYPE, attr );
825
826 attr = new wxGridCellAttr;
827 wxArrayString shapeNames = PinShapeNames();
828 shapeNames.push_back( INDETERMINATE_STATE );
829 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinShapeIcons(), shapeNames ) );
830 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinShapeIcons(), shapeNames ) );
831 m_grid->SetColAttr( COL_SHAPE, attr );
832
833 attr = new wxGridCellAttr;
834 wxArrayString orientationNames = PinOrientationNames();
835 orientationNames.push_back( INDETERMINATE_STATE );
836 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinOrientationIcons(), orientationNames ) );
837 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinOrientationIcons(), orientationNames ) );
838 m_grid->SetColAttr( COL_ORIENTATION, attr );
839
840 attr = new wxGridCellAttr;
841 wxArrayString unitNames;
842 unitNames.push_back( wxGetTranslation( UNITS_ALL ) );
843
844 for( int i = 1; i <= aSymbol->GetUnitCount(); i++ )
845 unitNames.push_back( m_symbol->GetUnitDisplayName( i, true ) );
846
847 attr->SetEditor( new GRID_CELL_COMBOBOX( unitNames ) );
848 m_grid->SetColAttr( COL_UNIT, attr );
849
850 attr = new wxGridCellAttr;
851 wxArrayString bodyStyleNames;
852 bodyStyleNames.push_back( wxGetTranslation( DEMORGAN_ALL ) );
853
854 if( aSymbol->HasDeMorganBodyStyles() )
855 {
856 bodyStyleNames.push_back( wxGetTranslation( DEMORGAN_STD ) );
857 bodyStyleNames.push_back( wxGetTranslation( DEMORGAN_ALT ) );
858 }
859 else
860 {
861 for( const wxString& body_style_name : aSymbol->GetBodyStyleNames() )
862 bodyStyleNames.push_back( body_style_name );
863 }
864
865 attr->SetEditor( new GRID_CELL_COMBOBOX( bodyStyleNames ) );
866 m_grid->SetColAttr( COL_BODY_STYLE, attr );
867
868 attr = new wxGridCellAttr;
869 attr->SetRenderer( new wxGridCellBoolRenderer() );
870 attr->SetEditor( new wxGridCellBoolEditor() );
871 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
872 m_grid->SetColAttr( COL_VISIBLE, attr );
873
874 /* Right-aligned position values look much better, but only MSW and GTK2+
875 * currently support right-aligned textEditCtrls, so the text jumps on all
876 * the other platforms when you edit it.
877 attr = new wxGridCellAttr;
878 attr->SetAlignment( wxALIGN_RIGHT, wxALIGN_TOP );
879 m_grid->SetColAttr( COL_POSX, attr );
880
881 attr = new wxGridCellAttr;
882 attr->SetAlignment( wxALIGN_RIGHT, wxALIGN_TOP );
883 m_grid->SetColAttr( COL_POSY, attr );
884 */
885
889 m_bMenu->SetBitmap( KiBitmapBundle( BITMAPS::config ) );
890
891 const int summaryW = m_pin_numbers_summary->GetCharWidth() * 30;
892
893 m_pin_numbers_summary->SetWindowStyleFlag( m_pin_numbers_summary->GetWindowStyleFlag() | wxST_ELLIPSIZE_END );
894 m_pin_numbers_summary->SetMaxSize( wxSize( summaryW, -1 ) );
895
896 m_duplicate_pins->SetWindowStyleFlag( m_duplicate_pins->GetWindowStyleFlag() | wxST_ELLIPSIZE_END );
897 m_duplicate_pins->SetMaxSize( wxSize( summaryW, -1 ) );
898
899 GetSizer()->SetSizeHints(this);
900 Centre();
901
902 if( aSymbol->IsMultiUnit() )
903 {
904 m_unitFilter->Append( wxGetTranslation( UNITS_ALL ) );
905
906 for( int ii = 0; ii < aSymbol->GetUnitCount(); ++ii )
907 m_unitFilter->Append( LIB_SYMBOL::LetterSubReference( ii + 1, 'A' ) );
908
909 m_unitFilter->SetSelection( -1 );
910 }
911 else
912 {
913 m_cbFilterByUnit->Enable( false );
914 m_unitFilter->Enable( false );
915 }
916
917 if( aSymbol->HasDeMorganBodyStyles() )
918 {
919 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALL ) );
920 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_STD ) );
921 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALT ) );
922
923 m_bodyStyleFilter->SetSelection( -1 );
924 }
925 else if( aSymbol->IsMultiBodyStyle() )
926 {
927 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALL ) );
928
929 for( const wxString& bodyStyle : aSymbol->GetBodyStyleNames() )
930 m_bodyStyleFilter->Append( bodyStyle );
931
932 m_bodyStyleFilter->SetSelection( -1 );
933 }
934 else
935 {
936 m_cbFilterByBodyStyle->Enable( false );
937 m_bodyStyleFilter->Enable( false );
938 }
939
941
943
944 if( !parent->IsSymbolGraphicallyEditable() )
945 {
946 // Export is OK, but Import is just going to produce frustration
947 m_btnImportFromClipboard->Enable( false );
948 m_btnImportFromFile->Enable( false );
949
950 m_ButtonsCancel->SetDefault();
951 m_ButtonsOK->SetLabel( _( "Read Only" ) );
952 m_ButtonsOK->Enable( false );
953 }
954
955 m_initialized = true;
956 m_modified = false;
957
958 // Connect Events
959 m_grid->Connect( wxEVT_GRID_COL_SORT, wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr,
960 this );
961}
962
963
965{
966 // Disconnect Events
967 m_grid->Disconnect( wxEVT_GRID_COL_SORT, wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr,
968 this );
969
970 // Prevents crash bug in wxGrid's d'tor
971 m_grid->DestroyTable( m_dataModel );
972
973 // Delete the GRID_TRICKS.
974 m_grid->PopEventHandler( true );
975
976 // This is our copy of the pins. If they were transferred to the part on an OK, then
977 // m_pins will already be empty.
978 for( SCH_PIN* pin : m_pins )
979 delete pin;
980
981 WINDOW_THAWER thawer( m_editFrame );
982
983 m_editFrame->ClearFocus();
984 m_editFrame->GetCanvas()->Refresh();
985}
986
987
989{
990 // Make a copy of the pins for editing
991 std::vector<SCH_PIN*> pins = m_symbol->GetGraphicalPins( 0, 0 );
992
993 for( SCH_PIN* pin : pins )
994 m_pins.push_back( new SCH_PIN( *pin ) );
995
996 if( m_cbFilterByUnit->GetValue() )
997 {
998 if( m_unitFilter->GetSelection() == -1 )
999 m_unitFilter->SetSelection( 0 );
1000
1001 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1002 }
1003
1004 if( m_cbFilterByBodyStyle->GetValue() )
1005 {
1006 if( m_bodyStyleFilter->GetSelection() == -1 )
1007 m_bodyStyleFilter->SetSelection( 0 );
1008
1009 m_dataModel->SetBodyStyleFilter( m_bodyStyleFilter->GetSelection() );
1010 }
1011
1012 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
1013
1014 updateSummary();
1015
1016 return true;
1017}
1018
1019
1021{
1022 if( !m_grid->CommitPendingChanges() )
1023 return false;
1024
1025 // Delete the part's pins
1026 std::vector<SCH_PIN*> pins = m_symbol->GetGraphicalPins( 0, 0 );
1027
1028 for( SCH_PIN* pin : pins )
1029 m_symbol->RemoveDrawItem( pin );
1030
1031 // Transfer our pins to the part
1032 for( SCH_PIN* pin : m_pins )
1033 m_symbol->AddDrawItem( pin );
1034
1035 m_pins.clear();
1036
1037 return true;
1038}
1039
1040
1041void DIALOG_LIB_EDIT_PIN_TABLE::OnColSort( wxGridEvent& aEvent )
1042{
1043 if( !m_grid->CommitPendingChanges() )
1044 return;
1045
1046 int sortCol = aEvent.GetCol();
1047 bool ascending;
1048
1049 // This is bonkers, but wxWidgets doesn't tell us ascending/descending in the
1050 // event, and if we ask it will give us pre-event info.
1051 if( m_grid->IsSortingBy( sortCol ) )
1052 // same column; invert ascending
1053 ascending = !m_grid->IsSortOrderAscending();
1054 else
1055 // different column; start with ascending
1056 ascending = true;
1057
1058 m_dataModel->SortRows( sortCol, ascending );
1059}
1060
1061
1062void DIALOG_LIB_EDIT_PIN_TABLE::OnAddRow( wxCommandEvent& event )
1063{
1064 m_grid->OnAddRow(
1065 [&]() -> std::pair<int, int>
1066 {
1067 SCH_PIN* newPin = new SCH_PIN( this->m_symbol );
1068
1069 // Copy the settings of the last pin onto the new pin.
1070 if( m_pins.size() > 0 )
1071 {
1072 SCH_PIN* last = m_pins.back();
1073
1074 newPin->SetOrientation( last->GetOrientation() );
1075 newPin->SetType( last->GetType() );
1076 newPin->SetShape( last->GetShape() );
1077 newPin->SetUnit( last->GetUnit() );
1078 newPin->SetBodyStyle( last->GetBodyStyle() );
1079
1080 VECTOR2I pos = last->GetPosition();
1081
1082 SYMBOL_EDITOR_SETTINGS* cfg = m_editFrame->GetSettings();
1083
1086 {
1087 pos.y -= schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
1088 }
1089 else
1090 {
1091 pos.x += schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
1092 }
1093
1094 newPin->SetPosition( pos );
1095 }
1096
1097 m_pins.push_back( newPin );
1098
1099 m_dataModel->AppendRow( m_pins[ m_pins.size() - 1 ] );
1100 updateSummary();
1101
1102 return { m_dataModel->GetNumberRows() - 1, COL_NUMBER };
1103 } );
1104}
1105
1106
1108{
1109 m_pins.push_back( pin );
1110 updateSummary();
1111}
1112
1113
1114void DIALOG_LIB_EDIT_PIN_TABLE::OnDeleteRow( wxCommandEvent& event )
1115{
1116 m_grid->OnDeleteRows(
1117 [&]( int row )
1118 {
1119 std::vector<SCH_PIN*> removedRow = m_dataModel->RemoveRow( row );
1120
1121 for( SCH_PIN* pin : removedRow )
1122 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1123 } );
1124
1125 updateSummary();
1126}
1127
1128
1130{
1131 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1132 updateSummary();
1133}
1134
1135
1137{
1138 // A bulk edit sends one event per row, so rebuild the summary once from OnUpdateUI
1139 m_summaryDirty = true;
1140}
1141
1142
1144{
1145 SCH_PIN* pin = nullptr;
1146
1147 if( event.GetRow() >= 0 && event.GetRow() < m_dataModel->GetNumberRows() )
1148 {
1149 const std::vector<SCH_PIN*>& pins = m_dataModel->GetRowPins( event.GetRow() );
1150
1151 if( pins.size() == 1 && m_editFrame->GetCurSymbol() )
1152 {
1153 for( SCH_PIN* candidate : m_editFrame->GetCurSymbol()->GetGraphicalPins( 0, 0 ) )
1154 {
1155 if( candidate->GetNumber() == pins.at( 0 )->GetNumber() )
1156 {
1157 pin = candidate;
1158 break;
1159 }
1160 }
1161 }
1162 }
1163
1164 SYMBOL_EDITOR_SETTINGS* cfg = static_cast<SYMBOL_EDITOR_SETTINGS*>( m_editFrame->config() );
1165
1167 {
1168 WINDOW_THAWER thawer( m_editFrame );
1169
1170 m_editFrame->FocusOnItem( pin );
1171 m_editFrame->GetCanvas()->Refresh();
1172 }
1173
1174 event.Skip();
1175}
1176
1177
1179{
1180 return m_cbGroup->GetValue();
1181}
1182
1183
1185{
1186 if( !m_grid->CommitPendingChanges() )
1187 return;
1188
1189 m_cbGroup->SetValue( false );
1190
1191 m_dataModel->RebuildRows( m_pins, false, true );
1192
1193 m_grid->ShowCol( COL_PIN_COUNT );
1194 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1195
1197}
1198
1199
1201{
1202 if( !m_grid->CommitPendingChanges() )
1203 return;
1204
1205 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
1206
1207 if( m_cbGroup->GetValue() )
1208 {
1209 m_grid->ShowCol( COL_PIN_COUNT );
1210 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1211 }
1212
1214}
1215
1216
1217void DIALOG_LIB_EDIT_PIN_TABLE::OnMenu( wxCommandEvent& event )
1218{
1219 SYMBOL_EDITOR_SETTINGS* cfg = static_cast<SYMBOL_EDITOR_SETTINGS*>( m_editFrame->config() );
1220
1221 // Build a pop menu:
1222 wxMenu menu;
1223
1224 menu.Append( 4206, _( "Highlight on Cross-probe" ),
1225 _( "Highlight corresponding pin on canvas when it is selected in the table" ),
1226 wxITEM_CHECK );
1227 menu.Check( 4206, cfg->m_PinTable.crossprobe_on_selection );
1228
1229 // menu_id is the selected submenu id from the popup menu or wxID_NONE
1230 int menu_id = m_bMenu->GetPopupMenuSelectionFromUser( menu );
1231
1232 if( menu_id == 0 || menu_id == 4206 )
1234}
1235
1236
1238{
1239 if( event.GetEventObject() == m_cbFilterByUnit )
1240 {
1241 if( event.IsChecked() )
1242 {
1243 if( m_unitFilter->GetSelection() == -1 )
1244 m_unitFilter->SetSelection( 0 );
1245
1246 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1247 }
1248 else
1249 {
1250 m_dataModel->SetUnitFilter( -1 );
1251 m_unitFilter->SetSelection( -1 );
1252 }
1253 }
1254 else if( event.GetEventObject() == m_cbFilterByBodyStyle )
1255 {
1256 if( event.IsChecked() )
1257 {
1258 if( m_bodyStyleFilter->GetSelection() == -1 )
1259 m_bodyStyleFilter->SetSelection( 0 );
1260
1261 m_dataModel->SetBodyStyleFilter( m_bodyStyleFilter->GetSelection() );
1262 }
1263 else
1264 {
1265 m_dataModel->SetBodyStyleFilter( ALL_BODY_STYLES );
1266 m_bodyStyleFilter->SetSelection( -1 );
1267 }
1268 }
1269 else if( event.GetEventObject() == m_cbFilterSelected )
1270 {
1271 m_dataModel->SetFilterBySelection( event.IsChecked() );
1272 }
1273
1274 OnRebuildRows( event );
1275}
1276
1277
1279{
1280 if( event.GetEventObject() == m_unitFilter )
1281 {
1282 m_cbFilterByUnit->SetValue( true );
1283 m_dataModel->SetUnitFilter( std::max( m_unitFilter->GetSelection(), ALL_UNITS ) );
1284 }
1285 else if( event.GetEventObject() == m_bodyStyleFilter )
1286 {
1287 m_cbFilterByBodyStyle->SetValue( true );
1288 m_dataModel->SetBodyStyleFilter( std::max( m_bodyStyleFilter->GetSelection(), ALL_BODY_STYLES ) );
1289 }
1290
1291 OnRebuildRows( event );
1292}
1293
1294
1296{
1297 if( !m_grid->CommitPendingChanges() )
1298 return;
1299
1300 bool fromFile = event.GetEventObject() == m_btnImportFromFile;
1301 bool replaceAll = m_rbReplaceAll->GetValue();
1302
1304
1306
1307 auto updateFn =
1308 [&]( SCH_PIN& aPin, const wxString& aVal, COL_ORDER aCol )
1309 {
1310 fmt.UpdatePin( aPin, aVal, aCol, *m_symbol );
1311 };
1312
1313 auto createFn =
1314 []( LIB_SYMBOL& aSym )
1315 {
1316 return std::make_unique<SCH_PIN>( &aSym );
1317 };
1318
1319 auto colLabelToEnumFn =
1320 []( const wxString& aStr ) -> COL_ORDER
1321 {
1322 return GetColTypeForString( aStr );
1323 };
1324
1325 std::optional<std::vector<std::vector<wxString>>> csvData = ReadTableFromFileOrClipboard( *m_editFrame, fromFile );
1326
1327 std::vector<std::unique_ptr<SCH_PIN>> newPins;
1328
1329 if( csvData && csvData->size() >= 2 )
1330 {
1331 std::vector<COL_ORDER> headerCols;
1332 wxArrayString unknownHeaders;
1333
1334 for( const wxString& label : ( *csvData )[0] )
1335 {
1336 COL_ORDER col = colLabelToEnumFn( label );
1337
1338 if( col >= COL_COUNT )
1339 unknownHeaders.push_back( label );
1340
1341 headerCols.push_back( col );
1342 }
1343
1344 if( !unknownHeaders.IsEmpty() )
1345 {
1346 wxString msg = wxString::Format( _( "Unknown columns in data: %s. These columns will be ignored." ),
1347 AccumulateDescriptions( unknownHeaders ) );
1348 reporter.Report( msg, RPT_SEVERITY_ERROR );
1349 }
1350
1351 for( size_t i = 1; i < csvData->size(); ++i )
1352 {
1353 const std::vector<wxString>& cols = ( *csvData )[i];
1354 std::unique_ptr<SCH_PIN> item = createFn( *m_symbol );
1355
1356 size_t maxCol = std::min( headerCols.size(), cols.size() );
1357
1358 for( size_t j = 0; j < maxCol; ++j )
1359 {
1360 if( headerCols[j] == COL_COUNT )
1361 continue;
1362
1363 updateFn( *item, cols[j], headerCols[j] );
1364 }
1365
1366 newPins.emplace_back( std::move( item ) );
1367 }
1368 }
1369
1370 if( reporter.HasMessage() )
1371 {
1372 int ret = wxMessageBox( reporter.GetMessages(), _( "Errors" ), wxOK | wxCANCEL | wxICON_ERROR, this );
1373
1374 // Give the user the option to cancel the import on errors.
1375 if( ret == wxCANCEL )
1376 return;
1377 }
1378
1379 if( !newPins.size() )
1380 return;
1381
1382 if( replaceAll )
1383 {
1384 // This is quite a dance with a segfault without smart pointers
1385 for( SCH_PIN* pin : m_pins )
1386 delete pin;
1387
1388 m_pins.clear();
1389 }
1390
1391 for( std::unique_ptr<SCH_PIN>& newPin : newPins )
1392 m_pins.push_back( newPin.release() );
1393
1394 m_cbGroup->SetValue( false );
1395 m_dataModel->RebuildRows( m_pins, false, false );
1396 m_modified = true;
1397
1398 updateSummary();
1399}
1400
1401
1403{
1404 if( !m_grid->CommitPendingChanges() )
1405 return;
1406
1407 bool toFile = event.GetEventObject() == m_btnExportToFile;
1408 bool onlyShown = m_rbExportOnlyShownPins->GetValue();
1409
1410 wxString filePath = wxEmptyString;
1411
1412 if( toFile )
1413 {
1414 wxFileName fn( m_symbol->GetName() );
1415 fn.SetExt( FILEEXT::CsvFileExtension );
1416 wxFileDialog dlg( this, _( "Select pin data file" ), "", fn.GetFullName(), FILEEXT::CsvFileWildcard(),
1417 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
1418
1420
1421 if( dlg.ShowModal() == wxID_CANCEL )
1422 return;
1423
1424 filePath = dlg.GetPath();
1425 }
1426
1427 std::vector<SCH_PIN*> pinsToExport;
1428
1429 if( onlyShown )
1430 {
1431 for( int i = 0; i < m_dataModel->GetNumberRows(); ++i )
1432 {
1433 for( SCH_PIN* pin : m_dataModel->GetRowPins( i ) )
1434 pinsToExport.push_back( pin );
1435 }
1436 }
1437 else
1438 {
1439 pinsToExport = m_pins;
1440 }
1441
1442 static const std::vector<COL_ORDER> exportCols {
1443 COL_NUMBER,
1444 COL_NAME,
1445 COL_TYPE,
1446 COL_SHAPE,
1450 COL_LENGTH,
1451 COL_POSX,
1452 COL_POSY,
1454 COL_UNIT,
1456 };
1457
1460
1461 std::vector<std::vector<wxString>> table;
1462 table.reserve( pinsToExport.size() + 1 );
1463
1464 std::vector<wxString> header;
1465 header.reserve( exportCols.size() );
1466
1467 for( COL_ORDER col : exportCols )
1468 header.emplace_back( wxGetTranslation( GetPinTableColLabel( static_cast<int>( col ) ) ) );
1469
1470 table.emplace_back( std::move( header ) );
1471
1472 for( const SCH_PIN* pin : pinsToExport )
1473 {
1474 std::vector<wxString>& row = table.emplace_back();
1475 row.reserve( exportCols.size() );
1476
1477 for( COL_ORDER col : exportCols )
1478 row.emplace_back( fmt.Format( *pin, static_cast<int>( col ) ) );
1479 }
1480
1482}
1483
1484
1486{
1487 // Account for scroll bars
1489
1490 wxGridUpdateLocker deferRepaintsTillLeavingScope;
1491
1492 // The Number and Name columns must be at least wide enough to hold their contents, but
1493 // no less wide than their original widths.
1494 m_grid->AutoSizeColumn( COL_NUMBER );
1495
1496 const int colNumberMax = std::max( m_originalColWidths[COL_NUMBER] * 4, 400 );
1497
1498 if( m_grid->GetColSize( COL_NUMBER ) > colNumberMax )
1499 m_grid->SetColSize( COL_NUMBER, colNumberMax );
1500
1501 if( m_grid->GetColSize( COL_NUMBER ) < m_originalColWidths[ COL_NUMBER ] )
1503
1504 m_grid->AutoSizeColumn( COL_NAME );
1505
1506 if( m_grid->GetColSize( COL_NAME ) < m_originalColWidths[ COL_NAME ] )
1507 m_grid->SetColSize( COL_NAME, m_originalColWidths[ COL_NAME ] );
1508
1509 // If the grid is still wider than the columns, then stretch the Number and Name columns
1510 // to fit.
1511 for( int i = 0; i < COL_COUNT; ++i )
1512 width -= m_grid->GetColSize( i );
1513
1514 if( width > 0 )
1515 {
1516 m_grid->SetColSize( COL_NUMBER, m_grid->GetColSize( COL_NUMBER ) + width / 2 );
1517 m_grid->SetColSize( COL_NAME, m_grid->GetColSize( COL_NAME ) + width / 2 );
1518 }
1519}
1520
1521
1522void DIALOG_LIB_EDIT_PIN_TABLE::OnSize( wxSizeEvent& event )
1523{
1524 wxSize new_size = event.GetSize();
1525
1526 if( m_initialized && m_size != new_size )
1527 {
1528 m_size = new_size;
1529
1531 }
1532
1533 // Always propagate for a grid repaint (needed if the height changes, as well as width)
1534 event.Skip();
1535}
1536
1537
1538void DIALOG_LIB_EDIT_PIN_TABLE::OnUpdateUI( wxUpdateUIEvent& event )
1539{
1540 std::bitset<64> columnsShown = m_grid->GetShownColumns();
1541
1542 if( columnsShown != m_columnsShown )
1543 {
1544 m_columnsShown = columnsShown;
1545
1546 if( !m_grid->IsCellEditControlShown() )
1548 }
1549
1550 if( m_summaryDirty )
1551 {
1552 m_summaryDirty = false;
1553 updateSummary();
1554 }
1555
1556 const size_t selectedRowCount = m_grid->GetSelectedRowIndices().size();
1557
1558 if( ( selectedRowCount > 1 ) != m_groupSelected->IsEnabled() )
1559 m_groupSelected->Enable( selectedRowCount > 1 );
1560}
1561
1562
1563void DIALOG_LIB_EDIT_PIN_TABLE::OnCancel( wxCommandEvent& event )
1564{
1565 Close();
1566}
1567
1568
1569void DIALOG_LIB_EDIT_PIN_TABLE::OnClose( wxCloseEvent& event )
1570{
1571 // Closing can still save, so a pending bulk edit needs the events a quiet commit skips
1572 if( !m_grid->CommitPendingChanges() && event.CanVeto() )
1573 {
1574 event.Veto();
1575 return;
1576 }
1577
1578 int retval = wxID_CANCEL;
1579
1580 if( ( m_modified || m_dataModel->IsEdited() ) && m_ButtonsOK->IsEnabled() )
1581 {
1582 if( HandleUnsavedChanges( this, _( "Save changes?" ),
1583 [&]() -> bool
1584 {
1586 {
1587 retval = wxID_OK;
1588 return true;
1589 }
1590
1591 return false;
1592 } ) )
1593 {
1594 EndDialogShim( retval );
1595 return;
1596 }
1597 else
1598 {
1599 event.Veto();
1600 return;
1601 }
1602 }
1603
1604 // No change in dialog: we can close it
1605 EndDialogShim( retval );
1606}
1607
1608
1610{
1611 PIN_NUMBERS pinNumbers;
1612
1613 for( SCH_PIN* pin : m_pins )
1614 {
1615 if( pin->GetNumber().Length() )
1616 pinNumbers.insert( pin->GetNumber() );
1617 }
1618
1619 const wxString summary = pinNumbers.GetSummary();
1620 const wxString duplicates = pinNumbers.GetDuplicates();
1621
1622 m_pin_numbers_summary->SetLabel( summary );
1623 m_pin_numbers_summary->SetToolTip( summary );
1624 m_pin_count->SetLabel( wxString::Format( wxT( "%u" ), (unsigned) m_pins.size() ) );
1625 m_duplicate_pins->SetLabel( duplicates );
1626 m_duplicate_pins->SetToolTip( duplicates );
1627
1628 Layout();
1629}
int index
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:130
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
COLUMN_FORMATTER(UNITS_PROVIDER &aUnitsProvider, bool aIncludeUnits, BOOL_FORMAT aBoolFormat, REPORTER &aReporter)
wxString stringFromBool(bool aValue) const
bool boolFromString(const wxString &aValue, REPORTER &aReporter) const
UNITS_PROVIDER & m_unitsProvider
DIALOG_LIB_EDIT_PIN_TABLE_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Pin Table"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
bool m_modified
true when there are unsaved changes
void OnExportButtonClick(wxCommandEvent &event) override
void OnRebuildRows(wxCommandEvent &event) override
void OnCancel(wxCommandEvent &event) override
void OnUpdateUI(wxUpdateUIEvent &event) override
void OnImportButtonClick(wxCommandEvent &event) override
void OnGroupSelected(wxCommandEvent &event) override
void OnClose(wxCloseEvent &event) override
void OnSize(wxSizeEvent &event) override
DIALOG_LIB_EDIT_PIN_TABLE(SYMBOL_EDIT_FRAME *parent, LIB_SYMBOL *aSymbol, const std::vector< SCH_PIN * > &aSelectedPins)
void OnMenu(wxCommandEvent &event) override
void OnCellSelected(wxGridEvent &event) override
void OnAddRow(wxCommandEvent &event) override
void OnDeleteRow(wxCommandEvent &event) override
void OnCellEdited(wxGridEvent &event) override
std::vector< SCH_PIN * > m_pins
void OnFilterCheckBox(wxCommandEvent &event) override
void OnFilterChoice(wxCommandEvent &event) override
void OptOut(wxWindow *aWindow)
Opt out of control state saving.
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void EndDialogShim(int aReturnCode)
A mode agnostic way to close a dialog.
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
void EnableColumnBulkEdit(bool aEnable=true)
Apply an edit to every selected cell in the edited cell's column.
Definition grid_tricks.h:72
Define a library symbol object.
Definition lib_symbol.h:114
bool IsMultiBodyStyle() const override
Definition lib_symbol.h:904
static wxString LetterSubReference(int aUnit, wxChar aInitialLetter)
const std::vector< wxString > & GetBodyStyleNames() const
bool HasDeMorganBodyStyles() const override
bool IsMultiUnit() const override
Definition lib_symbol.h:900
int GetBodyStyleCount() const override
The body styles are a property of the drawings, which a derived symbol inherits from its root symbol ...
int GetUnitCount() const override
A singleton reporter that reports to nowhere.
Definition reporter.h:267
Class that handles conversion of various pin data fields into strings for display in the UI or serial...
wxString Format(const SCH_PIN &aPin, int aFieldId) const
void UpdatePin(SCH_PIN &aPin, const wxString &aValue, int aFieldId, const LIB_SYMBOL &aSymbol) const
Update the pin from the given col/string.
PIN_INFO_FORMATTER(UNITS_PROVIDER &aUnitsProvider, bool aIncludeUnits, BOOL_FORMAT aBoolFormat, REPORTER &aReporter)
wxString GetDuplicates() const
Gets a formatted string of all the pins that have duplicate numbers.
static int Compare(const wxString &lhs, const wxString &rhs)
void insert(value_type const &v)
Definition pin_numbers.h:58
wxString GetSummary() const
static bool compare(const std::vector< SCH_PIN * > &lhs, const std::vector< SCH_PIN * > &rhs, int sortCol, bool ascending, EDA_DRAW_FRAME *parentFrame)
void RebuildRows(const std::vector< SCH_PIN * > &aPins, bool groupByName, bool groupBySelection)
static int findRow(const std::vector< std::vector< SCH_PIN * > > &aRowSet, const wxString &aName)
std::map< std::pair< std::vector< SCH_PIN * >, int >, wxString > m_evalOriginal
std::vector< std::vector< SCH_PIN * > > m_rows
const std::vector< SCH_PIN * > & m_origSelectedPins
The pins in the symbol that are selected at dialog start.
bool IsEmptyCell(int row, int col) override
DIALOG_LIB_EDIT_PIN_TABLE * m_pinTable
std::vector< SCH_PIN * > GetRowPins(int aRow)
static wxString GetValue(const std::vector< SCH_PIN * > &pins, int aCol, EDA_DRAW_FRAME *aParentFrame)
PIN_TABLE_DATA_MODEL(SYMBOL_EDIT_FRAME *aFrame, DIALOG_LIB_EDIT_PIN_TABLE *aPinTable, LIB_SYMBOL *aSymbol, const std::vector< SCH_PIN * > &aOrigSelectedPins)
std::unique_ptr< NUMERIC_EVALUATOR > m_eval
void SetValue(int aRow, int aCol, const wxString &aValue) override
wxString GetValue(int aRow, int aCol) override
void SetFilterBySelection(bool aFilter)
void SortRows(int aSortCol, bool ascending)
void onUnitsChanged(wxCommandEvent &aEvent)
wxString GetColLabelValue(int aCol) override
std::vector< SCH_PIN * > RemoveRow(int aRow)
void SortPins(std::vector< SCH_PIN * > &aRow)
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:73
virtual void SetBodyStyle(int aBodyStyle)
Definition sch_item.h:252
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:350
virtual wxString GetBodyStyleDescription(int aBodyStyle, bool aLabel) const
Definition sch_item.cpp:236
int GetBodyStyle() const
Definition sch_item.h:253
virtual wxString GetUnitDisplayName(int aUnit, bool aLabel) const
Definition sch_item.cpp:225
int GetUnit() const
Definition sch_item.h:243
virtual void SetUnit(int aUnit)
Definition sch_item.h:242
int GetNumberTextSize() const
Definition sch_pin.cpp:885
int GetLength() const
Definition sch_pin.cpp:424
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:845
void SetVisible(bool aVisible)
Definition sch_pin.h:134
void ChangeLength(int aLength)
Change the length of a pin and adjust its position based on orientation.
Definition sch_pin.cpp:1495
void SetOrientation(PIN_ORIENTATION aOrientation)
Definition sch_pin.h:113
void SetName(const wxString &aName)
Definition sch_pin.cpp:548
bool IsVisible() const
Definition sch_pin.cpp:516
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:318
const wxString & GetName() const
Definition sch_pin.cpp:530
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:389
void SetNumberTextSize(int aSize)
Definition sch_pin.cpp:899
void SetShape(GRAPHIC_PINSHAPE aShape)
Definition sch_pin.h:116
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:381
int GetNameTextSize() const
Definition sch_pin.cpp:859
void SetType(ELECTRICAL_PINTYPE aType)
Definition sch_pin.cpp:458
const wxString & GetNumber() const
Definition sch_pin.h:144
GRAPHIC_PINSHAPE GetShape() const
Definition sch_pin.cpp:403
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:438
void SetNameTextSize(int aSize)
Definition sch_pin.cpp:873
The symbol library editor main window.
bool IsSymbolGraphicallyEditable() const
Test if a symbol is loaded and can be edited graphically.
A base class for LIB_SYMBOL and SCH_SYMBOL.
Definition symbol.h:60
int ValueFromString(const wxString &aTextValue, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aTextValue in aUnits to internal units used by the frame.
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.
bool HandleUnsavedChanges(wxWindow *aParent, const wxString &aMessage, const std::function< bool()> &aSaveFunction)
Display a dialog with Save, Cancel and Discard Changes buttons.
Definition confirm.cpp:146
This file is part of the common library.
static wxString GetPinTableColLabel(int aCol)
Get the label for a given column in the pin table.
static COL_ORDER GetColTypeForString(const wxString &aStr)
#define _(s)
#define CANDIDATE
flag indicating that the structure is connected
static std::map< int, wxString > shapeNames
static const std::string CsvFileExtension
static wxString CsvFileWildcard()
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition wxgtk/ui.cpp:496
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:741
#define _HKI(x)
Definition page_info.cpp:40
see class PGM_BASE
const std::vector< BITMAPS > & PinTypeIcons()
Definition pin_type.cpp:159
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition pin_type.h:32
const wxArrayString & PinTypeNames()
Definition pin_type.cpp:150
int PinOrientationIndex(PIN_ORIENTATION code)
Definition pin_type.cpp:67
const wxArrayString & PinShapeNames()
Definition pin_type.cpp:168
const std::vector< BITMAPS > & PinShapeIcons()
Definition pin_type.cpp:177
const wxArrayString & PinOrientationNames()
Definition pin_type.cpp:186
PIN_ORIENTATION
The symbol library pin object orientations.
Definition pin_type.h:101
@ PIN_RIGHT
The pin extends rightwards from the connection point.
Definition pin_type.h:107
@ PIN_LEFT
The pin extends leftwards from the connection point: Probably on the right side of the symbol.
Definition pin_type.h:114
const std::vector< BITMAPS > & PinOrientationIcons()
Definition pin_type.cpp:195
GRAPHIC_PINSHAPE
Definition pin_type.h:80
@ RPT_SEVERITY_ERROR
T * GetAppSettings(const char *aFilename)
int StrNumCmp(const wxString &aString1, const wxString &aString2, CMP_T aCaseSensitivity)
Compare two strings with alphanumerical content.
void AccumulateDescriptions(wxString &aDesc, const T &aItemCollection)
Build a comma-separated list from a collection of wxStrings.
#define ALL_UNITS
Definition symbol.h:150
#define ALL_BODY_STYLES
Definition symbol.h:151
#define UNITS_ALL
#define DEMORGAN_ALL
#define DEMORGAN_ALT
#define DEMORGAN_STD
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
nanodbc::result res
IbisParser parser & reporter
KIBIS_PIN * pin
#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.