From b8748c4ef808c86d7c19d95685e22cbee41baf7e Mon Sep 17 00:00:00 2001 From: JamesJ <13408010-JamesJCode@users.noreply.gitlab.com> Date: Tue, 27 Feb 2024 18:04:15 +0000 Subject: [PATCH] Add import netclass color menu items to PCB appearance widget Fixes https://gitlab.com/kicad/code/kicad/-/issues/16908 ADDED: Context menu item in PCB appearances widget to import netclass color from schematic. ADDED: Context menu item to reset PCB netclass color (replicating same menu item from Nets inspector) --- common/project/net_settings.cpp | 11 ++++ include/project/net_settings.h | 8 +++ pcbnew/widgets/appearance_controls.cpp | 71 ++++++++++++++++++++++++-- pcbnew/widgets/appearance_controls.h | 1 + 4 files changed, 86 insertions(+), 5 deletions(-) diff --git a/common/project/net_settings.cpp b/common/project/net_settings.cpp index 613904b139..98453c48a4 100644 --- a/common/project/net_settings.cpp +++ b/common/project/net_settings.cpp @@ -435,6 +435,17 @@ std::shared_ptr NET_SETTINGS::GetEffectiveNetClass( const wxString& aN } +std::shared_ptr NET_SETTINGS::GetNetClassByName( const wxString& aNetClassName ) const +{ + auto ii = m_NetClasses.find( aNetClassName ); + + if( ii == m_NetClasses.end() ) + return m_DefaultNetClass; + else + return ii->second; +} + + static bool isSuperSubOverbar( wxChar c ) { return c == '_' || c == '^' || c == '~'; diff --git a/include/project/net_settings.h b/include/project/net_settings.h index 7416ff82ee..2ad4040f9b 100644 --- a/include/project/net_settings.h +++ b/include/project/net_settings.h @@ -62,6 +62,14 @@ public: public: std::shared_ptr GetEffectiveNetClass( const wxString& aNetName ) const; + /** + * Get a NETCLASS object from a given Netclass name string + * + * @param aNetClassName the Netclass name to resolve + * @return shared pointer to the requested NETCLASS object, or the default NETCLASS + */ + std::shared_ptr GetNetClassByName( const wxString& aNetName ) const; + /** * Parse a bus vector (e.g. A[7..0]) into name, begin, and end. * diff --git a/pcbnew/widgets/appearance_controls.cpp b/pcbnew/widgets/appearance_controls.cpp index 962ce5434d..f29d0407e8 100644 --- a/pcbnew/widgets/appearance_controls.cpp +++ b/pcbnew/widgets/appearance_controls.cpp @@ -2315,10 +2315,27 @@ void APPEARANCE_CONTROLS::syncObjectSettings() void APPEARANCE_CONTROLS::buildNetClassMenu( wxMenu& aMenu, bool isDefaultClass, const wxString& aName ) { + BOARD* board = m_frame->GetBoard(); + std::shared_ptr& netSettings = board->GetDesignSettings().m_NetSettings; + if( !isDefaultClass) { aMenu.Append( new wxMenuItem( &aMenu, ID_SET_NET_COLOR, _( "Set Netclass Color" ), wxEmptyString, wxITEM_NORMAL ) ); + + wxMenuItem* schematicColor = + new wxMenuItem( &aMenu, ID_USE_SCHEMATIC_NET_COLOR, _( "Use Color from Schematic" ), + wxEmptyString, wxITEM_NORMAL ); + std::shared_ptr nc = netSettings->GetNetClassByName( aName ); + const KIGFX::COLOR4D ncColor = nc->GetSchematicColor(); + aMenu.Append( schematicColor ); + + if( ncColor == KIGFX::COLOR4D::UNSPECIFIED ) + schematicColor->Enable( false ); + + aMenu.Append( new wxMenuItem( &aMenu, ID_CLEAR_NET_COLOR, _( "Clear Netclass Color" ), + wxEmptyString, wxITEM_NORMAL ) ); + aMenu.AppendSeparator(); } wxString name = UnescapeString( aName ); @@ -3101,14 +3118,16 @@ void APPEARANCE_CONTROLS::showNetclass( const wxString& aClassName, bool aShow ) void APPEARANCE_CONTROLS::onNetclassColorChanged( wxCommandEvent& aEvent ) { - KIGFX::PCB_RENDER_SETTINGS* rs = static_cast( - m_frame->GetCanvas()->GetView()->GetPainter()->GetSettings() ); - - std::map& netclassColors = rs->GetNetclassColorMap(); - COLOR_SWATCH* swatch = static_cast( aEvent.GetEventObject() ); wxString netclassName = netclassNameFromEvent( aEvent ); + BOARD* board = m_frame->GetBoard(); + std::shared_ptr& netSettings = board->GetDesignSettings().m_NetSettings; + netSettings->m_NetClasses[netclassName]->SetPcbColor( swatch->GetSwatchColor() ); + + KIGFX::PCB_RENDER_SETTINGS* rs = static_cast( + m_frame->GetCanvas()->GetView()->GetPainter()->GetSettings() ); + std::map& netclassColors = rs->GetNetclassColorMap(); netclassColors[netclassName] = swatch->GetSwatchColor(); m_frame->GetCanvas()->GetView()->UpdateAllLayersColor(); @@ -3202,6 +3221,7 @@ void APPEARANCE_CONTROLS::onNetclassContextMenu( wxCommandEvent& aEvent ) switch( aEvent.GetId() ) { case ID_SET_NET_COLOR: + { if( setting ) { setting->ctl_color->GetNewSwatchColor(); @@ -3219,8 +3239,44 @@ void APPEARANCE_CONTROLS::onNetclassContextMenu( wxCommandEvent& aEvent ) } break; + } + + case ID_CLEAR_NET_COLOR: + { + if( setting ) + { + setting->ctl_color->SetSwatchColor( COLOR4D( 0, 0, 0, 0 ), true ); + + std::map& netclassColors = rs->GetNetclassColorMap(); + netclassColors.erase( m_contextMenuNetclass ); + + view->UpdateAllLayersColor(); + } + + break; + } + + case ID_USE_SCHEMATIC_NET_COLOR: + { + if( setting ) + { + std::shared_ptr nc = + netSettings->GetNetClassByName( m_contextMenuNetclass ); + const KIGFX::COLOR4D ncColor = nc->GetSchematicColor(); + + setting->ctl_color->SetSwatchColor( ncColor, true ); + + std::map& netclassColors = rs->GetNetclassColorMap(); + netclassColors[m_contextMenuNetclass] = ncColor; + + view->UpdateAllLayersColor(); + } + + break; + } case ID_HIGHLIGHT_NET: + { if( !m_contextMenuNetclass.IsEmpty() ) { runOnNetsOfClass( m_contextMenuNetclass, @@ -3247,9 +3303,11 @@ void APPEARANCE_CONTROLS::onNetclassContextMenu( wxCommandEvent& aEvent ) } break; + } case ID_SELECT_NET: case ID_DESELECT_NET: + { if( !m_contextMenuNetclass.IsEmpty() ) { TOOL_MANAGER* toolMgr = m_frame->GetToolManager(); @@ -3263,9 +3321,11 @@ void APPEARANCE_CONTROLS::onNetclassContextMenu( wxCommandEvent& aEvent ) } ); } break; + } case ID_SHOW_ALL_NETS: + { showNetclass( NETCLASS::Default ); wxASSERT( m_netclassSettingsMap.count( NETCLASS::Default ) ); m_netclassSettingsMap.at( NETCLASS::Default )->ctl_visibility->SetValue( true ); @@ -3279,6 +3339,7 @@ void APPEARANCE_CONTROLS::onNetclassContextMenu( wxCommandEvent& aEvent ) } break; + } case ID_HIDE_OTHER_NETS: { diff --git a/pcbnew/widgets/appearance_controls.h b/pcbnew/widgets/appearance_controls.h index adfc53ec1f..d7bd4a2db2 100644 --- a/pcbnew/widgets/appearance_controls.h +++ b/pcbnew/widgets/appearance_controls.h @@ -467,6 +467,7 @@ private: ID_CHANGE_COLOR = wxID_HIGHEST, ID_SET_NET_COLOR, ID_CLEAR_NET_COLOR, + ID_USE_SCHEMATIC_NET_COLOR, ID_SHOW_ALL_NETS, ID_HIDE_OTHER_NETS, ID_HIGHLIGHT_NET,