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() == 0 )
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() == 0 )
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 ) );
202
203 break;
204
205 case COL_SHAPE:
206 if( PinShapeNames().Index( aValue, false ) != wxNOT_FOUND )
207 aPin.SetShape( (GRAPHIC_PINSHAPE) PinShapeNames().Index( aValue ) );
208
209 break;
210
211 case COL_ORIENTATION:
212 if( PinOrientationNames().Index( aValue, false ) != wxNOT_FOUND )
213 aPin.SetOrientation( (PIN_ORIENTATION) PinOrientationNames().Index( aValue ) );
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
299void getSelectedArea( WX_GRID* aGrid, int* aRowStart, int* aRowCount )
300{
301 wxGridCellCoordsArray topLeft = aGrid->GetSelectionBlockTopLeft();
302 wxGridCellCoordsArray botRight = aGrid->GetSelectionBlockBottomRight();
303
304 wxArrayInt cols = aGrid->GetSelectedCols();
305 wxArrayInt rows = aGrid->GetSelectedRows();
306
307 if( topLeft.Count() && botRight.Count() )
308 {
309 *aRowStart = topLeft[0].GetRow();
310 *aRowCount = botRight[0].GetRow() - *aRowStart + 1;
311 }
312 else if( cols.Count() )
313 {
314 *aRowStart = 0;
315 *aRowCount = aGrid->GetNumberRows();
316 }
317 else if( rows.Count() )
318 {
319 *aRowStart = rows[0];
320 *aRowCount = rows.Count();
321 }
322 else
323 {
324 *aRowStart = aGrid->GetGridCursorRow();
325 *aRowCount = *aRowStart >= 0 ? 1 : 0;
326 }
327}
328
329
331{
332public:
334 DIALOG_LIB_EDIT_PIN_TABLE* aPinTable,
335 LIB_SYMBOL* aSymbol,
336 const std::vector<SCH_PIN*>& aOrigSelectedPins ) :
337 m_frame( aFrame ),
338 m_unitFilter( -1 ),
339 m_bodyStyleFilter( -1 ),
340 m_filterBySelection( false ),
341 m_edited( false ),
342 m_pinTable( aPinTable ),
343 m_symbol( aSymbol ),
344 m_origSelectedPins( aOrigSelectedPins )
345 {
346 m_eval = std::make_unique<NUMERIC_EVALUATOR>( m_frame->GetUserUnits() );
347
348 m_frame->Bind( EDA_EVT_UNITS_CHANGED, &PIN_TABLE_DATA_MODEL::onUnitsChanged, this );
349 }
350
352 {
353 m_frame->Unbind( EDA_EVT_UNITS_CHANGED, &PIN_TABLE_DATA_MODEL::onUnitsChanged, this );
354 }
355
356 void onUnitsChanged( wxCommandEvent& aEvent )
357 {
358 if( GetView() )
359 GetView()->ForceRefresh();
360
361 aEvent.Skip();
362 }
363
364 void SetUnitFilter( int aFilter ) { m_unitFilter = aFilter; }
365 void SetBodyStyleFilter( int aFilter ) { m_bodyStyleFilter = aFilter; }
366 void SetFilterBySelection( bool aFilter ) { m_filterBySelection = aFilter; }
367
368 int GetNumberRows() override { return (int) m_rows.size(); }
369 int GetNumberCols() override { return COL_COUNT; }
370
371 wxString GetColLabelValue( int aCol ) override
372 {
373 return wxGetTranslation( GetPinTableColLabel( aCol ) );
374 }
375
376 bool IsEmptyCell( int row, int col ) override
377 {
378 return false; // don't allow adjacent cell overflow, even if we are actually empty
379 }
380
381 wxString GetValue( int aRow, int aCol ) override
382 {
383 wxGrid* grid = GetView();
384
385 if( grid->GetGridCursorRow() == aRow && grid->GetGridCursorCol() == aCol && grid->IsCellEditControlShown() )
386 {
387 auto it = m_evalOriginal.find( { m_rows[ aRow ], aCol } );
388
389 if( it != m_evalOriginal.end() )
390 return it->second;
391 }
392
393 return GetValue( m_rows[ aRow ], aCol, m_frame );
394 }
395
396 static wxString GetValue( const std::vector<SCH_PIN*>& pins, int aCol, EDA_DRAW_FRAME* aParentFrame )
397 {
398 wxString fieldValue;
399
400 if( pins.empty() )
401 return fieldValue;
402
405
406 for( const SCH_PIN* pin : pins )
407 {
408 wxString val;
409 switch( aCol )
410 {
411 case COL_PIN_COUNT:
412 val << pins.size();
413 break;
414 default:
415 val << formatter.Format( *pin, aCol );
416 break;
417 }
418
419 if( aCol == COL_NUMBER )
420 {
421 if( fieldValue.length() )
422 fieldValue += wxT( ", " );
423
424 fieldValue += val;
425 }
426 else
427 {
428 if( !fieldValue.Length() )
429 fieldValue = val;
430 else if( val != fieldValue )
431 fieldValue = INDETERMINATE_STATE;
432 }
433 }
434
435 return fieldValue;
436 }
437
438 void SetValue( int aRow, int aCol, const wxString &aValue ) override
439 {
440 if( aValue == INDETERMINATE_STATE )
441 return;
442
443 wxString value = aValue;
444
445 switch( aCol )
446 {
447 case COL_NUMBER_SIZE:
448 case COL_NAME_SIZE:
449 case COL_LENGTH:
450 case COL_POSX:
451 case COL_POSY:
452 m_eval->SetDefaultUnits( m_frame->GetUserUnits() );
453
454 if( m_eval->Process( value ) )
455 {
456 m_evalOriginal[ { m_rows[ aRow ], aCol } ] = value;
457 value = m_eval->Result();
458 }
459
460 break;
461
462 default:
463 break;
464 }
465
466 std::vector<SCH_PIN*> pins = m_rows[ aRow ];
467
468 // If the NUMBER column is edited and the pins are grouped, renumber, and add or
469 // remove pins based on the comma separated list of pins.
470 if( aCol == COL_NUMBER && m_pinTable->IsDisplayGrouped() )
471 {
472 wxStringTokenizer tokenizer( value, "," );
473 size_t i = 0;
474
475 while( tokenizer.HasMoreTokens() )
476 {
477 wxString pinName = tokenizer.GetNextToken();
478
479 // Trim whitespace from both ends of the string
480 pinName.Trim( true ).Trim( false );
481
482 if( i < pins.size() )
483 {
484 // Renumber the existing pins
485 pins.at( i )->SetNumber( pinName );
486 }
487 else
488 {
489 // Create new pins
490 SCH_PIN* newPin = new SCH_PIN( this->m_symbol );
491 SCH_PIN* last = pins.back();
492
493 newPin->SetNumber( pinName );
494 newPin->SetName( last->GetName() );
495 newPin->SetOrientation( last->GetOrientation() );
496 newPin->SetType( last->GetType() );
497 newPin->SetShape( last->GetShape() );
498 newPin->SetUnit( last->GetUnit() );
499
500 VECTOR2I pos = last->GetPosition();
501
503 {
506 {
507 pos.y -= schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
508 }
509 else
510 {
511 pos.x += schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
512 }
513 }
514
515 newPin->SetPosition( pos );
516
517 pins.push_back( newPin );
518 m_pinTable->AddPin( newPin );
519 }
520
521 i++;
522 }
523
524 while( pins.size() > i )
525 {
526 m_pinTable->RemovePin( pins.back() );
527 pins.pop_back();
528 }
529
530 m_rows[aRow] = pins;
531 m_edited = true;
532
533 return;
534 }
535
538
539 for( SCH_PIN* pin : pins )
540 formatter.UpdatePin( *pin, value, aCol, *m_symbol );
541
542 m_edited = true;
543 }
544
545 static int findRow( const std::vector<std::vector<SCH_PIN*>>& aRowSet, const wxString& aName )
546 {
547 for( size_t i = 0; i < aRowSet.size(); ++i )
548 {
549 if( aRowSet[ i ][ 0 ] && aRowSet[ i ][ 0 ]->GetName() == aName )
550 return i;
551 }
552
553 return -1;
554 }
555
556 static bool compare( const std::vector<SCH_PIN*>& lhs, const std::vector<SCH_PIN*>& rhs,
557 int sortCol, bool ascending, EDA_DRAW_FRAME* parentFrame )
558 {
559 wxString lhStr = GetValue( lhs, sortCol, parentFrame );
560 wxString rhStr = GetValue( rhs, sortCol, parentFrame );
561
562 if( lhStr == rhStr )
563 {
564 // Secondary sort key is always COL_NUMBER
565 sortCol = COL_NUMBER;
566 lhStr = GetValue( lhs, sortCol, parentFrame );
567 rhStr = GetValue( rhs, sortCol, parentFrame );
568 }
569
570 bool res;
571
572 // N.B. To meet the iterator sort conditions, we cannot simply invert the truth
573 // to get the opposite sort. i.e. ~(a<b) != (a>b)
574 auto cmp =
575 [ ascending ]( const auto a, const auto b )
576 {
577 if( ascending )
578 return a < b;
579 else
580 return b < a;
581 };
582
583 switch( sortCol )
584 {
585 case COL_NUMBER:
586 case COL_NAME:
587 res = cmp( PIN_NUMBERS::Compare( lhStr, rhStr ), 0 );
588 break;
589
590 case COL_NUMBER_SIZE:
591 case COL_NAME_SIZE:
592 res = cmp( parentFrame->ValueFromString( lhStr ), parentFrame->ValueFromString( rhStr ) );
593 break;
594
595 case COL_LENGTH:
596 case COL_POSX:
597 case COL_POSY:
598 res = cmp( parentFrame->ValueFromString( lhStr ), parentFrame->ValueFromString( rhStr ) );
599 break;
600
601 case COL_VISIBLE:
602 case COL_UNIT:
603 case COL_BODY_STYLE:
604 default:
605 res = cmp( StrNumCmp( lhStr, rhStr ), 0 );
606 break;
607 }
608
609 return res;
610 }
611
612 void RebuildRows( const std::vector<SCH_PIN*>& aPins, bool groupByName, bool groupBySelection )
613 {
614 WX_GRID* grid = dynamic_cast<WX_GRID*>( GetView() );
615 std::vector<SCH_PIN*> clear_flags;
616
617 clear_flags.reserve( aPins.size() );
618
619 if( grid )
620 {
621 if( groupBySelection )
622 {
623 for( SCH_PIN* pin : aPins )
624 pin->ClearTempFlags();
625
626 int firstSelectedRow;
627 int selectedRowCount;
628
629 getSelectedArea( grid, &firstSelectedRow, &selectedRowCount );
630
631 for( int ii = 0; ii < selectedRowCount; ++ii )
632 {
633 for( SCH_PIN* pin : m_rows[ firstSelectedRow + ii ] )
634 {
635 pin->SetFlags( CANDIDATE );
636 clear_flags.push_back( pin );
637 }
638 }
639 }
640
641 // Commit any pending in-place edits before the row gets moved out from under
642 // the editor.
643 grid->CommitPendingChanges( true );
644
645 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0, m_rows.size() );
646 GetView()->ProcessTableMessage( msg );
647 }
648
649 m_rows.clear();
650
651 if( groupBySelection )
652 m_rows.emplace_back( std::vector<SCH_PIN*>() );
653
654 std::set<wxString> selectedNumbers;
655
657 selectedNumbers.insert( pin->GetNumber() );
658
659 const auto pinIsInEditorSelection =
660 [&]( SCH_PIN* pin )
661 {
662 // Quick check before we iterate the whole thing in N^2 time.
663 // (3000^2 = FPGAs causing issues down the road).
664 if( selectedNumbers.count( pin->GetNumber() ) == 0 )
665 return false;
666
667 for( SCH_PIN* selectedPin : m_origSelectedPins )
668 {
669 // The selected pin is in the editor, but the pins in the table
670 // are copies. We will mark the pin as selected if it's a match
671 // on the critical items.
672 if( selectedPin->GetNumber() == pin->GetNumber()
673 && selectedPin->GetName() == pin->GetName()
674 && selectedPin->GetUnit() == pin->GetUnit()
675 && selectedPin->GetBodyStyle() == pin->GetBodyStyle() )
676 {
677 return true;
678 }
679 }
680
681 return false;
682 };
683
684 for( SCH_PIN* pin : aPins )
685 {
686 const bool includedByUnit = m_unitFilter <= 0 || pin->GetUnit() == 0 || pin->GetUnit() == m_unitFilter;
687 const bool includedByBodyStyle =
688 m_bodyStyleFilter <= 0 || pin->GetBodyStyle() == 0 || pin->GetBodyStyle() == m_bodyStyleFilter;
689 const bool includedBySelection = !m_filterBySelection || pinIsInEditorSelection( pin );
690
691 if( includedByUnit && includedByBodyStyle && includedBySelection )
692 {
693 int rowIndex = -1;
694
695 if( groupByName )
696 rowIndex = findRow( m_rows, pin->GetName() );
697 else if( groupBySelection && ( pin->GetFlags() & CANDIDATE ) )
698 rowIndex = 0;
699
700 if( rowIndex < 0 )
701 {
702 m_rows.emplace_back( std::vector<SCH_PIN*>() );
703 rowIndex = m_rows.size() - 1;
704 }
705
706 m_rows[ rowIndex ].push_back( pin );
707 }
708 }
709
710 int sortCol = 0;
711 bool ascending = true;
712
713 if( GetView() && GetView()->GetSortingColumn() != wxNOT_FOUND )
714 {
715 sortCol = GetView()->GetSortingColumn();
716 ascending = GetView()->IsSortOrderAscending();
717 }
718
719 for( std::vector<SCH_PIN*>& row : m_rows )
720 SortPins( row );
721
722 if( !groupBySelection )
723 SortRows( sortCol, ascending );
724
725 if ( GetView() )
726 {
727 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, (int) m_rows.size() );
728 GetView()->ProcessTableMessage( msg );
729
730 if( groupBySelection )
731 GetView()->SelectRow( 0 );
732 }
733
734 for( SCH_PIN* pin : clear_flags )
735 pin->ClearFlags( CANDIDATE );
736 }
737
738 void SortRows( int aSortCol, bool ascending )
739 {
740 std::sort( m_rows.begin(), m_rows.end(),
741 [ aSortCol, ascending, this ]( const std::vector<SCH_PIN*>& lhs,
742 const std::vector<SCH_PIN*>& rhs ) -> bool
743 {
744 return compare( lhs, rhs, aSortCol, ascending, m_frame );
745 } );
746 }
747
748 void SortPins( std::vector<SCH_PIN*>& aRow )
749 {
750 std::sort( aRow.begin(), aRow.end(),
751 []( SCH_PIN* lhs, SCH_PIN* rhs ) -> bool
752 {
753 return PIN_NUMBERS::Compare( lhs->GetNumber(), rhs->GetNumber() ) < 0;
754 } );
755 }
756
757 void AppendRow( SCH_PIN* aPin )
758 {
759 std::vector<SCH_PIN*> row;
760 row.push_back( aPin );
761 m_rows.push_back( row );
762
763 if ( GetView() )
764 {
765 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
766 GetView()->ProcessTableMessage( msg );
767 }
768 }
769
770 std::vector<SCH_PIN*> RemoveRow( int aRow )
771 {
772 std::vector<SCH_PIN*> removedRow = m_rows[ aRow ];
773
774 m_rows.erase( m_rows.begin() + aRow );
775
776 if( GetView() )
777 {
778 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aRow, 1 );
779 GetView()->ProcessTableMessage( msg );
780 }
781
782 return removedRow;
783 }
784
785 std::vector<SCH_PIN*> GetRowPins( int aRow )
786 {
787 return m_rows[ aRow ];
788 }
789
790 bool IsEdited()
791 {
792 return m_edited;
793 }
794
795private:
797
798 // Because the rows of the grid can either be a single pin or a group of pins, the
799 // data model is a 2D vector. If we're in the single pin case, each row's SCH_PINs
800 // contains only a single pin.
801 std::vector<std::vector<SCH_PIN*>> m_rows;
802 int m_unitFilter; // 0 or -1 to show pins for all units
803 int m_bodyStyleFilter; // 0 or -1 to show all body styles
805
807
809 LIB_SYMBOL* m_symbol; // Parent symbol that the pins belong to.
810
812 const std::vector<SCH_PIN*>& m_origSelectedPins;
813
814 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
815 std::map< std::pair<std::vector<SCH_PIN*>, int>, wxString > m_evalOriginal;
816};
817
818
820 const std::vector<SCH_PIN*>& aSelectedPins ) :
822 m_editFrame( parent ),
823 m_symbol( aSymbol )
824{
825 m_dataModel = new PIN_TABLE_DATA_MODEL( m_editFrame, this, this->m_symbol, aSelectedPins );
826
827 // Save original columns widths so we can do proportional sizing.
828 for( int i = 0; i < COL_COUNT; ++i )
829 m_originalColWidths[ i ] = m_grid->GetColSize( i );
830
831 m_grid->SetTable( m_dataModel );
832 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
833 {
834 OnAddRow( aEvent );
835 } ) );
836 m_grid->SetSelectionMode( wxGrid::wxGridSelectCells );
837 m_grid->ShowHideColumns( "0 1 2 3 4 5 9 10" );
838 m_columnsShown = m_grid->GetShownColumns();
839
840 // Set special attributes
841 wxGridCellAttr* attr;
842
843 attr = new wxGridCellAttr;
844 attr->SetReadOnly( true );
845 m_grid->SetColAttr( COL_PIN_COUNT, attr );
846
847 attr = new wxGridCellAttr;
848 wxArrayString typeNames = PinTypeNames();
849 typeNames.push_back( INDETERMINATE_STATE );
850 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinTypeIcons(), typeNames ) );
851 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinTypeIcons(), typeNames ) );
852 m_grid->SetColAttr( COL_TYPE, attr );
853
854 attr = new wxGridCellAttr;
855 wxArrayString shapeNames = PinShapeNames();
856 shapeNames.push_back( INDETERMINATE_STATE );
857 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinShapeIcons(), shapeNames ) );
858 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinShapeIcons(), shapeNames ) );
859 m_grid->SetColAttr( COL_SHAPE, attr );
860
861 attr = new wxGridCellAttr;
862 wxArrayString orientationNames = PinOrientationNames();
863 orientationNames.push_back( INDETERMINATE_STATE );
864 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinOrientationIcons(), orientationNames ) );
865 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinOrientationIcons(), orientationNames ) );
866 m_grid->SetColAttr( COL_ORIENTATION, attr );
867
868 attr = new wxGridCellAttr;
869 wxArrayString unitNames;
870 unitNames.push_back( wxGetTranslation( UNITS_ALL ) );
871
872 for( int i = 1; i <= aSymbol->GetUnitCount(); i++ )
873 unitNames.push_back( m_symbol->GetUnitDisplayName( i, true ) );
874
875 attr->SetEditor( new GRID_CELL_COMBOBOX( unitNames ) );
876 m_grid->SetColAttr( COL_UNIT, attr );
877
878 attr = new wxGridCellAttr;
879 wxArrayString bodyStyleNames;
880 bodyStyleNames.push_back( wxGetTranslation( DEMORGAN_ALL ) );
881
882 if( aSymbol->HasDeMorganBodyStyles() )
883 {
884 bodyStyleNames.push_back( wxGetTranslation( DEMORGAN_STD ) );
885 bodyStyleNames.push_back( wxGetTranslation( DEMORGAN_ALT ) );
886 }
887 else
888 {
889 for( const wxString& body_style_name : aSymbol->GetBodyStyleNames() )
890 bodyStyleNames.push_back( body_style_name );
891 }
892
893 attr->SetEditor( new GRID_CELL_COMBOBOX( bodyStyleNames ) );
894 m_grid->SetColAttr( COL_BODY_STYLE, attr );
895
896 attr = new wxGridCellAttr;
897 attr->SetRenderer( new wxGridCellBoolRenderer() );
898 attr->SetEditor( new wxGridCellBoolEditor() );
899 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
900 m_grid->SetColAttr( COL_VISIBLE, attr );
901
902 /* Right-aligned position values look much better, but only MSW and GTK2+
903 * currently support right-aligned textEditCtrls, so the text jumps on all
904 * the other platforms when you edit it.
905 attr = new wxGridCellAttr;
906 attr->SetAlignment( wxALIGN_RIGHT, wxALIGN_TOP );
907 m_grid->SetColAttr( COL_POSX, attr );
908
909 attr = new wxGridCellAttr;
910 attr->SetAlignment( wxALIGN_RIGHT, wxALIGN_TOP );
911 m_grid->SetColAttr( COL_POSY, attr );
912 */
913
917 m_bMenu->SetBitmap( KiBitmapBundle( BITMAPS::config ) );
918
919 const int summaryW = m_pin_numbers_summary->GetCharWidth() * 30;
920
921 m_pin_numbers_summary->SetWindowStyleFlag( m_pin_numbers_summary->GetWindowStyleFlag() | wxST_ELLIPSIZE_END );
922 m_pin_numbers_summary->SetMaxSize( wxSize( summaryW, -1 ) );
923
924 m_duplicate_pins->SetWindowStyleFlag( m_duplicate_pins->GetWindowStyleFlag() | wxST_ELLIPSIZE_END );
925 m_duplicate_pins->SetMaxSize( wxSize( summaryW, -1 ) );
926
927 GetSizer()->SetSizeHints(this);
928 Centre();
929
930 if( aSymbol->IsMultiUnit() )
931 {
932 m_unitFilter->Append( wxGetTranslation( UNITS_ALL ) );
933
934 for( int ii = 0; ii < aSymbol->GetUnitCount(); ++ii )
935 m_unitFilter->Append( LIB_SYMBOL::LetterSubReference( ii + 1, 'A' ) );
936
937 m_unitFilter->SetSelection( -1 );
938 }
939 else
940 {
941 m_cbFilterByUnit->Enable( false );
942 m_unitFilter->Enable( false );
943 }
944
945 if( aSymbol->HasDeMorganBodyStyles() )
946 {
947 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALL ) );
948 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_STD ) );
949 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALT ) );
950
951 m_bodyStyleFilter->SetSelection( -1 );
952 }
953 else if( aSymbol->IsMultiBodyStyle() )
954 {
955 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALL ) );
956
957 for( const wxString& bodyStyle : aSymbol->GetBodyStyleNames() )
958 m_bodyStyleFilter->Append( bodyStyle );
959
960 m_bodyStyleFilter->SetSelection( -1 );
961 }
962 else
963 {
964 m_cbFilterByBodyStyle->Enable( false );
965 m_bodyStyleFilter->Enable( false );
966 }
967
969
971
972 if( !parent->IsSymbolGraphicallyEditable() )
973 {
974 m_ButtonsCancel->SetDefault();
975 m_ButtonsOK->SetLabel( _( "Read Only" ) );
976 m_ButtonsOK->Enable( false );
977 }
978
979 m_initialized = true;
980 m_modified = false;
981
982 // Connect Events
983 m_grid->Connect( wxEVT_GRID_COL_SORT, wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr,
984 this );
985}
986
987
989{
990 // Disconnect Events
991 m_grid->Disconnect( wxEVT_GRID_COL_SORT, wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr,
992 this );
993
994 // Prevents crash bug in wxGrid's d'tor
995 m_grid->DestroyTable( m_dataModel );
996
997 // Delete the GRID_TRICKS.
998 m_grid->PopEventHandler( true );
999
1000 // This is our copy of the pins. If they were transferred to the part on an OK, then
1001 // m_pins will already be empty.
1002 for( SCH_PIN* pin : m_pins )
1003 delete pin;
1004
1005 WINDOW_THAWER thawer( m_editFrame );
1006
1007 m_editFrame->ClearFocus();
1008 m_editFrame->GetCanvas()->Refresh();
1009}
1010
1011
1013{
1014 // Make a copy of the pins for editing
1015 std::vector<SCH_PIN*> pins = m_symbol->GetGraphicalPins( 0, 0 );
1016
1017 for( SCH_PIN* pin : pins )
1018 m_pins.push_back( new SCH_PIN( *pin ) );
1019
1020 if( m_cbFilterByUnit->GetValue() )
1021 {
1022 if( m_unitFilter->GetSelection() == -1 )
1023 m_unitFilter->SetSelection( 0 );
1024
1025 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1026 }
1027
1028 if( m_cbFilterByBodyStyle->GetValue() )
1029 {
1030 if( m_bodyStyleFilter->GetSelection() == -1 )
1031 m_bodyStyleFilter->SetSelection( 0 );
1032
1033 m_dataModel->SetBodyStyleFilter( m_bodyStyleFilter->GetSelection() );
1034 }
1035
1036 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
1037
1038 updateSummary();
1039
1040 return true;
1041}
1042
1043
1045{
1046 if( !m_grid->CommitPendingChanges() )
1047 return false;
1048
1049 // Delete the part's pins
1050 std::vector<SCH_PIN*> pins = m_symbol->GetGraphicalPins( 0, 0 );
1051
1052 for( SCH_PIN* pin : pins )
1053 m_symbol->RemoveDrawItem( pin );
1054
1055 // Transfer our pins to the part
1056 for( SCH_PIN* pin : m_pins )
1057 m_symbol->AddDrawItem( pin );
1058
1059 m_pins.clear();
1060
1061 return true;
1062}
1063
1064
1065void DIALOG_LIB_EDIT_PIN_TABLE::OnColSort( wxGridEvent& aEvent )
1066{
1067 int sortCol = aEvent.GetCol();
1068 bool ascending;
1069
1070 // This is bonkers, but wxWidgets doesn't tell us ascending/descending in the
1071 // event, and if we ask it will give us pre-event info.
1072 if( m_grid->IsSortingBy( sortCol ) )
1073 // same column; invert ascending
1074 ascending = !m_grid->IsSortOrderAscending();
1075 else
1076 // different column; start with ascending
1077 ascending = true;
1078
1079 m_dataModel->SortRows( sortCol, ascending );
1080}
1081
1082
1083void DIALOG_LIB_EDIT_PIN_TABLE::OnAddRow( wxCommandEvent& event )
1084{
1085 m_grid->OnAddRow(
1086 [&]() -> std::pair<int, int>
1087 {
1088 SCH_PIN* newPin = new SCH_PIN( this->m_symbol );
1089
1090 // Copy the settings of the last pin onto the new pin.
1091 if( m_pins.size() > 0 )
1092 {
1093 SCH_PIN* last = m_pins.back();
1094
1095 newPin->SetOrientation( last->GetOrientation() );
1096 newPin->SetType( last->GetType() );
1097 newPin->SetShape( last->GetShape() );
1098 newPin->SetUnit( last->GetUnit() );
1099 newPin->SetBodyStyle( last->GetBodyStyle() );
1100
1101 VECTOR2I pos = last->GetPosition();
1102
1103 SYMBOL_EDITOR_SETTINGS* cfg = m_editFrame->GetSettings();
1104
1107 {
1108 pos.y -= schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
1109 }
1110 else
1111 {
1112 pos.x += schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
1113 }
1114
1115 newPin->SetPosition( pos );
1116 }
1117
1118 m_pins.push_back( newPin );
1119
1120 m_dataModel->AppendRow( m_pins[ m_pins.size() - 1 ] );
1121 updateSummary();
1122
1123 return { m_dataModel->GetNumberRows() - 1, COL_NUMBER };
1124 } );
1125}
1126
1127
1129{
1130 m_pins.push_back( pin );
1131 updateSummary();
1132}
1133
1134
1135void DIALOG_LIB_EDIT_PIN_TABLE::OnDeleteRow( wxCommandEvent& event )
1136{
1137 m_grid->OnDeleteRows(
1138 [&]( int row )
1139 {
1140 std::vector<SCH_PIN*> removedRow = m_dataModel->RemoveRow( row );
1141
1142 for( SCH_PIN* pin : removedRow )
1143 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1144 } );
1145
1146 updateSummary();
1147}
1148
1149
1151{
1152 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1153 updateSummary();
1154}
1155
1156
1158{
1159 updateSummary();
1160}
1161
1162
1164{
1165 SCH_PIN* pin = nullptr;
1166
1167 if( event.GetRow() >= 0 && event.GetRow() < m_dataModel->GetNumberRows() )
1168 {
1169 const std::vector<SCH_PIN*>& pins = m_dataModel->GetRowPins( event.GetRow() );
1170
1171 if( pins.size() == 1 && m_editFrame->GetCurSymbol() )
1172 {
1173 for( SCH_PIN* candidate : m_editFrame->GetCurSymbol()->GetGraphicalPins( 0, 0 ) )
1174 {
1175 if( candidate->GetNumber() == pins.at( 0 )->GetNumber() )
1176 {
1177 pin = candidate;
1178 break;
1179 }
1180 }
1181 }
1182 }
1183
1184 SYMBOL_EDITOR_SETTINGS* cfg = static_cast<SYMBOL_EDITOR_SETTINGS*>( m_editFrame->config() );
1185
1187 {
1188 WINDOW_THAWER thawer( m_editFrame );
1189
1190 m_editFrame->FocusOnItem( pin );
1191 m_editFrame->GetCanvas()->Refresh();
1192 }
1193}
1194
1195
1197{
1198 return m_cbGroup->GetValue();
1199}
1200
1201
1203{
1204 m_cbGroup->SetValue( false );
1205
1206 m_dataModel->RebuildRows( m_pins, false, true );
1207
1208 m_grid->ShowCol( COL_PIN_COUNT );
1209 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1210
1212}
1213
1214
1216{
1217 if( !m_grid->CommitPendingChanges() )
1218 return;
1219
1220 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
1221
1222 if( m_cbGroup->GetValue() )
1223 {
1224 m_grid->ShowCol( COL_PIN_COUNT );
1225 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1226 }
1227
1229}
1230
1231
1232void DIALOG_LIB_EDIT_PIN_TABLE::OnMenu( wxCommandEvent& event )
1233{
1234 SYMBOL_EDITOR_SETTINGS* cfg = static_cast<SYMBOL_EDITOR_SETTINGS*>( m_editFrame->config() );
1235
1236 // Build a pop menu:
1237 wxMenu menu;
1238
1239 menu.Append( 4206, _( "Highlight on Cross-probe" ),
1240 _( "Highlight corresponding pin on canvas when it is selected in the table" ),
1241 wxITEM_CHECK );
1242 menu.Check( 4206, cfg->m_PinTable.crossprobe_on_selection );
1243
1244 // menu_id is the selected submenu id from the popup menu or wxID_NONE
1245 int menu_id = m_bMenu->GetPopupMenuSelectionFromUser( menu );
1246
1247 if( menu_id == 0 || menu_id == 4206 )
1249}
1250
1251
1253{
1254 if( event.GetEventObject() == m_cbFilterByUnit )
1255 {
1256 if( event.IsChecked() )
1257 {
1258 if( m_unitFilter->GetSelection() == -1 )
1259 m_unitFilter->SetSelection( 0 );
1260
1261 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1262 }
1263 else
1264 {
1265 m_dataModel->SetUnitFilter( -1 );
1266 m_unitFilter->SetSelection( -1 );
1267 }
1268 }
1269 else if( event.GetEventObject() == m_cbFilterByBodyStyle )
1270 {
1271 if( event.IsChecked() )
1272 {
1273 if( m_bodyStyleFilter->GetSelection() == -1 )
1274 m_bodyStyleFilter->SetSelection( 0 );
1275
1276 m_dataModel->SetBodyStyleFilter( m_bodyStyleFilter->GetSelection() );
1277 }
1278 else
1279 {
1280 m_dataModel->SetBodyStyleFilter( -1 );
1281 m_bodyStyleFilter->SetSelection( -1 );
1282 }
1283 }
1284 else if( event.GetEventObject() == m_cbFilterSelected )
1285 {
1286 m_dataModel->SetFilterBySelection( event.IsChecked() );
1287 }
1288
1289 OnRebuildRows( event );
1290}
1291
1292
1294{
1295 if( event.GetEventObject() == m_unitFilter )
1296 {
1297 m_cbFilterByUnit->SetValue( true );
1298 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1299 }
1300 else if( event.GetEventObject() == m_bodyStyleFilter )
1301 {
1302 m_cbFilterByBodyStyle->SetValue( true );
1303 m_dataModel->SetBodyStyleFilter( m_bodyStyleFilter->GetSelection() );
1304 }
1305
1306 OnRebuildRows( event );
1307}
1308
1309
1311{
1312 bool fromFile = event.GetEventObject() == m_btnImportFromFile;
1313 bool replaceAll = m_rbReplaceAll->GetValue();
1314
1316
1318
1319 auto updateFn = [&]( SCH_PIN& aPin, const wxString& aVal, COL_ORDER aCol )
1320 {
1321 fmt.UpdatePin( aPin, aVal, aCol, *m_symbol );
1322 };
1323
1324 auto createFn = []( LIB_SYMBOL& aSym )
1325 {
1326 return std::make_unique<SCH_PIN>( &aSym );
1327 };
1328
1329 auto colLabelToEnumFn = []( const wxString& aStr ) -> COL_ORDER
1330 {
1331 return GetColTypeForString( aStr );
1332 };
1333
1334 std::optional<std::vector<std::vector<wxString>>> csvData = ReadTableFromFileOrClipboard( *m_editFrame, fromFile );
1335
1336 std::vector<std::unique_ptr<SCH_PIN>> newPins;
1337
1338 if( csvData && csvData->size() >= 2 )
1339 {
1340 std::vector<COL_ORDER> headerCols;
1341 wxArrayString unknownHeaders;
1342
1343 for( const wxString& label : ( *csvData )[0] )
1344 {
1345 COL_ORDER col = colLabelToEnumFn( label );
1346
1347 if( col >= COL_COUNT )
1348 unknownHeaders.push_back( label );
1349
1350 headerCols.push_back( col );
1351 }
1352
1353 if( !unknownHeaders.IsEmpty() )
1354 {
1355 wxString msg = wxString::Format( _( "Unknown columns in data: %s. These columns will be ignored." ),
1356 AccumulateDescriptions( unknownHeaders ) );
1357 reporter.Report( msg, RPT_SEVERITY_ERROR );
1358 }
1359
1360 for( size_t i = 1; i < csvData->size(); ++i )
1361 {
1362 const std::vector<wxString>& cols = ( *csvData )[i];
1363 std::unique_ptr<SCH_PIN> item = createFn( *m_symbol );
1364
1365 size_t maxCol = std::min( headerCols.size(), cols.size() );
1366
1367 for( size_t j = 0; j < maxCol; ++j )
1368 {
1369 if( headerCols[j] == COL_COUNT )
1370 continue;
1371
1372 updateFn( *item, cols[j], headerCols[j] );
1373 }
1374
1375 newPins.emplace_back( std::move( item ) );
1376 }
1377 }
1378
1379 if( reporter.HasMessage() )
1380 {
1381 int ret = wxMessageBox( reporter.GetMessages(), _( "Errors" ), wxOK | wxCANCEL | wxICON_ERROR, this );
1382
1383 // Give the user the option to cancel the import on errors.
1384 if( ret == wxCANCEL )
1385 return;
1386 }
1387
1388 if( !newPins.size() )
1389 return;
1390
1391 if( replaceAll )
1392 {
1393 // This is quite a dance with a segfault without smart pointers
1394 for( SCH_PIN* pin : m_pins )
1395 delete pin;
1396
1397 m_pins.clear();
1398 }
1399
1400 for( std::unique_ptr<SCH_PIN>& newPin : newPins )
1401 m_pins.push_back( newPin.release() );
1402
1403 m_cbGroup->SetValue( false );
1404 m_dataModel->RebuildRows( m_pins, false, false );
1405
1406 updateSummary();
1407}
1408
1409
1411{
1412 bool toFile = event.GetEventObject() == m_btnExportToFile;
1413 bool onlyShown = m_rbExportOnlyShownPins->GetValue();
1414
1415 wxString filePath = wxEmptyString;
1416
1417 if( toFile )
1418 {
1419 wxFileName fn( m_symbol->GetName() );
1420 fn.SetExt( FILEEXT::CsvFileExtension );
1421 wxFileDialog dlg( this, _( "Select pin data file" ), "", fn.GetFullName(), FILEEXT::CsvFileWildcard(),
1422 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
1423
1425
1426 if( dlg.ShowModal() == wxID_CANCEL )
1427 return;
1428
1429 filePath = dlg.GetPath();
1430 }
1431
1432 std::vector<SCH_PIN*> pinsToExport;
1433
1434 if( onlyShown )
1435 {
1436 for( int i = 0; i < m_dataModel->GetNumberRows(); ++i )
1437 {
1438 for( SCH_PIN* pin : m_dataModel->GetRowPins( i ) )
1439 pinsToExport.push_back( pin );
1440 }
1441 }
1442 else
1443 {
1444 pinsToExport = m_pins;
1445 }
1446
1447 static const std::vector<COL_ORDER> exportCols {
1448 COL_NUMBER,
1449 COL_NAME,
1450 COL_TYPE,
1451 COL_SHAPE,
1455 COL_LENGTH,
1456 COL_POSX,
1457 COL_POSY,
1459 COL_UNIT,
1461 };
1462
1465
1466 std::vector<std::vector<wxString>> table;
1467 table.reserve( pinsToExport.size() + 1 );
1468
1469 std::vector<wxString> header;
1470 header.reserve( exportCols.size() );
1471
1472 for( COL_ORDER col : exportCols )
1473 header.emplace_back( wxGetTranslation( GetPinTableColLabel( static_cast<int>( col ) ) ) );
1474
1475 table.emplace_back( std::move( header ) );
1476
1477 for( const SCH_PIN* pin : pinsToExport )
1478 {
1479 std::vector<wxString>& row = table.emplace_back();
1480 row.reserve( exportCols.size() );
1481
1482 for( COL_ORDER col : exportCols )
1483 row.emplace_back( fmt.Format( *pin, static_cast<int>( col ) ) );
1484 }
1485
1487}
1488
1489
1491{
1492 // Account for scroll bars
1494
1495 wxGridUpdateLocker deferRepaintsTillLeavingScope;
1496
1497 // The Number and Name columns must be at least wide enough to hold their contents, but
1498 // no less wide than their original widths.
1499 m_grid->AutoSizeColumn( COL_NUMBER );
1500
1501 const int colNumberMax = std::max( m_originalColWidths[COL_NUMBER] * 4, 400 );
1502
1503 if( m_grid->GetColSize( COL_NUMBER ) > colNumberMax )
1504 m_grid->SetColSize( COL_NUMBER, colNumberMax );
1505
1506 if( m_grid->GetColSize( COL_NUMBER ) < m_originalColWidths[ COL_NUMBER ] )
1508
1509 m_grid->AutoSizeColumn( COL_NAME );
1510
1511 if( m_grid->GetColSize( COL_NAME ) < m_originalColWidths[ COL_NAME ] )
1512 m_grid->SetColSize( COL_NAME, m_originalColWidths[ COL_NAME ] );
1513
1514 // If the grid is still wider than the columns, then stretch the Number and Name columns
1515 // to fit.
1516 for( int i = 0; i < COL_COUNT; ++i )
1517 width -= m_grid->GetColSize( i );
1518
1519 if( width > 0 )
1520 {
1521 m_grid->SetColSize( COL_NUMBER, m_grid->GetColSize( COL_NUMBER ) + width / 2 );
1522 m_grid->SetColSize( COL_NAME, m_grid->GetColSize( COL_NAME ) + width / 2 );
1523 }
1524}
1525
1526
1527void DIALOG_LIB_EDIT_PIN_TABLE::OnSize( wxSizeEvent& event )
1528{
1529 wxSize new_size = event.GetSize();
1530
1531 if( m_initialized && m_size != new_size )
1532 {
1533 m_size = new_size;
1534
1536 }
1537
1538 // Always propagate for a grid repaint (needed if the height changes, as well as width)
1539 event.Skip();
1540}
1541
1542
1543void DIALOG_LIB_EDIT_PIN_TABLE::OnUpdateUI( wxUpdateUIEvent& event )
1544{
1545 std::bitset<64> columnsShown = m_grid->GetShownColumns();
1546
1547 if( columnsShown != m_columnsShown )
1548 {
1549 m_columnsShown = columnsShown;
1550
1551 if( !m_grid->IsCellEditControlShown() )
1553 }
1554
1555 int firstSelectedRow;
1556 int selectedRowCount;
1557
1558 getSelectedArea( m_grid, &firstSelectedRow, &selectedRowCount );
1559
1560 if( ( selectedRowCount > 1 ) != m_groupSelected->IsEnabled() )
1561 m_groupSelected->Enable( selectedRowCount > 1 );
1562}
1563
1564
1565void DIALOG_LIB_EDIT_PIN_TABLE::OnCancel( wxCommandEvent& event )
1566{
1567 Close();
1568}
1569
1570
1571void DIALOG_LIB_EDIT_PIN_TABLE::OnClose( wxCloseEvent& event )
1572{
1573 // This is a cancel, so commit quietly as we're going to throw the results away anyway.
1574 m_grid->CommitPendingChanges( true );
1575
1576 int retval = wxID_CANCEL;
1577
1578 if( m_dataModel->IsEdited() )
1579 {
1580 if( HandleUnsavedChanges( this, _( "Save changes?" ),
1581 [&]() -> bool
1582 {
1584 {
1585 retval = wxID_OK;
1586 return true;
1587 }
1588
1589 return false;
1590 } ) )
1591 {
1592 EndDialogShim( retval );
1593 return;
1594 }
1595 else
1596 {
1597 event.Veto();
1598 return;
1599 }
1600 }
1601
1602 // No change in dialog: we can close it
1603 EndDialogShim( retval );
1604}
1605
1606
1608{
1609 PIN_NUMBERS pinNumbers;
1610
1611 for( SCH_PIN* pin : m_pins )
1612 {
1613 if( pin->GetNumber().Length() )
1614 pinNumbers.insert( pin->GetNumber() );
1615 }
1616
1617 const wxString summary = pinNumbers.GetSummary();
1618 const wxString duplicates = pinNumbers.GetDuplicates();
1619
1620 m_pin_numbers_summary->SetLabel( summary );
1621 m_pin_numbers_summary->SetToolTip( summary );
1622 m_pin_count->SetLabel( wxString::Format( wxT( "%u" ), (unsigned) m_pins.size() ) );
1623 m_duplicate_pins->SetLabel( duplicates );
1624 m_duplicate_pins->SetToolTip( duplicates );
1625
1626 Layout();
1627}
int index
constexpr EDA_IU_SCALE schIUScale
Definition base_units.h:123
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:57
Define a library symbol object.
Definition lib_symbol.h:119
bool IsMultiBodyStyle() const override
Definition lib_symbol.h:894
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:890
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:246
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:287
virtual wxString GetBodyStyleDescription(int aBodyStyle, bool aLabel) const
Definition sch_item.cpp:215
int GetBodyStyle() const
Definition sch_item.h:247
virtual wxString GetUnitDisplayName(int aUnit, bool aLabel) const
Definition sch_item.cpp:204
int GetUnit() const
Definition sch_item.h:237
virtual void SetUnit(int aUnit)
Definition sch_item.h:236
int GetNumberTextSize() const
Definition sch_pin.cpp:865
int GetLength() const
Definition sch_pin.cpp:397
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:825
void SetVisible(bool aVisible)
Definition sch_pin.h:132
void ChangeLength(int aLength)
Change the length of a pin and adjust its position based on orientation.
Definition sch_pin.cpp:1475
void SetOrientation(PIN_ORIENTATION aOrientation)
Definition sch_pin.h:111
void SetName(const wxString &aName)
Definition sch_pin.cpp:521
bool IsVisible() const
Definition sch_pin.cpp:489
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:314
const wxString & GetName() const
Definition sch_pin.cpp:503
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:362
void SetNumberTextSize(int aSize)
Definition sch_pin.cpp:879
void SetShape(GRAPHIC_PINSHAPE aShape)
Definition sch_pin.h:114
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:354
int GetNameTextSize() const
Definition sch_pin.cpp:839
void SetType(ELECTRICAL_PINTYPE aType)
Definition sch_pin.cpp:431
const wxString & GetNumber() const
Definition sch_pin.h:142
GRAPHIC_PINSHAPE GetShape() const
Definition sch_pin.cpp:376
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:411
void SetNameTextSize(int aSize)
Definition sch_pin.cpp:853
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:59
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.
void getSelectedArea(WX_GRID *aGrid, int *aRowStart, int *aRowCount)
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:367
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:521
#define _HKI(x)
Definition page_info.cpp:40
see class PGM_BASE
const std::vector< BITMAPS > & PinTypeIcons()
Definition pin_type.cpp:158
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:149
int PinOrientationIndex(PIN_ORIENTATION code)
Definition pin_type.cpp:66
const wxArrayString & PinShapeNames()
Definition pin_type.cpp:167
const std::vector< BITMAPS > & PinShapeIcons()
Definition pin_type.cpp:176
const wxArrayString & PinOrientationNames()
Definition pin_type.cpp:185
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:194
GRAPHIC_PINSHAPE
Definition pin_type.h:80
@ RPT_SEVERITY_ERROR
T * GetAppSettings(const char *aFilename)
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
void AccumulateDescriptions(wxString &aDesc, const T &aItemCollection)
Build a comma-separated list from a collection of wxStrings.
#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
IbisParser parser & reporter
KIBIS_PIN * pin
VECTOR3I res
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition ui_common.h:46
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
Definition of file extensions used in Kicad.