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.GetUnitDisplayName( i, true )
277 || aValue == aPin.GetUnitDisplayName( 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
1018
1019 if( dlg.ShowModal() == wxID_CANCEL )
1020 return wxEmptyString;
1021
1022 return dlg.GetPath();
1023 }
1024
1025 std::vector<COL_ORDER> getColOrderFromCSV( const std::vector<wxString>& aHeaderRow ) const
1026 {
1027 std::vector<COL_ORDER> colOrder;
1028 wxArrayString unknownHeaders;
1029
1030 for( size_t i = 0; i < aHeaderRow.size(); ++i )
1031 {
1032 COL_ORDER col = GetColTypeForString( aHeaderRow[i] );
1033
1034 if( col >= COL_COUNT )
1035 unknownHeaders.push_back( aHeaderRow[i] );
1036
1037 colOrder.push_back( col );
1038 }
1039
1040 if( unknownHeaders.size() )
1041 {
1042 wxString msg = wxString::Format( _( "Unknown columns in data: %s. These columns will be ignored." ),
1043 AccumulateDescriptions( unknownHeaders ) );
1044 m_reporter.Report( msg, RPT_SEVERITY_ERROR );
1045 }
1046
1047 return colOrder;
1048 }
1049
1052};
1053
1054
1056 const std::vector<SCH_PIN*>& aSelectedPins ) :
1058 m_editFrame( parent ),
1059 m_symbol( aSymbol )
1060{
1061 m_dataModel = new PIN_TABLE_DATA_MODEL( m_editFrame, this, this->m_symbol, aSelectedPins );
1062
1063 // Save original columns widths so we can do proportional sizing.
1064 for( int i = 0; i < COL_COUNT; ++i )
1065 m_originalColWidths[ i ] = m_grid->GetColSize( i );
1066
1067 m_grid->SetTable( m_dataModel );
1068 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
1069 {
1070 OnAddRow( aEvent );
1071 } ) );
1072 m_grid->SetSelectionMode( wxGrid::wxGridSelectRows );
1073 m_grid->ShowHideColumns( "0 1 2 3 4 5 9 10" );
1074 m_columnsShown = m_grid->GetShownColumns();
1075
1076 // Set special attributes
1077 wxGridCellAttr* attr;
1078
1079 attr = new wxGridCellAttr;
1080 attr->SetReadOnly( true );
1081 m_grid->SetColAttr( COL_PIN_COUNT, attr );
1082
1083 attr = new wxGridCellAttr;
1084 wxArrayString typeNames = PinTypeNames();
1085 typeNames.push_back( INDETERMINATE_STATE );
1086 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinTypeIcons(), typeNames ) );
1087 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinTypeIcons(), typeNames ) );
1088 m_grid->SetColAttr( COL_TYPE, attr );
1089
1090 attr = new wxGridCellAttr;
1091 wxArrayString shapeNames = PinShapeNames();
1092 shapeNames.push_back( INDETERMINATE_STATE );
1093 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinShapeIcons(), shapeNames ) );
1094 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinShapeIcons(), shapeNames ) );
1095 m_grid->SetColAttr( COL_SHAPE, attr );
1096
1097 attr = new wxGridCellAttr;
1098 wxArrayString orientationNames = PinOrientationNames();
1099 orientationNames.push_back( INDETERMINATE_STATE );
1100 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinOrientationIcons(), orientationNames ) );
1101 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinOrientationIcons(), orientationNames ) );
1102 m_grid->SetColAttr( COL_ORIENTATION, attr );
1103
1104 attr = new wxGridCellAttr;
1105 wxArrayString unitNames;
1106 unitNames.push_back( wxGetTranslation( UNITS_ALL ) );
1107
1108 for( int i = 1; i <= aSymbol->GetUnitCount(); i++ )
1109 unitNames.push_back( m_symbol->GetUnitDisplayName( i, true ) );
1110
1111 attr->SetEditor( new GRID_CELL_COMBOBOX( unitNames ) );
1112 m_grid->SetColAttr( COL_UNIT, attr );
1113
1114 attr = new wxGridCellAttr;
1115 wxArrayString bodyStyleNames;
1116 bodyStyleNames.push_back( wxGetTranslation( DEMORGAN_ALL ) );
1117
1118 if( aSymbol->HasDeMorganBodyStyles() )
1119 {
1120 bodyStyleNames.push_back( wxGetTranslation( DEMORGAN_STD ) );
1121 bodyStyleNames.push_back( wxGetTranslation( DEMORGAN_ALT ) );
1122 }
1123 else
1124 {
1125 for( const wxString& body_style_name : aSymbol->GetBodyStyleNames() )
1126 bodyStyleNames.push_back( body_style_name );
1127 }
1128
1129 attr->SetEditor( new GRID_CELL_COMBOBOX( bodyStyleNames ) );
1130 m_grid->SetColAttr( COL_BODY_STYLE, attr );
1131
1132 attr = new wxGridCellAttr;
1133 attr->SetRenderer( new wxGridCellBoolRenderer() );
1134 attr->SetEditor( new wxGridCellBoolEditor() );
1135 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1136 m_grid->SetColAttr( COL_VISIBLE, attr );
1137
1138 /* Right-aligned position values look much better, but only MSW and GTK2+
1139 * currently support right-aligned textEditCtrls, so the text jumps on all
1140 * the other platforms when you edit it.
1141 attr = new wxGridCellAttr;
1142 attr->SetAlignment( wxALIGN_RIGHT, wxALIGN_TOP );
1143 m_grid->SetColAttr( COL_POSX, attr );
1144
1145 attr = new wxGridCellAttr;
1146 attr->SetAlignment( wxALIGN_RIGHT, wxALIGN_TOP );
1147 m_grid->SetColAttr( COL_POSY, attr );
1148 */
1149
1153
1154 m_divider1->SetIsSeparator();
1155
1156 GetSizer()->SetSizeHints(this);
1157 Centre();
1158
1159 if( aSymbol->IsMultiUnit() )
1160 {
1161 m_unitFilter->Append( wxGetTranslation( UNITS_ALL ) );
1162
1163 for( int ii = 0; ii < aSymbol->GetUnitCount(); ++ii )
1164 m_unitFilter->Append( LIB_SYMBOL::LetterSubReference( ii + 1, 'A' ) );
1165
1166 m_unitFilter->SetSelection( -1 );
1167 }
1168 else
1169 {
1170 m_cbFilterByUnit->Enable( false );
1171 m_unitFilter->Enable( false );
1172 }
1173
1174 if( aSymbol->HasDeMorganBodyStyles() )
1175 {
1176 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALL ) );
1177 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_STD ) );
1178 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALT ) );
1179
1180 m_bodyStyleFilter->SetSelection( -1 );
1181 }
1182 else if( aSymbol->IsMultiBodyStyle() )
1183 {
1184 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALL ) );
1185
1186 for( const wxString& bodyStyle : aSymbol->GetBodyStyleNames() )
1187 m_bodyStyleFilter->Append( bodyStyle );
1188
1189 m_bodyStyleFilter->SetSelection( -1 );
1190 }
1191 else
1192 {
1193 m_cbFilterByBodyStyle->Enable( false );
1194 m_bodyStyleFilter->Enable( false );
1195 }
1196
1198
1199 if( !parent->IsSymbolEditable() || parent->IsSymbolAlias() )
1200 {
1201 m_ButtonsCancel->SetDefault();
1202 m_ButtonsOK->SetLabel( _( "Read Only" ) );
1203 m_ButtonsOK->Enable( false );
1204 }
1205
1206 m_initialized = true;
1207 m_modified = false;
1208
1209 // Connect Events
1210 m_grid->Connect( wxEVT_GRID_COL_SORT, wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr,
1211 this );
1212}
1213
1214
1216{
1217 // Disconnect Events
1218 m_grid->Disconnect( wxEVT_GRID_COL_SORT, wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr,
1219 this );
1220
1221 // Prevents crash bug in wxGrid's d'tor
1222 m_grid->DestroyTable( m_dataModel );
1223
1224 // Delete the GRID_TRICKS.
1225 m_grid->PopEventHandler( true );
1226
1227 // This is our copy of the pins. If they were transferred to the part on an OK, then
1228 // m_pins will already be empty.
1229 for( SCH_PIN* pin : m_pins )
1230 delete pin;
1231
1232 WINDOW_THAWER thawer( m_editFrame );
1233
1234 m_editFrame->ClearFocus();
1235 m_editFrame->GetCanvas()->Refresh();
1236}
1237
1238
1240{
1241 // Make a copy of the pins for editing
1242 std::vector<SCH_PIN*> pins = m_symbol->GetGraphicalPins( 0, 0 );
1243
1244 for( SCH_PIN* pin : pins )
1245 m_pins.push_back( new SCH_PIN( *pin ) );
1246
1247 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
1248
1249 updateSummary();
1250
1251 return true;
1252}
1253
1254
1256{
1257 if( !m_grid->CommitPendingChanges() )
1258 return false;
1259
1260 // Delete the part's pins
1261 std::vector<SCH_PIN*> pins = m_symbol->GetGraphicalPins( 0, 0 );
1262
1263 for( SCH_PIN* pin : pins )
1264 m_symbol->RemoveDrawItem( pin );
1265
1266 // Transfer our pins to the part
1267 for( SCH_PIN* pin : m_pins )
1268 m_symbol->AddDrawItem( pin );
1269
1270 m_pins.clear();
1271
1272 return true;
1273}
1274
1275
1276void DIALOG_LIB_EDIT_PIN_TABLE::OnColSort( wxGridEvent& aEvent )
1277{
1278 int sortCol = aEvent.GetCol();
1279 bool ascending;
1280
1281 // This is bonkers, but wxWidgets doesn't tell us ascending/descending in the
1282 // event, and if we ask it will give us pre-event info.
1283 if( m_grid->IsSortingBy( sortCol ) )
1284 // same column; invert ascending
1285 ascending = !m_grid->IsSortOrderAscending();
1286 else
1287 // different column; start with ascending
1288 ascending = true;
1289
1290 m_dataModel->SortRows( sortCol, ascending );
1291}
1292
1293
1294void DIALOG_LIB_EDIT_PIN_TABLE::OnAddRow( wxCommandEvent& event )
1295{
1296 m_grid->OnAddRow(
1297 [&]() -> std::pair<int, int>
1298 {
1299 SCH_PIN* newPin = new SCH_PIN( this->m_symbol );
1300
1301 // Copy the settings of the last pin onto the new pin.
1302 if( m_pins.size() > 0 )
1303 {
1304 SCH_PIN* last = m_pins.back();
1305
1306 newPin->SetOrientation( last->GetOrientation() );
1307 newPin->SetType( last->GetType() );
1308 newPin->SetShape( last->GetShape() );
1309 newPin->SetUnit( last->GetUnit() );
1310 newPin->SetBodyStyle( last->GetBodyStyle() );
1311
1312 VECTOR2I pos = last->GetPosition();
1313
1314 SYMBOL_EDITOR_SETTINGS* cfg = m_editFrame->GetSettings();
1315
1318 {
1319 pos.y -= schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
1320 }
1321 else
1322 {
1323 pos.x += schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
1324 }
1325
1326 newPin->SetPosition( pos );
1327 }
1328
1329 m_pins.push_back( newPin );
1330
1331 m_dataModel->AppendRow( m_pins[ m_pins.size() - 1 ] );
1332 updateSummary();
1333
1334 return { m_dataModel->GetNumberRows() - 1, COL_NUMBER };
1335 } );
1336}
1337
1338
1340{
1341 m_pins.push_back( pin );
1342 updateSummary();
1343}
1344
1345
1346void DIALOG_LIB_EDIT_PIN_TABLE::OnDeleteRow( wxCommandEvent& event )
1347{
1348 m_grid->OnDeleteRows(
1349 [&]( int row )
1350 {
1351 std::vector<SCH_PIN*> removedRow = m_dataModel->RemoveRow( row );
1352
1353 for( SCH_PIN* pin : removedRow )
1354 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1355 } );
1356
1357 updateSummary();
1358}
1359
1360
1362{
1363 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1364 updateSummary();
1365}
1366
1367
1369{
1370 updateSummary();
1371}
1372
1373
1375{
1376 SCH_PIN* pin = nullptr;
1377
1378 if( event.GetRow() >= 0 && event.GetRow() < m_dataModel->GetNumberRows() )
1379 {
1380 const std::vector<SCH_PIN*>& pins = m_dataModel->GetRowPins( event.GetRow() );
1381
1382 if( pins.size() == 1 && m_editFrame->GetCurSymbol() )
1383 {
1384 for( SCH_PIN* candidate : m_editFrame->GetCurSymbol()->GetGraphicalPins( 0, 0 ) )
1385 {
1386 if( candidate->GetNumber() == pins.at( 0 )->GetNumber() )
1387 {
1388 pin = candidate;
1389 break;
1390 }
1391 }
1392 }
1393 }
1394
1395 WINDOW_THAWER thawer( m_editFrame );
1396
1397 m_editFrame->FocusOnItem( pin );
1398 m_editFrame->GetCanvas()->Refresh();
1399}
1400
1401
1403{
1404 return m_cbGroup->GetValue();
1405}
1406
1407
1409{
1410 m_cbGroup->SetValue( false );
1411
1412 m_dataModel->RebuildRows( m_pins, false, true );
1413
1414 m_grid->ShowCol( COL_PIN_COUNT );
1415 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1416
1418}
1419
1420
1422{
1423 if( !m_grid->CommitPendingChanges() )
1424 return;
1425
1426 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
1427
1428 if( m_cbGroup->GetValue() )
1429 {
1430 m_grid->ShowCol( COL_PIN_COUNT );
1431 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1432 }
1433
1435}
1436
1437
1439{
1440 if( event.GetEventObject() == m_cbFilterByUnit )
1441 {
1442 if( event.IsChecked() )
1443 {
1444 if( m_unitFilter->GetSelection() == -1 )
1445 m_unitFilter->SetSelection( 0 );
1446
1447 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1448 }
1449 else
1450 {
1451 m_dataModel->SetUnitFilter( -1 );
1452 m_unitFilter->SetSelection( -1 );
1453 }
1454 }
1455 else if( event.GetEventObject() == m_cbFilterByBodyStyle )
1456 {
1457 if( event.IsChecked() )
1458 {
1459 if( m_bodyStyleFilter->GetSelection() == -1 )
1460 m_bodyStyleFilter->SetSelection( 0 );
1461
1462 m_dataModel->SetBodyStyleFilter( m_bodyStyleFilter->GetSelection() );
1463 }
1464 else
1465 {
1466 m_dataModel->SetBodyStyleFilter( -1 );
1467 m_bodyStyleFilter->SetSelection( -1 );
1468 }
1469 }
1470 else if( event.GetEventObject() == m_cbFilterSelected )
1471 {
1472 m_dataModel->SetFilterBySelection( event.IsChecked() );
1473 }
1474
1475 OnRebuildRows( event );
1476}
1477
1478
1480{
1481 if( event.GetEventObject() == m_unitFilter )
1482 {
1483 m_cbFilterByUnit->SetValue( true );
1484 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1485 }
1486 else if( event.GetEventObject() == m_bodyStyleFilter )
1487 {
1488 m_cbFilterByBodyStyle->SetValue( true );
1489 m_dataModel->SetBodyStyleFilter( m_bodyStyleFilter->GetSelection() );
1490 }
1491
1492 OnRebuildRows( event );
1493}
1494
1495
1497{
1498 bool fromFile = event.GetEventObject() == m_btnImportFromFile;
1499 bool replaceAll = m_rbReplaceAll->GetValue();
1500
1501 WX_STRING_REPORTER reporter;
1502 PIN_TABLE_IMPORT importer( *m_editFrame, reporter );
1503
1504 std::vector<std::unique_ptr<SCH_PIN>> newPins = importer.ImportData( fromFile, *m_symbol );
1505
1506 if( reporter.HasMessage() )
1507 {
1508 int ret = wxMessageBox( reporter.GetMessages(), _( "Errors" ), wxOK | wxCANCEL | wxICON_ERROR, this );
1509
1510 // Give the user the option to cancel the import on errors.
1511 if( ret == wxCANCEL )
1512 return;
1513 }
1514
1515 if( !newPins.size() )
1516 return;
1517
1518 if( replaceAll )
1519 {
1520 // This is quite a dance with a segfault without smart pointers
1521 for( SCH_PIN* pin : m_pins )
1522 delete pin;
1523
1524 m_pins.clear();
1525 }
1526
1527 for( auto& newPin : newPins )
1528 m_pins.push_back( newPin.release() );
1529
1530 m_cbGroup->SetValue( false );
1531 m_dataModel->RebuildRows( m_pins, false, false );
1532
1533 updateSummary();
1534}
1535
1536
1538{
1539 bool toFile = event.GetEventObject() == m_btnExportToFile;
1540 bool onlyShown = m_rbExportOnlyShownPins->GetValue();
1541
1542 wxString filePath = wxEmptyString;
1543
1544 if( toFile )
1545 {
1546 wxFileName fn( m_symbol->GetName() );
1547 fn.SetExt( FILEEXT::CsvFileExtension );
1548 wxFileDialog dlg( this, _( "Select pin data file" ), "", fn.GetFullName(), FILEEXT::CsvFileWildcard(),
1549 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
1550
1552
1553 if( dlg.ShowModal() == wxID_CANCEL )
1554 return;
1555
1556 filePath = dlg.GetPath();
1557 }
1558
1559 std::vector<SCH_PIN*> pinsToExport;
1560
1561 if( onlyShown )
1562 {
1563 for( int i = 0; i < m_dataModel->GetNumberRows(); ++i )
1564 {
1565 for( SCH_PIN* pin : m_dataModel->GetRowPins( i ) )
1566 pinsToExport.push_back( pin );
1567 }
1568 }
1569 else
1570 {
1571 pinsToExport = m_pins;
1572 }
1573
1574 PIN_TABLE_EXPORT exporter( *m_editFrame );
1575 exporter.ExportData( pinsToExport, filePath );
1576}
1577
1578
1580{
1581 // Account for scroll bars
1583
1584 wxGridUpdateLocker deferRepaintsTillLeavingScope;
1585
1586 // The Number and Name columns must be at least wide enough to hold their contents, but
1587 // no less wide than their original widths.
1588 m_grid->AutoSizeColumn( COL_NUMBER );
1589
1590 if( m_grid->GetColSize( COL_NUMBER ) < m_originalColWidths[ COL_NUMBER ] )
1592
1593 m_grid->AutoSizeColumn( COL_NAME );
1594
1595 if( m_grid->GetColSize( COL_NAME ) < m_originalColWidths[ COL_NAME ] )
1596 m_grid->SetColSize( COL_NAME, m_originalColWidths[ COL_NAME ] );
1597
1598 // If the grid is still wider than the columns, then stretch the Number and Name columns
1599 // to fit.
1600 for( int i = 0; i < COL_COUNT; ++i )
1601 width -= m_grid->GetColSize( i );
1602
1603 if( width > 0 )
1604 {
1605 m_grid->SetColSize( COL_NUMBER, m_grid->GetColSize( COL_NUMBER ) + width / 2 );
1606 m_grid->SetColSize( COL_NAME, m_grid->GetColSize( COL_NAME ) + width / 2 );
1607 }
1608}
1609
1610
1611void DIALOG_LIB_EDIT_PIN_TABLE::OnSize( wxSizeEvent& event )
1612{
1613 wxSize new_size = event.GetSize();
1614
1615 if( m_initialized && m_size != new_size )
1616 {
1617 m_size = new_size;
1618
1620 }
1621
1622 // Always propagate for a grid repaint (needed if the height changes, as well as width)
1623 event.Skip();
1624}
1625
1626
1627void DIALOG_LIB_EDIT_PIN_TABLE::OnUpdateUI( wxUpdateUIEvent& event )
1628{
1629 std::bitset<64> columnsShown = m_grid->GetShownColumns();
1630
1631 if( columnsShown != m_columnsShown )
1632 {
1633 m_columnsShown = columnsShown;
1634
1635 if( !m_grid->IsCellEditControlShown() )
1637 }
1638
1639 int firstSelectedRow;
1640 int selectedRowCount;
1641
1642 getSelectedArea( m_grid, &firstSelectedRow, &selectedRowCount );
1643
1644 if( ( selectedRowCount > 1 ) != m_groupSelected->IsEnabled() )
1645 m_groupSelected->Enable( selectedRowCount > 1 );
1646}
1647
1648
1649void DIALOG_LIB_EDIT_PIN_TABLE::OnCancel( wxCommandEvent& event )
1650{
1651 Close();
1652}
1653
1654
1655void DIALOG_LIB_EDIT_PIN_TABLE::OnClose( wxCloseEvent& event )
1656{
1657 // This is a cancel, so commit quietly as we're going to throw the results away anyway.
1658 m_grid->CommitPendingChanges( true );
1659
1660 int retval = wxID_CANCEL;
1661
1662 if( m_dataModel->IsEdited() )
1663 {
1664 if( HandleUnsavedChanges( this, _( "Save changes?" ),
1665 [&]() -> bool
1666 {
1668 {
1669 retval = wxID_OK;
1670 return true;
1671 }
1672
1673 return false;
1674 } ) )
1675 {
1676 if( IsQuasiModal() )
1677 EndQuasiModal( retval );
1678 else
1679 EndDialog( retval );
1680
1681 return;
1682 }
1683 else
1684 {
1685 event.Veto();
1686 return;
1687 }
1688 }
1689
1690 // No change in dialog: we can close it
1691 if( IsQuasiModal() )
1692 EndQuasiModal( retval );
1693 else
1694 EndDialog( retval );
1695}
1696
1697
1699{
1700 PIN_NUMBERS pinNumbers;
1701
1702 for( SCH_PIN* pin : m_pins )
1703 {
1704 if( pin->GetNumber().Length() )
1705 pinNumbers.insert( pin->GetNumber() );
1706 }
1707
1708 m_pin_numbers_summary->SetLabel( pinNumbers.GetSummary() );
1709 m_pin_count->SetLabel( wxString::Format( wxT( "%u" ), (unsigned) m_pins.size() ) );
1710 m_duplicate_pins->SetLabel( pinNumbers.GetDuplicates() );
1711
1712 Layout();
1713}
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:774
static wxString LetterSubReference(int aUnit, wxChar aInitialLetter)
const std::vector< wxString > & GetBodyStyleNames() const
Definition lib_symbol.h:787
bool HasDeMorganBodyStyles() const override
Definition lib_symbol.h:784
bool IsMultiUnit() const override
Definition lib_symbol.h:770
int GetBodyStyleCount() const override
Definition lib_symbol.h:776
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:258
virtual wxString GetBodyStyleDescription(int aBodyStyle, bool aLabel) const
Definition sch_item.cpp:186
int GetBodyStyle() const
Definition sch_item.h:247
virtual wxString GetUnitDisplayName(int aUnit, bool aLabel) const
Definition sch_item.cpp:175
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:667
int GetLength() const
Definition sch_pin.cpp:299
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:630
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:1226
void SetOrientation(PIN_ORIENTATION aOrientation)
Definition sch_pin.h:93
void SetName(const wxString &aName)
Definition sch_pin.cpp:409
bool IsVisible() const
Definition sch_pin.cpp:377
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:251
const wxString & GetName() const
Definition sch_pin.cpp:391
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:264
void SetNumberTextSize(int aSize)
Definition sch_pin.cpp:681
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:643
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:657
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:78
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:281
void AllowNetworkFileSystems(wxDialog *aDialog)
Configure a file dialog to show network and virtual file systems.
Definition wxgtk/ui.cpp:717
#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.