|
| 1 | +// |
| 2 | +// Copyright 2019 (C). Alex Robenko. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +// This file is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +#include "ShortcutMgr.h" |
| 19 | + |
| 20 | +#include <type_traits> |
| 21 | + |
| 22 | + |
| 23 | +namespace comms_champion |
| 24 | +{ |
| 25 | + |
| 26 | +ShortcutMgr::~ShortcutMgr() noexcept = default; |
| 27 | + |
| 28 | +QKeySequence ShortcutMgr::getKeySeq(Key k) const |
| 29 | +{ |
| 30 | + if ((Key_NumOfValues <= k) || (m_map[k].isEmpty())) { |
| 31 | + return QKeySequence(); |
| 32 | + } |
| 33 | + |
| 34 | + return m_map[k]; |
| 35 | +} |
| 36 | + |
| 37 | +ShortcutMgr* ShortcutMgr::instance() |
| 38 | +{ |
| 39 | + return &instanceRef(); |
| 40 | +} |
| 41 | + |
| 42 | +ShortcutMgr& ShortcutMgr::instanceRef() |
| 43 | +{ |
| 44 | + static ShortcutMgr Mgr; |
| 45 | + return Mgr; |
| 46 | +} |
| 47 | + |
| 48 | +void ShortcutMgr::updateShortcut(QAction& action, ShortcutMgr::Key key) |
| 49 | +{ |
| 50 | + auto keySeq = getKeySeq(key); |
| 51 | + if (keySeq.isEmpty()) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + auto text = action.text(); |
| 56 | + static const QString ShotrcutStart(" ["); |
| 57 | + auto shortcutPos = text.indexOf(ShotrcutStart); |
| 58 | + if (0 <= shortcutPos) { |
| 59 | + text = text.left(shortcutPos); |
| 60 | + } |
| 61 | + |
| 62 | + action.setText(text + ShotrcutStart + keySeq.toString(QKeySequence::NativeText) + ']'); |
| 63 | + action.setShortcut(keySeq); |
| 64 | +} |
| 65 | + |
| 66 | +ShortcutMgr::ShortcutMgr() |
| 67 | +{ |
| 68 | + createInitialMap(); |
| 69 | +} |
| 70 | + |
| 71 | +void ShortcutMgr::createInitialMap() |
| 72 | +{ |
| 73 | + static const char* Keys[] = { |
| 74 | + /* Key_Invalid */ nullptr, |
| 75 | + /* Key_AddMessage */ "Ctrl+N", |
| 76 | + /* Key_EditMessage */ "Ctrl+E", |
| 77 | + /* Key_Delete */ "Delete", |
| 78 | + /* Key_DupMessage */ "Ctrl+C", |
| 79 | + /* Key_Up */ "Ctrl+Up", |
| 80 | + /* Key_Down */ "Ctrl+Down", |
| 81 | + /* Key_Top */ "Ctrl+Shift+Up", |
| 82 | + /* Key_Bottom */ "Ctrl+Shift+Down", |
| 83 | + /* Key_Plugins */ "Ctrl+P", |
| 84 | + /* Key_ClearSend */ "Ctrl+L,S", |
| 85 | + /* Key_ClearRecv */ "Ctrl+L,R", |
| 86 | + /* Key_Comment */ "Ctrl+M", |
| 87 | + /* Key_Send */ "Ctrl+S", |
| 88 | + /* Key_SendAll */ "Ctrl+Shift+Alt+S", |
| 89 | + /* Key_AddRaw */ "Ctrl+R", |
| 90 | + /* Key_Connect */ "Ctrl+K", |
| 91 | + /* Key_Disconnect */ "Ctrl+Alt+K", |
| 92 | + /* Key_LoadSend */ "Ctrl+O,S", |
| 93 | + /* Key_LoadRecv */ "Ctrl+O,R", |
| 94 | + /* Key_SaveSend */ "Ctrl+V,S", |
| 95 | + /* Key_SaveRecv */ "Ctrl+V,R", |
| 96 | + /* Key_Receive */ "F5", |
| 97 | + }; |
| 98 | + |
| 99 | + static const std::size_t KeysSize = std::extent<decltype(Keys)>::value; |
| 100 | + static_assert(KeysSize == Key_NumOfValues, "Invalid map"); |
| 101 | + |
| 102 | + for (auto i = 0U; i < KeysSize; ++i) { |
| 103 | + if (Keys[i] == nullptr) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + m_map[i] = QKeySequence(Keys[i]); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +} // namespace comms_champion |
0 commit comments