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 (C) 1992-2022 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#include "grid_tricks.h"
26#include "lib_pin.h"
27#include "pin_numbers.h"
28#include "pgm_base.h"
29#include <base_units.h>
30#include <bitmaps.h>
31#include <confirm.h>
32#include <symbol_edit_frame.h>
34#include <kiplatform/ui.h>
37#include <widgets/wx_grid.h>
41#include <wx/tokenzr.h>
42#include <string_utils.h>
43
44#define UNITS_ALL _( "ALL" )
45#define DEMORGAN_ALL _( "ALL" )
46#define DEMORGAN_STD _( "Standard" )
47#define DEMORGAN_ALT _( "Alternate" )
48
49
50void getSelectedArea( WX_GRID* aGrid, int* aRowStart, int* aRowCount )
51{
52 wxGridCellCoordsArray topLeft = aGrid->GetSelectionBlockTopLeft();
53 wxGridCellCoordsArray botRight = aGrid->GetSelectionBlockBottomRight();
54
55 wxArrayInt cols = aGrid->GetSelectedCols();
56 wxArrayInt rows = aGrid->GetSelectedRows();
57
58 if( topLeft.Count() && botRight.Count() )
59 {
60 *aRowStart = topLeft[0].GetRow();
61 *aRowCount = botRight[0].GetRow() - *aRowStart + 1;
62 }
63 else if( cols.Count() )
64 {
65 *aRowStart = 0;
66 *aRowCount = aGrid->GetNumberRows();
67 }
68 else if( rows.Count() )
69 {
70 *aRowStart = rows[0];
71 *aRowCount = rows.Count();
72 }
73 else
74 {
75 *aRowStart = aGrid->GetGridCursorRow();
76 *aRowCount = *aRowStart >= 0 ? 1 : 0;
77 }
78}
79
80
81class PIN_TABLE_DATA_MODEL : public wxGridTableBase
82{
83public:
85 LIB_SYMBOL* aSymbol ) :
86 m_frame( aFrame ),
87 m_unitFilter( -1 ),
88 m_edited( false ),
89 m_pinTable( aPinTable ),
90 m_symbol( aSymbol )
91 {
92 m_eval = std::make_unique<NUMERIC_EVALUATOR>( m_frame->GetUserUnits() );
93
94 m_frame->Bind( EDA_EVT_UNITS_CHANGED, &PIN_TABLE_DATA_MODEL::onUnitsChanged, this );
95 }
96
98 {
99 m_frame->Unbind( EDA_EVT_UNITS_CHANGED, &PIN_TABLE_DATA_MODEL::onUnitsChanged, this );
100 }
101
102 void onUnitsChanged( wxCommandEvent& aEvent )
103 {
104 if( GetView() )
105 GetView()->ForceRefresh();
106
107 aEvent.Skip();
108 }
109
110 void SetUnitFilter( int aFilter ) { m_unitFilter = aFilter; }
111
112 int GetNumberRows() override { return (int) m_rows.size(); }
113 int GetNumberCols() override { return COL_COUNT; }
114
115 wxString GetColLabelValue( int aCol ) override
116 {
117 switch( aCol )
118 {
119 case COL_PIN_COUNT: return _( "Count" );
120 case COL_NUMBER: return _( "Number" );
121 case COL_NAME: return _( "Name" );
122 case COL_TYPE: return _( "Electrical Type" );
123 case COL_SHAPE: return _( "Graphic Style" );
124 case COL_ORIENTATION: return _( "Orientation" );
125 case COL_NUMBER_SIZE: return _( "Number Text Size" );
126 case COL_NAME_SIZE: return _( "Name Text Size" );
127 case COL_LENGTH: return _( "Length" );
128 case COL_POSX: return _( "X Position" );
129 case COL_POSY: return _( "Y Position" );
130 case COL_VISIBLE: return _( "Visible" );
131 case COL_UNIT: return _( "Unit" );
132 case COL_DEMORGAN: return _( "De Morgan" );
133 default: wxFAIL; return wxEmptyString;
134 }
135 }
136
137 bool IsEmptyCell( int row, int col ) override
138 {
139 return false; // don't allow adjacent cell overflow, even if we are actually empty
140 }
141
142 wxString GetValue( int aRow, int aCol ) override
143 {
144 wxGrid* grid = GetView();
145
146 if( grid->GetGridCursorRow() == aRow && grid->GetGridCursorCol() == aCol
147 && grid->IsCellEditControlShown() )
148 {
149 auto it = m_evalOriginal.find( { m_rows[ aRow ], aCol } );
150
151 if( it != m_evalOriginal.end() )
152 return it->second;
153 }
154
155 return GetValue( m_rows[ aRow ], aCol, m_frame );
156 }
157
158 static wxString GetValue( const LIB_PINS& pins, int aCol, EDA_DRAW_FRAME* aParentFrame )
159 {
160 wxString fieldValue;
161
162 if( pins.empty() )
163 return fieldValue;
164
165 for( LIB_PIN* pin : pins )
166 {
167 wxString val;
168
169 switch( aCol )
170 {
171 case COL_PIN_COUNT:
172 val << pins.size();
173 break;
174
175 case COL_NUMBER:
176 val = pin->GetNumber();
177 break;
178
179 case COL_NAME:
180 val = pin->GetName();
181 break;
182
183 case COL_TYPE:
184 val = PinTypeNames()[static_cast<int>( pin->GetType() )];
185 break;
186
187 case COL_SHAPE:
188 val = PinShapeNames()[static_cast<int>( pin->GetShape() )];
189 break;
190
191 case COL_ORIENTATION:
192 if( PinOrientationIndex( pin->GetOrientation() ) >= 0 )
193 val = PinOrientationNames()[ PinOrientationIndex( pin->GetOrientation() ) ];
194
195 break;
196
197 case COL_NUMBER_SIZE:
198 val = aParentFrame->StringFromValue( pin->GetNumberTextSize(), true );
199 break;
200
201 case COL_NAME_SIZE:
202 val = aParentFrame->StringFromValue( pin->GetNameTextSize(), true );
203 break;
204
205 case COL_LENGTH:
206 val = aParentFrame->StringFromValue( pin->GetLength(), true );
207 break;
208
209 case COL_POSX:
210 val = aParentFrame->StringFromValue( pin->GetPosition().x, true );
211 break;
212
213 case COL_POSY:
214 val = aParentFrame->StringFromValue( -pin->GetPosition().y, true );
215 break;
216
217 case COL_VISIBLE:
218 val = StringFromBool( pin->IsVisible() );
219 break;
220
221 case COL_UNIT:
222 if( pin->GetUnit() )
223 val = LIB_SYMBOL::LetterSubReference( pin->GetUnit(), 'A' );
224 else
225 val = UNITS_ALL;
226 break;
227
228 case COL_DEMORGAN:
229 switch( pin->GetBodyStyle() )
230 {
232 val = DEMORGAN_STD;
233 break;
235 val = DEMORGAN_ALT;
236 break;
237 default:
238 val = DEMORGAN_ALL;
239 break;
240 }
241 break;
242
243 default:
244 wxFAIL;
245 break;
246 }
247
248 if( aCol == COL_NUMBER )
249 {
250 if( fieldValue.length() )
251 fieldValue += wxT( ", " );
252 fieldValue += val;
253 }
254 else
255 {
256 if( !fieldValue.Length() )
257 fieldValue = val;
258 else if( val != fieldValue )
259 fieldValue = INDETERMINATE_STATE;
260 }
261 }
262
263 return fieldValue;
264 }
265
266 void SetValue( int aRow, int aCol, const wxString &aValue ) override
267 {
268 if( aValue == INDETERMINATE_STATE )
269 return;
270
271 wxString value = aValue;
272
273 switch( aCol )
274 {
275 case COL_NUMBER_SIZE:
276 case COL_NAME_SIZE:
277 case COL_LENGTH:
278 case COL_POSX:
279 case COL_POSY:
280 m_eval->SetDefaultUnits( m_frame->GetUserUnits() );
281
282 if( m_eval->Process( value ) )
283 {
284 m_evalOriginal[ { m_rows[ aRow ], aCol } ] = value;
285 value = m_eval->Result();
286 }
287
288 break;
289
290 default:
291 break;
292 }
293
294 LIB_PINS pins = m_rows[ aRow ];
295
296 // If the NUMBER column is edited and the pins are grouped, renumber, and add or
297 // remove pins based on the comma separated list of pins.
298 if( aCol == COL_NUMBER && m_pinTable->IsDisplayGrouped() )
299 {
300 wxStringTokenizer tokenizer( value, "," );
301 size_t i = 0;
302
303 while( tokenizer.HasMoreTokens() )
304 {
305 wxString pinName = tokenizer.GetNextToken();
306
307 // Trim whitespace from both ends of the string
308 pinName.Trim( true ).Trim( false );
309
310 if( i < pins.size() )
311 {
312 // Renumber the existing pins
313 pins.at( i )->SetNumber( pinName );
314 }
315 else
316 {
317 // Create new pins
318 LIB_PIN* newPin = new LIB_PIN( this->m_symbol );
319 LIB_PIN* last = pins.back();
320
321 newPin->SetNumber( pinName );
322 newPin->SetName( last->GetName() );
323 newPin->SetOrientation( last->GetOrientation() );
324 newPin->SetType( last->GetType() );
325 newPin->SetShape( last->GetShape() );
326 newPin->SetUnit( last->GetUnit() );
327
328 VECTOR2I pos = last->GetPosition();
329
330 auto* cfg = Pgm().GetSettingsManager().GetAppSettings<SYMBOL_EDITOR_SETTINGS>();
331
332 if( last->GetOrientation() == PIN_ORIENTATION::PIN_LEFT
333 || last->GetOrientation() == PIN_ORIENTATION::PIN_RIGHT )
334 {
335 pos.y -= schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
336 }
337 else
338 {
339 pos.x += schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
340 }
341
342 newPin->SetPosition( pos );
343
344 pins.push_back( newPin );
345 m_pinTable->AddPin( newPin );
346 }
347
348 i++;
349 }
350
351 while( pins.size() > i )
352 {
353 m_pinTable->RemovePin( pins.back() );
354 pins.pop_back();
355 }
356
357 m_rows[aRow] = pins;
358 m_edited = true;
359
360 return;
361 }
362
363 for( LIB_PIN* pin : pins )
364 {
365 switch( aCol )
366 {
367 case COL_NUMBER:
369 pin->SetNumber( value );
370
371 break;
372
373 case COL_NAME:
374 pin->SetName( value );
375 break;
376
377 case COL_TYPE:
378 if( PinTypeNames().Index( value ) != wxNOT_FOUND )
379 pin->SetType( (ELECTRICAL_PINTYPE) PinTypeNames().Index( value ) );
380
381 break;
382
383 case COL_SHAPE:
384 if( PinShapeNames().Index( value ) != wxNOT_FOUND )
385 pin->SetShape( (GRAPHIC_PINSHAPE) PinShapeNames().Index( value ) );
386
387 break;
388
389 case COL_ORIENTATION:
390 if( PinOrientationNames().Index( value ) != wxNOT_FOUND )
391 pin->SetOrientation( PinOrientationCode( PinOrientationNames().Index( value ) ) );
392 break;
393
394 case COL_NUMBER_SIZE:
395 pin->SetNumberTextSize( m_frame->ValueFromString( value ) );
396 break;
397
398 case COL_NAME_SIZE:
399 pin->SetNameTextSize( m_frame->ValueFromString( value ) );
400 break;
401
402 case COL_LENGTH:
403 pin->ChangeLength( m_frame->ValueFromString( value ) );
404 break;
405
406 case COL_POSX:
407 pin->SetPosition( VECTOR2I( m_frame->ValueFromString( value ),
408 pin->GetPosition().y ) );
409 break;
410
411 case COL_POSY:
412 pin->SetPosition( VECTOR2I( pin->GetPosition().x,
413 -m_frame->ValueFromString( value ) ) );
414 break;
415
416 case COL_VISIBLE:
417 pin->SetVisible(BoolFromString( value ));
418 break;
419
420 case COL_UNIT:
421 if( value == UNITS_ALL )
422 {
423 pin->SetUnit( 0 );
424 }
425 else
426 {
427 for( int i = 1; i <= m_symbol->GetUnitCount(); i++ )
428 {
429 if( value == LIB_SYMBOL::LetterSubReference( i, 'A' ) )
430 {
431 pin->SetUnit( i );
432 break;
433 }
434 }
435 }
436 break;
437
438 case COL_DEMORGAN:
439 if( value == DEMORGAN_STD )
440 pin->SetBodyStyle( 1 );
441 else if( value == DEMORGAN_ALT )
442 pin->SetBodyStyle( 2 );
443 else
444 pin->SetBodyStyle( 0 );
445 break;
446
447 default:
448 wxFAIL;
449 break;
450 }
451 }
452
453 m_edited = true;
454 }
455
456 static int findRow( const std::vector<LIB_PINS>& aRowSet, const wxString& aName )
457 {
458 for( size_t i = 0; i < aRowSet.size(); ++i )
459 {
460 if( aRowSet[ i ][ 0 ] && aRowSet[ i ][ 0 ]->GetName() == aName )
461 return i;
462 }
463
464 return -1;
465 }
466
467 static bool compare( const LIB_PINS& lhs, const LIB_PINS& rhs, int sortCol, bool ascending,
468 EDA_DRAW_FRAME* parentFrame )
469 {
470 wxString lhStr = GetValue( lhs, sortCol, parentFrame );
471 wxString rhStr = GetValue( rhs, sortCol, parentFrame );
472
473 if( lhStr == rhStr )
474 {
475 // Secondary sort key is always COL_NUMBER
476 sortCol = COL_NUMBER;
477 lhStr = GetValue( lhs, sortCol, parentFrame );
478 rhStr = GetValue( rhs, sortCol, parentFrame );
479 }
480
481 bool res;
482
483 // N.B. To meet the iterator sort conditions, we cannot simply invert the truth
484 // to get the opposite sort. i.e. ~(a<b) != (a>b)
485 auto cmp = [ ascending ]( const auto a, const auto b )
486 {
487 if( ascending )
488 return a < b;
489 else
490 return b < a;
491 };
492
493 switch( sortCol )
494 {
495 case COL_NUMBER:
496 case COL_NAME:
497 res = cmp( PIN_NUMBERS::Compare( lhStr, rhStr ), 0 );
498 break;
499 case COL_NUMBER_SIZE:
500 case COL_NAME_SIZE:
501 res = cmp( parentFrame->ValueFromString( lhStr ),
502 parentFrame->ValueFromString( rhStr ) );
503 break;
504 case COL_LENGTH:
505 case COL_POSX:
506 case COL_POSY:
507 res = cmp( parentFrame->ValueFromString( lhStr ),
508 parentFrame->ValueFromString( rhStr ) );
509 break;
510 case COL_VISIBLE:
511 case COL_DEMORGAN:
512 default:
513 res = cmp( StrNumCmp( lhStr, rhStr ), 0 );
514 break;
515 }
516
517 return res;
518 }
519
520 void RebuildRows( const LIB_PINS& aPins, bool groupByName, bool groupBySelection )
521 {
522 WX_GRID* grid = dynamic_cast<WX_GRID*>( GetView() );
523 std::vector<LIB_PIN*> clear_flags;
524
525 clear_flags.reserve( aPins.size() );
526
527 if( grid )
528 {
529 if( groupBySelection )
530 {
531 for( LIB_PIN* pin : aPins )
532 pin->ClearTempFlags();
533
534 int firstSelectedRow;
535 int selectedRowCount;
536
537 getSelectedArea( grid, &firstSelectedRow, &selectedRowCount );
538
539 for( int ii = 0; ii < selectedRowCount; ++ii )
540 {
541 for( LIB_PIN* pin : m_rows[ firstSelectedRow + ii ] )
542 {
543 pin->SetFlags( CANDIDATE );
544 clear_flags.push_back( pin );
545 }
546 }
547 }
548
549 // Commit any pending in-place edits before the row gets moved out from under
550 // the editor.
551 grid->CommitPendingChanges( true );
552
553 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, 0, m_rows.size() );
554 GetView()->ProcessTableMessage( msg );
555 }
556
557 m_rows.clear();
558
559 if( groupBySelection )
560 m_rows.emplace_back( LIB_PINS() );
561
562 for( LIB_PIN* pin : aPins )
563 {
564 if( m_unitFilter == -1 || pin->GetUnit() == 0 || pin->GetUnit() == m_unitFilter )
565 {
566 int rowIndex = -1;
567
568 if( groupByName )
569 rowIndex = findRow( m_rows, pin->GetName() );
570 else if( groupBySelection && ( pin->GetFlags() & CANDIDATE ) )
571 rowIndex = 0;
572
573 if( rowIndex < 0 )
574 {
575 m_rows.emplace_back( LIB_PINS() );
576 rowIndex = m_rows.size() - 1;
577 }
578
579 m_rows[ rowIndex ].push_back( pin );
580 }
581 }
582
583 int sortCol = 0;
584 bool ascending = true;
585
586 if( GetView() && GetView()->GetSortingColumn() != wxNOT_FOUND )
587 {
588 sortCol = GetView()->GetSortingColumn();
589 ascending = GetView()->IsSortOrderAscending();
590 }
591
592 for( LIB_PINS& row : m_rows )
593 SortPins( row );
594
595 if( !groupBySelection )
596 SortRows( sortCol, ascending );
597
598 if ( GetView() )
599 {
600 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_rows.size() );
601 GetView()->ProcessTableMessage( msg );
602
603 if( groupBySelection )
604 GetView()->SelectRow( 0 );
605 }
606
607 for( LIB_PIN* pin : clear_flags )
608 pin->ClearFlags( CANDIDATE );
609 }
610
611 void SortRows( int aSortCol, bool ascending )
612 {
613 std::sort( m_rows.begin(), m_rows.end(),
614 [ aSortCol, ascending, this ]( const LIB_PINS& lhs, const LIB_PINS& rhs ) -> bool
615 {
616 return compare( lhs, rhs, aSortCol, ascending, m_frame );
617 } );
618 }
619
620 void SortPins( LIB_PINS& aRow )
621 {
622 std::sort( aRow.begin(), aRow.end(),
623 []( LIB_PIN* lhs, LIB_PIN* rhs ) -> bool
624 {
625 return PIN_NUMBERS::Compare( lhs->GetNumber(), rhs->GetNumber() ) < 0;
626 } );
627 }
628
629 void AppendRow( LIB_PIN* aPin )
630 {
631 LIB_PINS row;
632 row.push_back( aPin );
633 m_rows.push_back( row );
634
635 if ( GetView() )
636 {
637 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
638 GetView()->ProcessTableMessage( msg );
639 }
640 }
641
642 LIB_PINS RemoveRow( int aRow )
643 {
644 LIB_PINS removedRow = m_rows[ aRow ];
645
646 m_rows.erase( m_rows.begin() + aRow );
647
648 if ( GetView() )
649 {
650 wxGridTableMessage msg( this, wxGRIDTABLE_NOTIFY_ROWS_DELETED, aRow, 1 );
651 GetView()->ProcessTableMessage( msg );
652 }
653
654 return removedRow;
655 }
656
658 {
659 return m_rows[ aRow ];
660 }
661
662 bool IsEdited()
663 {
664 return m_edited;
665 }
666
667private:
668 static wxString StringFromBool( bool aValue )
669 {
670 if( aValue )
671 return wxT( "1" );
672 else
673 return wxT( "0" );
674 }
675
676 static bool BoolFromString( wxString aValue )
677 {
678 if( aValue == wxS( "1" ) )
679 {
680 return true;
681 }
682 else if( aValue == wxS( "0" ) )
683 {
684 return false;
685 }
686 else
687 {
688 wxFAIL_MSG( wxString::Format( "string '%s' can't be converted to boolean correctly, "
689 "it will have been perceived as FALSE",
690 aValue ) );
691 return false;
692 }
693 }
694
695private:
697
698 // Because the rows of the grid can either be a single pin or a group of pins, the
699 // data model is a 2D vector. If we're in the single pin case, each row's LIB_PINS
700 // contains only a single pin.
701 std::vector<LIB_PINS> m_rows;
702 int m_unitFilter; // 0 to show pins for all units
703
705
707 LIB_SYMBOL* m_symbol; // Parent symbol that the pins belong to.
708
709 std::unique_ptr<NUMERIC_EVALUATOR> m_eval;
710 std::map< std::pair<LIB_PINS, int>, wxString > m_evalOriginal;
711};
712
713
715 LIB_SYMBOL* aSymbol ) :
717 m_editFrame( parent ),
718 m_symbol( aSymbol )
719{
721
722 // Save original columns widths so we can do proportional sizing.
723 for( int i = 0; i < COL_COUNT; ++i )
724 m_originalColWidths[ i ] = m_grid->GetColSize( i );
725
726 // Give a bit more room for combobox editors
727 m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
728
730 m_grid->PushEventHandler( new GRID_TRICKS( m_grid, [this]( wxCommandEvent& aEvent )
731 {
732 OnAddRow( aEvent );
733 } ) );
734
735 // Show/hide columns according to the user's preference
736 if( SYMBOL_EDITOR_SETTINGS* cfg = parent->GetSettings() )
737 {
738 m_grid->ShowHideColumns( cfg->m_PinTableVisibleColumns );
740 }
741
742 // Set special attributes
743 wxGridCellAttr* attr;
744
745 attr = new wxGridCellAttr;
746 attr->SetReadOnly( true );
747 m_grid->SetColAttr( COL_PIN_COUNT, attr );
748
749 attr = new wxGridCellAttr;
750 wxArrayString typeNames = PinTypeNames();
751 typeNames.push_back( INDETERMINATE_STATE );
752 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinTypeIcons(), typeNames ) );
753 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinTypeIcons(), typeNames ) );
754 m_grid->SetColAttr( COL_TYPE, attr );
755
756 attr = new wxGridCellAttr;
757 wxArrayString shapeNames = PinShapeNames();
758 shapeNames.push_back( INDETERMINATE_STATE );
759 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinShapeIcons(), shapeNames ) );
760 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinShapeIcons(), shapeNames ) );
761 m_grid->SetColAttr( COL_SHAPE, attr );
762
763 attr = new wxGridCellAttr;
764 wxArrayString orientationNames = PinOrientationNames();
765 orientationNames.push_back( INDETERMINATE_STATE );
766 attr->SetRenderer( new GRID_CELL_ICON_TEXT_RENDERER( PinOrientationIcons(),
767 orientationNames ) );
768 attr->SetEditor( new GRID_CELL_ICON_TEXT_POPUP( PinOrientationIcons(), orientationNames ) );
769 m_grid->SetColAttr( COL_ORIENTATION, attr );
770
771 attr = new wxGridCellAttr;
772 wxArrayString unitNames;
773 unitNames.push_back( UNITS_ALL );
774
775 for( int i = 1; i <= aSymbol->GetUnitCount(); i++ )
776 unitNames.push_back( LIB_SYMBOL::LetterSubReference( i, 'A' ) );
777
778 attr->SetEditor( new GRID_CELL_COMBOBOX( unitNames ) );
779 m_grid->SetColAttr( COL_UNIT, attr );
780
781 attr = new wxGridCellAttr;
782 wxArrayString demorganNames;
783 demorganNames.push_back( DEMORGAN_ALL );
784 demorganNames.push_back( DEMORGAN_STD );
785 demorganNames.push_back( DEMORGAN_ALT );
786 attr->SetEditor( new GRID_CELL_COMBOBOX( demorganNames ) );
787 m_grid->SetColAttr( COL_DEMORGAN, attr );
788
789 attr = new wxGridCellAttr;
790 attr->SetRenderer( new wxGridCellBoolRenderer() );
791 attr->SetEditor( new wxGridCellBoolEditor() );
792 attr->SetAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
793 m_grid->SetColAttr( COL_VISIBLE, attr );
794
795 /* Right-aligned position values look much better, but only MSW and GTK2+
796 * currently support right-aligned textEditCtrls, so the text jumps on all
797 * the other platforms when you edit it.
798 attr = new wxGridCellAttr;
799 attr->SetAlignment( wxALIGN_RIGHT, wxALIGN_TOP );
800 m_grid->SetColAttr( COL_POSX, attr );
801
802 attr = new wxGridCellAttr;
803 attr->SetAlignment( wxALIGN_RIGHT, wxALIGN_TOP );
804 m_grid->SetColAttr( COL_POSY, attr );
805 */
806
807 m_addButton->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
808 m_deleteButton->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
809 m_refreshButton->SetBitmap( KiBitmapBundle( BITMAPS::small_refresh ) );
810
813
814 GetSizer()->SetSizeHints(this);
815 Centre();
816
817 if( aSymbol->IsMulti() )
818 {
819 m_unitFilter->Append( UNITS_ALL );
820
821 for( int ii = 0; ii < aSymbol->GetUnitCount(); ++ii )
822 m_unitFilter->Append( aSymbol->GetUnitReference( ii + 1 ) );
823
824 m_unitFilter->SetSelection( -1 );
825 }
826 else
827 {
828 m_cbFilterByUnit->Show( false );
829 m_unitFilter->Show( false );
830 }
831
833
834 if( !parent->IsSymbolEditable() || parent->IsSymbolAlias() )
835 {
836 m_ButtonsCancel->SetDefault();
837 m_ButtonsOK->SetLabel( _( "Read Only" ) );
838 m_ButtonsOK->Enable( false );
839 }
840
841 m_initialized = true;
842 m_modified = false;
843
844 // Connect Events
845 m_grid->Connect( wxEVT_GRID_COL_SORT,
846 wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr, this );
847}
848
849
851{
853 cfg->m_PinTableVisibleColumns = m_grid->GetShownColumnsAsString();
854
855 // Disconnect Events
856 m_grid->Disconnect( wxEVT_GRID_COL_SORT,
857 wxGridEventHandler( DIALOG_LIB_EDIT_PIN_TABLE::OnColSort ), nullptr, this );
858
859 // Prevents crash bug in wxGrid's d'tor
861
862 // Delete the GRID_TRICKS.
863 m_grid->PopEventHandler( true );
864
865 // This is our copy of the pins. If they were transferred to the part on an OK, then
866 // m_pins will already be empty.
867 for( LIB_PIN* pin : m_pins )
868 delete pin;
869
870 WINDOW_THAWER thawer( m_editFrame );
871
872 m_editFrame->FocusOnItem( nullptr );
874}
875
876
878{
879 // Make a copy of the pins for editing
880 std::vector<LIB_PIN*> pins = m_symbol->GetAllLibPins();
881
882 for( LIB_PIN* pin : pins )
883 m_pins.push_back( new LIB_PIN( *pin ) );
884
885 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
886
887 if( m_symbol->IsMulti() )
888 m_grid->ShowCol( COL_UNIT );
889 else
890 m_grid->HideCol( COL_UNIT );
891
893 m_grid->ShowCol( COL_DEMORGAN );
894 else
895 m_grid->HideCol( COL_DEMORGAN );
896
898
899 return true;
900}
901
902
904{
906 return false;
907
908 // Delete the part's pins
909 std::vector<LIB_PIN*> pins = m_symbol->GetAllLibPins();
910
911 for( LIB_PIN* pin : pins )
913
914 // Transfer our pins to the part
915 for( LIB_PIN* pin : m_pins )
916 {
918 }
919
920 m_pins.clear();
921
922 return true;
923}
924
925
926void DIALOG_LIB_EDIT_PIN_TABLE::OnColSort( wxGridEvent& aEvent )
927{
928 int sortCol = aEvent.GetCol();
929 bool ascending;
930
931 // This is bonkers, but wxWidgets doesn't tell us ascending/descending in the
932 // event, and if we ask it will give us pre-event info.
933 if( m_grid->IsSortingBy( sortCol ) )
934 // same column; invert ascending
935 ascending = !m_grid->IsSortOrderAscending();
936 else
937 // different column; start with ascending
938 ascending = true;
939
940 m_dataModel->SortRows( sortCol, ascending );
941}
942
943
944void DIALOG_LIB_EDIT_PIN_TABLE::OnAddRow( wxCommandEvent& event )
945{
947 return;
948
949 LIB_PIN* newPin = new LIB_PIN( this->m_symbol );
950
951 // Copy the settings of the last pin onto the new pin.
952 if( m_pins.size() > 0 )
953 {
954 LIB_PIN* last = m_pins.back();
955
956 newPin->SetOrientation( last->GetOrientation() );
957 newPin->SetType( last->GetType() );
958 newPin->SetShape( last->GetShape() );
959 newPin->SetUnit( last->GetUnit() );
960
961 VECTOR2I pos = last->GetPosition();
962
964
965 if( last->GetOrientation() == PIN_ORIENTATION::PIN_LEFT
966 || last->GetOrientation() == PIN_ORIENTATION::PIN_RIGHT )
967 {
968 pos.y -= schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
969 }
970 else
971 {
972 pos.x += schIUScale.MilsToIU( cfg->m_Repeat.pin_step );
973 }
974
975 newPin->SetPosition( pos );
976 }
977
978 m_pins.push_back( newPin );
979
980 m_dataModel->AppendRow( m_pins[ m_pins.size() - 1 ] );
981
982 m_grid->MakeCellVisible( m_grid->GetNumberRows() - 1, 1 );
983 m_grid->SetGridCursor( m_grid->GetNumberRows() - 1, 1 );
984
985 m_grid->EnableCellEditControl( true );
986 m_grid->ShowCellEditControl();
987
989}
990
991
993{
994 m_pins.push_back( pin );
996}
997
998
999void DIALOG_LIB_EDIT_PIN_TABLE::OnDeleteRow( wxCommandEvent& event )
1000{
1001 // TODO: handle delete of multiple rows....
1003 return;
1004
1005 if( m_pins.size() == 0 ) // empty table
1006 return;
1007
1008 int curRow = m_grid->GetGridCursorRow();
1009
1010 if( curRow < 0 )
1011 return;
1012
1013 // move the selection first because wx internally will try to reselect the row we deleted in out of order events
1014 int nextSelRow = std::max( curRow-1, 0 );
1015 m_grid->GoToCell( nextSelRow, m_grid->GetGridCursorCol() );
1016 m_grid->SetGridCursor( nextSelRow, m_grid->GetGridCursorCol() );
1017 m_grid->SelectRow( nextSelRow );
1018
1019 LIB_PINS removedRow = m_dataModel->RemoveRow( curRow );
1020
1021 for( LIB_PIN* pin : removedRow )
1022 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1023
1024 updateSummary();
1025}
1026
1027
1029{
1030 m_pins.erase( std::find( m_pins.begin(), m_pins.end(), pin ) );
1031 updateSummary();
1032}
1033
1034
1036{
1037 updateSummary();
1038}
1039
1040
1042{
1043 LIB_PIN* pin = nullptr;
1044
1045 if( event.GetRow() >= 0 && event.GetRow() < m_dataModel->GetNumberRows() )
1046 {
1047 const LIB_PINS& pins = m_dataModel->GetRowPins( event.GetRow() );
1048
1049 if( pins.size() == 1 && m_editFrame->GetCurSymbol() )
1050 {
1051 for( LIB_PIN* candidate : m_editFrame->GetCurSymbol()->GetAllLibPins() )
1052 {
1053 if( candidate->GetNumber() == pins.at( 0 )->GetNumber() )
1054 {
1055 pin = candidate;
1056 break;
1057 }
1058 }
1059 }
1060 }
1061
1062 WINDOW_THAWER thawer( m_editFrame );
1063
1066}
1067
1068
1070{
1071 return m_cbGroup->GetValue();
1072}
1073
1074
1076{
1077 m_cbGroup->SetValue( false );
1078
1079 m_dataModel->RebuildRows( m_pins, false, true );
1080
1081 m_grid->ShowCol( COL_PIN_COUNT );
1082 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1083
1085}
1086
1087
1089{
1091 return;
1092
1093 m_dataModel->RebuildRows( m_pins, m_cbGroup->GetValue(), false );
1094
1095 if( m_cbGroup->GetValue() )
1096 {
1097 m_grid->ShowCol( COL_PIN_COUNT );
1098 m_grid->SetColLabelAlignment( wxALIGN_CENTER, wxALIGN_CENTER );
1099 }
1100
1102}
1103
1104
1106{
1107 if( event.IsChecked() )
1108 {
1109 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1110 }
1111 else
1112 {
1114 m_unitFilter->SetSelection( -1 );
1115 }
1116
1117 OnRebuildRows( event );
1118}
1119
1120
1122{
1123 m_cbFilterByUnit->SetValue( true );
1124 m_dataModel->SetUnitFilter( m_unitFilter->GetSelection() );
1125
1126 OnRebuildRows( event );
1127}
1128
1129
1131{
1132 // Account for scroll bars
1134
1135 wxGridUpdateLocker deferRepaintsTillLeavingScope;
1136
1137 // The Number and Name columns must be at least wide enough to hold their contents, but
1138 // no less wide than their original widths.
1139
1140 m_grid->AutoSizeColumn( COL_NUMBER );
1141
1142 if( m_grid->GetColSize( COL_NUMBER ) < m_originalColWidths[ COL_NUMBER ] )
1144
1145 m_grid->AutoSizeColumn( COL_NAME );
1146
1147 if( m_grid->GetColSize( COL_NAME ) < m_originalColWidths[ COL_NAME ] )
1148 m_grid->SetColSize( COL_NAME, m_originalColWidths[ COL_NAME ] );
1149
1150 // If the grid is still wider than the columns, then stretch the Number and Name columns
1151 // to fit.
1152
1153 for( int i = 0; i < COL_COUNT; ++i )
1154 width -= m_grid->GetColSize( i );
1155
1156 if( width > 0 )
1157 {
1158 m_grid->SetColSize( COL_NUMBER, m_grid->GetColSize( COL_NUMBER ) + width / 2 );
1159 m_grid->SetColSize( COL_NAME, m_grid->GetColSize( COL_NAME ) + width / 2 );
1160 }
1161}
1162
1163
1164void DIALOG_LIB_EDIT_PIN_TABLE::OnSize( wxSizeEvent& event )
1165{
1166 wxSize new_size = event.GetSize();
1167
1168 if( m_initialized && m_size != new_size )
1169 {
1170 m_size = new_size;
1171
1173 }
1174
1175 // Always propagate for a grid repaint (needed if the height changes, as well as width)
1176 event.Skip();
1177}
1178
1179
1180void DIALOG_LIB_EDIT_PIN_TABLE::OnUpdateUI( wxUpdateUIEvent& event )
1181{
1182 std::bitset<64> columnsShown = m_grid->GetShownColumns();
1183
1184 if( columnsShown != m_columnsShown )
1185 {
1186 m_columnsShown = columnsShown;
1187
1188 if( !m_grid->IsCellEditControlShown() )
1190 }
1191
1192 int firstSelectedRow;
1193 int selectedRowCount;
1194
1195 getSelectedArea( m_grid, &firstSelectedRow, &selectedRowCount );
1196
1197 if( ( selectedRowCount > 1 ) != m_groupSelected->IsEnabled() )
1198 m_groupSelected->Enable( selectedRowCount > 1 );
1199}
1200
1201
1202void DIALOG_LIB_EDIT_PIN_TABLE::OnCancel( wxCommandEvent& event )
1203{
1204 Close();
1205}
1206
1207
1208void DIALOG_LIB_EDIT_PIN_TABLE::OnClose( wxCloseEvent& event )
1209{
1210 // This is a cancel, so commit quietly as we're going to throw the results away anyway.
1212
1213 int retval = wxID_CANCEL;
1214
1215 if( m_dataModel->IsEdited() )
1216 {
1217 if( HandleUnsavedChanges( this, _( "Save changes?" ),
1218 [&]() -> bool
1219 {
1221 {
1222 retval = wxID_OK;
1223 return true;
1224 }
1225
1226 return false;
1227 } ) )
1228 {
1229 if( IsQuasiModal() )
1230 EndQuasiModal( retval );
1231 else
1232 EndDialog( retval );
1233
1234 return;
1235 }
1236 else
1237 {
1238 event.Veto();
1239 return;
1240 }
1241 }
1242
1243 // No change in dialog: we can close it
1244 if( IsQuasiModal() )
1245 EndQuasiModal( retval );
1246 else
1247 EndDialog( retval );
1248
1249 return;
1250}
1251
1252
1254{
1255 PIN_NUMBERS pinNumbers;
1256
1257 for( LIB_PIN* pin : m_pins )
1258 {
1259 if( pin->GetNumber().Length() )
1260 pinNumbers.insert( pin->GetNumber() );
1261 }
1262
1263 m_pin_numbers_summary->SetLabel( pinNumbers.GetSummary() );
1264 m_pin_count->SetLabel( wxString::Format( wxT( "%u" ), (unsigned) m_pins.size() ) );
1265 m_duplicate_pins->SetLabel( pinNumbers.GetDuplicates() );
1266
1267 Layout();
1268}
constexpr EDA_IU_SCALE schIUScale
Definition: base_units.h:111
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
void SetIsSeparator()
Render button as a toolbar separator.
Class DIALOG_LIB_EDIT_PIN_TABLE_BASE.
bool m_modified
true when there are unsaved changes
void OnRebuildRows(wxCommandEvent &event) override
void OnCancel(wxCommandEvent &event) override
void OnUpdateUI(wxUpdateUIEvent &event) override
void OnColSort(wxGridEvent &aEvent)
void OnGroupSelected(wxCommandEvent &event) override
void OnClose(wxCloseEvent &event) override
void OnSize(wxSizeEvent &event) override
PIN_TABLE_DATA_MODEL * m_dataModel
void OnCellSelected(wxGridEvent &event) override
void OnAddRow(wxCommandEvent &event) override
void OnDeleteRow(wxCommandEvent &event) override
void OnCellEdited(wxGridEvent &event) override
DIALOG_LIB_EDIT_PIN_TABLE(SYMBOL_EDIT_FRAME *parent, LIB_SYMBOL *aSymbol)
void OnFilterCheckBox(wxCommandEvent &event) override
void OnFilterChoice(wxCommandEvent &event) override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
bool IsQuasiModal() const
Definition: dialog_shim.h:106
void EndQuasiModal(int retCode)
The base class for create windows for drawing purpose.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
int GetUnit() const
Definition: lib_item.h:343
void SetUnit(int aUnit)
Definition: lib_item.h:342
@ BASE
Definition: lib_item.h:79
@ DEMORGAN
Definition: lib_item.h:80
void SetShape(GRAPHIC_PINSHAPE aShape)
Definition: lib_pin.h:72
ELECTRICAL_PINTYPE GetType() const
Definition: lib_pin.h:84
void SetPosition(const VECTOR2I &aPos) override
Definition: lib_pin.h:233
void SetName(const wxString &aName)
Definition: lib_pin.h:108
void SetType(ELECTRICAL_PINTYPE aType)
Definition: lib_pin.h:85
PIN_ORIENTATION GetOrientation() const
Definition: lib_pin.h:68
VECTOR2I GetPosition() const override
Definition: lib_pin.h:232
void SetOrientation(PIN_ORIENTATION aOrientation)
Definition: lib_pin.h:69
GRAPHIC_PINSHAPE GetShape() const
Definition: lib_pin.h:71
void SetNumber(const wxString &aNumber)
Definition: lib_pin.h:119
const wxString & GetName() const
Definition: lib_pin.h:106
Define a library symbol object.
Definition: lib_symbol.h:99
void RemoveDrawItem(LIB_ITEM *aItem)
Remove draw aItem from list.
bool IsMulti() const
Definition: lib_symbol.h:600
static wxString LetterSubReference(int aUnit, int aFirstId)
Definition: lib_symbol.cpp:760
void AddDrawItem(LIB_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
wxString GetUnitReference(int aUnit) override
Return an identifier for aUnit for symbols with units.
Definition: lib_symbol.cpp:539
std::vector< LIB_PIN * > GetAllLibPins() const
Return a list of pin pointers for all units / converts.
int GetUnitCount() const override
For items with units, return the number of units.
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:63
wxString GetSummary() const
Definition: pin_numbers.cpp:70
PIN_TABLE_DATA_MODEL(SYMBOL_EDIT_FRAME *aFrame, DIALOG_LIB_EDIT_PIN_TABLE *aPinTable, LIB_SYMBOL *aSymbol)
void RebuildRows(const LIB_PINS &aPins, bool groupByName, bool groupBySelection)
static wxString StringFromBool(bool aValue)
static wxString GetValue(const LIB_PINS &pins, int aCol, EDA_DRAW_FRAME *aParentFrame)
std::vector< LIB_PINS > m_rows
static bool compare(const LIB_PINS &lhs, const LIB_PINS &rhs, int sortCol, bool ascending, EDA_DRAW_FRAME *parentFrame)
bool IsEmptyCell(int row, int col) override
static int findRow(const std::vector< LIB_PINS > &aRowSet, const wxString &aName)
DIALOG_LIB_EDIT_PIN_TABLE * m_pinTable
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 SortRows(int aSortCol, bool ascending)
std::map< std::pair< LIB_PINS, int >, wxString > m_evalOriginal
void onUnitsChanged(wxCommandEvent &aEvent)
static bool BoolFromString(wxString aValue)
wxString GetColLabelValue(int aCol) override
SCH_DRAW_PANEL * GetCanvas() const override
Return a pointer to GAL-based canvas of given EDA draw frame.
void SetBitmap(const wxBitmapBundle &aBmp)
The symbol library editor main window.
void FocusOnItem(LIB_ITEM *aItem)
bool IsSymbolAlias() const
Return true if aLibId is an alias for the editor screen symbol.
bool GetShowDeMorgan() const
SYMBOL_EDITOR_SETTINGS * GetSettings() const
bool IsSymbolEditable() const
Test if a symbol is loaded and can be edited.
LIB_SYMBOL * GetCurSymbol() const
Return the current symbol being edited or NULL if none selected.
EDA_UNITS GetUserUnits() const
wxString StringFromValue(double aValue, bool aAddUnitLabel=false, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
Converts aValue in internal units into a united string.
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.
void ShowHideColumns(const wxString &shownColumns)
Show/hide the grid columns based on a tokenized string of shown column indexes.
Definition: wx_grid.cpp:312
void SetTable(wxGridTableBase *table, bool aTakeOwnership=false)
Hide wxGrid's SetTable() method with one which doesn't mess up the grid column widths when setting th...
Definition: wx_grid.cpp:156
void DestroyTable(wxGridTableBase *aTable)
Work-around for a bug in wxGrid which crashes when deleting the table if the cell edit control was no...
Definition: wx_grid.cpp:268
wxString GetShownColumnsAsString()
Get a tokenized string containing the shown column indexes.
Definition: wx_grid.cpp:282
std::bitset< 64 > GetShownColumns()
Definition: wx_grid.cpp:301
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:462
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:240
This file is part of the common library.
#define UNITS_ALL
void getSelectedArea(WX_GRID *aGrid, int *aRowStart, int *aRowCount)
#define DEMORGAN_ALL
#define DEMORGAN_ALT
#define DEMORGAN_STD
#define _(s)
#define CANDIDATE
flag indicating that the structure is connected
static std::map< int, wxString > shapeNames
std::vector< LIB_PIN * > LIB_PINS
Helper for defining a list of pin object pointers.
Definition: lib_item.h:61
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition: gtk/ui.cpp:195
see class PGM_BASE
const std::vector< BITMAPS > & PinTypeIcons()
Definition: pin_type.cpp:151
const wxArrayString & PinTypeNames()
Definition: pin_type.cpp:142
int PinOrientationIndex(PIN_ORIENTATION code)
Definition: pin_type.cpp:109
const wxArrayString & PinShapeNames()
Definition: pin_type.cpp:160
const std::vector< BITMAPS > & PinShapeIcons()
Definition: pin_type.cpp:169
const wxArrayString & PinOrientationNames()
Definition: pin_type.cpp:178
PIN_ORIENTATION PinOrientationCode(size_t index)
Definition: pin_type.cpp:102
const std::vector< BITMAPS > & PinOrientationIcons()
Definition: pin_type.cpp:187
ELECTRICAL_PINTYPE
The symbol library pin object electrical types used in ERC tests.
Definition: pin_type.h:36
GRAPHIC_PINSHAPE
Definition: pin_type.h:56
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:119
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
constexpr int MilsToIU(int mils) const
Definition: base_units.h:94
VECTOR3I res
#define INDETERMINATE_STATE
Used for holding indeterminate values, such as with multiple selections holding different values or c...
Definition: ui_common.h:44
VECTOR2< int > VECTOR2I
Definition: vector2d.h:588