KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_symbol_properties.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 <memory>
27
28#include <bitmaps.h>
29#include <wx/tooltip.h>
30#include <grid_tricks.h>
31#include <confirm.h>
32#include <kiface_base.h>
33#include <pin_numbers.h>
34#include <string_utils.h>
35#include <kiplatform/ui.h>
40#include <sch_collectors.h>
41#include <fields_grid_table.h>
42#include <sch_edit_frame.h>
43#include <sch_reference_list.h>
44#include <schematic.h>
45#include <sch_commit.h>
46#include <tool/tool_manager.h>
47#include <tool/actions.h>
48
49#include <dialog_sim_model.h>
51
52
53wxDEFINE_EVENT( SYMBOL_DELAY_FOCUS, wxCommandEvent );
54wxDEFINE_EVENT( SYMBOL_DELAY_SELECTION, wxCommandEvent );
55
66
67
68class SCH_PIN_TABLE_DATA_MODEL : public WX_GRID_TABLE_BASE, public std::vector<SCH_PIN>
69{
70public:
72 m_readOnlyAttr( nullptr ),
73 m_typeAttr( nullptr ),
74 m_shapeAttr( nullptr )
75 {
76 }
77
79 {
80 for( wxGridCellAttr* attr : m_nameAttrs )
81 attr->DecRef();
82
83 m_readOnlyAttr->DecRef();
84 m_typeAttr->DecRef();
85 m_shapeAttr->DecRef();
86 }
87
89 {
90 for( wxGridCellAttr* attr : m_nameAttrs )
91 attr->DecRef();
92
93 m_nameAttrs.clear();
94
95 if( m_readOnlyAttr )
96 m_readOnlyAttr->DecRef();
97
98 m_readOnlyAttr = new wxGridCellAttr;
99 m_readOnlyAttr->SetReadOnly( true );
100
101 for( const SCH_PIN& pin : *this )
102 {
103 SCH_PIN* lib_pin = pin.GetLibPin();
104 wxGridCellAttr* attr = nullptr;
105
106 if( !lib_pin || lib_pin->GetAlternates().empty() )
107 {
108 attr = new wxGridCellAttr;
109 attr->SetReadOnly( true );
110 attr->SetBackgroundColour( KIPLATFORM::UI::GetDialogBGColour() );
111 }
112 else
113 {
114 wxArrayString choices;
115 choices.push_back( lib_pin->GetName() );
116
117 for( const std::pair<const wxString, SCH_PIN::ALT>& alt : lib_pin->GetAlternates() )
118 choices.push_back( alt.first );
119
120 attr = new wxGridCellAttr();
121 attr->SetEditor( new GRID_CELL_COMBOBOX( choices ) );
122 }
123
124 m_nameAttrs.push_back( attr );
125 }
126
127 if( m_typeAttr )
128 m_typeAttr->DecRef();
129
130 m_typeAttr = new wxGridCellAttr;
132 m_typeAttr->SetReadOnly( true );
133
134 if( m_shapeAttr )
135 m_shapeAttr->DecRef();
136
137 m_shapeAttr = new wxGridCellAttr;
139 m_shapeAttr->SetReadOnly( true );
140 }
141
142 int GetNumberRows() override { return (int) size(); }
143 int GetNumberCols() override { return COL_COUNT; }
144
145 wxString GetColLabelValue( int aCol ) override
146 {
147 switch( aCol )
148 {
149 case COL_NUMBER: return _( "Number" );
150 case COL_BASE_NAME: return _( "Base Name" );
151 case COL_ALT_NAME: return _( "Alternate Assignment" );
152 case COL_TYPE: return _( "Electrical Type" );
153 case COL_SHAPE: return _( "Graphic Style" );
154 default: wxFAIL; return wxEmptyString;
155 }
156 }
157
158 bool IsEmptyCell( int row, int col ) override
159 {
160 return false; // don't allow adjacent cell overflow, even if we are actually empty
161 }
162
163 bool CanSetValueAs( int aRow, int aCol, const wxString& aTypeName ) override
164 {
165 // Don't accept random values; must use the popup to change to a known alternate
166 return false;
167 }
168
169 wxString GetValue( int aRow, int aCol ) override
170 {
171 return GetValue( at( aRow ), aCol );
172 }
173
174 static wxString GetValue( const SCH_PIN& aPin, int aCol )
175 {
176 if( aCol == COL_ALT_NAME )
177 {
178 if( !aPin.GetLibPin() || aPin.GetLibPin()->GetAlternates().empty() )
179 return wxEmptyString;
180 else if( aPin.GetAlt().IsEmpty() )
181 return aPin.GetName();
182 else
183 return aPin.GetAlt();
184 }
185
186 switch( aCol )
187 {
188 case COL_NUMBER: return aPin.GetNumber();
189 case COL_BASE_NAME: return aPin.GetBaseName();
190 case COL_TYPE: return PinTypeNames()[static_cast<int>( aPin.GetType() )];
191 case COL_SHAPE: return PinShapeNames()[static_cast<int>( aPin.GetShape() )];
192 default: wxFAIL; return wxEmptyString;
193 }
194 }
195
196 wxGridCellAttr* GetAttr( int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind ) override
197 {
198 switch( aCol )
199 {
200 case COL_NUMBER:
201 case COL_BASE_NAME:
202 m_readOnlyAttr->IncRef();
203 return enhanceAttr( m_readOnlyAttr, aRow, aCol, aKind );
204
205 case COL_ALT_NAME:
206 m_nameAttrs[ aRow ]->IncRef();
207 return enhanceAttr( m_nameAttrs[ aRow ], aRow, aCol, aKind );
208
209 case COL_TYPE:
210 m_typeAttr->IncRef();
211 return enhanceAttr( m_typeAttr, aRow, aCol, aKind );
212
213 case COL_SHAPE:
214 m_shapeAttr->IncRef();
215 return enhanceAttr( m_shapeAttr, aRow, aCol, aKind );
216
217 default:
218 wxFAIL;
219 return nullptr;
220 }
221 }
222
223 void SetValue( int aRow, int aCol, const wxString &aValue ) override
224 {
225 SCH_PIN& pin = at( aRow );
226
227 switch( aCol )
228 {
229 case COL_ALT_NAME:
230 if( pin.GetLibPin() && aValue == pin.GetLibPin()->GetName() )
231 pin.SetAlt( wxEmptyString );
232 else
233 pin.SetAlt( aValue );
234 break;
235
236 case COL_NUMBER:
237 case COL_BASE_NAME:
238 case COL_TYPE:
239 case COL_SHAPE:
240 // Read-only.
241 break;
242
243 default:
244 wxFAIL;
245 break;
246 }
247 }
248
249 static bool compare( const SCH_PIN& lhs, const SCH_PIN& rhs, int sortCol, bool ascending )
250 {
251 wxString lhStr = GetValue( lhs, sortCol );
252 wxString rhStr = GetValue( rhs, sortCol );
253
254 if( lhStr == rhStr )
255 {
256 // Secondary sort key is always COL_NUMBER
257 sortCol = COL_NUMBER;
258 lhStr = GetValue( lhs, sortCol );
259 rhStr = GetValue( rhs, sortCol );
260 }
261
262 bool res;
263
264 // N.B. To meet the iterator sort conditions, we cannot simply invert the truth
265 // to get the opposite sort. i.e. ~(a<b) != (a>b)
266 auto cmp = [ ascending ]( const auto a, const auto b )
267 {
268 if( ascending )
269 return a < b;
270 else
271 return b < a;
272 };
273
274 switch( sortCol )
275 {
276 case COL_NUMBER:
277 case COL_BASE_NAME:
278 case COL_ALT_NAME:
279 res = cmp( PIN_NUMBERS::Compare( lhStr, rhStr ), 0 );
280 break;
281 case COL_TYPE:
282 case COL_SHAPE:
283 res = cmp( lhStr.CmpNoCase( rhStr ), 0 );
284 break;
285 default:
286 res = cmp( StrNumCmp( lhStr, rhStr ), 0 );
287 break;
288 }
289
290 return res;
291 }
292
293 void SortRows( int aSortCol, bool ascending )
294 {
295 std::sort( begin(), end(),
296 [ aSortCol, ascending ]( const SCH_PIN& lhs, const SCH_PIN& rhs ) -> bool
297 {
298 return compare( lhs, rhs, aSortCol, ascending );
299 } );
300 }
301
302protected:
303 std::vector<wxGridCellAttr*> m_nameAttrs;
304 wxGridCellAttr* m_readOnlyAttr;
305 wxGridCellAttr* m_typeAttr;
306 wxGridCellAttr* m_shapeAttr;
307};
308
309
312 m_symbol( nullptr ),
313 m_part( nullptr ),
315 m_editorShown( false ),
316 m_fields( nullptr ),
317 m_dataModel( nullptr ),
318 m_embeddedFiles( nullptr )
319{
320 m_symbol = aSymbol;
321 m_part = m_symbol->GetLibSymbolRef().get();
322
323 // GetLibSymbolRef() now points to the cached part in the schematic, which should always be
324 // there for usual cases, but can be null when opening old schematics not storing the part
325 // so we need to handle m_part == nullptr
326 // wxASSERT( m_part );
327
328 m_fields = new FIELDS_GRID_TABLE( this, aParent, m_fieldsGrid, m_symbol );
329
330 m_fieldsGrid->SetTable( m_fields );
331 m_fieldsGrid->PushEventHandler( new FIELDS_GRID_TRICKS( m_fieldsGrid, this,
332 { &aParent->Schematic(), m_part },
333 [&]( wxCommandEvent& aEvent )
334 {
335 OnAddField( aEvent );
336 } ) );
337 m_fieldsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
338 m_fieldsGrid->ShowHideColumns( "0 1 2 3 4 5 6 7" );
339 m_shownColumns = m_fieldsGrid->GetShownColumns();
340
341 if( m_symbol->GetEmbeddedFiles() )
342 {
343 m_embeddedFiles = new PANEL_EMBEDDED_FILES( m_notebook1, m_symbol->GetEmbeddedFiles() );
344 m_notebook1->AddPage( m_embeddedFiles, _( "Embedded Files" ) );
345 }
346
347 if( m_part && m_part->IsMultiBodyStyle() )
348 {
349 // Multiple body styles are a superclass of alternate pin assignments, so don't allow
350 // free-form alternate assignments as well. (We won't know how to map the alternates
351 // back and forth when the body style is changed.)
352 m_pinTablePage->Disable();
353 m_pinTablePage->SetToolTip( _( "Alternate pin assignments are not available for symbols with multiple "
354 "body styles." ) );
355 }
356 else
357 {
359
360 // Make a copy of the pins for editing
361 for( const std::unique_ptr<SCH_PIN>& pin : m_symbol->GetRawPins() )
362 m_dataModel->push_back( *pin );
363
364 m_dataModel->SortRows( COL_NUMBER, true );
365 m_dataModel->BuildAttrs();
366
367 m_pinGrid->SetTable( m_dataModel );
368 }
369
370 if( m_part && m_part->IsPower() )
371 m_spiceFieldsButton->Hide();
372
373 m_pinGrid->PushEventHandler( new GRID_TRICKS( m_pinGrid ) );
374 m_pinGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
375
376 wxFont infoFont = KIUI::GetSmallInfoFont( this );
377 m_libraryIDLabel->SetFont( infoFont );
378 m_tcLibraryID->SetFont( infoFont );
379 m_tcLibraryID->SetBackgroundColour( KIPLATFORM::UI::GetDialogBGColour() );
380
381 wxToolTip::Enable( true );
383
384 // Configure button logos
389
390 // wxFormBuilder doesn't include this event...
391 m_fieldsGrid->Bind( wxEVT_GRID_CELL_CHANGING, &DIALOG_SYMBOL_PROPERTIES::OnGridCellChanging, this );
392 m_pinGrid->Bind( wxEVT_GRID_COL_SORT, &DIALOG_SYMBOL_PROPERTIES::OnPinTableColSort, this );
393 Bind( SYMBOL_DELAY_FOCUS, &DIALOG_SYMBOL_PROPERTIES::HandleDelayedFocus, this );
394 Bind( SYMBOL_DELAY_SELECTION, &DIALOG_SYMBOL_PROPERTIES::HandleDelayedSelection, this );
395
396 wxCommandEvent* evt = new wxCommandEvent( SYMBOL_DELAY_SELECTION );
397 evt->SetClientData( new VECTOR2I( 0, FDC_VALUE ) );
398 QueueEvent( evt );
399 evt = new wxCommandEvent( SYMBOL_DELAY_FOCUS );
400 evt->SetClientData( new VECTOR2I( 0, FDC_VALUE ) );
401 QueueEvent( evt );
402
404}
405
406
408{
409 // Prevents crash bug in wxGrid's d'tor
410 m_fieldsGrid->DestroyTable( m_fields );
411
412 if( m_dataModel )
413 m_pinGrid->DestroyTable( m_dataModel );
414
415 m_fieldsGrid->Unbind( wxEVT_GRID_CELL_CHANGING, &DIALOG_SYMBOL_PROPERTIES::OnGridCellChanging, this );
416 m_pinGrid->Unbind( wxEVT_GRID_COL_SORT, &DIALOG_SYMBOL_PROPERTIES::OnPinTableColSort, this );
417 Unbind( SYMBOL_DELAY_FOCUS, &DIALOG_SYMBOL_PROPERTIES::HandleDelayedFocus, this );
418 Unbind( SYMBOL_DELAY_SELECTION, &DIALOG_SYMBOL_PROPERTIES::HandleDelayedSelection, this );
419
420 // Delete the GRID_TRICKS.
421 m_fieldsGrid->PopEventHandler( true );
422 m_pinGrid->PopEventHandler( true );
423}
424
425
427{
428 return dynamic_cast<SCH_EDIT_FRAME*>( wxDialog::GetParent() );
429}
430
431
433{
434 if( !wxDialog::TransferDataToWindow() )
435 return false;
436
437 std::set<wxString> defined;
438
439 // Push a copy of each field into m_updateFields
440 for( SCH_FIELD& srcField : m_symbol->GetFields() )
441 {
442 SCH_FIELD field( srcField );
443
444 // change offset to be symbol-relative
445 field.Offset( -m_symbol->GetPosition() );
446
447 field.SetText( m_symbol->Schematic()->ConvertKIIDsToRefs( field.GetText() ) );
448
449 defined.insert( field.GetName() );
450 m_fields->push_back( field );
451 }
452
453 // Add in any template fieldnames not yet defined:
454 for( const TEMPLATE_FIELDNAME& templateFieldname :
455 GetParent()->Schematic().Settings().m_TemplateFieldNames.GetTemplateFieldNames() )
456 {
457 if( defined.count( templateFieldname.m_Name ) <= 0 )
458 {
459 SCH_FIELD field( m_symbol, FIELD_T::USER, templateFieldname.m_Name );
460 field.SetVisible( templateFieldname.m_Visible );
461 m_fields->push_back( field );
462 }
463 }
464
465 // notify the grid
466 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_fields->GetNumberRows() );
467 m_fieldsGrid->ProcessTableMessage( msg );
468
469 // If a multi-unit symbol, set up the unit selector and interchangeable checkbox.
470 if( m_symbol->IsMultiUnit() )
471 {
472 // Ensure symbol unit is the currently selected unit (mandatory in complex hierarchies)
473 // from the current sheet path, because it can be modified by previous calculations
474 m_symbol->SetUnit( m_symbol->GetUnitSelection( &GetParent()->GetCurrentSheet() ) );
475
476 for( int ii = 1; ii <= m_symbol->GetUnitCount(); ii++ )
477 m_unitChoice->Append( m_symbol->GetUnitDisplayName( ii, false ) );
478
479 if( m_symbol->GetUnit() <= ( int )m_unitChoice->GetCount() )
480 m_unitChoice->SetSelection( m_symbol->GetUnit() - 1 );
481 }
482 else
483 {
484 m_unitLabel->Enable( false );
485 m_unitChoice->Enable( false );
486 }
487
488 if( m_part && m_part->IsMultiBodyStyle() )
489 {
490 if( m_part->HasDeMorganBodyStyles() )
491 {
492 m_bodyStyleChoice->Append( _( "Standard" ) );
493 m_bodyStyleChoice->Append( _( "Alternate" ) );
494 }
495 else
496 {
497 wxASSERT( (int)m_part->GetBodyStyleNames().size() == m_part->GetBodyStyleCount() );
498
499 for( int ii = 0; ii < m_part->GetBodyStyleCount(); ii++ )
500 {
501 try
502 {
503 m_bodyStyleChoice->Append( m_part->GetBodyStyleNames().at( ii ) );
504 }
505 catch( ... )
506 {
507 m_bodyStyleChoice->Append( wxT( "???" ) );
508 }
509 }
510 }
511
512 if( m_symbol->GetBodyStyle() <= (int) m_bodyStyleChoice->GetCount() )
513 m_bodyStyleChoice->SetSelection( m_symbol->GetBodyStyle() - 1 );
514 }
515 else
516 {
517 m_bodyStyle->Enable( false );
518 m_bodyStyleChoice->Enable( false );
519 }
520
521 // Set the symbol orientation and mirroring.
522 int orientation = m_symbol->GetOrientation() & ~( SYM_MIRROR_X | SYM_MIRROR_Y );
523
524 switch( orientation )
525 {
526 default:
527 case SYM_ORIENT_0: m_orientationCtrl->SetSelection( 0 ); break;
528 case SYM_ORIENT_90: m_orientationCtrl->SetSelection( 1 ); break;
529 case SYM_ORIENT_270: m_orientationCtrl->SetSelection( 2 ); break;
530 case SYM_ORIENT_180: m_orientationCtrl->SetSelection( 3 ); break;
531 }
532
533 int mirror = m_symbol->GetOrientation() & ( SYM_MIRROR_X | SYM_MIRROR_Y );
534
535 switch( mirror )
536 {
537 default: m_mirrorCtrl->SetSelection( 0 ) ; break;
538 case SYM_MIRROR_X: m_mirrorCtrl->SetSelection( 1 ); break;
539 case SYM_MIRROR_Y: m_mirrorCtrl->SetSelection( 2 ); break;
540 }
541
542 m_cbExcludeFromSim->SetValue( m_symbol->GetExcludedFromSim() );
543 m_cbExcludeFromBom->SetValue( m_symbol->GetExcludedFromBOM() );
544 m_cbExcludeFromBoard->SetValue( m_symbol->GetExcludedFromBoard() );
545 m_cbDNP->SetValue( m_symbol->GetDNP( &GetParent()->GetCurrentSheet() ) );
546
547 if( m_part )
548 {
549 m_ShowPinNumButt->SetValue( m_part->GetShowPinNumbers() );
550 m_ShowPinNameButt->SetValue( m_part->GetShowPinNames() );
551 }
552
553 // Set the symbol's library name.
554 m_tcLibraryID->SetValue( UnescapeString( m_symbol->GetLibId().Format() ) );
555
556 if( m_embeddedFiles && !m_embeddedFiles->TransferDataToWindow() )
557 return false;
558
559 Layout();
560 m_fieldsGrid->Layout();
561
562#ifdef __WXGTK__
563 wxSafeYield();
564#endif
565
566 return true;
567}
568
569
571{
572 if( !m_fieldsGrid->CommitPendingChanges() )
573 return;
574
575 m_fieldsGrid->ClearSelection();
576
577 std::vector<SCH_FIELD> fields;
578
579 for( const SCH_FIELD& field : *m_fields )
580 fields.emplace_back( field );
581
582 DIALOG_SIM_MODEL dialog( this, m_parentFrame, *m_symbol, fields );
583
584 if( dialog.ShowModal() != wxID_OK )
585 return;
586
587 // Add in any new fields
588 for( const SCH_FIELD& editedField : fields )
589 {
590 bool found = false;
591
592 for( SCH_FIELD& existingField : *m_fields )
593 {
594 if( existingField.GetName() == editedField.GetName() )
595 {
596 found = true;
597 existingField.SetText( editedField.GetText() );
598 break;
599 }
600 }
601
602 if( !found )
603 {
604 m_fields->emplace_back( editedField );
605 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
606 m_fieldsGrid->ProcessTableMessage( msg );
607 }
608 }
609
610 // Remove any deleted fields
611 for( int ii = (int) m_fields->size() - 1; ii >= 0; --ii )
612 {
613 SCH_FIELD& existingField = m_fields->at( ii );
614 bool found = false;
615
616 for( SCH_FIELD& editedField : fields )
617 {
618 if( editedField.GetName() == existingField.GetName() )
619 {
620 found = true;
621 break;
622 }
623 }
624
625 if( !found )
626 {
627 m_fieldsGrid->ClearSelection();
628 m_fields->erase( m_fields->begin() + ii );
629
630 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, ii, 1 );
631 m_fieldsGrid->ProcessTableMessage( msg );
632 }
633 }
634
635 OnModify();
636 m_fieldsGrid->ForceRefresh();
637}
638
639
641{
642 // Running the Footprint Browser gums up the works and causes the automatic cancel
643 // stuff to no longer work. So we do it here ourselves.
644 EndQuasiModal( wxID_CANCEL );
645}
646
647
649{
650 LIB_ID id;
651
652 if( !m_fieldsGrid->CommitPendingChanges() || !m_fieldsGrid->Validate() )
653 return false;
654
655 // Check for missing field names.
656 for( size_t i = 0; i < m_fields->size(); ++i )
657 {
658 SCH_FIELD& field = m_fields->at( i );
659
660 if( field.IsMandatory() )
661 continue;
662
663 wxString fieldName = field.GetName( false );
664
665 if( fieldName.IsEmpty() )
666 {
667 DisplayErrorMessage( this, _( "Fields must have a name." ) );
668
669 wxCommandEvent *evt = new wxCommandEvent( SYMBOL_DELAY_FOCUS );
670 evt->SetClientData( new VECTOR2I( i, FDC_VALUE ) );
671 QueueEvent( evt );
672
673 return false;
674 }
675 }
676
677 return true;
678}
679
680
682{
683 if( !wxDialog::TransferDataFromWindow() ) // Calls our Validate() method.
684 return false;
685
686 if( m_embeddedFiles && !m_embeddedFiles->TransferDataFromWindow() )
687 return false;
688
689 if( !m_fieldsGrid->CommitPendingChanges() )
690 return false;
691
692 if( !m_pinGrid->CommitPendingChanges() )
693 return false;
694
695 SCH_COMMIT commit( GetParent() );
696 SCH_SCREEN* currentScreen = GetParent()->GetScreen();
697 bool replaceOnCurrentScreen;
698 wxCHECK( currentScreen, false );
699
700 // This needs to be done before the LIB_ID is changed to prevent stale library symbols in
701 // the schematic file.
702 replaceOnCurrentScreen = currentScreen->Remove( m_symbol );
703
704 // save old cmp in undo list if not already in edit, or moving ...
705 if( m_symbol->GetEditFlags() == 0 )
706 commit.Modify( m_symbol, currentScreen );
707
708 // Save current flags which could be modified by next change settings
709 EDA_ITEM_FLAGS flags = m_symbol->GetFlags();
710
711 //Set the part selection in multiple part per package
712 int unit_selection = m_unitChoice->IsEnabled() ? m_unitChoice->GetSelection() + 1 : 1;
713 m_symbol->SetUnitSelection( &GetParent()->GetCurrentSheet(), unit_selection );
714 m_symbol->SetUnit( unit_selection );
715
716 int bodyStyle_selection = m_bodyStyleChoice->IsEnabled() ? m_bodyStyleChoice->GetSelection() + 1 : 1;
717 m_symbol->SetBodyStyle( bodyStyle_selection );
718
719 switch( m_orientationCtrl->GetSelection() )
720 {
721 case 0: m_symbol->SetOrientation( SYM_ORIENT_0 ); break;
722 case 1: m_symbol->SetOrientation( SYM_ORIENT_90 ); break;
723 case 2: m_symbol->SetOrientation( SYM_ORIENT_270 ); break;
724 case 3: m_symbol->SetOrientation( SYM_ORIENT_180 ); break;
725 }
726
727 switch( m_mirrorCtrl->GetSelection() )
728 {
729 case 0: break;
730 case 1: m_symbol->SetOrientation( SYM_MIRROR_X ); break;
731 case 2: m_symbol->SetOrientation( SYM_MIRROR_Y ); break;
732 }
733
734 m_symbol->SetShowPinNames( m_ShowPinNameButt->GetValue() );
735 m_symbol->SetShowPinNumbers( m_ShowPinNumButt->GetValue() );
736
737 // Restore m_Flag modified by SetUnit() and other change settings from the dialog
738 m_symbol->ClearFlags();
739 m_symbol->SetFlags( flags );
740
741 // change all field positions from relative to absolute
742 for( SCH_FIELD& field : *m_fields )
743 {
744 field.Offset( m_symbol->GetPosition() );
745 field.SetText( m_symbol->Schematic()->ConvertRefsToKIIDs( field.GetText() ) );
746 }
747
748 SCH_FIELDS& fields = m_symbol->GetFields();
749 fields.clear();
750
751 for( SCH_FIELD& field : *m_fields )
752 {
753 const wxString& fieldName = field.GetCanonicalName();
754
755 if( fieldName.IsEmpty() && field.GetText().IsEmpty() )
756 continue;
757 else if( fieldName.IsEmpty() )
758 field.SetName( _( "untitled" ) );
759
760 fields.push_back( field );
761 }
762
763 int ordinal = 42; // Arbitrarily larger than any mandatory FIELD_T ids.
764
765 for( SCH_FIELD& field : fields )
766 {
767 if( !field.IsMandatory() )
768 field.SetOrdinal( ordinal++ );
769 }
770
771 // Reference has a specific initialization, depending on the current active sheet
772 // because for a given symbol, in a complex hierarchy, there are more than one
773 // reference.
774 m_symbol->SetRef( &GetParent()->GetCurrentSheet(), m_fields->GetField( FIELD_T::REFERENCE )->GetText() );
775
776 // Similar for Value and Footprint, except that the GUI behavior is that they are kept
777 // in sync between multiple instances.
778 m_symbol->SetValueFieldText( m_fields->GetField( FIELD_T::VALUE )->GetText() );
779 m_symbol->SetFootprintFieldText( m_fields->GetField( FIELD_T::FOOTPRINT )->GetText() );
780
781 m_symbol->SetExcludedFromSim( m_cbExcludeFromSim->IsChecked() );
782 m_symbol->SetExcludedFromBOM( m_cbExcludeFromBom->IsChecked() );
783 m_symbol->SetExcludedFromBoard( m_cbExcludeFromBoard->IsChecked() );
784 m_symbol->SetDNP( m_cbDNP->IsChecked(), &GetParent()->GetCurrentSheet() );
785
786 // Update any assignments
787 if( m_dataModel )
788 {
789 for( const SCH_PIN& model_pin : *m_dataModel )
790 {
791 // map from the edited copy back to the "real" pin in the symbol.
792 SCH_PIN* src_pin = m_symbol->GetPin( model_pin.GetNumber() );
793
794 if( src_pin )
795 src_pin->SetAlt( model_pin.GetAlt() );
796 }
797 }
798
799 // Keep fields other than the reference, include/exclude flags, and alternate pin assignements
800 // in sync in multi-unit parts.
801 m_symbol->SyncOtherUnits( GetParent()->GetCurrentSheet(), commit, nullptr );
802
803 if( replaceOnCurrentScreen )
804 currentScreen->Append( m_symbol );
805
806 if( !commit.Empty() )
807 commit.Push( _( "Edit Symbol Properties" ) );
808
809 return true;
810}
811
812
814{
815 wxGridCellEditor* editor = m_fieldsGrid->GetCellEditor( event.GetRow(), event.GetCol() );
816 wxControl* control = editor->GetControl();
817
818 if( control && control->GetValidator() && !control->GetValidator()->Validate( control ) )
819 {
820 event.Veto();
821 wxCommandEvent *evt = new wxCommandEvent( SYMBOL_DELAY_FOCUS );
822 evt->SetClientData( new VECTOR2I( event.GetRow(), event.GetCol() ) );
823 QueueEvent( evt );
824 }
825 else if( event.GetCol() == FDC_NAME )
826 {
827 wxString newName = event.GetString();
828
829 for( int i = 0; i < m_fieldsGrid->GetNumberRows(); ++i )
830 {
831 if( i == event.GetRow() )
832 continue;
833
834 if( newName.CmpNoCase( m_fieldsGrid->GetCellValue( i, FDC_NAME ) ) == 0 )
835 {
836 DisplayError( this, wxString::Format( _( "Field name '%s' already in use." ),
837 newName ) );
838 event.Veto();
839 wxCommandEvent *evt = new wxCommandEvent( SYMBOL_DELAY_FOCUS );
840 evt->SetClientData( new VECTOR2I( event.GetRow(), event.GetCol() ) );
841 QueueEvent( evt );
842 }
843 }
844 }
845
846 editor->DecRef();
847}
848
849
851{
852 if( m_fields->at( aEvent.GetRow() ).GetId() == FIELD_T::REFERENCE
853 && aEvent.GetCol() == FDC_VALUE )
854 {
855 wxCommandEvent* evt = new wxCommandEvent( SYMBOL_DELAY_SELECTION );
856 evt->SetClientData( new VECTOR2I( aEvent.GetRow(), aEvent.GetCol() ) );
857 QueueEvent( evt );
858 }
859
860 m_editorShown = true;
861}
862
863
865{
866 m_editorShown = false;
867}
868
869
870void DIALOG_SYMBOL_PROPERTIES::OnAddField( wxCommandEvent& event )
871{
872 m_fieldsGrid->OnAddRow(
873 [&]() -> std::pair<int, int>
874 {
876
877 newField.SetTextAngle( m_fields->GetField( FIELD_T::REFERENCE )->GetTextAngle() );
878 newField.SetVisible( false );
879
880 m_fields->push_back( newField );
881
882 // notify the grid
883 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
884 m_fieldsGrid->ProcessTableMessage( msg );
885 OnModify();
886
887 return { m_fields->size() - 1, FDC_NAME };
888 } );
889}
890
891
892void DIALOG_SYMBOL_PROPERTIES::OnDeleteField( wxCommandEvent& event )
893{
894 m_fieldsGrid->OnDeleteRows(
895 [&]( int row )
896 {
897 if( row < m_fields->GetMandatoryRowCount() )
898 {
899 DisplayError( this, wxString::Format( _( "The first %d fields are mandatory." ),
900 m_fields->GetMandatoryRowCount() ) );
901 return false;
902 }
903
904 return true;
905 },
906 [&]( int row )
907 {
908 m_fields->erase( m_fields->begin() + row );
909
910 // notify the grid
911 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, row, 1 );
912 m_fieldsGrid->ProcessTableMessage( msg );
913 } );
914
915 OnModify();
916}
917
918
919void DIALOG_SYMBOL_PROPERTIES::OnMoveUp( wxCommandEvent& event )
920{
921 m_fieldsGrid->OnMoveRowUp(
922 [&]( int row )
923 {
924 return row > m_fields->GetMandatoryRowCount();
925 },
926 [&]( int row )
927 {
928 std::swap( *( m_fields->begin() + row ), *( m_fields->begin() + row - 1 ) );
929 m_fieldsGrid->ForceRefresh();
930 OnModify();
931 } );
932}
933
934
935void DIALOG_SYMBOL_PROPERTIES::OnMoveDown( wxCommandEvent& event )
936{
937 m_fieldsGrid->OnMoveRowDown(
938 [&]( int row )
939 {
940 return row >= m_fields->GetMandatoryRowCount();
941 },
942 [&]( int row )
943 {
944 std::swap( *( m_fields->begin() + row ), *( m_fields->begin() + row + 1 ) );
945 m_fieldsGrid->ForceRefresh();
946 OnModify();
947 } );
948}
949
950
956
957
963
964
970
971
977
978
980{
981 int row = aEvent.GetRow();
982
983 if( m_pinGrid->GetCellValue( row, COL_ALT_NAME ) == m_dataModel->GetValue( row, COL_BASE_NAME ) )
984 m_dataModel->SetValue( row, COL_ALT_NAME, wxEmptyString );
985
986 // These are just to get the cells refreshed
987 m_dataModel->SetValue( row, COL_TYPE, m_dataModel->GetValue( row, COL_TYPE ) );
988 m_dataModel->SetValue( row, COL_SHAPE, m_dataModel->GetValue( row, COL_SHAPE ) );
989
990 OnModify();
991}
992
993
995{
996 int sortCol = aEvent.GetCol();
997 bool ascending;
998
999 // This is bonkers, but wxWidgets doesn't tell us ascending/descending in the
1000 // event, and if we ask it will give us pre-event info.
1001 if( m_pinGrid->IsSortingBy( sortCol ) )
1002 // same column; invert ascending
1003 ascending = !m_pinGrid->IsSortOrderAscending();
1004 else
1005 // different column; start with ascending
1006 ascending = true;
1007
1008 m_dataModel->SortRows( sortCol, ascending );
1009 m_dataModel->BuildAttrs();
1010}
1011
1012
1014{
1015 wxGridUpdateLocker deferRepaintsTillLeavingScope( m_pinGrid );
1016
1017 // Account for scroll bars
1018 int pinTblWidth = KIPLATFORM::UI::GetUnobscuredSize( m_pinGrid ).x;
1019
1020 // Stretch the Base Name and Alternate Assignment columns to fit.
1021 for( int i = 0; i < COL_COUNT; ++i )
1022 {
1023 if( i != COL_BASE_NAME && i != COL_ALT_NAME )
1024 pinTblWidth -= m_pinGrid->GetColSize( i );
1025 }
1026
1027 if( pinTblWidth > 2 )
1028 {
1029 m_pinGrid->SetColSize( COL_BASE_NAME, pinTblWidth / 2 );
1030 m_pinGrid->SetColSize( COL_ALT_NAME, pinTblWidth / 2 );
1031 }
1032}
1033
1034
1035void DIALOG_SYMBOL_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
1036{
1037 std::bitset<64> shownColumns = m_fieldsGrid->GetShownColumns();
1038
1039 if( shownColumns != m_shownColumns )
1040 {
1041 m_shownColumns = shownColumns;
1042
1043 if( !m_fieldsGrid->IsCellEditControlShown() )
1044 m_fieldsGrid->SetGridWidthsDirty();
1045 }
1046}
1047
1048
1050{
1051 VECTOR2I *loc = static_cast<VECTOR2I*>( event.GetClientData() );
1052
1053 wxCHECK_RET( loc, wxT( "Missing focus cell location" ) );
1054
1055 // Handle a delayed focus
1056
1057 m_fieldsGrid->SetFocus();
1058 m_fieldsGrid->MakeCellVisible( loc->x, loc->y );
1059 m_fieldsGrid->SetGridCursor( loc->x, loc->y );
1060
1061 m_fieldsGrid->EnableCellEditControl( true );
1062 m_fieldsGrid->ShowCellEditControl();
1063
1064 delete loc;
1065}
1066
1067
1069{
1070 VECTOR2I *loc = static_cast<VECTOR2I*>( event.GetClientData() );
1071
1072 wxCHECK_RET( loc, wxT( "Missing focus cell location" ) );
1073
1074 // Handle a delayed selection
1075 wxGridCellEditor* cellEditor = m_fieldsGrid->GetCellEditor( loc->x, loc->y );
1076
1077 if( wxTextEntry* txt = dynamic_cast<wxTextEntry*>( cellEditor->GetControl() ) )
1079
1080 cellEditor->DecRef(); // we're done; must release
1081}
1082
1083
1085{
1086 wxSize new_size = event.GetSize();
1087
1088 if( ( !m_editorShown || m_lastRequestedPinsSize != new_size ) && m_pinsSize != new_size )
1089 {
1090 m_pinsSize = new_size;
1091
1093 }
1094
1095 // We store this value to check whether the dialog is changing size. This might indicate
1096 // that the user is scaling the dialog with a grid-cell-editor shown. Some editors do not
1097 // close (at least on GTK) when the user drags a dialog corner
1098 m_lastRequestedPinsSize = new_size;
1099
1100 // Always propagate for a grid repaint (needed if the height changes, as well as width)
1101 event.Skip();
1102}
1103
1104
1105void DIALOG_SYMBOL_PROPERTIES::OnInitDlg( wxInitDialogEvent& event )
1106{
1108
1109 // Now all widgets have the size fixed, call FinishDialogSettings
1111}
1112
1113
1114void DIALOG_SYMBOL_PROPERTIES::OnCheckBox( wxCommandEvent& event )
1115{
1116 OnModify();
1117}
1118
1119
1120void DIALOG_SYMBOL_PROPERTIES::OnUnitChoice( wxCommandEvent& event )
1121{
1122 if( m_dataModel )
1123 {
1124 EDA_ITEM_FLAGS flags = m_symbol->GetFlags();
1125
1126 int unit_selection = m_unitChoice->GetSelection() + 1;
1127
1128 // We need to select a new unit to build the new unit pin list
1129 // but we should not change the symbol, so the initial unit will be selected
1130 // after rebuilding the pin list
1131 int old_unit = m_symbol->GetUnit();
1132 m_symbol->SetUnit( unit_selection );
1133
1134 // Rebuild a copy of the pins of the new unit for editing
1135 m_dataModel->clear();
1136
1137 for( const std::unique_ptr<SCH_PIN>& pin : m_symbol->GetRawPins() )
1138 m_dataModel->push_back( *pin );
1139
1140 m_dataModel->SortRows( COL_NUMBER, true );
1141 m_dataModel->BuildAttrs();
1142
1143 m_symbol->SetUnit( old_unit );
1144
1145 // Restore m_Flag modified by SetUnit()
1146 m_symbol->ClearFlags();
1147 m_symbol->SetFlags( flags );
1148 }
1149
1150 OnModify();
1151}
1152
1153
1155{
1156 event.Enable( m_symbol && m_symbol->GetLibSymbolRef() );
1157}
1158
1159
1161{
1162 event.Enable( m_symbol && m_symbol->GetLibSymbolRef() );
1163}
1164
1165
1166void DIALOG_SYMBOL_PROPERTIES::OnPageChanging( wxBookCtrlEvent& aEvent )
1167{
1168 if( !m_fieldsGrid->CommitPendingChanges() )
1169 aEvent.Veto();
1170
1171 if( !m_pinGrid->CommitPendingChanges() )
1172 aEvent.Veto();
1173}
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
bool Empty() const
Definition commit.h:137
COMMIT & Modify(EDA_ITEM *aItem, BASE_SCREEN *aScreen=nullptr, RECURSE_MODE aRecurse=RECURSE_MODE::NO_RECURSE)
Modify a given item in the model.
Definition commit.h:106
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void EndQuasiModal(int retCode)
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
EDA_BASE_FRAME * m_parentFrame
int ShowModal() override
DIALOG_SYMBOL_PROPERTIES_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Symbol Properties"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxCAPTION|wxCLOSE_BOX|wxDEFAULT_DIALOG_STYLE|wxMAXIMIZE_BOX|wxMINIMIZE_BOX|wxRESIZE_BORDER|wxSYSTEM_MENU)
void OnSizePinsGrid(wxSizeEvent &event) override
void OnPinTableCellEdited(wxGridEvent &event) override
void OnGridEditorShown(wxGridEvent &event) override
void OnUpdateUI(wxUpdateUIEvent &event) override
void OnEditSymbol(wxCommandEvent &) override
void OnCancelButtonClick(wxCommandEvent &event) override
virtual void onUpdateEditLibrarySymbol(wxUpdateUIEvent &event) override
SCH_PIN_TABLE_DATA_MODEL * m_dataModel
void OnMoveDown(wxCommandEvent &event) override
void OnPinTableColSort(wxGridEvent &aEvent)
void OnMoveUp(wxCommandEvent &event) override
void OnDeleteField(wxCommandEvent &event) override
void OnInitDlg(wxInitDialogEvent &event) override
void OnAddField(wxCommandEvent &event) override
void OnGridEditorHidden(wxGridEvent &event) override
PANEL_EMBEDDED_FILES * m_embeddedFiles
void OnUnitChoice(wxCommandEvent &event) override
void OnEditSpiceModel(wxCommandEvent &event) override
void OnPageChanging(wxNotebookEvent &event) override
DIALOG_SYMBOL_PROPERTIES(SCH_EDIT_FRAME *aParent, SCH_SYMBOL *aSymbol)
void HandleDelayedFocus(wxCommandEvent &event)
void HandleDelayedSelection(wxCommandEvent &event)
void OnCheckBox(wxCommandEvent &event) override
void OnGridCellChanging(wxGridEvent &event)
void OnEditLibrarySymbol(wxCommandEvent &) override
void OnUpdateSymbol(wxCommandEvent &) override
void OnExchangeSymbol(wxCommandEvent &) override
virtual void onUpdateEditSymbol(wxUpdateUIEvent &event) override
virtual const wxString & GetText() const
Return the string associated with the text object.
Definition eda_text.h:98
void Offset(const VECTOR2I &aOffset)
Definition eda_text.cpp:602
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:394
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:307
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:61
A logical library item identifier and consists of various portions much like a URI.
Definition lib_id.h:49
static int Compare(const wxString &lhs, const wxString &rhs)
virtual void Push(const wxString &aMessage=wxT("A commit"), int aCommitFlags=0) override
Execute the changes.
Schematic editor (Eeschema) main window.
SCH_SCREEN * GetScreen() const override
Return a pointer to a BASE_SCREEN or one of its derivatives.
SCHEMATIC & Schematic() const
bool IsMandatory() const
wxString GetName(bool aUseDefaultName=true) const
Return the field name (not translated).
void SetText(const wxString &aText) override
bool IsEmptyCell(int row, int col) override
wxString GetValue(int aRow, int aCol) override
std::vector< wxGridCellAttr * > m_nameAttrs
static bool compare(const SCH_PIN &lhs, const SCH_PIN &rhs, int sortCol, bool ascending)
void SortRows(int aSortCol, bool ascending)
bool CanSetValueAs(int aRow, int aCol, const wxString &aTypeName) override
static wxString GetValue(const SCH_PIN &aPin, int aCol)
wxString GetColLabelValue(int aCol) override
void SetValue(int aRow, int aCol, const wxString &aValue) override
wxGridCellAttr * GetAttr(int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind) override
void SetAlt(const wxString &aAlt)
Set the name of the alternate pin.
Definition sch_pin.cpp:433
const std::map< wxString, ALT > & GetAlternates() const
Definition sch_pin.h:160
ALT GetAlt(const wxString &aAlt)
Definition sch_pin.h:174
SCH_PIN * GetLibPin() const
Definition sch_pin.h:89
const wxString & GetName() const
Definition sch_pin.cpp:401
const wxString & GetBaseName() const
Get the name without any alternates.
Definition sch_pin.cpp:410
const wxString & GetNumber() const
Definition sch_pin.h:124
GRAPHIC_PINSHAPE GetShape() const
Definition sch_pin.cpp:278
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:313
void Append(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
bool Remove(SCH_ITEM *aItem, bool aUpdateLibSymbol=true)
Remove aItem from the schematic associated with this screen.
Schematic symbol object.
Definition sch_symbol.h:76
wxGridCellAttr * enhanceAttr(wxGridCellAttr *aInputAttr, int aRow, int aCol, wxGridCellAttr::wxAttrKind aKind)
Definition wx_grid.cpp:46
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:202
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:177
This file is part of the common library.
wxDEFINE_EVENT(SYMBOL_DELAY_FOCUS, wxCommandEvent)
@ SYMBOL_PROPS_EDIT_SCHEMATIC_SYMBOL
@ SYMBOL_PROPS_WANT_EXCHANGE_SYMBOL
@ SYMBOL_PROPS_WANT_UPDATE_SYMBOL
@ SYMBOL_PROPS_EDIT_LIBRARY_SYMBOL
#define _(s)
std::uint32_t EDA_ITEM_FLAGS
@ FDC_NAME
@ FDC_VALUE
wxColour GetDialogBGColour()
Definition wxgtk/ui.cpp:61
wxSize GetUnobscuredSize(const wxWindow *aWindow)
Tries to determine the size of the viewport of a scrollable widget (wxDataViewCtrl,...
Definition wxgtk/ui.cpp:264
KICOMMON_API wxFont GetSmallInfoFont(wxWindow *aWindow)
KICOMMON_API void SelectReferenceNumber(wxTextEntry *aTextEntry)
Select the number (or "?") in a reference for ease of editing.
const std::vector< BITMAPS > & PinTypeIcons()
Definition pin_type.cpp:162
const wxArrayString & PinTypeNames()
Definition pin_type.cpp:153
const wxArrayString & PinShapeNames()
Definition pin_type.cpp:171
const std::vector< BITMAPS > & PinShapeIcons()
Definition pin_type.cpp:180
std::vector< SCH_FIELD > SCH_FIELDS
A container for several SCH_FIELD items.
Definition sch_symbol.h:64
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
wxString UnescapeString(const wxString &aSource)
Hold a name of a symbol's field, field value, and default visibility.
@ SYM_ORIENT_270
Definition symbol.h:42
@ SYM_MIRROR_Y
Definition symbol.h:44
@ SYM_ORIENT_180
Definition symbol.h:41
@ SYM_MIRROR_X
Definition symbol.h:43
@ SYM_ORIENT_90
Definition symbol.h:40
@ SYM_ORIENT_0
Definition symbol.h:39
wxString GetUserFieldName(int aFieldNdx, bool aTranslateForHI)
#define DO_TRANSLATE
@ USER
The field ID hasn't been set yet; field is invalid.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ VALUE
Field Value of part, i.e. "3.3K".
KIBIS_PIN * pin
VECTOR3I res
VECTOR2I end
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695