diff --git a/common/tool/common_tools.cpp b/common/tool/common_tools.cpp index dcdce1db04..b85d7b8a17 100644 --- a/common/tool/common_tools.cpp +++ b/common/tool/common_tools.cpp @@ -315,32 +315,56 @@ int COMMON_TOOLS::doZoomToPreset( int idx, bool aCenterOnCursor ) } +/** + * Advance a BASE_SCREEN's grid forwards or backwards by the given offset and + * return the cmd ID of that grid (doesn't change the grid). + * + * This works even if the base screen's grid do not have consecutive command IDs. + * + * @param aScreen the base screen to use + * @param aOffset how many grids to advance by (negative to go backwards) + * @return the cmd ID of the requested grid, or empty if it can't be found + */ +static OPT getNextPreviousGrid( const BASE_SCREEN& aScreen, int aOffset ) +{ + const GRIDS& grids = aScreen.GetGrids(); + const GRID_TYPE& currGrid = aScreen.GetGrid(); + + auto iter = std::find_if( grids.begin(), grids.end(), + [&]( const GRID_TYPE& aCandidate ) { return aCandidate.m_CmdId == currGrid.m_CmdId; } ); + + wxCHECK_MSG( iter != grids.end(), {}, "Grid not found in screen's grid list" ); + + int index = std::distance( grids.begin(), iter ) + aOffset; + + // If we go off the end, return invalid, but we could also wrap around if wanted. + if( index < 0 || static_cast( index ) >= grids.size() ) + return {}; + + return grids[index].m_CmdId; +} + + // Grid control int COMMON_TOOLS::GridNext( const TOOL_EVENT& aEvent ) { - BASE_SCREEN * screen = m_frame->GetScreen(); + const OPT next_grid_id = getNextPreviousGrid( *m_frame->GetScreen(), 1 ); - int new_grid_cmd = screen->GetGridCmdId(); + if( next_grid_id ) + return doGridPreset( *next_grid_id - ID_POPUP_GRID_LEVEL_1000 ); - // if the grid id is the not the last, increment it - if( screen->GridExists( new_grid_cmd + 1 ) ) - new_grid_cmd += 1; - - return doGridPreset( new_grid_cmd - ID_POPUP_GRID_LEVEL_1000 ); + return 1; } int COMMON_TOOLS::GridPrev( const TOOL_EVENT& aEvent ) { - BASE_SCREEN * screen = m_frame->GetScreen(); + const OPT next_grid_id = getNextPreviousGrid( *m_frame->GetScreen(), -1 ); - int new_grid_cmd = screen->GetGridCmdId(); + if( next_grid_id ) + return doGridPreset( *next_grid_id - ID_POPUP_GRID_LEVEL_1000 ); - // if the grid id is the not the first, increment it - if( screen->GridExists( new_grid_cmd - 1 ) ) - new_grid_cmd -= 1; - - return doGridPreset( new_grid_cmd - ID_POPUP_GRID_LEVEL_1000 ); + return 1; } diff --git a/eeschema/ee_hotkeys.cpp b/eeschema/ee_hotkeys.cpp index 7f171ab25d..3bf4a8f19f 100644 --- a/eeschema/ee_hotkeys.cpp +++ b/eeschema/ee_hotkeys.cpp @@ -130,6 +130,11 @@ static EDA_HOTKEY HkResetLocalCoord( _HKI( "Reset Local Coordinates" ), HK_RESET static EDA_HOTKEY HkLeaveSheet( _HKI( "Leave Sheet" ), HK_LEAVE_SHEET, GR_KB_ALT + WXK_BACK, ID_SCH_LEAVE_SHEET ); +static EDA_HOTKEY HkSwitchGridToNext( _HKI( "Switch Grid To Next" ), + HK_SWITCH_GRID_TO_NEXT, 'N' ); +static EDA_HOTKEY HkSwitchGridToPrevious( _HKI( "Switch Grid To Previous" ), + HK_SWITCH_GRID_TO_PREVIOUS, 'N' + GR_KB_SHIFT ); + // mouse click command: static EDA_HOTKEY HkMouseLeftClick( _HKI( "Mouse Left Click" ), HK_LEFT_CLICK, WXK_RETURN, ID_MOUSE_CLICK ); @@ -283,6 +288,8 @@ static EDA_HOTKEY* common_Hotkey_List[] = &HkZoomSelection, &HkSwitchUnits, &HkResetLocalCoord, + &HkSwitchGridToNext, + &HkSwitchGridToPrevious, &HkEdit, &HkDuplicateItem, &HkDelete,