KiCad PCB EDA Suite
hotkey_store.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2018 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 
24 #include <hotkey_store.h>
25 
27 {
28  for( EDA_HOTKEY_CONFIG* section = aHotkeys; section->m_HK_InfoList; ++section )
29  {
30  m_hk_sections.push_back( genSection( *section ) );
31  }
32 }
33 
34 
36 {
37  HOTKEY_SECTION generated_section { {}, {}, aSection };
38 
39  generated_section.m_name = wxGetTranslation( *aSection.m_Title );
40 
41  for( EDA_HOTKEY** info_ptr = aSection.m_HK_InfoList; *info_ptr; ++info_ptr )
42  {
43  generated_section.m_hotkeys.push_back( { **info_ptr, *aSection.m_SectionTag } );
44  }
45 
46  return generated_section;
47 }
48 
49 
50 std::vector<HOTKEY_SECTION>& HOTKEY_STORE::GetSections()
51 {
52  return m_hk_sections;
53 }
54 
55 
56 CHANGED_HOTKEY* HOTKEY_STORE::FindHotkey( const wxString& aTag, int aCmdId )
57 {
58  CHANGED_HOTKEY* found_key = nullptr;
59 
60  for( auto& section: m_hk_sections )
61  {
62  if( *section.m_section.m_SectionTag != aTag)
63  continue;
64 
65  for( auto& hotkey: section.m_hotkeys )
66  {
67  auto& curr_hk = hotkey.GetCurrentValue();
68  if( curr_hk.m_Idcommand == aCmdId )
69  {
70  found_key = &hotkey;
71  break;
72  }
73  }
74  }
75 
76  return found_key;
77 }
78 
79 
81 {
82  for( auto& section: m_hk_sections )
83  {
84  for( auto& hotkey: section.m_hotkeys )
85  {
86  hotkey.SaveHotkey();
87  }
88  }
89 }
90 
91 
93 {
94  for( auto& section: m_hk_sections )
95  {
96  for( auto& hotkey: section.m_hotkeys )
97  {
98  hotkey.GetCurrentValue().ResetKeyCodeToDefault();
99  }
100  }
101 }
102 
103 
105 {
106  for( auto& section: m_hk_sections )
107  {
108  for( auto& hotkey: section.m_hotkeys )
109  {
110  hotkey.GetCurrentValue().m_KeyCode = hotkey.GetOriginalValue().m_KeyCode;
111  }
112  }
113 }
114 
115 
116 bool HOTKEY_STORE::CheckKeyConflicts( long aKey, const wxString& aSectionTag,
117  EDA_HOTKEY** aConfKey, EDA_HOTKEY_CONFIG** aConfSect )
118 {
119  EDA_HOTKEY* conflicting_key = nullptr;
120  EDA_HOTKEY_CONFIG* conflicting_section = nullptr;
121 
122  for( auto& section: m_hk_sections )
123  {
124  const auto& sectionTag = *section.m_section.m_SectionTag;
125 
126  if( aSectionTag != g_CommonSectionTag
127  && sectionTag != g_CommonSectionTag
128  && sectionTag != aSectionTag )
129  {
130  // This key and its conflict candidate are in orthogonal sections, so skip.
131  continue;
132  }
133 
134  // See if any *current* hotkeys are in conflict
135  for( auto& hotkey: section.m_hotkeys )
136  {
137  auto& curr_hk = hotkey.GetCurrentValue();
138  if( aKey == curr_hk.m_KeyCode )
139  {
140  conflicting_key = &curr_hk;
141  conflicting_section = &section.m_section;
142  }
143  }
144  }
145 
146  // Write the outparams
147  if( aConfKey )
148  *aConfKey = conflicting_key;
149 
150  if( aConfSect )
151  *aConfSect = conflicting_section;
152 
153  return conflicting_key == nullptr;
154 }
Structure EDA_HOTKEY_CONFIG contains the information required to save hot key information to a config...
Definition: hotkeys_basic.h:94
HOTKEY_SECTION genSection(EDA_HOTKEY_CONFIG &aSection)
Generate a HOTKEY_SECTION for a single section described by an EDA_HOTKEY_CONFIG. ...
Class that manages a hotkey that can be changed, reset to its old value, a default or saved...
Definition: hotkey_store.h:39
EDA_HOTKEY ** m_HK_InfoList
Definition: hotkeys_basic.h:98
void SaveAllHotkeys()
Persist all changes to hotkeys in the store to the underlying data structures.
EDA_HOTKEY & GetCurrentValue()
Definition: hotkey_store.h:48
void ResetAllHotkeysToDefault()
Reset every hotkey in the store to the default values.
wxString * m_SectionTag
Definition: hotkeys_basic.h:97
SECTION_LIST m_hk_sections
Definition: hotkey_store.h:184
SECTION_LIST & GetSections()
Get the list of sections managed by this store.
class EDA_HOTKEY is a class to handle hot key commands.
Definition: hotkeys_basic.h:59
wxString * m_Title
Definition: hotkeys_basic.h:99
void ResetAllHotkeysToOriginal()
Resets every hotkey to the original values.
CHANGED_HOTKEY * FindHotkey(const wxString &aTag, int aCmdId)
Find a hotkey with the given command ID and in the given section.
HOTKEY_STORE(EDA_HOTKEY_CONFIG *aHotkeys)
Construct a HOTKEY_STORE from a list of hotkey sections.
wxString g_CommonSectionTag
Associates a set of hotkeys (a section) with a display name and the hotkeys.
Definition: hotkey_store.h:106
bool CheckKeyConflicts(long aKey, const wxString &aSectionTag, EDA_HOTKEY **aConfKey, EDA_HOTKEY_CONFIG **aConfSect)
Check whether the given key conflicts with anything in this store.