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.

240 lines
7.6 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2019 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 )
  34. {
  35. m_label = aLabel;
  36. m_hotKey = aHotKey;
  37. }
  38. };
  39. static PSEUDO_ACTION* g_gesturePseudoActions[] = {
  40. new PSEUDO_ACTION( _( "Pan Left/Right" ), MD_CTRL + PSEUDO_WXK_WHEEL ),
  41. new PSEUDO_ACTION( _( "Pan Up/Down" ), MD_SHIFT + PSEUDO_WXK_WHEEL ),
  42. new PSEUDO_ACTION( _( "Finish Drawing" ), PSEUDO_WXK_DBLCLICK ),
  43. new PSEUDO_ACTION( _( "Add to Selection" ), MD_SHIFT + PSEUDO_WXK_CLICK ),
  44. new PSEUDO_ACTION( _( "Highlight Net" ), MD_CTRL + PSEUDO_WXK_CLICK ),
  45. new PSEUDO_ACTION( _( "Remove from Selection" ), MD_SHIFT + MD_CTRL + PSEUDO_WXK_CLICK ),
  46. new PSEUDO_ACTION( _( "Ignore Grid Snaps" ), MD_CTRL ),
  47. new PSEUDO_ACTION( _( "Ignore Other Snaps" ), MD_SHIFT ),
  48. };
  49. static PSEUDO_ACTION* g_standardPlatformCommands[] = {
  50. #ifndef __WINDOWS__
  51. new PSEUDO_ACTION( _( "Close" ), MD_CTRL + 'W' ),
  52. #endif
  53. new PSEUDO_ACTION( _( "Quit" ), MD_CTRL + 'Q' )
  54. };
  55. wxString HOTKEY_STORE::GetAppName( TOOL_ACTION* aAction )
  56. {
  57. wxString name( aAction->GetName() );
  58. return name.BeforeFirst( '.' );
  59. }
  60. wxString HOTKEY_STORE::GetSectionName( TOOL_ACTION* aAction )
  61. {
  62. std::map<wxString, wxString> s_AppNames = {
  63. { wxT( "common" ), _( "Common" ) },
  64. { wxT( "kicad" ), _( "Project Manager" ) },
  65. { wxT( "eeschema" ), _( "Schematic Editor" ) },
  66. { wxT( "pcbnew" ), _( "PCB Editor" ) },
  67. { wxT( "plEditor" ), _( "Drawing Sheet Editor" ), },
  68. { wxT( "3DViewer" ), _( "3D Viewer" ) },
  69. { wxT( "gerbview" ), _( "Gerber Viewer" ) }
  70. };
  71. wxString appName = GetAppName( aAction );
  72. if( s_AppNames.count( appName ) )
  73. return s_AppNames[ appName ];
  74. else
  75. return appName;
  76. }
  77. HOTKEY_STORE::HOTKEY_STORE()
  78. {
  79. }
  80. void HOTKEY_STORE::Init( std::vector<TOOL_ACTION*> aActionsList, bool aIncludeReadOnlyCmds )
  81. {
  82. std::map<std::string, HOTKEY> masterMap;
  83. for( TOOL_ACTION* action : aActionsList )
  84. {
  85. // Internal actions probably shouldn't be allowed hotkeys
  86. if( action->GetLabel().IsEmpty() )
  87. continue;
  88. if( !ADVANCED_CFG::GetCfg().m_ExtraZoneDisplayModes )
  89. {
  90. if( action->GetName() == "pcbnew.Control.zoneDisplayOutlines"
  91. || action->GetName() == "pcbnew.Control.zoneDisplayTesselation" )
  92. {
  93. continue;
  94. }
  95. }
  96. HOTKEY& hotkey = masterMap[ action->GetName() ];
  97. hotkey.m_Actions.push_back( action );
  98. if( !hotkey.m_EditKeycode )
  99. hotkey.m_EditKeycode = action->GetHotKey();
  100. }
  101. wxString currentApp;
  102. HOTKEY_SECTION* currentSection = nullptr;
  103. // If a previous list was built, ensure this previous list is cleared:
  104. m_hk_sections.clear();
  105. for( const std::pair<const std::string, HOTKEY>& entry : masterMap )
  106. {
  107. TOOL_ACTION* entryAction = entry.second.m_Actions[ 0 ];
  108. wxString entryApp = GetAppName( entryAction );
  109. if( !currentSection || entryApp != currentApp )
  110. {
  111. m_hk_sections.emplace_back( HOTKEY_SECTION() );
  112. currentApp = entryApp;
  113. currentSection = &m_hk_sections.back();
  114. currentSection->m_SectionName = GetSectionName( entryAction );
  115. if( aIncludeReadOnlyCmds && currentApp == "common" )
  116. {
  117. for( TOOL_ACTION* command : g_standardPlatformCommands )
  118. currentSection->m_HotKeys.emplace_back( HOTKEY( command ) );
  119. }
  120. }
  121. currentSection->m_HotKeys.emplace_back( HOTKEY( entry.second ) );
  122. }
  123. if( aIncludeReadOnlyCmds )
  124. {
  125. m_hk_sections.emplace_back( HOTKEY_SECTION() );
  126. currentSection = &m_hk_sections.back();
  127. currentSection->m_SectionName = _( "Gestures" );
  128. for( TOOL_ACTION* gesture : g_gesturePseudoActions )
  129. currentSection->m_HotKeys.emplace_back( HOTKEY( gesture ) );
  130. }
  131. }
  132. std::vector<HOTKEY_SECTION>& HOTKEY_STORE::GetSections()
  133. {
  134. return m_hk_sections;
  135. }
  136. void HOTKEY_STORE::SaveAllHotkeys()
  137. {
  138. for( HOTKEY_SECTION& section : m_hk_sections )
  139. {
  140. for( HOTKEY& hotkey : section.m_HotKeys )
  141. {
  142. for( TOOL_ACTION* action : hotkey.m_Actions )
  143. action->SetHotKey( hotkey.m_EditKeycode );
  144. }
  145. }
  146. }
  147. void HOTKEY_STORE::ResetAllHotkeysToDefault()
  148. {
  149. for( HOTKEY_SECTION& section : m_hk_sections )
  150. {
  151. for( HOTKEY& hotkey : section.m_HotKeys )
  152. hotkey.m_EditKeycode = hotkey.m_Actions[ 0 ]->GetDefaultHotKey();
  153. }
  154. }
  155. void HOTKEY_STORE::ResetAllHotkeysToOriginal()
  156. {
  157. for( HOTKEY_SECTION& section : m_hk_sections )
  158. {
  159. for( HOTKEY& hotkey : section.m_HotKeys )
  160. hotkey.m_EditKeycode = hotkey.m_Actions[ 0 ]->GetHotKey();
  161. }
  162. }
  163. bool HOTKEY_STORE::CheckKeyConflicts( TOOL_ACTION* aAction, long aKey, HOTKEY** aConflict )
  164. {
  165. wxString sectionName = GetSectionName( aAction );
  166. // Create a fake "TOOL_ACTION" so we can get the section name for "Common" through the API.
  167. // Simply declaring a wxString with the value "Common" works, but the goal is to futureproof
  168. // the code here as much as possible.
  169. TOOL_ACTION commonAction( "common.Control.Fake", AS_GLOBAL, 0, "", "", "" );
  170. wxString commonName = GetSectionName( &commonAction );
  171. for( HOTKEY_SECTION& section : m_hk_sections )
  172. {
  173. // We can have the same hotkey in multiple sections (i.e. Kicad programs), but if a hotkey
  174. // is in "Common" it can't be in any other section and vice versa.
  175. if( !( section.m_SectionName == sectionName || section.m_SectionName == commonName ) )
  176. continue;
  177. for( HOTKEY& hotkey : section.m_HotKeys )
  178. {
  179. if( hotkey.m_Actions[0] == aAction )
  180. continue;
  181. if( hotkey.m_EditKeycode == aKey )
  182. {
  183. // We can use the same key for a different action if both actions are contextual and
  184. // for different tools.
  185. if( hotkey.m_Actions[0]->GetScope() == AS_CONTEXT &&
  186. aAction->GetScope() == AS_CONTEXT &&
  187. hotkey.m_Actions[0]->GetToolName() != aAction->GetToolName() )
  188. {
  189. continue;
  190. }
  191. *aConflict = &hotkey;
  192. return true;
  193. }
  194. }
  195. }
  196. return false;
  197. }