1 /* 2 Copyright (C) 2010-2014 Kristian Duske 3 4 This file is part of TrenchBroom. 5 6 TrenchBroom is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 TrenchBroom 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 TrenchBroom. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "ViewShortcut.h" 21 22 #include "StringUtils.h" 23 #include "Preference.h" 24 #include "PreferenceManager.h" 25 #include "View/ActionContext.h" 26 27 namespace TrenchBroom { 28 namespace View { ViewShortcut(const KeyboardShortcut & shortcut,const int context,const Action & action2D,const Action & action3D)29 ViewShortcut::ViewShortcut(const KeyboardShortcut& shortcut, const int context, const Action& action2D, const Action& action3D) : 30 m_preference(path(action2D, action3D), shortcut), 31 m_context(context) { 32 m_actions[ActionView_Map2D] = action2D; 33 m_actions[ActionView_Map3D] = action3D; 34 } 35 ViewShortcut(const KeyboardShortcut & shortcut,const int context,const Action & action)36 ViewShortcut::ViewShortcut(const KeyboardShortcut& shortcut, const int context, const Action& action) : 37 m_preference(path(action, action), shortcut), 38 m_context(context) { 39 m_actions[ActionView_Map2D] = action; 40 m_actions[ActionView_Map3D] = action; 41 } 42 resetShortcut()43 void ViewShortcut::resetShortcut() { 44 } 45 doGetActionContext() const46 int ViewShortcut::doGetActionContext() const { 47 return m_context; 48 } 49 doGetModifiable() const50 bool ViewShortcut::doGetModifiable() const { 51 return true; 52 } 53 doGetActionDescription() const54 wxString ViewShortcut::doGetActionDescription() const { 55 return buildDescription(m_actions[ActionView_Map2D], m_actions[ActionView_Map3D]); 56 } 57 doGetJsonString() const58 wxString ViewShortcut::doGetJsonString() const { 59 return shortcut().asJsonString(); 60 } 61 doGetPreference() const62 const Preference<KeyboardShortcut>& ViewShortcut::doGetPreference() const { 63 return m_preference; 64 } 65 66 doGetShortcut() const67 const KeyboardShortcut& ViewShortcut::doGetShortcut() const { 68 PreferenceManager& prefs = PreferenceManager::instance(); 69 return prefs.get(m_preference); 70 } 71 doUpdateShortcut(const KeyboardShortcut & shortcut)72 void ViewShortcut::doUpdateShortcut(const KeyboardShortcut& shortcut) { 73 PreferenceManager& prefs = PreferenceManager::instance(); 74 prefs.set(m_preference, shortcut); 75 } 76 doGetAcceleratorEntry(const ActionView view) const77 wxAcceleratorEntry ViewShortcut::doGetAcceleratorEntry(const ActionView view) const { 78 const Action& action = m_actions[view]; 79 return shortcut().acceleratorEntry(action.id()); 80 } 81 path(const Action & action2D,const Action & action3D) const82 IO::Path ViewShortcut::path(const Action& action2D, const Action& action3D) const { 83 return IO::Path("Controls/Map view") + IO::Path(buildDescription(action2D, action3D)); 84 } 85 buildDescription(const Action & action2D,const Action & action3D) const86 String ViewShortcut::buildDescription(const Action& action2D, const Action& action3D) const { 87 if (action2D.id() == action3D.id() || 88 action3D.id() == wxID_NONE) 89 return action2D.name(); 90 if (action2D.id() == wxID_NONE) 91 return action3D.name(); 92 93 StringStream result; 94 result << action2D.name() << "; " << action3D.name(); 95 return result.str(); 96 } 97 } 98 } 99