From b69d396afc541b7c0678a8b5aae1cfe68294c0da Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Thu, 1 Oct 2020 22:55:17 +0100 Subject: [PATCH] Turn the toolbar context menus into unique_ptr for easier management --- common/tool/action_toolbar.cpp | 43 +++++++++++++++------------------- include/tool/action_toolbar.h | 7 +++--- pcbnew/toolbars_pcb_editor.cpp | 9 +++---- 3 files changed, 28 insertions(+), 31 deletions(-) diff --git a/common/tool/action_toolbar.cpp b/common/tool/action_toolbar.cpp index 7ddc0a023a..fdecb80b25 100644 --- a/common/tool/action_toolbar.cpp +++ b/common/tool/action_toolbar.cpp @@ -188,11 +188,12 @@ ACTION_TOOLBAR::~ACTION_TOOLBAR() { delete m_paletteTimer; - // Delete all the menus - for( auto it = m_toolMenus.begin(); it != m_toolMenus.end(); it++ ) - delete it->second; - + // Clear all the maps keeping track of our items on the toolbar m_toolMenus.clear(); + m_actionGroups.clear(); + m_toolCancellable.clear(); + m_toolKinds.clear(); + m_toolActions.clear(); } @@ -242,23 +243,12 @@ void ACTION_TOOLBAR::AddScaledSeparator( wxWindow* aWindow ) } -void ACTION_TOOLBAR::AddToolContextMenu( const TOOL_ACTION& aAction, ACTION_MENU* aMenu ) +void ACTION_TOOLBAR::AddToolContextMenu( const TOOL_ACTION& aAction, + std::unique_ptr aMenu ) { int toolId = aAction.GetUIId(); - // If this is replacing an existing menu, delete the existing menu before adding the new one - const auto it = m_toolMenus.find( toolId ); - - if( it != m_toolMenus.end() ) - { - // Don't delete it if it is the same menu, just ignore this call - if( it->second == aMenu ) - return; - - delete it->second; - } - - m_toolMenus[toolId] = aMenu; + m_toolMenus[toolId] = std::move( aMenu ); } @@ -333,12 +323,14 @@ void ACTION_TOOLBAR::doSelectAction( ACTION_GROUP* aGroup, const TOOL_ACTION& aA void ACTION_TOOLBAR::ClearToolbar() { - // Delete all the menus - for( auto it = m_toolMenus.begin(); it != m_toolMenus.end(); it++ ) - delete it->second; - - // Clear the menu items and the actual toolbar + // Clear all the maps keeping track of our items on the toolbar m_toolMenus.clear(); + m_actionGroups.clear(); + m_toolCancellable.clear(); + m_toolKinds.clear(); + m_toolActions.clear(); + + // Remove the actual tools from the toolbar Clear(); } @@ -436,7 +428,10 @@ void ACTION_TOOLBAR::onToolRightClick( wxAuiToolBarEvent& aEvent ) return; // Update and show the menu - ACTION_MENU* menu = menuIt->second; + std::unique_ptr& owningMenu = menuIt->second; + + // Get the actual menu pointer to show it + ACTION_MENU* menu = owningMenu.get(); SELECTION dummySel; if( CONDITIONAL_MENU* condMenu = dynamic_cast( menu ) ) diff --git a/include/tool/action_toolbar.h b/include/tool/action_toolbar.h index 3f6a90b848..8b5c63f934 100644 --- a/include/tool/action_toolbar.h +++ b/include/tool/action_toolbar.h @@ -25,6 +25,7 @@ #define ACTION_TOOLBAR_H #include +#include #include #include // Needed for the auibar include #include @@ -232,7 +233,7 @@ public: * @param aAction is the action to get the menu * @param aMenu is the context menu */ - void AddToolContextMenu( const TOOL_ACTION& aAction, ACTION_MENU* aMenu ); + void AddToolContextMenu( const TOOL_ACTION& aAction, std::unique_ptr aMenu ); /** * Add a set of actions to a toolbar as a group. One action from the group will be displayed @@ -321,9 +322,9 @@ protected: std::map m_toolKinds; std::map m_toolCancellable; std::map m_toolActions; - std::map m_toolMenus; - std::map m_actionGroups; + + std::map> m_toolMenus; }; #endif diff --git a/pcbnew/toolbars_pcb_editor.cpp b/pcbnew/toolbars_pcb_editor.cpp index 7d74ca7c40..0c76a16ea9 100644 --- a/pcbnew/toolbars_pcb_editor.cpp +++ b/pcbnew/toolbars_pcb_editor.cpp @@ -451,19 +451,20 @@ void PCB_EDIT_FRAME::ReCreateVToolbar() m_drawToolBar->Add( ACTIONS::measureTool, ACTION_TOOLBAR::TOGGLE ); SELECTION_TOOL* selTool = m_toolManager->GetTool(); - ACTION_MENU* routeMenu = new ACTION_MENU( false, selTool ); + + std::unique_ptr routeMenu = std::make_unique( false, selTool ); routeMenu->Add( PCB_ACTIONS::routerHighlightMode, ACTION_MENU::CHECK ); routeMenu->Add( PCB_ACTIONS::routerShoveMode, ACTION_MENU::CHECK ); routeMenu->Add( PCB_ACTIONS::routerWalkaroundMode, ACTION_MENU::CHECK ); routeMenu->AppendSeparator(); routeMenu->Add( PCB_ACTIONS::routerSettingsDialog ); - m_drawToolBar->AddToolContextMenu( PCB_ACTIONS::routeSingleTrack, routeMenu ); + m_drawToolBar->AddToolContextMenu( PCB_ACTIONS::routeSingleTrack, std::move( routeMenu ) ); - ACTION_MENU* zoneMenu = new ACTION_MENU( false, selTool ); + std::unique_ptr zoneMenu = std::make_unique( false, selTool ); zoneMenu->Add( PCB_ACTIONS::zoneFillAll ); zoneMenu->Add( PCB_ACTIONS::zoneUnfillAll ); - m_drawToolBar->AddToolContextMenu( PCB_ACTIONS::drawZone, zoneMenu ); + m_drawToolBar->AddToolContextMenu( PCB_ACTIONS::drawZone, std::move( zoneMenu ) ); m_drawToolBar->Realize(); }