Skip to content

Commit

Permalink
feat: activate tool while a key is pressed
Browse files Browse the repository at this point in the history
- add UBActionGroupHistory
- listen for key release events to return to previous tool
  • Loading branch information
letsfindaway committed Aug 16, 2024
1 parent d501638 commit bdaf1bf
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/core/UBApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,15 @@ bool UBApplication::eventFilter(QObject *obj, QEvent *event)
}
}

else if (event->type() == QEvent::KeyRelease)
{
// intercept key release events for shortcut handler
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);

return UBShortcutManager::shortcutManager()->handleKeyReleaseEvent(keyEvent)
|| result;
}

return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/UBPreferencesController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ bool UBPreferencesController::handleKeyEvent(QKeyEvent *event)

int keys = mods;

if (key < Qt::Key_Shift || key > Qt::Key_Alt)
if (key < Qt::Key_Shift || key > Qt::Key_ScrollLock)
{
keys += key;
}
Expand Down
102 changes: 102 additions & 0 deletions src/core/UBShortcutManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ void UBShortcutManager::addMainActions(UBMainWindow *mainWindow)
ignoreCtrl(UBSettings::settings()->value("Shortcut/IgnoreCtrl").toBool());
}

void UBShortcutManager::addActionGroup(QActionGroup *actionGroup)
{
mActionGroupHistoryMap[actionGroup] = new UBActionGroupHistory(actionGroup);
}

void UBShortcutManager::removeActionGroup(QActionGroup *actionGroup)
{
if (mActionGroupHistoryMap.contains(actionGroup))
{
delete mActionGroupHistoryMap[actionGroup];
mActionGroupHistoryMap.remove(actionGroup);
}
}

bool UBShortcutManager::handleMouseEvent(QMouseEvent *event)
{
if (mMouseActions.contains(event->button()))
Expand Down Expand Up @@ -328,6 +342,18 @@ bool UBShortcutManager::handleTabletEvent(QTabletEvent *event)
return false;
}

bool UBShortcutManager::handleKeyReleaseEvent(QKeyEvent *event)
{
for (UBActionGroupHistory* actionGroupHistory : mActionGroupHistoryMap.values())
{
if (actionGroupHistory->keyReleased(event)) {
return true;
}
}

return false;
}

int UBShortcutManager::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
Expand Down Expand Up @@ -479,13 +505,33 @@ bool UBShortcutManager::setData(const QModelIndex &index, const QVariant &value,

case 3:
action->setProperty(mouseButtonProperty, value);

for (Qt::MouseButton key : mMouseActions.keys())
{
if (mMouseActions[key] == action)
{
mMouseActions.remove(key);
break;
}
}

mMouseActions[static_cast<Qt::MouseButton>(value.toInt())] = action;
updateSettings(action);
emit dataChanged(index, index);
return true;

case 4:
action->setProperty(tabletButtonProperty, value);

for (Qt::MouseButton key : mTabletActions.keys())
{
if (mTabletActions[key] == action)
{
mTabletActions.remove(key);
break;
}
}

mTabletActions[static_cast<Qt::MouseButton>(value.toInt())] = action;
updateSettings(action);
emit dataChanged(index, index);
Expand Down Expand Up @@ -749,3 +795,59 @@ void UBShortcutManager::updateSettings(const QAction *action) const
UBSettings::settings()->setValue(key, list);
}
}

// ---------- UBActionGroupHistory ----------

UBActionGroupHistory::UBActionGroupHistory(QActionGroup *parent)
: QObject(parent)
, mActionGroup(parent)
, mCurrentAction(parent->checkedAction())
, mPreviousAction(nullptr)
, mRevertingAction(nullptr)
{
connect(parent, &QActionGroup::triggered, this, &UBActionGroupHistory::triggered);
}

void UBActionGroupHistory::triggered(QAction *action)
{
if (mCurrentAction != action)
{
mPreviousAction = mCurrentAction;
mCurrentAction = action;
}
}

bool UBActionGroupHistory::keyReleased(QKeyEvent *event)
{
int key = event->key() & ~Qt::KeyboardModifierMask;

for (QAction* action : mActionGroup->actions())
{
QKeySequence keySequence = action->shortcut();

if (keySequence.count() > 0)
{
int actionKey = action->shortcut()[0] & ~Qt::KeyboardModifierMask;

if (key == actionKey)
{
if (event->isAutoRepeat())
{
if (!mRevertingAction)
{
mRevertingAction = mPreviousAction;;
}
}
else if (mRevertingAction)
{
mRevertingAction->trigger();
mRevertingAction = nullptr;
}

return true;
}
}
}

return false;
}
27 changes: 26 additions & 1 deletion src/core/UBShortcutManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
#include <QTabletEvent>

class QAction;
class QActionGroup;
class UBMainWindow;
class UBActionGroupHistory;

class UBShortcutManager : public QAbstractTableModel
{
Q_OBJECT;
Q_OBJECT

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

void addActionGroup(QActionGroup* actionGroup);
void removeActionGroup(QActionGroup* actionGroup);

bool handleMouseEvent(QMouseEvent* event);
bool handleTabletEvent(QTabletEvent* event);
bool handleKeyReleaseEvent(QKeyEvent* event);

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

static UBShortcutManager* sShortcutManager;
};

class UBActionGroupHistory : public QObject
{
Q_OBJECT

public:
UBActionGroupHistory(QActionGroup* parent);

public slots:
void triggered(QAction* action);
bool keyReleased(QKeyEvent* event);

private:
QActionGroup* mActionGroup;
QAction* mCurrentAction;
QAction* mPreviousAction;
QAction* mRevertingAction;
};

#endif // UBSHORTCUTMANAGER_H
9 changes: 8 additions & 1 deletion src/gui/UBStylusPalette.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "core/UBApplication.h"
#include "core/UBSettings.h"
#include "core/UBApplicationController.h"
#include "core/UBShortcutManager.h"


#include "board/UBDrawingController.h"

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

UBShortcutManager::shortcutManager()->addActionGroup(mActionGroup);

adjustSizeAndPosition();

initPosition();
Expand Down Expand Up @@ -126,7 +130,10 @@ void UBStylusPalette::initPosition()

UBStylusPalette::~UBStylusPalette()
{

if (mActionGroup)
{
UBShortcutManager::shortcutManager()->removeActionGroup(mActionGroup);
}
}

void UBStylusPalette::stylusToolDoubleClicked()
Expand Down

0 comments on commit bdaf1bf

Please sign in to comment.