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