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 <symbol_library.h>
42#include <fields_grid_table.h>
43#include <sch_edit_frame.h>
44#include <sch_reference_list.h>
45#include <schematic.h>
46#include <sch_commit.h>
47#include <tool/tool_manager.h>
48#include <tool/actions.h>
49
50#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{
319 m_symbol = aSymbol;
320 m_part = m_symbol->GetLibSymbolRef().get();
321
322 // GetLibSymbolRef() now points to the cached part in the schematic, which should always be
323 // there for usual cases, but can be null when opening old schematics not storing the part
324 // so we need to handle m_part == nullptr
325 // wxASSERT( m_part );
326
327 m_fields = new FIELDS_GRID_TABLE( this, aParent, m_fieldsGrid, m_symbol );
328
329 m_fieldsGrid->SetTable( m_fields );
330 m_fieldsGrid->PushEventHandler( new FIELDS_GRID_TRICKS( m_fieldsGrid, this,
331 { &aParent->Schematic(), m_part },
332 [&]( wxCommandEvent& aEvent )
333 {
334 OnAddField( aEvent );
335 } ) );
336 m_fieldsGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
337
338 // Show/hide columns according to user's preference
339 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
340 {
341 m_fieldsGrid->ShowHideColumns( cfg->m_Appearance.edit_symbol_visible_columns );
342 m_shownColumns = m_fieldsGrid->GetShownColumns();
343 }
344
345 if( m_part && m_part->IsMultiBodyStyle() )
346 {
347 // Multiple body styles are a superclass of alternate pin assignments, so don't allow
348 // free-form alternate assignments as well. (We won't know how to map the alternates
349 // back and forth when the body style is changed.)
350 m_pinTablePage->Disable();
351 m_pinTablePage->SetToolTip( _( "Alternate pin assignments are not available for symbols with multiple "
352 "body styles." ) );
353 }
354 else
355 {
357
358 // Make a copy of the pins for editing
359 for( const std::unique_ptr<SCH_PIN>& pin : m_symbol->GetRawPins() )
360 m_dataModel->push_back( *pin );
361
362 m_dataModel->SortRows( COL_NUMBER, true );
363 m_dataModel->BuildAttrs();
364
365 m_pinGrid->SetTable( m_dataModel );
366 }
367
368 if( m_part && m_part->IsPower() )
369 m_spiceFieldsButton->Hide();
370
371 m_pinGrid->PushEventHandler( new GRID_TRICKS( m_pinGrid ) );
372 m_pinGrid->SetSelectionMode( wxGrid::wxGridSelectRows );
373
374 wxFont infoFont = KIUI::GetSmallInfoFont( this );
375 m_libraryIDLabel->SetFont( infoFont );
376 m_tcLibraryID->SetFont( infoFont );
377 m_tcLibraryID->SetBackgroundColour( KIPLATFORM::UI::GetDialogBGColour() );
378
379 wxToolTip::Enable( true );
381
382 // Configure button logos
387
388 // wxFormBuilder doesn't include this event...
389 m_fieldsGrid->Bind( wxEVT_GRID_CELL_CHANGING, &DIALOG_SYMBOL_PROPERTIES::OnGridCellChanging, this );
390 m_pinGrid->Bind( wxEVT_GRID_COL_SORT, &DIALOG_SYMBOL_PROPERTIES::OnPinTableColSort, this );
391 Bind( SYMBOL_DELAY_FOCUS, &DIALOG_SYMBOL_PROPERTIES::HandleDelayedFocus, this );
392 Bind( SYMBOL_DELAY_SELECTION, &DIALOG_SYMBOL_PROPERTIES::HandleDelayedSelection, this );
393
394 wxCommandEvent* evt = new wxCommandEvent( SYMBOL_DELAY_SELECTION );
395 evt->SetClientData( new VECTOR2I( 0, FDC_VALUE ) );
396 QueueEvent( evt );
397 evt = new wxCommandEvent( SYMBOL_DELAY_FOCUS );
398 evt->SetClientData( new VECTOR2I( 0, FDC_VALUE ) );
399 QueueEvent( evt );
400
402}
403
404
406{
407 if( EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() ) )
408 {
409 cfg->m_Appearance.edit_symbol_visible_columns = m_fieldsGrid->GetShownColumnsAsString();
410 cfg->m_Appearance.edit_symbol_width = GetSize().x;
411 cfg->m_Appearance.edit_symbol_height = GetSize().y;
412 }
413
414 // Prevents crash bug in wxGrid's d'tor
415 m_fieldsGrid->DestroyTable( m_fields );
416
417 if( m_dataModel )
418 m_pinGrid->DestroyTable( m_dataModel );
419
420 m_fieldsGrid->Unbind( wxEVT_GRID_CELL_CHANGING, &DIALOG_SYMBOL_PROPERTIES::OnGridCellChanging, this );
421 m_pinGrid->Unbind( wxEVT_GRID_COL_SORT, &DIALOG_SYMBOL_PROPERTIES::OnPinTableColSort, this );
422 Unbind( SYMBOL_DELAY_FOCUS, &DIALOG_SYMBOL_PROPERTIES::HandleDelayedFocus, this );
423 Unbind( SYMBOL_DELAY_SELECTION, &DIALOG_SYMBOL_PROPERTIES::HandleDelayedSelection, this );
424
425 // Delete the GRID_TRICKS.
426 m_fieldsGrid->PopEventHandler( true );
427 m_pinGrid->PopEventHandler( true );
428}
429
430
432{
433 return dynamic_cast<SCH_EDIT_FRAME*>( wxDialog::GetParent() );
434}
435
436
438{
439 if( !wxDialog::TransferDataToWindow() )
440 return false;
441
442 std::set<wxString> defined;
443
444 // Push a copy of each field into m_updateFields
445 for( SCH_FIELD& srcField : m_symbol->GetFields() )
446 {
447 SCH_FIELD field( srcField );
448
449 // change offset to be symbol-relative
450 field.Offset( -m_symbol->GetPosition() );
451
452 field.SetText( m_symbol->Schematic()->ConvertKIIDsToRefs( field.GetText() ) );
453
454 defined.insert( field.GetName() );
455 m_fields->push_back( field );
456 }
457
458 // Add in any template fieldnames not yet defined:
459 for( const TEMPLATE_FIELDNAME& templateFieldname :
460 GetParent()->Schematic().Settings().m_TemplateFieldNames.GetTemplateFieldNames() )
461 {
462 if( defined.count( templateFieldname.m_Name ) <= 0 )
463 {
464 SCH_FIELD field( m_symbol, FIELD_T::USER, templateFieldname.m_Name );
465 field.SetVisible( templateFieldname.m_Visible );
466 m_fields->push_back( field );
467 }
468 }
469
470 // notify the grid
471 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, m_fields->GetNumberRows() );
472 m_fieldsGrid->ProcessTableMessage( msg );
473
474 // If a multi-unit symbol, set up the unit selector and interchangeable checkbox.
475 if( m_symbol->IsMultiUnit() )
476 {
477 // Ensure symbol unit is the currently selected unit (mandatory in complex hierarchies)
478 // from the current sheet path, because it can be modified by previous calculations
479 m_symbol->SetUnit( m_symbol->GetUnitSelection( &GetParent()->GetCurrentSheet() ) );
480
481 for( int ii = 1; ii <= m_symbol->GetUnitCount(); ii++ )
482 m_unitChoice->Append( m_symbol->GetUnitDisplayName( ii, false ) );
483
484 if( m_symbol->GetUnit() <= ( int )m_unitChoice->GetCount() )
485 m_unitChoice->SetSelection( m_symbol->GetUnit() - 1 );
486 }
487 else
488 {
489 m_unitLabel->Enable( false );
490 m_unitChoice->Enable( false );
491 }
492
493 if( m_part && m_part->IsMultiBodyStyle() )
494 {
495 for( int ii = 0; ii < m_part->GetBodyStyleCount(); ii++ )
496 m_bodyStyleChoice->Append( m_part->GetBodyStyleNames()[ii] );
497
498 if( m_symbol->GetBodyStyle() <= (int) m_bodyStyleChoice->GetCount() )
499 m_bodyStyleChoice->SetSelection( m_symbol->GetBodyStyle() - 1 );
500 }
501 else
502 {
503 m_bodyStyle->Enable( false );
504 m_bodyStyleChoice->Enable( false );
505 }
506
507 // Set the symbol orientation and mirroring.
508 int orientation = m_symbol->GetOrientation() & ~( SYM_MIRROR_X | SYM_MIRROR_Y );
509
510 switch( orientation )
511 {
512 default:
513 case SYM_ORIENT_0: m_orientationCtrl->SetSelection( 0 ); break;
514 case SYM_ORIENT_90: m_orientationCtrl->SetSelection( 1 ); break;
515 case SYM_ORIENT_270: m_orientationCtrl->SetSelection( 2 ); break;
516 case SYM_ORIENT_180: m_orientationCtrl->SetSelection( 3 ); break;
517 }
518
519 int mirror = m_symbol->GetOrientation() & ( SYM_MIRROR_X | SYM_MIRROR_Y );
520
521 switch( mirror )
522 {
523 default: m_mirrorCtrl->SetSelection( 0 ) ; break;
524 case SYM_MIRROR_X: m_mirrorCtrl->SetSelection( 1 ); break;
525 case SYM_MIRROR_Y: m_mirrorCtrl->SetSelection( 2 ); break;
526 }
527
528 m_cbExcludeFromSim->SetValue( m_symbol->GetExcludedFromSim() );
529 m_cbExcludeFromBom->SetValue( m_symbol->GetExcludedFromBOM() );
530 m_cbExcludeFromBoard->SetValue( m_symbol->GetExcludedFromBoard() );
531 m_cbDNP->SetValue( m_symbol->GetDNP( &GetParent()->GetCurrentSheet() ) );
532
533 if( m_part )
534 {
535 m_ShowPinNumButt->SetValue( m_part->GetShowPinNumbers() );
536 m_ShowPinNameButt->SetValue( m_part->GetShowPinNames() );
537 }
538
539 // Set the symbol's library name.
540 m_tcLibraryID->SetValue( UnescapeString( m_symbol->GetLibId().Format() ) );
541
542 Layout();
543 m_fieldsGrid->Layout();
544
545#ifdef __WXGTK__
546 wxSafeYield();
547#endif
548
549 return true;
550}
551
552
554{
555 if( !m_fieldsGrid->CommitPendingChanges() )
556 return;
557
558 m_fieldsGrid->ClearSelection();
559
560 std::vector<SCH_FIELD> fields;
561
562 for( const SCH_FIELD& field : *m_fields )
563 fields.emplace_back( field );
564
565 DIALOG_SIM_MODEL dialog( this, m_parentFrame, *m_symbol, fields );
566
567 if( dialog.ShowModal() != wxID_OK )
568 return;
569
570 // Add in any new fields
571 for( const SCH_FIELD& editedField : fields )
572 {
573 bool found = false;
574
575 for( SCH_FIELD& existingField : *m_fields )
576 {
577 if( existingField.GetName() == editedField.GetName() )
578 {
579 found = true;
580 existingField.SetText( editedField.GetText() );
581 break;
582 }
583 }
584
585 if( !found )
586 {
587 m_fields->emplace_back( editedField );
588 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
589 m_fieldsGrid->ProcessTableMessage( msg );
590 }
591 }
592
593 // Remove any deleted fields
594 for( int ii = (int) m_fields->size() - 1; ii >= 0; --ii )
595 {
596 SCH_FIELD& existingField = m_fields->at( ii );
597 bool found = false;
598
599 for( SCH_FIELD& editedField : fields )
600 {
601 if( editedField.GetName() == existingField.GetName() )
602 {
603 found = true;
604 break;
605 }
606 }
607
608 if( !found )
609 {
610 m_fieldsGrid->ClearSelection();
611 m_fields->erase( m_fields->begin() + ii );
612
613 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, ii, 1 );
614 m_fieldsGrid->ProcessTableMessage( msg );
615 }
616 }
617
618 OnModify();
619 m_fieldsGrid->ForceRefresh();
620}
621
622
624{
625 // Running the Footprint Browser gums up the works and causes the automatic cancel
626 // stuff to no longer work. So we do it here ourselves.
627 EndQuasiModal( wxID_CANCEL );
628}
629
630
632{
633 LIB_ID id;
634
635 if( !m_fieldsGrid->CommitPendingChanges() || !m_fieldsGrid->Validate() )
636 return false;
637
638 // Check for missing field names.
639 for( size_t i = 0; i < m_fields->size(); ++i )
640 {
641 SCH_FIELD& field = m_fields->at( i );
642
643 if( field.IsMandatory() )
644 continue;
645
646 wxString fieldName = field.GetName( false );
647
648 if( fieldName.IsEmpty() )
649 {
650 DisplayErrorMessage( this, _( "Fields must have a name." ) );
651
652 wxCommandEvent *evt = new wxCommandEvent( SYMBOL_DELAY_FOCUS );
653 evt->SetClientData( new VECTOR2I( i, FDC_VALUE ) );
654 QueueEvent( evt );
655
656 return false;
657 }
658 }
659
660 return true;
661}
662
663
665{
666 if( !wxDialog::TransferDataFromWindow() ) // Calls our Validate() method.
667 return false;
668
669 if( !m_fieldsGrid->CommitPendingChanges() )
670 return false;
671
672 if( !m_pinGrid->CommitPendingChanges() )
673 return false;
674
675 SCH_COMMIT commit( GetParent() );
676 SCH_SCREEN* currentScreen = GetParent()->GetScreen();
677 bool replaceOnCurrentScreen;
678 wxCHECK( currentScreen, false );
679
680 // This needs to be done before the LIB_ID is changed to prevent stale library symbols in
681 // the schematic file.
682 replaceOnCurrentScreen = currentScreen->Remove( m_symbol );
683
684 // save old cmp in undo list if not already in edit, or moving ...
685 if( m_symbol->GetEditFlags() == 0 )
686 commit.Modify( m_symbol, currentScreen );
687
688 // Save current flags which could be modified by next change settings
689 EDA_ITEM_FLAGS flags = m_symbol->GetFlags();
690
691 //Set the part selection in multiple part per package
692 int unit_selection = m_unitChoice->IsEnabled() ? m_unitChoice->GetSelection() + 1 : 1;
693 m_symbol->SetUnitSelection( &GetParent()->GetCurrentSheet(), unit_selection );
694 m_symbol->SetUnit( unit_selection );
695
696 int bodyStyle_selection = m_bodyStyleChoice->IsEnabled() ? m_bodyStyleChoice->GetSelection() + 1 : 1;
697 m_symbol->SetBodyStyle( bodyStyle_selection );
698
699 switch( m_orientationCtrl->GetSelection() )
700 {
701 case 0: m_symbol->SetOrientation( SYM_ORIENT_0 ); break;
702 case 1: m_symbol->SetOrientation( SYM_ORIENT_90 ); break;
703 case 2: m_symbol->SetOrientation( SYM_ORIENT_270 ); break;
704 case 3: m_symbol->SetOrientation( SYM_ORIENT_180 ); break;
705 }
706
707 switch( m_mirrorCtrl->GetSelection() )
708 {
709 case 0: break;
710 case 1: m_symbol->SetOrientation( SYM_MIRROR_X ); break;
711 case 2: m_symbol->SetOrientation( SYM_MIRROR_Y ); break;
712 }
713
714 m_symbol->SetShowPinNames( m_ShowPinNameButt->GetValue() );
715 m_symbol->SetShowPinNumbers( m_ShowPinNumButt->GetValue() );
716
717 // Restore m_Flag modified by SetUnit() and other change settings from the dialog
718 m_symbol->ClearFlags();
719 m_symbol->SetFlags( flags );
720
721 // change all field positions from relative to absolute
722 for( SCH_FIELD& field : *m_fields )
723 {
724 field.Offset( m_symbol->GetPosition() );
725 field.SetText( m_symbol->Schematic()->ConvertRefsToKIIDs( field.GetText() ) );
726 }
727
728 SCH_FIELDS& fields = m_symbol->GetFields();
729 fields.clear();
730
731 for( SCH_FIELD& field : *m_fields )
732 {
733 const wxString& fieldName = field.GetCanonicalName();
734
735 if( fieldName.IsEmpty() && field.GetText().IsEmpty() )
736 continue;
737 else if( fieldName.IsEmpty() )
738 field.SetName( _( "untitled" ) );
739
740 fields.push_back( field );
741 }
742
743 int ordinal = 42; // Arbitrarily larger than any mandatory FIELD_T ids.
744
745 for( SCH_FIELD& field : fields )
746 {
747 if( !field.IsMandatory() )
748 field.SetOrdinal( ordinal++ );
749 }
750
751 // Reference has a specific initialization, depending on the current active sheet
752 // because for a given symbol, in a complex hierarchy, there are more than one
753 // reference.
754 m_symbol->SetRef( &GetParent()->GetCurrentSheet(), m_fields->GetField( FIELD_T::REFERENCE )->GetText() );
755
756 // Similar for Value and Footprint, except that the GUI behavior is that they are kept
757 // in sync between multiple instances.
758 m_symbol->SetValueFieldText( m_fields->GetField( FIELD_T::VALUE )->GetText() );
759 m_symbol->SetFootprintFieldText( m_fields->GetField( FIELD_T::FOOTPRINT )->GetText() );
760
761 m_symbol->SetExcludedFromSim( m_cbExcludeFromSim->IsChecked() );
762 m_symbol->SetExcludedFromBOM( m_cbExcludeFromBom->IsChecked() );
763 m_symbol->SetExcludedFromBoard( m_cbExcludeFromBoard->IsChecked() );
764 m_symbol->SetDNP( m_cbDNP->IsChecked(), &GetParent()->GetCurrentSheet() );
765
766 // Update any assignments
767 if( m_dataModel )
768 {
769 for( const SCH_PIN& model_pin : *m_dataModel )
770 {
771 // map from the edited copy back to the "real" pin in the symbol.
772 SCH_PIN* src_pin = m_symbol->GetPin( model_pin.GetNumber() );
773
774 if( src_pin )
775 src_pin->SetAlt( model_pin.GetAlt() );
776 }
777 }
778
779 // Keep fields other than the reference, include/exclude flags, and alternate pin assignements
780 // in sync in multi-unit parts.
781 m_symbol->SyncOtherUnits( GetParent()->GetCurrentSheet(), commit, nullptr );
782
783 if( replaceOnCurrentScreen )
784 currentScreen->Append( m_symbol );
785
786 if( !commit.Empty() )
787 commit.Push( _( "Edit Symbol Properties" ) );
788
789 return true;
790}
791
792
794{
795 wxGridCellEditor* editor = m_fieldsGrid->GetCellEditor( event.GetRow(), event.GetCol() );
796 wxControl* control = editor->GetControl();
797
798 if( control && control->GetValidator() && !control->GetValidator()->Validate( control ) )
799 {
800 event.Veto();
801 wxCommandEvent *evt = new wxCommandEvent( SYMBOL_DELAY_FOCUS );
802 evt->SetClientData( new VECTOR2I( event.GetRow(), event.GetCol() ) );
803 QueueEvent( evt );
804 }
805 else if( event.GetCol() == FDC_NAME )
806 {
807 wxString newName = event.GetString();
808
809 for( int i = 0; i < m_fieldsGrid->GetNumberRows(); ++i )
810 {
811 if( i == event.GetRow() )
812 continue;
813
814 if( newName.CmpNoCase( m_fieldsGrid->GetCellValue( i, FDC_NAME ) ) == 0 )
815 {
816 DisplayError( this, wxString::Format( _( "Field name '%s' already in use." ),
817 newName ) );
818 event.Veto();
819 wxCommandEvent *evt = new wxCommandEvent( SYMBOL_DELAY_FOCUS );
820 evt->SetClientData( new VECTOR2I( event.GetRow(), event.GetCol() ) );
821 QueueEvent( evt );
822 }
823 }
824 }
825
826 editor->DecRef();
827}
828
829
831{
832 if( m_fields->at( aEvent.GetRow() ).GetId() == FIELD_T::REFERENCE
833 && aEvent.GetCol() == FDC_VALUE )
834 {
835 wxCommandEvent* evt = new wxCommandEvent( SYMBOL_DELAY_SELECTION );
836 evt->SetClientData( new VECTOR2I( aEvent.GetRow(), aEvent.GetCol() ) );
837 QueueEvent( evt );
838 }
839
840 m_editorShown = true;
841}
842
843
845{
846 m_editorShown = false;
847}
848
849
850void DIALOG_SYMBOL_PROPERTIES::OnAddField( wxCommandEvent& event )
851{
852 m_fieldsGrid->OnAddRow(
853 [&]() -> std::pair<int, int>
854 {
856
857 newField.SetTextAngle( m_fields->GetField( FIELD_T::REFERENCE )->GetTextAngle() );
858 newField.SetVisible( false );
859
860 m_fields->push_back( newField );
861
862 // notify the grid
863 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_APPENDED, 1 );
864 m_fieldsGrid->ProcessTableMessage( msg );
865 OnModify();
866
867 return { m_fields->size() - 1, FDC_NAME };
868 } );
869}
870
871
872void DIALOG_SYMBOL_PROPERTIES::OnDeleteField( wxCommandEvent& event )
873{
874 m_fieldsGrid->OnDeleteRows(
875 [&]( int row )
876 {
877 if( row < m_fields->GetMandatoryRowCount() )
878 {
879 DisplayError( this, wxString::Format( _( "The first %d fields are mandatory." ),
880 m_fields->GetMandatoryRowCount() ) );
881 return false;
882 }
883
884 return true;
885 },
886 [&]( int row )
887 {
888 m_fields->erase( m_fields->begin() + row );
889
890 // notify the grid
891 wxGridTableMessage msg( m_fields, wxGRIDTABLE_NOTIFY_ROWS_DELETED, row, 1 );
892 m_fieldsGrid->ProcessTableMessage( msg );
893 } );
894
895 OnModify();
896}
897
898
899void DIALOG_SYMBOL_PROPERTIES::OnMoveUp( wxCommandEvent& event )
900{
901 m_fieldsGrid->OnMoveRowUp(
902 [&]( int row )
903 {
904 return row > m_fields->GetMandatoryRowCount();
905 },
906 [&]( int row )
907 {
908 std::swap( *( m_fields->begin() + row ), *( m_fields->begin() + row - 1 ) );
909 m_fieldsGrid->ForceRefresh();
910 OnModify();
911 } );
912}
913
914
915void DIALOG_SYMBOL_PROPERTIES::OnMoveDown( wxCommandEvent& event )
916{
917 m_fieldsGrid->OnMoveRowDown(
918 [&]( int row )
919 {
920 return row >= m_fields->GetMandatoryRowCount();
921 },
922 [&]( int row )
923 {
924 std::swap( *( m_fields->begin() + row ), *( m_fields->begin() + row + 1 ) );
925 m_fieldsGrid->ForceRefresh();
926 OnModify();
927 } );
928}
929
930
936
937
943
944
950
951
957
958
960{
961 int row = aEvent.GetRow();
962
963 if( m_pinGrid->GetCellValue( row, COL_ALT_NAME ) == m_dataModel->GetValue( row, COL_BASE_NAME ) )
964 m_dataModel->SetValue( row, COL_ALT_NAME, wxEmptyString );
965
966 // These are just to get the cells refreshed
967 m_dataModel->SetValue( row, COL_TYPE, m_dataModel->GetValue( row, COL_TYPE ) );
968 m_dataModel->SetValue( row, COL_SHAPE, m_dataModel->GetValue( row, COL_SHAPE ) );
969
970 OnModify();
971}
972
973
975{
976 int sortCol = aEvent.GetCol();
977 bool ascending;
978
979 // This is bonkers, but wxWidgets doesn't tell us ascending/descending in the
980 // event, and if we ask it will give us pre-event info.
981 if( m_pinGrid->IsSortingBy( sortCol ) )
982 // same column; invert ascending
983 ascending = !m_pinGrid->IsSortOrderAscending();
984 else
985 // different column; start with ascending
986 ascending = true;
987
988 m_dataModel->SortRows( sortCol, ascending );
989 m_dataModel->BuildAttrs();
990}
991
992
994{
995 wxGridUpdateLocker deferRepaintsTillLeavingScope( m_pinGrid );
996
997 // Account for scroll bars
998 int pinTblWidth = KIPLATFORM::UI::GetUnobscuredSize( m_pinGrid ).x;
999
1000 // Stretch the Base Name and Alternate Assignment columns to fit.
1001 for( int i = 0; i < COL_COUNT; ++i )
1002 {
1003 if( i != COL_BASE_NAME && i != COL_ALT_NAME )
1004 pinTblWidth -= m_pinGrid->GetColSize( i );
1005 }
1006
1007 if( pinTblWidth > 2 )
1008 {
1009 m_pinGrid->SetColSize( COL_BASE_NAME, pinTblWidth / 2 );
1010 m_pinGrid->SetColSize( COL_ALT_NAME, pinTblWidth / 2 );
1011 }
1012}
1013
1014
1015void DIALOG_SYMBOL_PROPERTIES::OnUpdateUI( wxUpdateUIEvent& event )
1016{
1017 std::bitset<64> shownColumns = m_fieldsGrid->GetShownColumns();
1018
1019 if( shownColumns != m_shownColumns )
1020 {
1021 m_shownColumns = shownColumns;
1022
1023 if( !m_fieldsGrid->IsCellEditControlShown() )
1024 m_fieldsGrid->SetGridWidthsDirty();
1025 }
1026}
1027
1028
1030{
1031 VECTOR2I *loc = static_cast<VECTOR2I*>( event.GetClientData() );
1032
1033 wxCHECK_RET( loc, wxT( "Missing focus cell location" ) );
1034
1035 // Handle a delayed focus
1036
1037 m_fieldsGrid->SetFocus();
1038 m_fieldsGrid->MakeCellVisible( loc->x, loc->y );
1039 m_fieldsGrid->SetGridCursor( loc->x, loc->y );
1040
1041 m_fieldsGrid->EnableCellEditControl( true );
1042 m_fieldsGrid->ShowCellEditControl();
1043
1044 delete loc;
1045}
1046
1047
1049{
1050 VECTOR2I *loc = static_cast<VECTOR2I*>( event.GetClientData() );
1051
1052 wxCHECK_RET( loc, wxT( "Missing focus cell location" ) );
1053
1054 // Handle a delayed selection
1055 wxGridCellEditor* cellEditor = m_fieldsGrid->GetCellEditor( loc->x, loc->y );
1056
1057 if( wxTextEntry* txt = dynamic_cast<wxTextEntry*>( cellEditor->GetControl() ) )
1059
1060 cellEditor->DecRef(); // we're done; must release
1061}
1062
1063
1065{
1066 wxSize new_size = event.GetSize();
1067
1068 if( ( !m_editorShown || m_lastRequestedPinsSize != new_size ) && m_pinsSize != new_size )
1069 {
1070 m_pinsSize = new_size;
1071
1073 }
1074
1075 // We store this value to check whether the dialog is changing size. This might indicate
1076 // that the user is scaling the dialog with a grid-cell-editor shown. Some editors do not
1077 // close (at least on GTK) when the user drags a dialog corner
1078 m_lastRequestedPinsSize = new_size;
1079
1080 // Always propagate for a grid repaint (needed if the height changes, as well as width)
1081 event.Skip();
1082}
1083
1084
1085void DIALOG_SYMBOL_PROPERTIES::OnInitDlg( wxInitDialogEvent& event )
1086{
1088
1089 // Now all widgets have the size fixed, call FinishDialogSettings
1091
1092 EESCHEMA_SETTINGS* cfg = dynamic_cast<EESCHEMA_SETTINGS*>( Kiface().KifaceSettings() );
1093
1094 if( cfg && cfg->m_Appearance.edit_symbol_width > 0 && cfg->m_Appearance.edit_symbol_height > 0 )
1096}
1097
1098
1099void DIALOG_SYMBOL_PROPERTIES::OnCheckBox( wxCommandEvent& event )
1100{
1101 OnModify();
1102}
1103
1104
1105void DIALOG_SYMBOL_PROPERTIES::OnUnitChoice( wxCommandEvent& event )
1106{
1107 if( m_dataModel )
1108 {
1109 EDA_ITEM_FLAGS flags = m_symbol->GetFlags();
1110
1111 int unit_selection = m_unitChoice->GetSelection() + 1;
1112
1113 // We need to select a new unit to build the new unit pin list
1114 // but we should not change the symbol, so the initial unit will be selected
1115 // after rebuilding the pin list
1116 int old_unit = m_symbol->GetUnit();
1117 m_symbol->SetUnit( unit_selection );
1118
1119 // Rebuild a copy of the pins of the new unit for editing
1120 m_dataModel->clear();
1121
1122 for( const std::unique_ptr<SCH_PIN>& pin : m_symbol->GetRawPins() )
1123 m_dataModel->push_back( *pin );
1124
1125 m_dataModel->SortRows( COL_NUMBER, true );
1126 m_dataModel->BuildAttrs();
1127
1128 m_symbol->SetUnit( old_unit );
1129
1130 // Restore m_Flag modified by SetUnit()
1131 m_symbol->ClearFlags();
1132 m_symbol->SetFlags( flags );
1133 }
1134
1135 OnModify();
1136}
1137
1138
1140{
1141 event.Enable( m_symbol && m_symbol->GetLibSymbolRef() );
1142}
1143
1144
1146{
1147 event.Enable( m_symbol && m_symbol->GetLibSymbolRef() );
1148}
1149
1150
1151void DIALOG_SYMBOL_PROPERTIES::OnPageChanging( wxBookCtrlEvent& aEvent )
1152{
1153 if( !m_fieldsGrid->CommitPendingChanges() )
1154 aEvent.Veto();
1155
1156 if( !m_pinGrid->CommitPendingChanges() )
1157 aEvent.Veto();
1158}
KIFACE_BASE & Kiface()
Global KIFACE_BASE "get" accessor.
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
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:607
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:397
virtual void SetTextAngle(const EDA_ANGLE &aAngle)
Definition eda_text.cpp:310
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:61
APP_SETTINGS_BASE * KifaceSettings() const
Definition kiface_base.h:95
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:432
const std::map< wxString, ALT > & GetAlternates() const
Definition sch_pin.h:147
ALT GetAlt(const wxString &aAlt)
Definition sch_pin.h:161
SCH_PIN * GetLibPin() const
Definition sch_pin.h:89
const wxString & GetName() const
Definition sch_pin.cpp:400
const wxString & GetBaseName() const
Get the name without any alternates.
Definition sch_pin.cpp:409
const wxString & GetNumber() const
Definition sch_pin.h:124
GRAPHIC_PINSHAPE GetShape() const
Definition sch_pin.cpp:277
ELECTRICAL_PINTYPE GetType() const
Definition sch_pin.cpp:312
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:75
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:194
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:169
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:63
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
Definition for symbol library class.
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".
VECTOR3I res
VECTOR2I end
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695