From 5fc5a2132bc2bf49814d5be11c06324335de3296 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Tue, 29 Nov 2022 08:31:00 -0800 Subject: [PATCH] Fix Properties panel to reference the correct frame The properties editors are global but the frame that they reference is not static, so we need to be able to update the frame reference when restarting pcb editor from the main window. This updates the frame, being careful to remove the signalling when closing pcbnew but keeping the instance of the editor in place. Fixes https://gitlab.com/kicad/code/kicad/issues/12297 --- common/properties/pg_editors.cpp | 20 ++++++++++++++++ include/properties/pg_editors.h | 11 ++++++++- pcbnew/widgets/pcb_properties_panel.cpp | 32 +++++++++++++++++-------- pcbnew/widgets/pcb_properties_panel.h | 4 ++-- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/common/properties/pg_editors.cpp b/common/properties/pg_editors.cpp index bcba17f0c9..8cf53afc27 100644 --- a/common/properties/pg_editors.cpp +++ b/common/properties/pg_editors.cpp @@ -39,9 +39,27 @@ PG_UNIT_EDITOR::~PG_UNIT_EDITOR() } +void PG_UNIT_EDITOR::UpdateFrame( EDA_DRAW_FRAME* aFrame ) +{ + m_frame = aFrame; + + if( aFrame ) + { + m_unitBinder = std::make_unique( m_frame ); + m_unitBinder->SetUnits( m_frame->GetUserUnits() ); + } + else + { + m_unitBinder = nullptr; + } +} + + wxPGWindowList PG_UNIT_EDITOR::CreateControls( wxPropertyGrid* aPropGrid, wxPGProperty* aProperty, const wxPoint& aPos, const wxSize& aSize ) const { + wxASSERT( m_unitBinder ); + wxPGWindowList ret = wxPGTextCtrlEditor::CreateControls( aPropGrid, aProperty, aPos, aSize ); m_unitBinder->SetControl( ret.m_primary ); @@ -57,6 +75,8 @@ wxPGWindowList PG_UNIT_EDITOR::CreateControls( wxPropertyGrid* aPropGrid, wxPGPr bool PG_UNIT_EDITOR::GetValueFromControl( wxVariant& aVariant, wxPGProperty* aProperty, wxWindow* aCtrl ) const { + wxASSERT( m_unitBinder ); + wxTextCtrl* textCtrl = dynamic_cast( aCtrl ); wxCHECK_MSG( textCtrl, false, "PG_UNIT_EDITOR requires a text control!" ); wxString textVal = textCtrl->GetValue(); diff --git a/include/properties/pg_editors.h b/include/properties/pg_editors.h index fe2a603d61..9f38724670 100644 --- a/include/properties/pg_editors.h +++ b/include/properties/pg_editors.h @@ -35,13 +35,22 @@ public: virtual ~PG_UNIT_EDITOR(); - wxString GetName() const override { return wxT( "UnitEditor" ); } + wxString GetName() const override { return wxT( "KiCadUnitEditor" ); } wxPGWindowList CreateControls( wxPropertyGrid* aPropGrid, wxPGProperty* aProperty, const wxPoint& aPos, const wxSize& aSize ) const override; bool GetValueFromControl( wxVariant& aVariant, wxPGProperty* aProperty, wxWindow* aCtrl ) const override; + + /** + * When restarting an editor, the instance of PG_UNIT_EDITOR may be the same + * but the referenced frame is different. This re-binds the frame to the editor + * and associated controls + * @param aFrame New frame to bind + */ + void UpdateFrame( EDA_DRAW_FRAME* aFrame ); + protected: EDA_DRAW_FRAME* m_frame; diff --git a/pcbnew/widgets/pcb_properties_panel.cpp b/pcbnew/widgets/pcb_properties_panel.cpp index 4a833712fb..ea04ee9a91 100644 --- a/pcbnew/widgets/pcb_properties_panel.cpp +++ b/pcbnew/widgets/pcb_properties_panel.cpp @@ -39,23 +39,35 @@ PCB_PROPERTIES_PANEL::PCB_PROPERTIES_PANEL( wxWindow* aParent, PCB_EDIT_FRAME* a : PROPERTIES_PANEL( aParent, aFrame ), m_frame( aFrame ), m_propMgr( PROPERTY_MANAGER::Instance() ) { m_propMgr.Rebuild(); + bool found = false; + + PG_UNIT_EDITOR* new_editor = new PG_UNIT_EDITOR( m_frame ); + + if( wxPGGlobalVars ) + { + auto it = wxPGGlobalVars->m_mapEditorClasses.find( new_editor->GetName() ); + + if( it != wxPGGlobalVars->m_mapEditorClasses.end() ) + { + m_editor = static_cast( it->second ); + m_editor->UpdateFrame( m_frame ); + found = true; + } + } + + if( found ) + delete new_editor; + else + m_editor = static_cast( wxPropertyGrid::RegisterEditorClass( new_editor ) ); + - m_editor = wxPropertyGrid::RegisterEditorClass( new PG_UNIT_EDITOR( m_frame ) ); } PCB_PROPERTIES_PANEL::~PCB_PROPERTIES_PANEL() { - auto it = wxPGGlobalVars->m_mapEditorClasses.find( m_editor->GetName() ); - - if( it != wxPGGlobalVars->m_mapEditorClasses.end() ) - { - wxPGGlobalVars->m_mapEditorClasses.erase( it ); - - delete static_cast( it->second ); - it->second = nullptr; - } + m_editor->UpdateFrame( nullptr ); } diff --git a/pcbnew/widgets/pcb_properties_panel.h b/pcbnew/widgets/pcb_properties_panel.h index b44e9c8195..a5cf87ddb4 100644 --- a/pcbnew/widgets/pcb_properties_panel.h +++ b/pcbnew/widgets/pcb_properties_panel.h @@ -27,7 +27,7 @@ class SELECTION; class BOARD; class PCB_EDIT_FRAME; class PROPERTY_MANAGER; -class wxPGEditor; +class PG_UNIT_EDITOR; class PCB_PROPERTIES_PANEL : public PROPERTIES_PANEL { @@ -48,7 +48,7 @@ protected: PCB_EDIT_FRAME* m_frame; PROPERTY_MANAGER& m_propMgr; - wxPGEditor* m_editor; + PG_UNIT_EDITOR* m_editor; wxPGChoices m_nets; };