Skip to content

Commit a90f8c3

Browse files
committed
Introducing keyboard shortcuts for toolbuttons.
1 parent 327964b commit a90f8c3

File tree

6 files changed

+233
-4
lines changed

6 files changed

+233
-4
lines changed

comms_champion/app/cc_view/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function (bin_cc_view)
1919
MsgFileMgrG.cpp
2020
PluginMgrG.cpp
2121
MsgMgrG.cpp
22+
ShortcutMgr.cpp
2223
icon.cpp
2324
widget/MainWindowWidget.cpp
2425
widget/MainToolbar.cpp
@@ -63,6 +64,7 @@ function (bin_cc_view)
6364
qt5_wrap_cpp(
6465
moc
6566
GuiAppMgr.h
67+
ShortcutMgr.h
6668
widget/MessageDisplayWidget.h
6769
widget/MessageWidget.h
6870
widget/DefaultMessageWidget.h
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
19+
#pragma once
20+
21+
#include <array>
22+
23+
#include "comms/CompileControl.h"
24+
25+
CC_DISABLE_WARNINGS()
26+
#include <QtCore/QObject>
27+
#include <QtGui/QKeySequence>
28+
#include <QtWidgets/QAction>
29+
CC_ENABLE_WARNINGS()
30+
31+
namespace comms_champion
32+
{
33+
34+
class ShortcutMgr : public QObject
35+
{
36+
Q_OBJECT
37+
typedef QObject Base;
38+
public:
39+
40+
enum Key
41+
{
42+
Key_Invalid,
43+
Key_AddMessage,
44+
Key_EditMessage,
45+
Key_Delete,
46+
Key_DupMessage,
47+
Key_Up,
48+
Key_Down,
49+
Key_Top,
50+
Key_Bottom,
51+
Key_Plugins,
52+
Key_ClearSend,
53+
Key_ClearRecv,
54+
Key_Comment,
55+
Key_Send,
56+
Key_SendAll,
57+
Key_AddRaw,
58+
Key_Connect,
59+
Key_Disconnect,
60+
Key_LoadSend,
61+
Key_LoadRecv,
62+
Key_SaveSend,
63+
Key_SaveRecv,
64+
Key_Receive,
65+
Key_NumOfValues // Must be last
66+
};
67+
68+
~ShortcutMgr() noexcept;
69+
70+
QKeySequence getKeySeq(Key k) const;
71+
72+
static ShortcutMgr* instance();
73+
static ShortcutMgr& instanceRef();
74+
75+
void updateShortcut(QAction& action, ShortcutMgr::Key key);
76+
77+
signals:
78+
void sigKeysUpdated();
79+
80+
private:
81+
using KeyMap = std::array<QKeySequence, Key_NumOfValues>;
82+
83+
ShortcutMgr();
84+
85+
void createInitialMap();
86+
87+
KeyMap m_map;
88+
};
89+
90+
} // namespace comms_champion
91+

comms_champion/app/cc_view/src/widget/MainToolbar.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "GuiAppMgr.h"
2121
#include "icon.h"
22+
#include "ShortcutMgr.h"
2223

2324
namespace comms_champion
2425
{
@@ -29,17 +30,20 @@ MainToolbar::MainToolbar()
2930
QObject::connect(
3031
config, SIGNAL(triggered()),
3132
GuiAppMgr::instance(), SLOT(pluginsEditClicked()));
33+
ShortcutMgr::instanceRef().updateShortcut(*config, ShortcutMgr::Key_Plugins);
3234

3335
m_socketConnect = addAction(icon::connect(), "Connect socket");
3436
QObject::connect(
3537
m_socketConnect, SIGNAL(triggered()),
3638
GuiAppMgr::instance(), SLOT(connectSocketClicked()));
3739
m_socketConnect->setEnabled(false);
40+
ShortcutMgr::instanceRef().updateShortcut(*m_socketConnect, ShortcutMgr::Key_Connect);
3841

3942
m_socketDisconnect = addAction(icon::disconnect(), "Disconnect socket");
4043
QObject::connect(
4144
m_socketDisconnect, SIGNAL(triggered()),
4245
GuiAppMgr::instance(), SLOT(disconnectSocketClicked()));
46+
ShortcutMgr::instanceRef().updateShortcut(*m_socketDisconnect, ShortcutMgr::Key_Disconnect);
4347

4448
m_socketDisconnect->setVisible(false);
4549
m_socketConnect->setEnabled(false);

comms_champion/app/cc_view/src/widget/RecvAreaToolBar.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CC_DISABLE_WARNINGS()
2828
CC_ENABLE_WARNINGS()
2929

3030
#include "icon.h"
31+
#include "ShortcutMgr.h"
3132

3233
namespace comms_champion
3334
{
@@ -49,7 +50,7 @@ QAction* createLoadButton(QToolBar& bar)
4950
auto* action = bar.addAction(icon::upload(), "Load Messages");
5051
QObject::connect(action, SIGNAL(triggered()),
5152
GuiAppMgr::instance(), SLOT(recvLoadClicked()));
52-
53+
ShortcutMgr::instanceRef().updateShortcut(*action, ShortcutMgr::Key_LoadRecv);
5354
return action;
5455
}
5556

@@ -59,7 +60,7 @@ QAction* createSaveButton(QToolBar& bar)
5960
auto* action = bar.addAction(icon::save(), "Save Messages");
6061
QObject::connect(action, SIGNAL(triggered()),
6162
GuiAppMgr::instance(), SLOT(recvSaveClicked()));
62-
63+
ShortcutMgr::instanceRef().updateShortcut(*action, ShortcutMgr::Key_SaveRecv);
6364
return action;
6465
}
6566

@@ -69,6 +70,7 @@ QAction* createCommentButton(QToolBar& bar)
6970
QObject::connect(
7071
action, SIGNAL(triggered()),
7172
GuiAppMgr::instance(), SLOT(recvCommentClicked()));
73+
ShortcutMgr::instanceRef().updateShortcut(*action, ShortcutMgr::Key_Comment);
7274
return action;
7375
}
7476

@@ -78,6 +80,7 @@ QAction* createDupButton(QToolBar& bar)
7880
QObject::connect(
7981
action, SIGNAL(triggered()),
8082
GuiAppMgr::instance(), SLOT(recvDupClicked()));
83+
ShortcutMgr::instanceRef().updateShortcut(*action, ShortcutMgr::Key_DupMessage);
8184
return action;
8285
}
8386

@@ -86,6 +89,7 @@ QAction* createDeleteButton(QToolBar& bar)
8689
auto* action = bar.addAction(icon::remove(), "Delete Selected Message");
8790
QObject::connect(action, SIGNAL(triggered()),
8891
GuiAppMgr::instance(), SLOT(recvDeleteClicked()));
92+
ShortcutMgr::instanceRef().updateShortcut(*action, ShortcutMgr::Key_Delete);
8993
return action;
9094
}
9195

@@ -94,6 +98,7 @@ QAction* createClearButton(QToolBar& bar)
9498
auto* action = bar.addAction(icon::editClear(), "Delete All Displayed Messages");
9599
QObject::connect(action, SIGNAL(triggered()),
96100
GuiAppMgr::instance(), SLOT(recvClearClicked()));
101+
ShortcutMgr::instanceRef().updateShortcut(*action, ShortcutMgr::Key_ClearRecv);
97102
return action;
98103
}
99104

@@ -270,6 +275,7 @@ void RecvAreaToolBar::refreshStartStopButton()
270275
button->setText(StartTooltip);
271276
}
272277
button->setEnabled(enabled);
278+
ShortcutMgr::instanceRef().updateShortcut(*button, ShortcutMgr::Key_Receive);
273279
}
274280

275281
void RecvAreaToolBar::refreshLoadButton()

0 commit comments

Comments
 (0)