From 72b08f2b18d3fdbdee812e19433ac6b323ecc6d9 Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Fri, 10 Jul 2020 21:06:17 -0400 Subject: [PATCH] ADDED: Hide ratsnest for specific nets List of hidden nets stored in project local settings Hide/Show actions in context menu only for now, will add them to some more GUI places soon. Ref https://gitlab.com/kicad/code/kicad/-/issues/1951 --- common/project/net_settings.cpp | 2 - common/project/project_local_settings.cpp | 5 + include/project/net_settings.h | 6 -- include/project/project_local_settings.h | 9 ++ include/settings/parameters.h | 69 +++++++++++++ pcbnew/pcb_edit_frame.cpp | 3 + pcbnew/pcb_painter.cpp | 12 +-- pcbnew/pcb_painter.h | 6 +- pcbnew/pcbnew_config.cpp | 33 ++++-- pcbnew/tools/pcb_actions.cpp | 9 +- pcbnew/tools/pcb_actions.h | 14 ++- pcbnew/tools/pcb_inspection_tool.cpp | 117 +++++++++++++++++++++- pcbnew/tools/pcb_inspection_tool.h | 8 ++ 13 files changed, 259 insertions(+), 34 deletions(-) diff --git a/common/project/net_settings.cpp b/common/project/net_settings.cpp index 573f5bf0d2..cd84f0e396 100644 --- a/common/project/net_settings.cpp +++ b/common/project/net_settings.cpp @@ -177,8 +177,6 @@ NET_SETTINGS::NET_SETTINGS( JSON_SETTINGS* aParent, const std::string& aPath ) : } }, {} ) ); - - m_params.emplace_back( new PARAM_LIST( "hidden_nets", &m_HiddenNets, {} ) ); } diff --git a/common/project/project_local_settings.cpp b/common/project/project_local_settings.cpp index 7c48ed3c66..079d6052fd 100644 --- a/common/project/project_local_settings.cpp +++ b/common/project/project_local_settings.cpp @@ -138,6 +138,11 @@ PROJECT_LOCAL_SETTINGS::PROJECT_LOCAL_SETTINGS( const std::string& aFilename ) : { "dimensions", true }, { "otherItems", true } } ) ); + + m_params.emplace_back( new PARAM_ENUM( + "active_layer", &m_ActiveLayer, F_Cu, PCBNEW_LAYER_ID_START, F_Fab ) ); + + m_params.emplace_back( new PARAM_LIST( "hidden_nets", &m_HiddenNets, {} ) ); } diff --git a/include/project/net_settings.h b/include/project/net_settings.h index 82466707f7..e3a3f393aa 100644 --- a/include/project/net_settings.h +++ b/include/project/net_settings.h @@ -50,12 +50,6 @@ public: */ std::map m_PcbNetColors; - /** - * A list of netnames that have been manually hidden in the board editor. - * Currently, hiding nets means hiding the ratsnest for those nets. - */ - std::vector m_HiddenNets; - public: /** * Parses a bus vector (e.g. A[7..0]) into name, begin, and end. diff --git a/include/project/project_local_settings.h b/include/project/project_local_settings.h index e8c9338a96..278bf636a3 100644 --- a/include/project/project_local_settings.h +++ b/include/project/project_local_settings.h @@ -85,6 +85,15 @@ public: /// The GAL layers (aka items) that are turned on for viewing (@see GAL_LAYER_ID) GAL_SET m_VisibleItems; + /// The current (active) board layer for editing + PCB_LAYER_ID m_ActiveLayer; + + /** + * A list of netnames that have been manually hidden in the board editor. + * Currently, hiding nets means hiding the ratsnest for those nets. + */ + std::vector m_HiddenNets; + /// State of the selection filter widget SELECTION_FILTER_OPTIONS m_SelectionFilter; }; diff --git a/include/settings/parameters.h b/include/settings/parameters.h index 98f851951d..3294c195df 100644 --- a/include/settings/parameters.h +++ b/include/settings/parameters.h @@ -219,6 +219,75 @@ private: } }; +/** + * Stores an enum as an integer + */ +template +class PARAM_ENUM : public PARAM_BASE +{ +public: + PARAM_ENUM( const std::string& aJsonPath, EnumType* aPtr, EnumType aDefault, + EnumType aMin, EnumType aMax, bool aReadOnly = false ) : + PARAM_BASE( aJsonPath, aReadOnly ), + m_ptr( aPtr ), + m_min( aMin ), + m_max( aMax ), + m_default( aDefault ) + { + } + + void Load( JSON_SETTINGS* aSettings, bool aResetIfMissing = true ) const override + { + if( m_readOnly ) + return; + + if( OPT val = aSettings->Get( m_path ) ) + { + if( *val >= static_cast( m_min ) && *val <= static_cast( m_max ) ) + *m_ptr = static_cast( *val ); + else if( aResetIfMissing ) + *m_ptr = m_default; + + } + else if( aResetIfMissing ) + *m_ptr = m_default; + } + + void Store( JSON_SETTINGS* aSettings ) const override + { + aSettings->Set( m_path, static_cast( *m_ptr ) ); + } + + EnumType GetDefault() const + { + return m_default; + } + + void SetDefault() override + { + *m_ptr = m_default; + } + + bool IsDefault() const override + { + return *m_ptr == m_default; + } + + bool MatchesFile( JSON_SETTINGS* aSettings ) const override + { + if( OPT val = aSettings->Get( m_path ) ) + return *val == static_cast( *m_ptr ); + + return false; + } + +private: + EnumType* m_ptr; + EnumType m_min; + EnumType m_max; + EnumType m_default; +}; + /** * Like a normal param, but with custom getter and setter functions * @tparam ValueType is the value to store diff --git a/pcbnew/pcb_edit_frame.cpp b/pcbnew/pcb_edit_frame.cpp index f168dbe308..dbd79947aa 100644 --- a/pcbnew/pcb_edit_frame.cpp +++ b/pcbnew/pcb_edit_frame.cpp @@ -605,6 +605,9 @@ void PCB_EDIT_FRAME::OnCloseWindow( wxCloseEvent& aEvent ) wxMessageBox( msg, Pgm().App().GetAppName(), wxOK | wxICON_ERROR, this ); } + // Make sure local settings are persisted + SaveProjectSettings(); + // Do not show the layer manager during closing to avoid flicker // on some platforms (Windows) that generate useless redraw of items in // the Layer Manger diff --git a/pcbnew/pcb_painter.cpp b/pcbnew/pcb_painter.cpp index f179fcb228..e342a13c58 100644 --- a/pcbnew/pcb_painter.cpp +++ b/pcbnew/pcb_painter.cpp @@ -218,8 +218,8 @@ void PCB_RENDER_SETTINGS::LoadDisplayOptions( const PCB_DISPLAY_OPTIONS& aOption } -void PCB_RENDER_SETTINGS::LoadNetSettings( const NET_SETTINGS& aSettings, - const NETINFO_LIST& aList ) +void PCB_RENDER_SETTINGS::LoadNetSettings( const NET_SETTINGS& aSettings, const NETINFO_LIST& aList, + const std::set& aHiddenNets ) { m_netColors.clear(); @@ -237,13 +237,9 @@ void PCB_RENDER_SETTINGS::LoadNetSettings( const NET_SETTINGS& aSettings, m_netclassColors[pair.first] = pair.second->GetPcbColor(); } - m_hiddenNets.clear(); + m_hiddenNets = aHiddenNets; + - for( const wxString& hidden : aSettings.m_HiddenNets ) - { - if( NETINFO_ITEM* net = aList.GetNetItem( hidden ) ) - m_hiddenNets.insert( net->GetNet() ); - } } diff --git a/pcbnew/pcb_painter.h b/pcbnew/pcb_painter.h index e63120b72c..930e718543 100644 --- a/pcbnew/pcb_painter.h +++ b/pcbnew/pcb_painter.h @@ -108,9 +108,11 @@ public: /** * Loads net-specific render settings * @param aSettings is the NET_SETTINGS for the current proejct - * @param aList is the list of nets in the board + * @param aList is the list of nets on the board + * @param aHiddenNets is a list of nets to hide from the ratsnest */ - void LoadNetSettings( const NET_SETTINGS& aSettings, const NETINFO_LIST& aList ); + void LoadNetSettings( const NET_SETTINGS& aSettings, const NETINFO_LIST& aList, + const std::set& aHiddenNets ); virtual void LoadColors( const COLOR_SETTINGS* aSettings ) override; diff --git a/pcbnew/pcbnew_config.cpp b/pcbnew/pcbnew_config.cpp index f30e2c1509..fbeeab8271 100644 --- a/pcbnew/pcbnew_config.cpp +++ b/pcbnew/pcbnew_config.cpp @@ -84,7 +84,8 @@ bool PCB_EDIT_FRAME::LoadProjectSettings() { wxLogDebug( wxT( "Loading project '%s' settings." ), GetChars( Prj().GetProjectFullName() ) ); - PROJECT_FILE& project = Prj().GetProjectFile(); + PROJECT_FILE& project = Prj().GetProjectFile(); + PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings(); BASE_SCREEN::m_PageLayoutDescrFileName = project.m_BoardPageLayoutDescrFile; @@ -100,9 +101,16 @@ bool PCB_EDIT_FRAME::LoadProjectSettings() KIGFX::PCB_RENDER_SETTINGS* rs = static_cast( GetCanvas()->GetView()->GetPainter()->GetSettings() ); - rs->LoadNetSettings( project.NetSettings(), GetBoard()->GetNetInfo() ); + NETINFO_LIST& nets = GetBoard()->GetNetInfo(); + std::set hiddenNets; - PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings(); + for( const wxString& hidden : localSettings.m_HiddenNets ) + { + if( NETINFO_ITEM* net = nets.GetNetItem( hidden ) ) + hiddenNets.insert( net->GetNet() ); + } + + rs->LoadNetSettings( project.NetSettings(), nets, hiddenNets ); SELECTION_FILTER_OPTIONS& filterOpts = GetToolManager()->GetTool()->GetFilter(); @@ -126,18 +134,29 @@ void PCB_EDIT_FRAME::SaveProjectSettings() if( !IsWritable( fn ) ) return; - PROJECT_FILE& project = Prj().GetProjectFile(); + PROJECT_FILE& project = Prj().GetProjectFile(); + PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings(); // TODO: Can this be pulled out of BASE_SCREEN? project.m_BoardPageLayoutDescrFile = BASE_SCREEN::m_PageLayoutDescrFileName; RecordDRCExclusions(); - PROJECT_LOCAL_SETTINGS& localSettings = Prj().GetLocalSettings(); + KIGFX::PCB_RENDER_SETTINGS* rs = static_cast( + GetCanvas()->GetView()->GetPainter()->GetSettings() ); + + NETINFO_LIST& nets = GetBoard()->GetNetInfo(); + + localSettings.m_HiddenNets.clear(); + + for( int netcode : rs->GetHiddenNets() ) + { + if( NETINFO_ITEM* net = nets.GetNetItem( netcode ) ) + localSettings.m_HiddenNets.emplace_back( net->GetNetname() ); + } SELECTION_FILTER_OPTIONS& filterOpts = GetToolManager()->GetTool()->GetFilter(); - - localSettings.m_SelectionFilter = filterOpts; + localSettings.m_SelectionFilter = filterOpts; GetSettingsManager()->SaveProject(); } diff --git a/pcbnew/tools/pcb_actions.cpp b/pcbnew/tools/pcb_actions.cpp index 68a1fc2f38..8f46b24a59 100644 --- a/pcbnew/tools/pcb_actions.cpp +++ b/pcbnew/tools/pcb_actions.cpp @@ -604,7 +604,8 @@ TOOL_ACTION PCB_ACTIONS::appendBoard( "pcbnew.EditorControl.appendBoard", add_board_xpm ); TOOL_ACTION PCB_ACTIONS::highlightNet( "pcbnew.EditorControl.highlightNet", - AS_GLOBAL ); + AS_GLOBAL, 0, "", _( "Highlight Net" ), _( "Highlight the selected net" ), + net_highlight_xpm ); TOOL_ACTION PCB_ACTIONS::toggleLastNetHighlight( "pcbnew.EditorControl.toggleLastNetHighlight", AS_GLOBAL, 0, "", @@ -629,6 +630,12 @@ TOOL_ACTION PCB_ACTIONS::highlightNetSelection( "pcbnew.EditorControl.highlightN TOOL_ACTION PCB_ACTIONS::highlightItem( "pcbnew.EditorControl.highlightItem", AS_GLOBAL ); +TOOL_ACTION PCB_ACTIONS::hideNet( "pcbnew.EditorControl.hideNet", AS_GLOBAL, 0, "", + _( "Hide Net" ), _( "Hide the ratsnest for the selected net" ), ratsnest_xpm ); + +TOOL_ACTION PCB_ACTIONS::showNet( "pcbnew.EditorControl.showNet", AS_GLOBAL, 0, "", + _( "Show Net" ), _( "Show the ratsnest for the selected net" ), ratsnest_xpm ); + TOOL_ACTION PCB_ACTIONS::showEeschema( "pcbnew.EditorControl.showEeschema", AS_GLOBAL, 0, "", _( "Switch to Schematic Editor" ), _( "Open schematic in Eeschema" ), diff --git a/pcbnew/tools/pcb_actions.h b/pcbnew/tools/pcb_actions.h index 16c490e00f..ba19bad457 100644 --- a/pcbnew/tools/pcb_actions.h +++ b/pcbnew/tools/pcb_actions.h @@ -404,17 +404,21 @@ public: static TOOL_ACTION pickerTool; static TOOL_ACTION measureTool; static TOOL_ACTION updateUnits; + static TOOL_ACTION drillOrigin; + static TOOL_ACTION placeFileOrigin; + static TOOL_ACTION appendBoard; + static TOOL_ACTION showEeschema; + static TOOL_ACTION boardStatistics; + + // Appearance controls static TOOL_ACTION clearHighlight; static TOOL_ACTION highlightNet; static TOOL_ACTION toggleLastNetHighlight; static TOOL_ACTION highlightNetTool; static TOOL_ACTION highlightNetSelection; static TOOL_ACTION highlightItem; - static TOOL_ACTION drillOrigin; - static TOOL_ACTION placeFileOrigin; - static TOOL_ACTION appendBoard; - static TOOL_ACTION showEeschema; - static TOOL_ACTION boardStatistics; + static TOOL_ACTION hideNet; + static TOOL_ACTION showNet; // Ratsnest static TOOL_ACTION localRatsnestTool; diff --git a/pcbnew/tools/pcb_inspection_tool.cpp b/pcbnew/tools/pcb_inspection_tool.cpp index 12e3d04dba..82f04d586e 100644 --- a/pcbnew/tools/pcb_inspection_tool.cpp +++ b/pcbnew/tools/pcb_inspection_tool.cpp @@ -21,12 +21,12 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ - +#include #include #include #include #include -#include +#include #include #include #include "pcb_inspection_tool.h" @@ -43,10 +43,69 @@ PCB_INSPECTION_TOOL::PCB_INSPECTION_TOOL() : } +class NET_CONTEXT_MENU : public ACTION_MENU +{ +public: + NET_CONTEXT_MENU() : ACTION_MENU( true ) + { + SetIcon( ratsnest_xpm ); + SetTitle( _( "Net Tools" ) ); + + Add( PCB_ACTIONS::showNet ); + Add( PCB_ACTIONS::hideNet ); + // Add( PCB_ACTIONS::highlightNet ); + } + +private: + + void update() override + { + const auto& selection = getToolManager()->GetTool()->GetSelection(); + + bool haveNetCode = false; + + for( EDA_ITEM* item : selection ) + { + if( BOARD_CONNECTED_ITEM* bci = dynamic_cast( item ) ) + { + if( bci->GetNetCode() > 0 ) + { + haveNetCode = true; + break; + } + } + } + + Enable( getMenuId( PCB_ACTIONS::showNet ), haveNetCode ); + Enable( getMenuId( PCB_ACTIONS::hideNet ), haveNetCode ); + // Enable( getMenuId( PCB_ACTIONS::highlightNet ), haveNetCode ); + } + + ACTION_MENU* create() const override + { + return new NET_CONTEXT_MENU(); + } +}; + + bool PCB_INSPECTION_TOOL::Init() { + SELECTION_TOOL* selectionTool = m_toolMgr->GetTool(); + + auto netSubMenu = std::make_shared(); + netSubMenu->SetTool( this ); + + static KICAD_T connectedTypes[] = { PCB_TRACE_T, PCB_VIA_T, PCB_ARC_T, PCB_PAD_T, + PCB_ZONE_AREA_T, EOT }; + + CONDITIONAL_MENU& menu = selectionTool->GetToolMenu().GetMenu(); + + selectionTool->GetToolMenu().AddSubMenu( netSubMenu ); + menu.AddMenu( netSubMenu.get(), SELECTION_CONDITIONS::OnlyTypes( connectedTypes ) ); + m_ratsnestTimer.SetOwner( this ); - Connect( m_ratsnestTimer.GetId(), wxEVT_TIMER, wxTimerEventHandler( PCB_INSPECTION_TOOL::ratsnestTimer ), NULL, this ); + Connect( m_ratsnestTimer.GetId(), wxEVT_TIMER, + wxTimerEventHandler( PCB_INSPECTION_TOOL::ratsnestTimer ), NULL, this ); return true; } @@ -462,6 +521,7 @@ void PCB_INSPECTION_TOOL::calculateSelectionRatsnest() connectivity->ComputeDynamicRatsnest( items ); } + int PCB_INSPECTION_TOOL::ListNets( const TOOL_EVENT& aEvent ) { if( m_listNetsDialog == nullptr ) @@ -482,6 +542,7 @@ int PCB_INSPECTION_TOOL::ListNets( const TOOL_EVENT& aEvent ) return 0; } + void PCB_INSPECTION_TOOL::onListNetsDialogClosed( wxCommandEvent& event ) { m_listNetsDialogSettings = m_listNetsDialog->Settings(); @@ -496,6 +557,53 @@ void PCB_INSPECTION_TOOL::onListNetsDialogClosed( wxCommandEvent& event ) m_listNetsDialog.release(); } + +int PCB_INSPECTION_TOOL::HideNet( const TOOL_EVENT& aEvent ) +{ + doHideNet( aEvent.Parameter(), true ); + return 0; +} + + +int PCB_INSPECTION_TOOL::ShowNet( const TOOL_EVENT& aEvent ) +{ + doHideNet( aEvent.Parameter(), false ); + return 0; +} + + +void PCB_INSPECTION_TOOL::doHideNet( int aNetCode, bool aHide ) +{ + KIGFX::PCB_RENDER_SETTINGS* rs = static_cast( + m_toolMgr->GetView()->GetPainter()->GetSettings() ); + + SELECTION_TOOL* selectionTool = m_toolMgr->GetTool(); + SELECTION& selection = selectionTool->GetSelection(); + + if( aNetCode <= 0 && !selection.Empty() ) + { + for( EDA_ITEM* item : selection ) + { + if( BOARD_CONNECTED_ITEM* bci = dynamic_cast( item ) ) + { + if( bci->GetNetCode() > 0 ) + doHideNet( bci->GetNetCode(), aHide ); + } + } + + return; + } + + if( aHide ) + rs->GetHiddenNets().insert( aNetCode ); + else + rs->GetHiddenNets().erase( aNetCode ); + + m_frame->GetCanvas()->RedrawRatsnest(); + m_frame->GetCanvas()->Refresh(); +} + + void PCB_INSPECTION_TOOL::setTransitions() { Go( &PCB_INSPECTION_TOOL::CrossProbePcbToSch, EVENTS::SelectedEvent ); @@ -516,4 +624,7 @@ void PCB_INSPECTION_TOOL::setTransitions() Go( &PCB_INSPECTION_TOOL::HighlightNetTool, PCB_ACTIONS::highlightNetTool.MakeEvent() ); Go( &PCB_INSPECTION_TOOL::ClearHighlight, ACTIONS::cancelInteractive.MakeEvent() ); Go( &PCB_INSPECTION_TOOL::HighlightItem, PCB_ACTIONS::highlightItem.MakeEvent() ); + + Go( &PCB_INSPECTION_TOOL::HideNet, PCB_ACTIONS::hideNet.MakeEvent() ); + Go( &PCB_INSPECTION_TOOL::ShowNet, PCB_ACTIONS::showNet.MakeEvent() ); } diff --git a/pcbnew/tools/pcb_inspection_tool.h b/pcbnew/tools/pcb_inspection_tool.h index 23a5d85511..abbf9622e6 100644 --- a/pcbnew/tools/pcb_inspection_tool.h +++ b/pcbnew/tools/pcb_inspection_tool.h @@ -82,6 +82,12 @@ public: int ListNets( const TOOL_EVENT& aEvent ); + ///> Hide the ratsnest for a given net + int HideNet( const TOOL_EVENT& aEvent ); + + ///> Show the ratsnest for a given net + int ShowNet( const TOOL_EVENT& aEvent ); + private: ///> Event handler to recalculate dynamic ratsnest void ratsnestTimer( wxTimerEvent& aEvent ); @@ -91,6 +97,8 @@ private: bool highlightNet( const VECTOR2D& aPosition, bool aUseSelection ); + void doHideNet( int aNetCode, bool aHide ); + ///> Bind handlers to corresponding TOOL_ACTIONs void setTransitions() override;