KiCad PCB EDA Suite
Loading...
Searching...
No Matches
sch_properties_panel.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) 2020 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 3
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
23
25#include <font/fontconfig.h>
26#include <pgm_base.h>
27#include <common.h>
28#include <confirm.h>
29#include <connection_graph.h>
33#include <properties/property.h>
34#include <sch_commit.h>
35#include <sch_edit_frame.h>
36#include <sch_sheet.h>
37#include <symbol_edit_frame.h>
38#include <symbol_viewer_frame.h>
39#include <schematic.h>
40#include <sch_symbol.h>
41#include <lib_symbol.h>
42#include <sch_line.h>
43#include <sch_junction.h>
44#include <sch_field.h>
45#include <validators.h>
46#include <pin_map.h>
47#include <template_fieldnames.h>
49#include <string_utils.h>
50#include <tool/tool_manager.h>
51#include <tools/sch_actions.h>
54#include <wx_filename.h>
55
56
59
61 PROPERTIES_PANEL( aParent, aFrame ),
62 m_frame( aFrame ),
63 m_propMgr( PROPERTY_MANAGER::Instance() ),
64 m_unitEditorInstance( nullptr ),
65 m_checkboxEditorInstance( nullptr ),
66 m_colorEditorInstance( nullptr ),
67 m_fpEditorInstance( nullptr ),
68 m_urlEditorInstance( nullptr )
69{
70 addCategoryButton( _HKI( "Fields" ), _( "Add Field" ), BITMAPS::small_plus,
71 [this]()
72 {
74 } );
75
76 addCategoryButton( _HKI( "Custom Properties" ), _( "Add Custom Property" ), BITMAPS::small_plus,
77 [this]()
78 {
80 } );
81
83 _HKI( "Pin Map" ), _( "Edit Pin Map..." ), BITMAPS::small_edit,
84 [this]()
85 {
87 },
88 [this]()
89 {
90 return m_frame->IsType( FRAME_SCH ) && getSinglePinMappedSymbol() != nullptr;
91 },
92 /* aForceCategory */ true );
93
94 m_propMgr.Rebuild();
95 bool found = false;
96
97 wxASSERT( wxPGGlobalVars );
98
99 wxString editorKey = PG_UNIT_EDITOR::BuildEditorName( m_frame );
100
101 auto it = wxPGGlobalVars->m_mapEditorClasses.find( editorKey );
102
103 if( it != wxPGGlobalVars->m_mapEditorClasses.end() )
104 {
105 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( it->second );
106 m_unitEditorInstance->UpdateFrame( m_frame );
107 found = true;
108 }
109
110 if( !found )
111 {
112 PG_UNIT_EDITOR* new_editor = new PG_UNIT_EDITOR( m_frame );
113 m_unitEditorInstance = static_cast<PG_UNIT_EDITOR*>( wxPropertyGrid::RegisterEditorClass( new_editor ) );
114 }
115
116 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_CHECKBOX_EDITOR::EDITOR_NAME );
117
118 if( it == wxPGGlobalVars->m_mapEditorClasses.end() )
119 {
120 PG_CHECKBOX_EDITOR* cbEditor = new PG_CHECKBOX_EDITOR();
121 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( wxPropertyGrid::RegisterEditorClass( cbEditor ) );
122 }
123 else
124 {
125 m_checkboxEditorInstance = static_cast<PG_CHECKBOX_EDITOR*>( it->second );
126 }
127
128 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_COLOR_EDITOR::EDITOR_NAME );
129
130 if( it == wxPGGlobalVars->m_mapEditorClasses.end() )
131 {
132 PG_COLOR_EDITOR* colorEditor = new PG_COLOR_EDITOR();
133 m_colorEditorInstance = static_cast<PG_COLOR_EDITOR*>( wxPropertyGrid::RegisterEditorClass( colorEditor ) );
134 }
135 else
136 {
137 m_colorEditorInstance = static_cast<PG_COLOR_EDITOR*>( it->second );
138 }
139
140 auto netlistCallback = [this]()
141 {
142 SCH_SELECTION& sel = m_frame->GetToolManager()->GetTool<SCH_SELECTION_TOOL>()->GetSelection();
143 LIB_SYMBOL* libSymbol = nullptr;
144
145 for( EDA_ITEM* item : sel )
146 {
147 if( item->Type() == SCH_SYMBOL_T )
148 {
149 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
150
151 if( !libSymbol )
152 libSymbol = symbol->GetLibSymbolRef().get();
153 else if( libSymbol != symbol->GetLibSymbolRef().get() )
154 return std::string( "" );
155 }
156 }
157
158 if( !libSymbol )
159 return std::string( "" );
160
161 wxString symbolNetlist;
162 wxArrayString pins;
163
164 for( SCH_PIN* pin : libSymbol->GetGraphicalPins( 0 /* all units */, 1 /* single bodyStyle */ ) )
165 pins.push_back( pin->GetNumber() + ' ' + pin->GetShownName() );
166
167 if( !pins.IsEmpty() )
168 symbolNetlist << EscapeString( wxJoin( pins, '\t' ), CTX_LINE );
169
170 symbolNetlist << wxS( "\r" );
171
172 wxArrayString fpFilters = libSymbol->GetFPFilters();
173
174 if( !fpFilters.IsEmpty() )
175 symbolNetlist << EscapeString( wxJoin( fpFilters, ' ' ), CTX_LINE );
176
177 symbolNetlist << wxS( "\r" );
178
179 return symbolNetlist.ToStdString();
180 };
181
182 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_FPID_EDITOR::BuildEditorName( m_frame ) );
183
184 if( it != wxPGGlobalVars->m_mapEditorClasses.end() )
185 {
186 m_fpEditorInstance = static_cast<PG_FPID_EDITOR*>( it->second );
187 m_fpEditorInstance->UpdateFrame( m_frame );
188 m_fpEditorInstance->UpdateCallback( netlistCallback );
189 }
190 else
191 {
192 PG_FPID_EDITOR* fpEditor = new PG_FPID_EDITOR( m_frame, netlistCallback );
193 m_fpEditorInstance = static_cast<PG_FPID_EDITOR*>( wxPropertyGrid::RegisterEditorClass( fpEditor ) );
194 }
195
196 it = wxPGGlobalVars->m_mapEditorClasses.find( PG_URL_EDITOR::BuildEditorName( m_frame ) );
197
198 if( it != wxPGGlobalVars->m_mapEditorClasses.end() )
199 {
200 m_urlEditorInstance = static_cast<PG_URL_EDITOR*>( it->second );
201 m_urlEditorInstance->UpdateFrame( m_frame );
202 }
203 else
204 {
205 PG_URL_EDITOR* urlEditor = new PG_URL_EDITOR( m_frame );
206 m_urlEditorInstance = static_cast<PG_URL_EDITOR*>( wxPropertyGrid::RegisterEditorClass( urlEditor ) );
207 }
208
209 Bind( wxEVT_MENU, &SCH_PROPERTIES_PANEL::onContextMenu, this, ID_CTX_ADD_FIELD );
213}
214
215
217{
218 m_unitEditorInstance->UpdateFrame( nullptr );
219 m_fpEditorInstance->UpdateFrame( nullptr );
220 m_urlEditorInstance->UpdateFrame( nullptr );
221}
222
223
225{
226 SCH_SELECTION_TOOL* selectionTool = m_frame->GetToolManager()->GetTool<SCH_SELECTION_TOOL>();
227 const SELECTION& selection = selectionTool->GetSelection();
228
229 if( selection.Empty() && m_frame->IsType( FRAME_SCH_SYMBOL_EDITOR ) )
230 {
231 SYMBOL_EDIT_FRAME* symbolFrame = static_cast<SYMBOL_EDIT_FRAME*>( m_frame );
232
233 if( symbolFrame->GetCurSymbol() )
234 {
235 aFallbackSelection.Clear();
236 aFallbackSelection.Add( symbolFrame->GetCurSymbol() );
237 return aFallbackSelection;
238 }
239 }
240
241 return selection;
242}
243
244
246{
247 SELECTION fallbackSelection;
248 const SELECTION& selection = getSelection( fallbackSelection );
249
250 return selection.Empty() ? nullptr : selection.Front();
251}
252
253
255{
256 SELECTION fallbackSelection;
257 const SELECTION& selection = getSelection( fallbackSelection );
258
259 // Will actually just be updatePropertyValues() if selection hasn't changed
260 rebuildProperties( selection );
261}
262
263
265{
266 SELECTION fallbackSelection;
267 const SELECTION& selection = getSelection( fallbackSelection );
268
269 rebuildProperties( selection );
270}
271
272
274{
277
278 for( EDA_ITEM* item : aSelection )
279 {
280 if( item->Type() == SCH_JUNCTION_T )
281 {
283
284 // Dummy property which allows concurrently-selected wires to be edited
285 m_propMgr.AddProperty( new DUMMY_PROPERTY<SCH_JUNCTION, WIRE_STYLE>( _HKI( "Wire Style" ) ) )
286 .SetAvailableFunc(
287 []( INSPECTABLE* )
288 {
290 } );
291
292 // Dummy property which allows concurrently-selected wires to be edited
293 m_propMgr.AddProperty( new DUMMY_PROPERTY<SCH_JUNCTION, int>( _HKI( "Line Width" ) ) )
294 .SetAvailableFunc(
295 []( INSPECTABLE* )
296 {
298 } );
299 }
300 else if( item->IsType( { SCH_ITEM_LOCATE_WIRE_T, SCH_ITEM_LOCATE_BUS_T } ) )
301 {
303
304 // Dummy property which allows concurrently-selected junctions to be edited
305 m_propMgr.AddProperty( new DUMMY_PROPERTY<SCH_LINE, int>( _HKI( "Diameter" ) ) )
306 .SetAvailableFunc(
307 []( INSPECTABLE* )
308 {
310 } );
311 }
312 }
313
315}
316
317
319{
320 SELECTION fallbackSelection;
321 const SELECTION& selection = getSelection( fallbackSelection );
322
323 if( selection.Size() != 1 || selection.Front()->Type() != SCH_SYMBOL_T )
324 return nullptr;
325
326 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( selection.Front() );
327
328 return symbol->HasEffectiveAssociatedFootprint() ? symbol : nullptr;
329}
330
331
333{
335
336 if( !symbol || !m_frame->IsType( FRAME_SCH ) )
337 return;
338
339 SCH_EDIT_FRAME* editFrame = static_cast<SCH_EDIT_FRAME*>( m_frame );
340
341 DIALOG_SYMBOL_PROPERTIES dlg( editFrame, symbol );
342 dlg.SelectPinMapPage();
343
344 // The dialog can subsequently invoke a KIWAY_PLAYER as a quasimodal frame, so it must run
345 // quasimodally to keep that support working.
346 int retval = dlg.ShowQuasiModal();
347
348 if( retval == SYMBOL_PROPS_EDIT_OK )
349 {
350 editFrame->OnModify();
351 AfterCommit();
352 }
353 else if( retval == SYMBOL_PROPS_WANT_SET_VARIANT_SYMBOL )
354 {
356 }
357 else if( retval == SYMBOL_PROPS_WANT_CLEAR_VARIANT_SYMBOL )
358 {
360 }
361}
362
363
364wxPGProperty* SCH_PROPERTIES_PANEL::createPGProperty( const PROPERTY_BASE* aProperty ) const
365{
366 wxPGProperty* prop = PGPropertyFactory( aProperty, m_frame );
367
368 if( auto colorProp = dynamic_cast<PGPROPERTY_COLOR4D*>( prop ) )
369 {
370 COLOR4D bg = m_frame->GetColorSettings()->GetColor( LAYER_SCHEMATIC_BACKGROUND );
371 colorProp->SetBackgroundColor( bg );
372 }
373
375 prop->SetEditor( PG_FPID_EDITOR::BuildEditorName( m_frame ) );
376 else if( aProperty->Name() == GetDefaultFieldName( FIELD_T::DATASHEET, UNTRANSLATED ) )
377 prop->SetEditor( PG_URL_EDITOR::BuildEditorName( m_frame ) );
378
379 return prop;
380}
381
382
383PROPERTY_BASE* SCH_PROPERTIES_PANEL::getPropertyFromEvent( const wxPropertyGridEvent& aEvent ) const
384{
385 EDA_ITEM* item = const_cast<SCH_PROPERTIES_PANEL*>( this )->getFrontItem();
386
387 if( !item || !item->IsSCH_ITEM() )
388 return nullptr;
389
390 SCH_ITEM* firstItem = static_cast<SCH_ITEM*>( item );
391
392 wxCHECK_MSG( firstItem, nullptr, wxT( "getPropertyFromEvent for a property with nothing selected!") );
393
394 PROPERTY_BASE* property = m_propMgr.GetProperty( firstItem, aEvent.GetPropertyName() );
395 wxCHECK_MSG( property, nullptr, wxT( "getPropertyFromEvent for a property not found on the selected item!" ) );
396
397 return property;
398}
399
400
401void SCH_PROPERTIES_PANEL::valueChanging( wxPropertyGridEvent& aEvent )
402{
404 return;
405
406 if( !getFrontItem() || !getPropertyFromEvent( aEvent ) )
407 return;
408
409 SELECTION fallbackSelection;
410 const SELECTION& selection = getSelection( fallbackSelection );
411 wxVariant newValue = aEvent.GetPropertyValue();
412
413 // valueChanged() writes every selected item, so a mixed selection must be valid for each of them
414 for( EDA_ITEM* item : selection )
415 {
416 PROPERTY_BASE* property = m_propMgr.GetProperty( item, aEvent.GetPropertyName() );
417
418 if( !property )
419 continue;
420
421 wxString errorMsg;
422
423 if( VALIDATOR_RESULT validationFailure = property->Validate( newValue.GetAny(), item ) )
424 {
425 errorMsg = wxString::Format( wxS( "%s: %s" ), wxGetTranslation( property->Name() ),
426 validationFailure->get()->Format( m_frame ) );
427 }
428 else if( SCH_FIELD* field = dynamic_cast<SCH_FIELD*>( item );
429 field && field->GetId() == FIELD_T::SHEET_REF_SUFFIX && property->Name() == wxS( "Text" ) )
430 {
431 // The suffix field's own Text property is generic and carries no field rules
432 errorMsg = GetFieldValidationErrorMessage( FIELD_T::SHEET_REF_SUFFIX, newValue.GetString() );
433 }
434 else if( SCH_SYMBOL* symbol = field ? dynamic_cast<SCH_SYMBOL*>( field->GetParentSymbol() ) : nullptr;
435 symbol && symbol->Schematic() && field->GetId() == FIELD_T::REFERENCE
436 && property->Name() == wxS( "Text" ) )
437 {
438 wxString base;
439 errorMsg = symbol->BaseRefFromUser( symbol->Schematic()->CurrentSheet(), newValue.GetString(), base );
440 }
441
442 if( !errorMsg.IsEmpty() )
443 {
444 m_frame->ShowInfoBarError( errorMsg );
445 aEvent.Veto();
446 m_ValueChangeVetoed = true;
447 return;
448 }
449 }
450
451 aEvent.Skip();
452}
453
454
455void SCH_PROPERTIES_PANEL::valueChanged( wxPropertyGridEvent& aEvent )
456{
458 return;
459
460 SELECTION fallbackSelection;
461 const SELECTION& selection = getSelection( fallbackSelection );
462
463 wxCHECK( getPropertyFromEvent( aEvent ), /* void */ );
464
465 wxVariant newValue = aEvent.GetPropertyValue();
466 SCH_COMMIT changes( m_frame );
467 SCH_SCREEN* screen = m_frame->GetScreen();
468
469 PROPERTY_COMMIT_HANDLER handler( &changes );
470
471 for( EDA_ITEM* edaItem : selection )
472 {
473 if( !edaItem->IsSCH_ITEM() )
474 continue;
475
476 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
477 PROPERTY_BASE* property = m_propMgr.GetProperty( item, aEvent.GetPropertyName() );
478 wxCHECK2( property, continue );
479
480 // Editing reference text in the schematic must go through the parent symbol in order to handle
481 // symbol instance data properly.
482 if( item->Type() == SCH_FIELD_T && static_cast<SCH_FIELD*>( item )->GetId() == FIELD_T::REFERENCE
483 && m_frame->IsType( FRAME_SCH )
484 && property->Name() == wxT( "Text" ) )
485 {
486 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item->GetParentSymbol() );
487 wxCHECK2( symbol, continue );
488
489 changes.Modify( symbol, screen, RECURSE_MODE::NO_RECURSE );
490 symbol->SetRefProp( newValue.GetString() );
491 symbol->SyncOtherUnits( symbol->Schematic()->CurrentSheet(), changes, property );
492 continue;
493 }
494
495 // Editing field text in the schematic when a variant is active must use variant-aware
496 // SetText to properly store the value as a variant override.
497 if( item->Type() == SCH_FIELD_T
498 && m_frame->IsType( FRAME_SCH )
499 && property->Name() == wxT( "Text" ) )
500 {
501 SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
502 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item->GetParentSymbol() );
503
504 if( symbol && symbol->Schematic() )
505 {
506 wxString variantName = symbol->Schematic()->GetCurrentVariant();
507
508 if( !variantName.IsEmpty() )
509 {
510 changes.Modify( symbol, screen, RECURSE_MODE::NO_RECURSE );
511 field->SetText( newValue.GetString(), &symbol->Schematic()->CurrentSheet(), variantName );
512 symbol->SyncOtherUnits( symbol->Schematic()->CurrentSheet(), changes, nullptr, variantName );
513 continue;
514 }
515 }
516 }
517
518 // Changing a sheet's filename field requires file operations to match the dialog behavior.
519 if( item->Type() == SCH_FIELD_T
520 && m_frame->IsType( FRAME_SCH )
521 && property->Name() == wxT( "Text" ) )
522 {
523 SCH_FIELD* field = static_cast<SCH_FIELD*>( item );
524 SCH_SHEET* sheet = dynamic_cast<SCH_SHEET*>( item->GetParent() );
525
526 if( sheet && field->GetId() == FIELD_T::SHEET_FILENAME )
527 {
528 SCH_EDIT_FRAME* editFrame = static_cast<SCH_EDIT_FRAME*>( m_frame );
529
530 if( !handleSheetFilenameChange( editFrame, sheet, changes, newValue.GetString() ) )
531 {
532 UpdateData();
533 return;
534 }
535
536 continue;
537 }
538 }
539
540 if( item->Type() == SCH_TABLECELL_T )
541 changes.Modify( item->GetParent(), screen, RECURSE_MODE::NO_RECURSE );
542 else
543 changes.Modify( item, screen, RECURSE_MODE::NO_RECURSE );
544
545 item->Set( property, newValue );
546
547 if( SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( item ) )
548 {
549 symbol->SyncOtherUnits( symbol->Schematic()->CurrentSheet(), changes, property,
550 symbol->Schematic()->GetCurrentVariant() );
551 }
552 }
553
554 changes.Push( _( "Edit Properties" ) );
555
556 // Force a repaint of the items whose properties were changed
557 // This is necessary to update field displays in the schematic view
558 for( EDA_ITEM* edaItem : selection )
559 m_frame->UpdateItem( edaItem );
560
561 // Perform grid updates as necessary based on value change
562 AfterCommit();
563
564 aEvent.Skip();
565}
566
567
569 SCH_COMMIT& aChanges,
570 const wxString& aNewFilename )
571{
572 wxString newFilename = EnsureFileExtension( aNewFilename, FILEEXT::KiCadSchematicFileExtension );
573
574 if( newFilename.IsEmpty() || !IsFullFileNameValid( newFilename ) )
575 {
576 DisplayError( aFrame, _( "A sheet must have a valid file name." ) );
577 return false;
578 }
579
580 // Normalize separators to unix notation
581 newFilename.Replace( wxT( "\\" ), wxT( "/" ) );
582
583 wxString oldFilename = aSheet->GetFileName();
584 oldFilename.Replace( wxT( "\\" ), wxT( "/" ) );
585
586 if( newFilename == oldFilename )
587 return true;
588
589 if( !aFrame->ChangeSheetFile( aSheet, newFilename ) )
590 return false;
591
592 SCH_SCREEN* currentScreen = aFrame->GetCurrentSheet().LastScreen();
593 aChanges.Modify( aSheet, currentScreen, RECURSE_MODE::NO_RECURSE );
594 aSheet->SetFileName( newFilename );
595
596 return true;
597}
598
599
600bool SCH_PROPERTIES_PANEL::isKeyEditable( const wxPGProperty* aPGProp ) const
601{
602 PROPERTY_BASE* prop = static_cast<PROPERTY_BASE*>( aPGProp->GetClientData() );
603
604 if( !prop )
605 return false;
606
607 EDA_ITEM* item = const_cast<SCH_PROPERTIES_PANEL*>( this )->getFrontItem();
608
609 if( !item )
610 return false;
611
612 if( prop->Group() == _HKI( "Custom Properties" ) )
613 return true;
614
615 if( item->Type() == SCH_SYMBOL_T )
616 {
617 SCH_FIELD* field = static_cast<SCH_SYMBOL*>( item )->GetField( prop->Name() );
618 return field && !field->IsMandatory();
619 }
620 else if( item->Type() == SCH_SHEET_T )
621 {
622 SCH_FIELD* field = static_cast<SCH_SHEET*>( item )->GetField( prop->Name() );
623 return field && !field->IsMandatory();
624 }
625
626 return false;
627}
628
629
630bool SCH_PROPERTIES_PANEL::isKeyNameInUse( const wxString& aName ) const
631{
632 EDA_ITEM* item = const_cast<SCH_PROPERTIES_PANEL*>( this )->getFrontItem();
633
634 if( !item )
635 return false;
636
637 return m_propMgr.GetProperty( item, aName ) != nullptr;
638}
639
640
641void SCH_PROPERTIES_PANEL::onKeyRenamed( const wxString& aOldName, const wxString& aNewName )
642{
643 SELECTION fallbackSelection;
644 const SELECTION& selection = getSelection( fallbackSelection );
645
646 // Renaming to the suffix name retypes the field, so its existing text must already be a valid suffix
648 {
649 for( EDA_ITEM* edaItem : selection )
650 {
651 if( edaItem->Type() != SCH_SHEET_T )
652 continue;
653
654 const SCH_FIELD* field = std::as_const( *static_cast<SCH_SHEET*>( edaItem ) ).GetField( aOldName );
655
656 if( !field )
657 continue;
658
660
661 if( !msg.IsEmpty() )
662 {
663 m_frame->ShowInfoBarError( msg );
664 UpdateData();
665 return;
666 }
667 }
668 }
669
670 SCH_COMMIT changes( m_frame );
671 SCH_SCREEN* screen = m_frame->GetScreen();
672 PROPERTY_COMMIT_HANDLER handler( &changes );
673
674 for( EDA_ITEM* edaItem : selection )
675 {
676 if( !edaItem->IsSCH_ITEM() )
677 continue;
678
679 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
680 bool renamed = false;
681
682 if( item->Type() == SCH_SYMBOL_T )
683 {
684 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
685 SCH_FIELD* field = symbol->GetField( aOldName );
686
687 if( field && !field->IsMandatory() )
688 {
689 changes.Modify( symbol, screen, RECURSE_MODE::NO_RECURSE );
690 field->SetName( aNewName );
691 renamed = true;
692 }
693 }
694 else if( item->Type() == SCH_SHEET_T )
695 {
696 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
697 SCH_FIELD* field = sheet->GetField( aOldName );
698
699 if( field && !field->IsMandatory() )
700 {
701 changes.Modify( sheet, screen, RECURSE_MODE::NO_RECURSE );
702 field->SetName( aNewName );
703 renamed = true;
704 }
705 }
706
707 if( !renamed )
708 {
709 if( wxString value; item->GetCustomProperty( aOldName, value ) )
710 {
711 changes.Modify( item, screen, RECURSE_MODE::NO_RECURSE );
712 item->RemoveCustomProperty( aOldName );
713 item->SetCustomProperty( aNewName, value );
714 }
715 }
716 }
717
718 changes.Push( _( "Rename Property" ) );
719
720 AfterCommit();
721}
722
723
724bool SCH_PROPERTIES_PANEL::buildContextMenu( wxMenu& aMenu, wxPGProperty* aPGProp )
725{
726 if( aPGProp->IsCategory() )
727 {
728 if( aPGProp->GetLabel() == wxGetTranslation( _HKI( "Fields" ) ) )
729 aMenu.Append( ID_CTX_ADD_FIELD, _( "Add Field" ) );
730 else if( aPGProp->GetLabel() == wxGetTranslation( _HKI( "Custom Properties" ) ) )
731 aMenu.Append( ID_CTX_ADD_CUSTOM_PROPERTY, _( "Add Custom Property" ) );
732 }
733 else
734 {
735 PROPERTY_BASE* prop = static_cast<PROPERTY_BASE*>( aPGProp->GetClientData() );
736
737 if( !prop )
738 return false;
739
740 if( prop->Group() == _HKI( "Fields" ) )
741 {
742 if( isKeyEditable( aPGProp ) )
743 aMenu.Append( ID_CTX_REMOVE_FIELD, _( "Remove Field" ) );
744
745 aMenu.Append( ID_CTX_ADD_FIELD, _( "Add Field" ) );
746 }
747 else if( prop->Group() == _HKI( "Custom Properties" ) )
748 {
749 aMenu.Append( ID_CTX_REMOVE_CUSTOM_PROPERTY, _( "Remove Custom Property" ) );
750 aMenu.Append( ID_CTX_ADD_CUSTOM_PROPERTY, _( "Add Custom Property" ) );
751 }
752 }
753
754 return aMenu.GetMenuItemCount() > 0;
755}
756
757
758void SCH_PROPERTIES_PANEL::onContextMenu( wxCommandEvent& aEvent )
759{
760 switch( aEvent.GetId() )
761 {
762 case ID_CTX_ADD_FIELD: addBlankField(); break;
766 default: break;
767 }
768}
769
770
772{
773 SELECTION fallbackSelection;
774 const SELECTION& selection = getSelection( fallbackSelection );
775
777
778 if( selection.Empty() )
779 return;
780
781 wxString name;
782
783 for( int n = 0; ; ++n )
784 {
786 bool used = false;
787
788 for( EDA_ITEM* item : selection )
789 {
790 if( item->Type() == SCH_SYMBOL_T && static_cast<SCH_SYMBOL*>( item )->GetField( name ) )
791 used = true;
792 else if( item->Type() == SCH_SHEET_T && static_cast<SCH_SHEET*>( item )->GetField( name ) )
793 used = true;
794 }
795
796 if( !used )
797 break;
798 }
799
800 SCH_COMMIT changes( m_frame );
801 SCH_SCREEN* screen = m_frame->GetScreen();
802 PROPERTY_COMMIT_HANDLER handler( &changes );
803
804 for( EDA_ITEM* edaItem : selection )
805 {
806 if( !edaItem->IsSCH_ITEM() )
807 continue;
808
809 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
810
811 if( item->Type() == SCH_SYMBOL_T )
812 {
813 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
814
815 changes.Modify( symbol, screen, RECURSE_MODE::NO_RECURSE );
816 symbol->AddField( SCH_FIELD( symbol, FIELD_T::USER, name ) )->SetVisible( false );
817 }
818 else if( item->Type() == SCH_SHEET_T )
819 {
820 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
821
822 changes.Modify( sheet, screen, RECURSE_MODE::NO_RECURSE );
823 sheet->AddField( SCH_FIELD( sheet, FIELD_T::SHEET_USER, name ) )->SetVisible( false );
824 }
825 }
826
827 changes.Push( _( "Add Field" ) );
828 AfterCommit();
829
831
832 beginLabelEdit( name, true );
833}
834
835
837{
838 SELECTION fallbackSelection;
839 const SELECTION& selection = getSelection( fallbackSelection );
840
842
843 if( selection.Empty() )
844 return;
845
846 wxString name;
847
848 for( int n = 0; ; ++n )
849 {
850 name = wxString::Format( wxS( "Property%d" ), n );
851 bool used = false;
852
853 for( EDA_ITEM* item : selection )
854 {
855 if( wxString dummy; item->GetCustomProperty( name, dummy ) )
856 {
857 used = true;
858 break;
859 }
860 }
861
862 if( !used )
863 break;
864 }
865
866 SCH_COMMIT changes( m_frame );
867 SCH_SCREEN* screen = m_frame->GetScreen();
868 PROPERTY_COMMIT_HANDLER handler( &changes );
869
870 for( EDA_ITEM* edaItem : selection )
871 {
872 if( !edaItem->IsSCH_ITEM() )
873 continue;
874
875 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
876
877 changes.Modify( item, screen, RECURSE_MODE::NO_RECURSE );
878 item->SetCustomProperty( name, wxEmptyString );
879 }
880
881 changes.Push( _( "Add Custom Property" ) );
882 AfterCommit();
883
885
886 beginLabelEdit( name, true );
887}
888
889
890void SCH_PROPERTIES_PANEL::removeField( const wxString& aName )
891{
892 SELECTION fallbackSelection;
893 const SELECTION& selection = getSelection( fallbackSelection );
894
895 SCH_COMMIT changes( m_frame );
896 SCH_SCREEN* screen = m_frame->GetScreen();
897 PROPERTY_COMMIT_HANDLER handler( &changes );
898
899 for( EDA_ITEM* edaItem : selection )
900 {
901 if( !edaItem->IsSCH_ITEM() )
902 continue;
903
904 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
905
906 if( item->Type() == SCH_SYMBOL_T )
907 {
908 SCH_SYMBOL* symbol = static_cast<SCH_SYMBOL*>( item );
909 SCH_FIELD* field = symbol->GetField( aName );
910
911 if( field && !field->IsMandatory() )
912 {
913 changes.Modify( symbol, screen, RECURSE_MODE::NO_RECURSE );
914 symbol->RemoveField( aName );
915 }
916 }
917 else if( item->Type() == SCH_SHEET_T )
918 {
919 SCH_SHEET* sheet = static_cast<SCH_SHEET*>( item );
920 auto& fields = sheet->GetFields();
921
922 for( auto it = fields.begin(); it != fields.end(); ++it )
923 {
924 if( !it->IsMandatory() && it->GetName() == aName )
925 {
926 changes.Modify( sheet, screen, RECURSE_MODE::NO_RECURSE );
927 fields.erase( it );
928 break;
929 }
930 }
931 }
932 }
933
934 changes.Push( _( "Remove Field" ) );
935 AfterCommit();
936}
937
938
940{
941 SELECTION fallbackSelection;
942 const SELECTION& selection = getSelection( fallbackSelection );
943
944 SCH_COMMIT changes( m_frame );
945 SCH_SCREEN* screen = m_frame->GetScreen();
946 PROPERTY_COMMIT_HANDLER handler( &changes );
947
948 for( EDA_ITEM* edaItem : selection )
949 {
950 if( !edaItem->IsSCH_ITEM() )
951 continue;
952
953 SCH_ITEM* item = static_cast<SCH_ITEM*>( edaItem );
954
955 changes.Modify( item, screen, RECURSE_MODE::NO_RECURSE );
956 item->RemoveCustomProperty( aName );
957 }
958
959 changes.Push( _( "Remove Custom Property" ) );
960 AfterCommit();
961}
962
963
965{
966 // Note: currently assuming that any newly-created item from the panel is either a field or
967 // a custom property because those are the types we currently support.
968 if( EDA_ITEM* item = getFrontItem() )
969 {
970 if( item->Type() == SCH_SYMBOL_T && static_cast<SCH_SYMBOL*>( item )->GetField( aKey ) )
971 removeField( aKey );
972 else if( item->Type() == SCH_SHEET_T && static_cast<SCH_SHEET*>( item )->GetField( aKey ) )
973 removeField( aKey );
974 else
975 removeCustomProperty( aKey );
976 }
977}
978
979
980void SCH_PROPERTIES_PANEL::OnLanguageChanged( wxCommandEvent& aEvent )
981{
983
984 aEvent.Skip();
985}
986
987
988bool SCH_PROPERTIES_PANEL::getItemValue( EDA_ITEM* aItem, PROPERTY_BASE* aProperty, wxVariant& aValue )
989{
990 // For SCH_FIELD "Text" property, return the variant-aware value when a variant is active
991 if( aItem->Type() == SCH_FIELD_T
992 && m_frame->IsType( FRAME_SCH )
993 && aProperty->Name() == wxT( "Text" ) )
994 {
995 SCH_FIELD* field = static_cast<SCH_FIELD*>( aItem );
996 SCH_SYMBOL* symbol = dynamic_cast<SCH_SYMBOL*>( field->GetParentSymbol() );
997
998 if( symbol && symbol->Schematic() )
999 {
1000 wxString variantName = symbol->Schematic()->GetCurrentVariant();
1001
1002 if( !variantName.IsEmpty() )
1003 {
1004 wxString text = field->GetText( &symbol->Schematic()->CurrentSheet(), variantName );
1005 aValue = wxVariant( text );
1006 return true;
1007 }
1008 }
1009 }
1010
1011 return PROPERTIES_PANEL::getItemValue( aItem, aProperty, aValue );
1012}
const char * name
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:102
Dialog used to edit SCH_SYMBOL objects in a schematic.
void SelectPinMapPage()
Select the Pin Map page so the dialog opens directly on the pin-to-pad mapping editor (issue #2282).
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
bool GetCustomProperty(const wxString &aKey, wxString &aValue) const
Definition eda_item.cpp:195
void RemoveCustomProperty(const wxString &aKey)
Definition eda_item.cpp:161
KICAD_T Type() const
Returns the type of object.
Definition eda_item.h:110
EDA_ITEM * GetParent() const
Definition eda_item.h:112
void SetCustomProperty(const wxString &aKey, const wxString &aValue)
Definition eda_item.h:255
virtual void SetVisible(bool aVisible)
Definition eda_text.cpp:348
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:39
bool Set(PROPERTY_BASE *aProperty, wxAny &aValue, bool aNotify=true)
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
bool IsSCH_ITEM() const
Definition view_item.h:97
Define a library symbol object.
Definition lib_symbol.h:114
wxArrayString GetFPFilters() const
Definition lib_symbol.h:254
std::vector< SCH_PIN * > GetGraphicalPins(int aUnit=0, int aBodyStyle=0) const override
Graphical pins: Return schematic pin objects as drawn (unexpanded), filtered by unit/body.
static const wxString EDITOR_NAME
Definition pg_editors.h:75
static const wxString EDITOR_NAME
Definition pg_editors.h:91
static wxString BuildEditorName(EDA_DRAW_FRAME *aFrame)
static wxString BuildEditorName(EDA_DRAW_FRAME *aFrame)
static wxString BuildEditorName(EDA_DRAW_FRAME *aFrame)
wxString m_contextMenuPropertyName
PROPERTIES_PANEL(wxWindow *aParent, EDA_BASE_FRAME *aFrame)
void beginLabelEdit(const wxString &aKey, bool aStartBlank=false)
void settlePendingLabelEdit()
Synchronously ends any in-progress label edit; potentially canceling a newly added row.
virtual void OnLanguageChanged(wxCommandEvent &aEvent)
virtual bool getItemValue(EDA_ITEM *aItem, PROPERTY_BASE *aProperty, wxVariant &aValue)
Utility to fetch a property value and convert to wxVariant Precondition: aItem is known to have prope...
virtual void rebuildProperties(const SELECTION &aSelection)
Generates the property grid for a given selection of items.
void addCategoryButton(const wxString &aGroupKey, const wxString &aTooltip, BITMAPS aBitmap, std::function< void()> aAction, std::function< bool()> aEnableFunc=nullptr, bool aForceCategory=false)
Registers an overlay button on a category caption row.
wxString m_pendingNewKey
Key of a freshly-added blank field/custom property awaiting a name from the user.
wxString Group() const
Definition property.h:394
const wxString & Name() const
Definition property.h:221
Provide class metadata.
wxString GetCurrentVariant() const
Return the current variant being edited.
SCH_SHEET_PATH & CurrentSheet() const
Definition schematic.h:399
static TOOL_ACTION clearVariantSymbol
static TOOL_ACTION setVariantSymbol
A shim class between EDA_DRAW_FRAME and several derived classes: SYMBOL_EDIT_FRAME,...
Commit for the schematic and symbol editors.
Definition sch_commit.h:54
void Push(const wxString &aMessage=wxEmptyString, int aCommitFlags=0) override
Execute the changes.
Schematic editor (Eeschema) main window.
void OnModify() override
Must be called after a schematic change in order to set the "modify" flag and update other data struc...
SCH_SHEET_PATH & GetCurrentSheet() const
bool ChangeSheetFile(SCH_SHEET *aSheet, const wxString &aNewFilename, bool *aClearAnnotationNewItems=nullptr, bool *aIsUndoable=nullptr, const wxString *aSourceSheetFilename=nullptr)
Change the file backing a schematic sheet.
Definition sheet.cpp:175
bool IsMandatory() const
virtual const wxString & GetText() const override
Return the string associated with the text object.
Definition sch_field.h:139
FIELD_T GetId() const
Definition sch_field.h:143
void SetName(const wxString &aName)
void SetText(const wxString &aText) override
Base class for any item which can be embedded within the SCHEMATIC container class,...
Definition sch_item.h:170
const SYMBOL * GetParentSymbol() const
Definition sch_item.cpp:350
SCHEMATIC * Schematic() const
Search the item hierarchy to find a SCHEMATIC.
Definition sch_item.cpp:302
bool getItemValue(EDA_ITEM *aItem, PROPERTY_BASE *aProperty, wxVariant &aValue) override
Utility to fetch a property value and convert to wxVariant Precondition: aItem is known to have prope...
void onContextMenu(wxCommandEvent &aEvent)
bool buildContextMenu(wxMenu &aMenu, wxPGProperty *aPGProp) override
bool isKeyEditable(const wxPGProperty *aPGProp) const override
wxPGProperty * createPGProperty(const PROPERTY_BASE *aProperty) const override
PROPERTY_MANAGER & m_propMgr
void removeCustomProperty(const wxString &aName)
PG_CHECKBOX_EDITOR * m_checkboxEditorInstance
PG_FPID_EDITOR * m_fpEditorInstance
const SELECTION & getSelection(SELECTION &aFallbackSelection)
Get the current selection from the selection tool.
void onEditPinMap()
Open the symbol properties dialog on its Pin Map page for the single selected symbol (issue #2282).
void valueChanging(wxPropertyGridEvent &aEvent) override
PG_UNIT_EDITOR * m_unitEditorInstance
bool isKeyNameInUse(const wxString &aName) const override
void removeField(const wxString &aName)
void valueChanged(wxPropertyGridEvent &aEvent) override
void onKeyRenamed(const wxString &aOldName, const wxString &aNewName) override
SCH_PROPERTIES_PANEL(wxWindow *aParent, SCH_BASE_FRAME *aFrame)
void onNewItemLeftBlank(const wxString &aKey) override
PG_COLOR_EDITOR * m_colorEditorInstance
EDA_ITEM * getFrontItem()
Get the front item of the current selection.
PROPERTY_BASE * getPropertyFromEvent(const wxPropertyGridEvent &aEvent) const
void OnLanguageChanged(wxCommandEvent &aEvent) override
bool handleSheetFilenameChange(SCH_EDIT_FRAME *aFrame, SCH_SHEET *aSheet, SCH_COMMIT &aChanges, const wxString &aNewFilename)
void rebuildProperties(const SELECTION &aSelection) override
Generates the property grid for a given selection of items.
PG_URL_EDITOR * m_urlEditorInstance
SCH_SYMBOL * getSinglePinMappedSymbol()
SCH_SELECTION & GetSelection()
SCH_SCREEN * LastScreen()
Sheet symbol placed in a schematic, and is the entry point for a sub schematic.
Definition sch_sheet.h:50
void SetFileName(const wxString &aFilename)
Definition sch_sheet.h:434
wxString GetFileName() const
Return the filename corresponding to this sheet.
Definition sch_sheet.h:428
std::vector< SCH_FIELD > & GetFields()
Return a reference to the vector holding the sheet's fields.
Definition sch_sheet.h:93
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this sheet.
SCH_FIELD * AddField(const SCH_FIELD &aField)
Add aFieldName to the list of fields.
Schematic symbol object.
Definition sch_symbol.h:76
void SetRefProp(const wxString &aRef)
void RemoveField(const wxString &aFieldName)
Remove a user field from the symbol.
void SyncOtherUnits(const SCH_SHEET_PATH &aSourceSheet, SCH_COMMIT &aCommit, PROPERTY_BASE *aProperty, const wxString &aVariantName=wxEmptyString)
Keep fields other than the reference, include/exclude flags, and alternate pin assignments in sync in...
SCH_FIELD * AddField(const SCH_FIELD &aField)
Add a field to the symbol.
std::unique_ptr< LIB_SYMBOL > & GetLibSymbolRef()
Definition sch_symbol.h:185
bool HasEffectiveAssociatedFootprint() const
SCH_FIELD * GetField(FIELD_T aFieldType)
Return a mandatory field in this symbol.
virtual void Add(EDA_ITEM *aItem)
A null aItem is ignored; the selection never holds null members.
Definition selection.cpp:38
EDA_ITEM * Front() const
Definition selection.h:176
virtual void Clear() override
Remove all the stored items from the group.
Definition selection.h:97
int Size() const
Returns the number of selected parts.
Definition selection.h:120
bool Empty() const
Checks if there is anything selected.
Definition selection.h:114
The symbol library editor main window.
LIB_SYMBOL * GetCurSymbol() const
Return the current symbol being edited or NULL if none selected.
TOOL_MANAGER * GetToolManager() const
Return the MVC controller.
bool RunAction(const std::string &aActionName, T aParam)
Run the specified action immediately, pausing the current action to run the new one.
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition common.cpp:880
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:192
This file is part of the common library.
@ SYMBOL_PROPS_WANT_SET_VARIANT_SYMBOL
@ SYMBOL_PROPS_EDIT_OK
@ SYMBOL_PROPS_WANT_CLEAR_VARIANT_SYMBOL
#define _(s)
@ NO_RECURSE
Definition eda_item.h:52
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
@ FRAME_SCH
Definition frame_type.h:30
static const std::string KiCadSchematicFileExtension
@ LAYER_SCHEMATIC_BACKGROUND
Definition layer_ids.h:519
#define _HKI(x)
Definition page_info.cpp:40
wxPGProperty * PGPropertyFactory(const PROPERTY_BASE *aProperty, EDA_DRAW_FRAME *aFrame)
see class PGM_BASE
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
@ ID_CTX_REMOVE_CUSTOM_PROPERTY
@ ID_CTX_REMOVE_FIELD
@ ID_CTX_ADD_CUSTOM_PROPERTY
@ ID_CTX_ADD_FIELD
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.
std::vector< FAB_LAYER_COLOR > dummy
wxString EscapeString(const wxString &aSource, ESCAPE_CONTEXT aContext)
The Escape/Unescape routines use HTML-entity-reference-style encoding to handle characters which are:...
bool IsFullFileNameValid(const wxString &aFullFilename)
Checks if a full filename is valid, i.e.
@ CTX_LINE
wxString GetUserFieldName(int aFieldNdx, TRANSLATION aTranslation)
wxString GetDefaultFieldName(FIELD_T aFieldId, TRANSLATION aTranslation)
Return a default symbol field name for a mandatory field type.
@ USER
The field ID hasn't been set yet; field is invalid.
@ FOOTPRINT
Field Name Module PCB, i.e. "16DIP300".
@ SHEET_REF_SUFFIX
Optional sheet field appended to the references of the sheet's symbols.
@ DATASHEET
name of datasheet
@ REFERENCE
Field Reference of part, i.e. "IC21".
@ UNTRANSLATED
KIBIS_PIN * pin
@ SCH_SYMBOL_T
Definition typeinfo.h:168
@ SCH_TABLECELL_T
Definition typeinfo.h:162
@ SCH_FIELD_T
Definition typeinfo.h:146
@ SCH_SHEET_T
Definition typeinfo.h:171
@ SCH_JUNCTION_T
Definition typeinfo.h:155
wxString GetFieldValidationErrorMessage(FIELD_T aFieldId, const wxString &aValue)
Return the error message if aValue is invalid for aFieldId.
Custom text control validator definitions.
Definition of file extensions used in Kicad.