Skip to content

Commit c7e71b4

Browse files
committed
feat: activate tool while a key is pressed
- add UBActionGroupHistory - listen for key release events to return to previous tool
1 parent 9c1a65c commit c7e71b4

File tree

5 files changed

+146
-3
lines changed

5 files changed

+146
-3
lines changed

src/core/UBApplication.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,15 @@ bool UBApplication::eventFilter(QObject *obj, QEvent *event)
703703
}
704704
}
705705

706+
else if (event->type() == QEvent::KeyRelease)
707+
{
708+
// intercept key release events for shortcut handler
709+
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
710+
711+
return UBShortcutManager::shortcutManager()->handleKeyReleaseEvent(keyEvent)
712+
|| result;
713+
}
714+
706715
return result;
707716
}
708717

src/core/UBPreferencesController.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ bool UBPreferencesController::handleKeyEvent(QKeyEvent *event)
119119

120120
int keys = mods;
121121

122-
if (key < Qt::Key_Shift || key > Qt::Key_Alt)
122+
if (key < Qt::Key_Shift || key > Qt::Key_ScrollLock)
123123
{
124124
keys += key;
125125
}

src/core/UBShortcutManager.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ void UBShortcutManager::addMainActions(UBMainWindow *mainWindow)
294294
ignoreCtrl(UBSettings::settings()->value("Shortcut/IgnoreCtrl").toBool());
295295
}
296296

297+
void UBShortcutManager::addActionGroup(QActionGroup *actionGroup)
298+
{
299+
mActionGroupHistoryMap[actionGroup] = new UBActionGroupHistory(actionGroup);
300+
}
301+
302+
void UBShortcutManager::removeActionGroup(QActionGroup *actionGroup)
303+
{
304+
if (mActionGroupHistoryMap.contains(actionGroup))
305+
{
306+
delete mActionGroupHistoryMap[actionGroup];
307+
mActionGroupHistoryMap.remove(actionGroup);
308+
}
309+
}
310+
297311
bool UBShortcutManager::handleMouseEvent(QMouseEvent *event)
298312
{
299313
if (mMouseActions.contains(event->button()))
@@ -328,6 +342,18 @@ bool UBShortcutManager::handleTabletEvent(QTabletEvent *event)
328342
return false;
329343
}
330344

345+
bool UBShortcutManager::handleKeyReleaseEvent(QKeyEvent *event)
346+
{
347+
for (UBActionGroupHistory* actionGroupHistory : mActionGroupHistoryMap.values())
348+
{
349+
if (actionGroupHistory->keyReleased(event)) {
350+
return true;
351+
}
352+
}
353+
354+
return false;
355+
}
356+
331357
int UBShortcutManager::rowCount(const QModelIndex &parent) const
332358
{
333359
Q_UNUSED(parent);
@@ -479,13 +505,33 @@ bool UBShortcutManager::setData(const QModelIndex &index, const QVariant &value,
479505

480506
case 3:
481507
action->setProperty(mouseButtonProperty, value);
508+
509+
for (Qt::MouseButton key : mMouseActions.keys())
510+
{
511+
if (mMouseActions[key] == action)
512+
{
513+
mMouseActions.remove(key);
514+
break;
515+
}
516+
}
517+
482518
mMouseActions[static_cast<Qt::MouseButton>(value.toInt())] = action;
483519
updateSettings(action);
484520
emit dataChanged(index, index);
485521
return true;
486522

487523
case 4:
488524
action->setProperty(tabletButtonProperty, value);
525+
526+
for (Qt::MouseButton key : mTabletActions.keys())
527+
{
528+
if (mTabletActions[key] == action)
529+
{
530+
mTabletActions.remove(key);
531+
break;
532+
}
533+
}
534+
489535
mTabletActions[static_cast<Qt::MouseButton>(value.toInt())] = action;
490536
updateSettings(action);
491537
emit dataChanged(index, index);
@@ -749,3 +795,59 @@ void UBShortcutManager::updateSettings(const QAction *action) const
749795
UBSettings::settings()->setValue(key, list);
750796
}
751797
}
798+
799+
// ---------- UBActionGroupHistory ----------
800+
801+
UBActionGroupHistory::UBActionGroupHistory(QActionGroup *parent)
802+
: QObject(parent)
803+
, mActionGroup(parent)
804+
, mCurrentAction(parent->checkedAction())
805+
, mPreviousAction(nullptr)
806+
, mRevertingAction(nullptr)
807+
{
808+
connect(parent, &QActionGroup::triggered, this, &UBActionGroupHistory::triggered);
809+
}
810+
811+
void UBActionGroupHistory::triggered(QAction *action)
812+
{
813+
if (mCurrentAction != action)
814+
{
815+
mPreviousAction = mCurrentAction;
816+
mCurrentAction = action;
817+
}
818+
}
819+
820+
bool UBActionGroupHistory::keyReleased(QKeyEvent *event)
821+
{
822+
int key = event->key() & ~Qt::KeyboardModifierMask;
823+
824+
for (QAction* action : mActionGroup->actions())
825+
{
826+
QKeySequence keySequence = action->shortcut();
827+
828+
if (keySequence.count() > 0)
829+
{
830+
int actionKey = action->shortcut()[0] & ~Qt::KeyboardModifierMask;
831+
832+
if (key == actionKey)
833+
{
834+
if (event->isAutoRepeat())
835+
{
836+
if (!mRevertingAction)
837+
{
838+
mRevertingAction = mPreviousAction;;
839+
}
840+
}
841+
else if (mRevertingAction)
842+
{
843+
mRevertingAction->trigger();
844+
mRevertingAction = nullptr;
845+
}
846+
847+
return true;
848+
}
849+
}
850+
}
851+
852+
return false;
853+
}

src/core/UBShortcutManager.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
#include <QTabletEvent>
3737

3838
class QAction;
39+
class QActionGroup;
3940
class UBMainWindow;
41+
class UBActionGroupHistory;
4042

4143
class UBShortcutManager : public QAbstractTableModel
4244
{
43-
Q_OBJECT;
45+
Q_OBJECT
4446

4547
private:
4648
UBShortcutManager();
@@ -57,8 +59,12 @@ class UBShortcutManager : public QAbstractTableModel
5759
void addActions(const QString& group, const QList<QAction*> actions, QWidget* widget = nullptr);
5860
void addMainActions(UBMainWindow* mainWindow);
5961

62+
void addActionGroup(QActionGroup* actionGroup);
63+
void removeActionGroup(QActionGroup* actionGroup);
64+
6065
bool handleMouseEvent(QMouseEvent* event);
6166
bool handleTabletEvent(QTabletEvent* event);
67+
bool handleKeyReleaseEvent(QKeyEvent* event);
6268

6369
// QAbstractTableModel overrides
6470
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
@@ -87,9 +93,28 @@ public slots:
8793
QList<QPair<QString,QList<QAction*>>> mActionGroups;
8894
QMap<Qt::MouseButton, QAction*> mMouseActions;
8995
QMap<Qt::MouseButton, QAction*> mTabletActions;
96+
QMap<QActionGroup*, UBActionGroupHistory*> mActionGroupHistoryMap;
9097
bool mIgnoreCtrl;
9198

9299
static UBShortcutManager* sShortcutManager;
93100
};
94101

102+
class UBActionGroupHistory : public QObject
103+
{
104+
Q_OBJECT
105+
106+
public:
107+
UBActionGroupHistory(QActionGroup* parent);
108+
109+
public slots:
110+
void triggered(QAction* action);
111+
bool keyReleased(QKeyEvent* event);
112+
113+
private:
114+
QActionGroup* mActionGroup;
115+
QAction* mCurrentAction;
116+
QAction* mPreviousAction;
117+
QAction* mRevertingAction;
118+
};
119+
95120
#endif // UBSHORTCUTMANAGER_H

src/gui/UBStylusPalette.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "core/UBApplication.h"
3737
#include "core/UBSettings.h"
3838
#include "core/UBApplicationController.h"
39+
#include "core/UBShortcutManager.h"
40+
3941

4042
#include "board/UBDrawingController.h"
4143

@@ -87,6 +89,8 @@ UBStylusPalette::UBStylusPalette(QWidget *parent, Qt::Orientation orient)
8789
connect(mActionGroup, SIGNAL(triggered(QAction*)), this, SIGNAL(buttonGroupClicked(QAction*)));
8890
}
8991

92+
UBShortcutManager::shortcutManager()->addActionGroup(mActionGroup);
93+
9094
adjustSizeAndPosition();
9195

9296
initPosition();
@@ -126,7 +130,10 @@ void UBStylusPalette::initPosition()
126130

127131
UBStylusPalette::~UBStylusPalette()
128132
{
129-
133+
if (mActionGroup)
134+
{
135+
UBShortcutManager::shortcutManager()->removeActionGroup(mActionGroup);
136+
}
130137
}
131138

132139
void UBStylusPalette::stylusToolDoubleClicked()

0 commit comments

Comments
 (0)