You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
8.2 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <hotkey_store.h>
  24. #include <eda_base_frame.h>
  25. #include <tool/tool_manager.h>
  26. #include <tool/action_manager.h>
  27. #include <tool/tool_event.h>
  28. #include <tool/tool_action.h>
  29. #include <advanced_config.h>
  30. class PSEUDO_ACTION : public TOOL_ACTION
  31. {
  32. public:
  33. PSEUDO_ACTION( const wxString& aLabel, int aHotKey, int aHotKeyAlt = 0 )
  34. {
  35. m_friendlyName = aLabel;
  36. m_hotKey = aHotKey;
  37. m_hotKeyAlt = aHotKeyAlt;
  38. }
  39. };
  40. static PSEUDO_ACTION* g_gesturePseudoActions[] = {
  41. new PSEUDO_ACTION( _( "Accept Autocomplete" ), WXK_RETURN, WXK_NUMPAD_ENTER ),
  42. new PSEUDO_ACTION( _( "Cancel Autocomplete" ), WXK_ESCAPE ),
  43. new PSEUDO_ACTION( _( "Toggle Checkbox" ), WXK_SPACE ),
  44. new PSEUDO_ACTION( _( "Pan Left/Right" ), MD_CTRL + PSEUDO_WXK_WHEEL ),
  45. new PSEUDO_ACTION( _( "Pan Up/Down" ), MD_SHIFT + PSEUDO_WXK_WHEEL ),
  46. new PSEUDO_ACTION( _( "Finish Drawing" ), PSEUDO_WXK_DBLCLICK ),
  47. new PSEUDO_ACTION( _( "Add to Selection" ), MD_SHIFT + PSEUDO_WXK_CLICK ),
  48. new PSEUDO_ACTION( _( "Highlight Net" ), MD_CTRL + PSEUDO_WXK_CLICK ),
  49. new PSEUDO_ACTION( _( "Remove from Selection" ), MD_CTRL + MD_SHIFT + PSEUDO_WXK_CLICK ),
  50. new PSEUDO_ACTION( _( "Ignore Grid Snaps" ), MD_CTRL ),
  51. new PSEUDO_ACTION( _( "Ignore Other Snaps" ), MD_SHIFT ),
  52. };
  53. static PSEUDO_ACTION* g_standardPlatformCommands[] = {
  54. #ifndef __WINDOWS__
  55. new PSEUDO_ACTION( _( "Close" ), MD_CTRL + 'W' ),
  56. #endif
  57. new PSEUDO_ACTION( _( "Quit" ), MD_CTRL + 'Q' )
  58. };
  59. wxString HOTKEY_STORE::GetAppName( TOOL_ACTION* aAction )
  60. {
  61. wxString name( aAction->GetName() );
  62. return name.BeforeFirst( '.' );
  63. }
  64. wxString HOTKEY_STORE::GetSectionName( TOOL_ACTION* aAction )
  65. {
  66. std::map<wxString, wxString> s_AppNames = {
  67. { wxT( "common" ), _( "Common" ) },
  68. { wxT( "kicad" ), _( "Project Manager" ) },
  69. { wxT( "eeschema" ), _( "Schematic Editor" ) },
  70. { wxT( "pcbnew" ), _( "PCB Editor" ) },
  71. { wxT( "plEditor" ), _( "Drawing Sheet Editor" ), },
  72. { wxT( "3DViewer" ), _( "3D Viewer" ) },
  73. { wxT( "gerbview" ), _( "Gerber Viewer" ) }
  74. };
  75. wxString appName = GetAppName( aAction );
  76. if( s_AppNames.count( appName ) )
  77. return s_AppNames[ appName ];
  78. else
  79. return appName;
  80. }
  81. HOTKEY_STORE::HOTKEY_STORE()
  82. {
  83. }
  84. void HOTKEY_STORE::Init( std::vector<TOOL_ACTION*> aActionsList, bool aIncludeReadOnlyCmds )
  85. {
  86. std::map<std::string, HOTKEY> masterMap;
  87. for( TOOL_ACTION* action : aActionsList )
  88. {
  89. // Internal actions probably shouldn't be allowed hotkeys
  90. if( action->GetFriendlyName().IsEmpty() )
  91. continue;
  92. if( !ADVANCED_CFG::GetCfg().m_ExtraZoneDisplayModes )
  93. {
  94. if( action->GetName() == "pcbnew.Control.zoneDisplayOutlines"
  95. || action->GetName() == "pcbnew.Control.zoneDisplayTesselation" )
  96. {
  97. continue;
  98. }
  99. }
  100. HOTKEY& hotkey = masterMap[ action->GetName() ];
  101. hotkey.m_Actions.push_back( action );
  102. if( !hotkey.m_EditKeycode )
  103. {
  104. hotkey.m_EditKeycode = action->GetHotKey();
  105. hotkey.m_EditKeycodeAlt = action->GetHotKeyAlt();
  106. }
  107. }
  108. wxString currentApp;
  109. HOTKEY_SECTION* currentSection = nullptr;
  110. // If a previous list was built, ensure this previous list is cleared:
  111. m_hk_sections.clear();
  112. for( const std::pair<const std::string, HOTKEY>& entry : masterMap )
  113. {
  114. TOOL_ACTION* entryAction = entry.second.m_Actions[ 0 ];
  115. wxString entryApp = GetAppName( entryAction );
  116. if( !currentSection || entryApp != currentApp )
  117. {
  118. m_hk_sections.emplace_back( HOTKEY_SECTION() );
  119. currentApp = entryApp;
  120. currentSection = &m_hk_sections.back();
  121. currentSection->m_SectionName = GetSectionName( entryAction );
  122. if( aIncludeReadOnlyCmds && currentApp == "common" )
  123. {
  124. for( TOOL_ACTION* command : g_standardPlatformCommands )
  125. currentSection->m_HotKeys.emplace_back( HOTKEY( command ) );
  126. }
  127. }
  128. currentSection->m_HotKeys.emplace_back( HOTKEY( entry.second ) );
  129. }
  130. if( aIncludeReadOnlyCmds )
  131. {
  132. m_hk_sections.emplace_back( HOTKEY_SECTION() );
  133. currentSection = &m_hk_sections.back();
  134. currentSection->m_SectionName = _( "Gestures" );
  135. for( TOOL_ACTION* gesture : g_gesturePseudoActions )
  136. currentSection->m_HotKeys.emplace_back( HOTKEY( gesture ) );
  137. }
  138. }
  139. std::vector<HOTKEY_SECTION>& HOTKEY_STORE::GetSections()
  140. {
  141. return m_hk_sections;
  142. }
  143. void HOTKEY_STORE::SaveAllHotkeys()
  144. {
  145. for( HOTKEY_SECTION& section : m_hk_sections )
  146. {
  147. for( HOTKEY& hotkey : section.m_HotKeys )
  148. {
  149. for( TOOL_ACTION* action : hotkey.m_Actions )
  150. action->SetHotKey( hotkey.m_EditKeycode, hotkey.m_EditKeycodeAlt );
  151. }
  152. }
  153. }
  154. void HOTKEY_STORE::ResetAllHotkeysToDefault()
  155. {
  156. for( HOTKEY_SECTION& section : m_hk_sections )
  157. {
  158. for( HOTKEY& hotkey : section.m_HotKeys )
  159. {
  160. hotkey.m_EditKeycode = hotkey.m_Actions[ 0 ]->GetDefaultHotKey();
  161. hotkey.m_EditKeycodeAlt = hotkey.m_Actions[ 0 ]->GetDefaultHotKeyAlt();
  162. }
  163. }
  164. }
  165. void HOTKEY_STORE::ResetAllHotkeysToOriginal()
  166. {
  167. for( HOTKEY_SECTION& section : m_hk_sections )
  168. {
  169. for( HOTKEY& hotkey : section.m_HotKeys )
  170. {
  171. hotkey.m_EditKeycode = hotkey.m_Actions[ 0 ]->GetHotKey();
  172. hotkey.m_EditKeycodeAlt = hotkey.m_Actions[ 0 ]->GetHotKeyAlt();
  173. }
  174. }
  175. }
  176. bool HOTKEY_STORE::CheckKeyConflicts( TOOL_ACTION* aAction, long aKey, HOTKEY** aConflict )
  177. {
  178. wxString sectionName = GetSectionName( aAction );
  179. // Create a fake "TOOL_ACTION" so we can get the section name for "Common" through the API.
  180. // Simply declaring a wxString with the value "Common" works, but the goal is to futureproof
  181. // the code here as much as possible.
  182. TOOL_ACTION commonAction( TOOL_ACTION_ARGS().Name( "common.Control.Fake" ).Scope( AS_GLOBAL ) );
  183. wxString commonName = GetSectionName( &commonAction );
  184. for( HOTKEY_SECTION& section : m_hk_sections )
  185. {
  186. // We can have the same hotkey in multiple sections (i.e. Kicad programs), but if a hotkey
  187. // is in "Common" it can't be in any other section and vice versa.
  188. if( !( section.m_SectionName == sectionName || section.m_SectionName == commonName ) )
  189. continue;
  190. for( HOTKEY& hotkey : section.m_HotKeys )
  191. {
  192. if( hotkey.m_Actions[0] == aAction )
  193. continue;
  194. if( hotkey.m_EditKeycode == aKey || hotkey.m_EditKeycodeAlt == aKey )
  195. {
  196. // We can use the same key for a different action if both actions are contextual and
  197. // for different tools.
  198. if( hotkey.m_Actions[0]->GetScope() == AS_CONTEXT &&
  199. aAction->GetScope() == AS_CONTEXT &&
  200. hotkey.m_Actions[0]->GetToolName() != aAction->GetToolName() )
  201. {
  202. continue;
  203. }
  204. *aConflict = &hotkey;
  205. return true;
  206. }
  207. }
  208. }
  209. return false;
  210. }