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 m_bMenu->SetBitmap( KiBitmapBundle( BITMAPS::config ) );
1154
1155 GetSizer()->SetSizeHints(this);
1156 Centre();
1157
1158 if( aSymbol->IsMultiUnit() )
1159 {
1160 m_unitFilter->Append( wxGetTranslation( UNITS_ALL ) );
1161
1162 for( int ii = 0; ii < aSymbol->GetUnitCount(); ++ii )
1163 m_unitFilter->Append( LIB_SYMBOL::LetterSubReference( ii + 1, 'A' ) );
1164
1165 m_unitFilter->SetSelection( -1 );
1166 }
1167 else
1168 {
1169 m_cbFilterByUnit->Enable( false );
1170 m_unitFilter->Enable( false );
1171 }
1172
1173 if( aSymbol->HasDeMorganBodyStyles() )
1174 {
1175 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALL ) );
1176 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_STD ) );
1177 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALT ) );
1178
1179 m_bodyStyleFilter->SetSelection( -1 );
1180 }
1181 else if( aSymbol->IsMultiBodyStyle() )
1182 {
1183 m_bodyStyleFilter->Append( wxGetTranslation( DEMORGAN_ALL ) );
1184
1185 for( const wxString& bodyStyle : aSymbol->GetBodyStyleNames() )
1186 m_bodyStyleFilter->Append( bodyStyle );
1187
1188 m_bodyStyleFilter->SetSelection( -1 );
1189 }
1190 else
1191 {
1192 m_cbFilterByBodyStyle->Enable( false );
1193 m_bodyStyleFilter->Enable( false );
1194 }
1195
1197
1198 if( !parent->IsSymbolEditable() || parent->IsSymbolAlias() )
1199 {
1200 m_ButtonsCancel->SetDefault();
1201 m_ButtonsOK->SetLabel( _( "Read Only" ) );
1202 m_ButtonsOK->Enable( false );
1203 }
1204
1205 m_initialized = true;
1206 m_modified = false;
1207
1208 // Connect Events
1209 m_grid->Connect( wxEVT_GRID_COL_SORT, wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr,
1210 this );
1211}
1212
1213
1215{
1216 // Disconnect Events
1217 m_grid->Disconnect( wxEVT_GRID_COL_SORT, wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr,
1218 this );
1219
1220 // Prevents crash bug in wxGrid's d'tor
1221 m_grid->DestroyTable( m_dataModel );
1222
1223 // Delete the GRID_TRICKS.
1224 m_grid->PopEventHandler( true );
1225
1226 // This is our copy of the pins. If they were transferred to the part on an OK, then
1227 // m_pins will already be empty.
1228 for( SCH_PIN* pin : m_pins )
1229 delete pin;
1230
1231 WINDOW_THAWER thawer( m_editFrame );
1232
1233 m_editFrame->ClearFocus();
1234 m_editFrame->GetCanvas()->Refresh();
1235}
1236
1237
1239{
1240 // Make a copy of the pins for editing
1241 std::vector<SCH_PIN*> pins = m_symbol->GetGraphicalPins( 0, 0 );
1242
1243 for( SCH_PIN* pin : pins )
1244 m_pins.push_back( new SCH_PIN( *pin ) );
1245
1246 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
1247
1248 updateSummary();
1249
1250 return true;
1251}
1252
1253
1255{
1256 if( !m_grid->CommitPendingChanges() )
1257 return false;
1258
1259 // Delete the part's pins
1260 std::vector<SCH_PIN*> pins = m_symbol->GetGraphicalPins( 0, 0 );
1261
1262 for( SCH_PIN* pin : pins )
1263 m_symbol->RemoveDrawItem( pin );
1264
1265 // Transfer our pins to the part
1266 for( SCH_PIN* pin : m_pins )
1267 m_symbol->AddDrawItem( pin );
1268
1269 m_pins.clear();
1270
1271 return true;
1272}
1273
1274
1275void DIALOG_LIB_EDIT_PIN_TABLE::OnColSort( wxGridEvent& aEvent )
1276{
1277 int sortCol = aEvent.GetCol();
1278 bool ascending;
1279
1280 // This is bonkers, but wxWidgets doesn't tell us ascending/descending in the
1281 // event, and if we ask it will give us pre-event info.
1282 if( m_grid->IsSortingBy( sortCol ) )
1283 // same column; invert ascending
1284 ascending = !m_grid->IsSortOrderAscending();
1285 else
1286 // different column; start with ascending
1287 ascending = true;
1288
1289 m_dataModel->SortRows( sortCol, ascending );
1290}
1291
1292
1293void DIALOG_LIB_EDIT_PIN_TABLE::OnAddRow( wxCommandEvent& event )
1294{
1295 m_grid->OnAddRow(
1296 [&]() -> std::pair<int, int>
1297 {
1298 SCH_PIN* newPin = new SCH_PIN( this->m_symbol );
1299
1300 // Copy the settings of the last pin onto the new pin.
1301 if( m_pins.size() > 0 )
1302 {
1303 SCH_PIN* last = m_pins.back();
1304
1305 newPin->SetOrientation( last->GetOrientation() );
1306 newPin->SetType( last->GetType() );
1307 newPin->SetShape( last->GetShape() );
1308 newPin->SetUnit( last->GetUnit() );
1309 newPin->SetBodyStyle( last->GetBodyStyle() );
1310
1311 VECTOR2I pos = last->GetPosition();
1312
1313 SYMBOL_EDITOR_SETTINGS* cfg = m_editFrame->GetSettings();
1314
1317 {
1318 pos.y -= schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
1319 }
1320 else
1321 {
1322 pos.x += schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
1323 }
1324
1325 newPin->SetPosition( pos );
1326 }
1327
1328 m_pins.push_back( newPin );
1329
1330 m_dataModel->AppendRow( m_pins[ m_pins.size() - 1 ] );
1331 updateSummary();
1332
1333 return { m_dataModel->GetNumberRows() - 1, COL_NUMBER };
1334 } );
1335}
1336
1337
1339{
1340 m_pins.push_back( pin );
1341 updateSummary();
1342}
1343
1344
1345void DIALOG_LIB_EDIT_PIN_TABLE::OnDeleteRow( wxCommandEvent& event )
1346{
1347 m_grid->OnDeleteRows(
1348 [&]( int row )
1349 {
1350 std::vector<SCH_PIN*> removedRow = m_dataModel->RemoveRow( row );
1351
1352 for( SCH_PIN* pin : removedRow )
1353 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1354 } );
1355
1356 updateSummary();
1357}
1358
1359
1361{
1362 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1363 updateSummary();
1364}
1365
1366
1368{
1369 updateSummary();
1370}
1371
1372
1374{
1375 SCH_PIN* pin = nullptr;
1376
1377 if( event.GetRow() >= 0 && event.GetRow() < m_dataModel->GetNumberRows() )
1378 {
1379 const std::vector<SCH_PIN*>& pins = m_dataModel->GetRowPins( event.GetRow() );
1380
1381 if( pins.size() == 1 && m_editFrame->GetCurSymbol() )
1382 {
1383 for( SCH_PIN* candidate : m_editFrame->GetCurSymbol()->GetGraphicalPins( 0, 0 ) )
1384 {
1385 if( candidate->GetNumber() == pins.at( 0 )->GetNumber() )
1386 {
1387 pin = candidate;
1388 break;
1389 }
1390 }
1391 }
1392 }
1393
1394 SYMBOL_EDITOR_SETTINGS* cfg = static_cast<SYMBOL_EDITOR_SETTINGS*>( m_editFrame->config() );
1395
1397 {
1398 WINDOW_THAWER thawer( m_editFrame );
1399
1400 m_editFrame->FocusOnItem( pin );
1401 m_editFrame->GetCanvas()->Refresh();
1402 }
1403}
1404
1405
1407{
1408 return m_cbGroup->GetValue();
1409}
1410
1411
1413{
1414 m_cbGroup->SetValue( false );
1415
1416 m_dataModel->RebuildRows( m_pins, false, true );
1417
1418 m_grid->ShowCol( COL_PIN_COUNT );
1419 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1420
1422}
1423
1424
1426{
1427 if( !m_grid->CommitPendingChanges() )
1428 return;
1429
1430 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
1431
1432 if( m_cbGroup->GetValue() )
1433 {
1434 m_grid->ShowCol( COL_PIN_COUNT );
1435 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1436 }
1437
1439}
1440
1441
1442void DIALOG_LIB_EDIT_PIN_TABLE::OnMenu( wxCommandEvent& event )
1443{
1444 SYMBOL_EDITOR_SETTINGS* cfg = static_cast<SYMBOL_EDITOR_SETTINGS*>( m_editFrame->config() );
1445
1446 // Build a pop menu:
1447 wxMenu menu;
1448
1449 menu.Append( 4206, _( "Highlight on Cross-probe" ),
1450 _( "Highlight corresponding pin on canvas when it is selected in the table" ),
1451 wxITEM_CHECK );
1452 menu.Check( 4206, cfg->m_PinTable.crossprobe_on_selection );
1453
1454 // menu_id is the selected submenu id from the popup menu or wxID_NONE
1455 int menu_id = m_bMenu->GetPopupMenuSelectionFromUser( menu );
1456
1457 if( menu_id == 0 || menu_id == 4206 )
1459}
1460
1461
1463{
1464 if( event.GetEventObject() == m_cbFilterByUnit )
1465 {
1466 if( event.IsChecked() )
1467 {
1468 if( m_unitFilter->GetSelection() == -1 )
1469 m_unitFilter->SetSelection( 0 );
1470
1471 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1472 }
1473 else
1474 {
1475 m_dataModel->SetUnitFilter( -1 );
1476 m_unitFilter->SetSelection( -1 );
1477 }
1478 }
1479 else if( event.GetEventObject() == m_cbFilterByBodyStyle )
1480 {
1481 if( event.IsChecked() )
1482 {
1483 if( m_bodyStyleFilter->GetSelection() == -1 )
1484 m_bodyStyleFilter->SetSelection( 0 );
1485
1486 m_dataModel->SetBodyStyleFilter( m_bodyStyleFilter->GetSelection() );
1487 }
1488 else
1489 {
1490 m_dataModel->SetBodyStyleFilter( -1 );
1491 m_bodyStyleFilter->SetSelection( -1 );
1492 }
1493 }
1494 else if( event.GetEventObject() == m_cbFilterSelected )
1495 {
1496 m_dataModel->SetFilterBySelection( event.IsChecked() );
1497 }
1498
1499 OnRebuildRows( event );
1500}
1501
1502
1504{
1505 if( event.GetEventObject() == m_unitFilter )
1506 {
1507 m_cbFilterByUnit->SetValue( true );
1508 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1509 }
1510 else if( event.GetEventObject() == m_bodyStyleFilter )
1511 {
1512 m_cbFilterByBodyStyle->SetValue( true );
1513 m_dataModel->SetBodyStyleFilter( m_bodyStyleFilter->GetSelection() );
1514 }
1515
1516 OnRebuildRows( event );
1517}
1518
1519
1521{
1522 bool fromFile = event.GetEventObject() == m_btnImportFromFile;
1523 bool replaceAll = m_rbReplaceAll->GetValue();
1524
1525 WX_STRING_REPORTER reporter;
1526 PIN_TABLE_IMPORT importer( *m_editFrame, reporter );
1527
1528 std::vector<std::unique_ptr<SCH_PIN>> newPins = importer.ImportData( fromFile, *m_symbol );
1529
1530 if( reporter.HasMessage() )
1531 {
1532 int ret = wxMessageBox( reporter.GetMessages(), _( "Errors" ), wxOK | wxCANCEL | wxICON_ERROR, this );
1533
1534 // Give the user the option to cancel the import on errors.
1535 if( ret == wxCANCEL )
1536 return;
1537 }
1538
1539 if( !newPins.size() )
1540 return;
1541
1542 if( replaceAll )
1543 {
1544 // This is quite a dance with a segfault without smart pointers
1545 for( SCH_PIN* pin : m_pins )
1546 delete pin;
1547
1548 m_pins.clear();
1549 }
1550
1551 for( auto& newPin : newPins )
1552 m_pins.push_back( newPin.release() );
1553
1554 m_cbGroup->SetValue( false );
1555 m_dataModel->RebuildRows( m_pins, false, false );
1556
1557 updateSummary();
1558}
1559
1560
1562{
1563 bool toFile = event.GetEventObject() == m_btnExportToFile;
1564 bool onlyShown = m_rbExportOnlyShownPins->GetValue();
1565
1566 wxString filePath = wxEmptyString;
1567
1568 if( toFile )
1569 {
1570 wxFileName fn( m_symbol->GetName() );
1571 fn.SetExt( FILEEXT::CsvFileExtension );
1572 wxFileDialog dlg( this, _( "Select pin data file" ), "", fn.GetFullName(), FILEEXT::CsvFileWildcard(),
1573 wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
1574
1576
1577 if( dlg.ShowModal() == wxID_CANCEL )
1578 return;
1579
1580 filePath = dlg.GetPath();
1581 }
1582
1583 std::vector<SCH_PIN*> pinsToExport;
1584
1585 if( onlyShown )
1586 {
1587 for( int i = 0; i < m_dataModel->GetNumberRows(); ++i )
1588 {
1589 for( SCH_PIN* pin : m_dataModel->GetRowPins( i ) )
1590 pinsToExport.push_back( pin );
1591 }
1592 }
1593 else
1594 {
1595 pinsToExport = m_pins;
1596 }
1597
1598 PIN_TABLE_EXPORT exporter( *m_editFrame );
1599 exporter.ExportData( pinsToExport, filePath );
1600}
1601
1602
1604{
1605 // Account for scroll bars
1607
1608 wxGridUpdateLocker deferRepaintsTillLeavingScope;
1609
1610 // The Number and Name columns must be at least wide enough to hold their contents, but
1611 // no less wide than their original widths.
1612 m_grid->AutoSizeColumn( COL_NUMBER );
1613
1614 if( m_grid->GetColSize( COL_NUMBER ) < m_originalColWidths[ COL_NUMBER ] )
1616
1617 m_grid->AutoSizeColumn( COL_NAME );
1618
1619 if( m_grid->GetColSize( COL_NAME ) < m_originalColWidths[ COL_NAME ] )
1620 m_grid->SetColSize( COL_NAME, m_originalColWidths[ COL_NAME ] );
1621
1622 // If the grid is still wider than the columns, then stretch the Number and Name columns
1623 // to fit.
1624 for( int i = 0; i < COL_COUNT; ++i )
1625 width -= m_grid->GetColSize( i );
1626
1627 if( width > 0 )
1628 {
1629 m_grid->SetColSize( COL_NUMBER, m_grid->GetColSize( COL_NUMBER ) + width / 2 );
1630 m_grid->SetColSize( COL_NAME, m_grid->GetColSize( COL_NAME ) + width / 2 );
1631 }
1632}
1633
1634
1635void DIALOG_LIB_EDIT_PIN_TABLE::OnSize( wxSizeEvent& event )
1636{
1637 wxSize new_size = event.GetSize();
1638
1639 if( m_initialized && m_size != new_size )
1640 {
1641 m_size = new_size;
1642
1644 }
1645
1646 // Always propagate for a grid repaint (needed if the height changes, as well as width)
1647 event.Skip();
1648}
1649
1650
1651void DIALOG_LIB_EDIT_PIN_TABLE::OnUpdateUI( wxUpdateUIEvent& event )
1652{
1653 std::bitset<64> columnsShown = m_grid->GetShownColumns();
1654
1655 if( columnsShown != m_columnsShown )
1656 {
1657 m_columnsShown = columnsShown;
1658
1659 if( !m_grid->IsCellEditControlShown() )
1661 }
1662
1663 int firstSelectedRow;
1664 int selectedRowCount;
1665
1666 getSelectedArea( m_grid, &firstSelectedRow, &selectedRowCount );
1667
1668 if( ( selectedRowCount > 1 ) != m_groupSelected->IsEnabled() )
1669 m_groupSelected->Enable( selectedRowCount > 1 );
1670}
1671
1672
1673void DIALOG_LIB_EDIT_PIN_TABLE::OnCancel( wxCommandEvent& event )
1674{
1675 Close();
1676}
1677
1678
1679void DIALOG_LIB_EDIT_PIN_TABLE::OnClose( wxCloseEvent& event )
1680{
1681 // This is a cancel, so commit quietly as we're going to throw the results away anyway.
1682 m_grid->CommitPendingChanges( true );
1683
1684 int retval = wxID_CANCEL;
1685
1686 if( m_dataModel->IsEdited() )
1687 {
1688 if( HandleUnsavedChanges( this, _( "Save changes?" ),
1689 [&]() -> bool
1690 {
1692 {
1693 retval = wxID_OK;
1694 return true;
1695 }
1696
1697 return false;
1698 } ) )
1699 {
1700 if( IsQuasiModal() )
1701 EndQuasiModal( retval );
1702 else
1703 EndDialog( retval );
1704
1705 return;
1706 }
1707 else
1708 {
1709 event.Veto();
1710 return;
1711 }
1712 }
1713
1714 // No change in dialog: we can close it
1715 if( IsQuasiModal() )
1716 EndQuasiModal( retval );
1717 else
1718 EndDialog( retval );
1719}
1720
1721
1723{
1724 PIN_NUMBERS pinNumbers;
1725
1726 for( SCH_PIN* pin : m_pins )
1727 {
1728 if( pin->GetNumber().Length() )
1729 pinNumbers.insert( pin->GetNumber() );
1730 }
1731
1732 m_pin_numbers_summary->SetLabel( pinNumbers.GetSummary() );
1733 m_pin_count->SetLabel( wxString::Format( wxT( "%u" ), (unsigned) m_pins.size() ) );
1734 m_duplicate_pins->SetLabel( pinNumbers.GetDuplicates() );
1735
1736 Layout();
1737}
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 OnMenu(wxCommandEvent &event) override
void OnCellSelected(wxGridEvent &event) override
void OnAddRow(wxCommandEvent &event) override
void OnDeleteRow(wxCommandEvent &event) override
void OnCellEdited(wxGridEvent &event) override
std::vector< SCH_PIN * > m_pins
void OnFilterCheckBox(wxCommandEvent &event) override
void OnFilterChoice(wxCommandEvent &event) override
void 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:247
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:260
virtual wxString GetBodyStyleDescription(int aBodyStyle, bool aLabel) const
Definition sch_item.cpp:188
int GetBodyStyle() const
Definition sch_item.h:248
virtual wxString GetUnitDisplayName(int aUnit, bool aLabel) const
Definition sch_item.cpp:177
int GetUnit() const
Definition sch_item.h:239
virtual void SetUnit(int aUnit)
Definition sch_item.h:238
int GetNumberTextSize() const
Definition sch_pin.cpp:681
int GetLength() const
Definition sch_pin.cpp:301
void SetNumber(const wxString &aNumber)
Definition sch_pin.cpp:644
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:1240
void SetOrientation(PIN_ORIENTATION aOrientation)
Definition sch_pin.h:93
void SetName(const wxString &aName)
Definition sch_pin.cpp:423
bool IsVisible() const
Definition sch_pin.cpp:391
void SetPosition(const VECTOR2I &aPos) override
Definition sch_pin.h:251
const wxString & GetName() const
Definition sch_pin.cpp:405
PIN_ORIENTATION GetOrientation() const
Definition sch_pin.cpp:266
void SetNumberTextSize(int aSize)
Definition sch_pin.cpp:695
void SetShape(GRAPHIC_PINSHAPE aShape)
Definition sch_pin.h:96
VECTOR2I GetPosition() const override
Definition sch_pin.cpp:258
int GetNameTextSize() const
Definition sch_pin.cpp:657
void SetType(ELECTRICAL_PINTYPE aType)
Definition sch_pin.cpp:335
const wxString & GetNumber() const
Definition sch_pin.h:124
GRAPHIC_PINSHAPE GetShape() const
Definition sch_pin.cpp:280
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:315
void SetNameTextSize(int aSize)
Definition sch_pin.cpp:671
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:435
#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.