Make HotkeyManager a singleton

main
Chris Rizzitello 2023-10-04 10:46:33 -04:00 committed by jkennedyvz
parent d1c85440de
commit 87e7ba56e9
7 changed files with 53 additions and 54 deletions

View File

@ -22,9 +22,8 @@
#include "components/custom_keyseq_edit/singlestrokekeysequenceedit.h"
#include "components/loading_button/loadingbutton.h"
Settings::Settings(HotkeyManager *hotkeyManager, QWidget *parent)
Settings::Settings(QWidget *parent)
: AShirtDialog(parent, AShirtDialog::commonWindowFlags)
, hotkeyManager(hotkeyManager)
, connStatusLabel(new QLabel(this))
, eviRepoTextBox(new QLineEdit(this))
, accessKeyTextBox(new QLineEdit(this))
@ -163,7 +162,7 @@ void Settings::checkForDuplicateShortcuts(const QKeySequence& keySequence, QKeyS
void Settings::showEvent(QShowEvent *evt) {
QDialog::showEvent(evt);
hotkeyManager->disableHotkeys();
HotkeyManager::disableHotkeys();
eviRepoTextBox->setFocus(); //setting focus to prevent retaining focus for macs
// reset the form in case a user left junk in the text boxes and pressed "cancel"
@ -184,12 +183,12 @@ void Settings::showEvent(QShowEvent *evt) {
void Settings::closeEvent(QCloseEvent *event) {
onSaveClicked();
hotkeyManager->enableHotkeys();
HotkeyManager::enableHotkeys();
QDialog::closeEvent(event);
}
void Settings::onCancelClicked() {
hotkeyManager->enableHotkeys();
HotkeyManager::enableHotkeys();
reject();
}
@ -211,7 +210,7 @@ void Settings::onSaveClicked() {
AppConfig::setValue(CONFIG::SHORTCUT_CAPTUREWINDOW, captureWindowShortcutTextBox->keySequence().toString());
AppConfig::setValue(CONFIG::SHORTCUT_CAPTURECLIPBOARD, captureClipboardShortcutTextBox->keySequence().toString());
hotkeyManager->updateHotkeys();
HotkeyManager::updateHotkeys();
close();
}

View File

@ -26,10 +26,9 @@ class Settings : public AShirtDialog {
public:
/**
* @brief Settings constructs the settings menu. UI will be built and wired.
* @param hotkeyManager a handle to the HotkeyManager, so hotkeys may be updated upon saving.
* @param parent
*/
explicit Settings(HotkeyManager* hotkeyManager, QWidget* parent = nullptr);
explicit Settings(QWidget* parent = nullptr);
~Settings() = default;
private:
@ -61,9 +60,6 @@ class Settings : public AShirtDialog {
void onBrowseClicked();
private:
/// hotkeyManager is a (shared) reference to the HotkeyManager. Not to be deleted.
HotkeyManager* hotkeyManager;
// UI components
QLabel* connStatusLabel = nullptr;

View File

@ -107,7 +107,9 @@ bool UGlobalHotkeys::registerHotkey(const UKeySequence &keySeq, size_t id)
void UGlobalHotkeys::unregisterHotkey(size_t id)
{
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
Q_ASSERT(Registered.find(id) != Registered.end() && "Unregistered hotkey");
if(Registered.find(id) == Registered.end()) {
return;
}
#endif
#if defined(Q_OS_WIN)
UnregisterHotKey(nullptr, id);

View File

@ -4,51 +4,52 @@
#include "hotkeymanager.h"
#include "appconfig.h"
HotkeyManager::HotkeyManager(QObject *parent)
: QObject (parent)
, hotkeyManager(new UGlobalHotkeys(this))
HotkeyManager::HotkeyManager()
: m_hotkeyManager(new UGlobalHotkeys(this))
{
connect(hotkeyManager, &UGlobalHotkeys::activated, this, &HotkeyManager::hotkeyTriggered);
connect(m_hotkeyManager, &UGlobalHotkeys::activated, this, &HotkeyManager::hotkeyTriggered);
}
HotkeyManager::~HotkeyManager() { delete hotkeyManager; }
HotkeyManager::~HotkeyManager() { delete m_hotkeyManager; }
void HotkeyManager::regKey(QString combo, GlobalHotkeyEvent evt)
{
if (combo.isEmpty())
return;
registerKey(combo, evt);
}
void HotkeyManager::registerKey(const QString& binding, GlobalHotkeyEvent evt) {
hotkeyManager->registerHotkey(binding, size_t(evt));
get()->m_hotkeyManager->registerHotkey(binding, size_t(evt));
}
void HotkeyManager::unregisterKey(GlobalHotkeyEvent evt) {
hotkeyManager->unregisterHotkey(size_t(evt));
get()->m_hotkeyManager->unregisterHotkey(size_t(evt));
}
void HotkeyManager::hotkeyTriggered(size_t hotkeyIndex) {
if (hotkeyIndex == ACTION_CAPTURE_AREA) {
Q_EMIT captureAreaHotkeyPressed();
Q_EMIT get()->captureAreaHotkeyPressed();
}
else if (hotkeyIndex == ACTION_CAPTURE_WINDOW) {
Q_EMIT captureWindowHotkeyPressed();
Q_EMIT get()->captureWindowHotkeyPressed();
}
else if (hotkeyIndex == ACTION_CAPTURE_CLIPBOARD) {
Q_EMIT clipboardHotkeyPressed();
Q_EMIT get()->clipboardHotkeyPressed();
}
}
void HotkeyManager::disableHotkeys() {
hotkeyManager->unregisterAllHotkeys();
get()->m_hotkeyManager->unregisterAllHotkeys();
}
void HotkeyManager::enableHotkeys() {
updateHotkeys();
get()->updateHotkeys();
}
void HotkeyManager::updateHotkeys() {
hotkeyManager->unregisterAllHotkeys();
auto regKey = [this](QString combo, GlobalHotkeyEvent evt) {
if (!combo.isEmpty()) {
registerKey(combo, evt);
}
};
regKey(AppConfig::value(CONFIG::SHORTCUT_SCREENSHOT), ACTION_CAPTURE_AREA);
regKey(AppConfig::value(CONFIG::SHORTCUT_CAPTUREWINDOW), ACTION_CAPTURE_WINDOW);
regKey(AppConfig::value(CONFIG::SHORTCUT_CAPTURECLIPBOARD), ACTION_CAPTURE_CLIPBOARD);
get()->m_hotkeyManager->unregisterAllHotkeys();
get()->regKey(AppConfig::value(CONFIG::SHORTCUT_SCREENSHOT), ACTION_CAPTURE_AREA);
get()->regKey(AppConfig::value(CONFIG::SHORTCUT_CAPTUREWINDOW), ACTION_CAPTURE_WINDOW);
get()->regKey(AppConfig::value(CONFIG::SHORTCUT_CAPTURECLIPBOARD), ACTION_CAPTURE_CLIPBOARD);
}

View File

@ -13,12 +13,9 @@
*/
class HotkeyManager : public QObject {
Q_OBJECT
/// GlobalHotkeyEvent provides names for all possible application-global hotkeys
public:
HotkeyManager(QObject *parent = nullptr);
~HotkeyManager();
/// GlobalHotkeyEvent provides names for all possible application-global hotkeys
enum GlobalHotkeyEvent {
// Reserving 1 (UGlobalHotkey default)
ACTION_CAPTURE_AREA = 2,
@ -26,23 +23,26 @@ class HotkeyManager : public QObject {
ACTION_CAPTURE_CLIPBOARD = 4,
};
public:
static HotkeyManager* get() {
static HotkeyManager m;
return &m;
}
/**
* @brief registerKey pairs a given key combination with a given event type.
* @param binding A string representing the actual command (e.g. Alt+f1)
* @param evt A GlobalHotkeyEvent specifying what should happen when a key is pressed.
*/
void registerKey(const QString& binding, GlobalHotkeyEvent evt);
static void registerKey(const QString& binding, GlobalHotkeyEvent evt);
/// unregisterKey removes the handling specified for the given event. Safe to call even if
/// no key has been registered.
void unregisterKey(GlobalHotkeyEvent evt);
static void unregisterKey(GlobalHotkeyEvent evt);
/// disableHotkeys removes all of the keybindings.
void disableHotkeys();
static void disableHotkeys();
/// enableHotkeys "restores" all of the currently set hotkeys. This acts as the counterpoint to
/// disableHotkeys, but functionally is identical to updateHotKeys.
void enableHotkeys();
static void enableHotkeys();
signals:
/// clipboardHotkeyPressed signals when the ACTION_CAPTURE_CLIPBOARD event has been triggered.
@ -55,13 +55,17 @@ class HotkeyManager : public QObject {
public slots:
/// updateHotkeys retrives AppConfig data to set known global hotkeys. Removes _all_ (Application)
/// hotkeys when called.
void updateHotkeys();
static void updateHotkeys();
private slots:
/// hotkeyTriggered provides a slot for interacting with the underlying UGlobalHotkey manager.
void hotkeyTriggered(size_t hotkeyIndex);
static void hotkeyTriggered(size_t hotkeyIndex);
private:
HotkeyManager();
~HotkeyManager();
/// Interal Reg method used to filter Empty keys
void regKey(QString combo, GlobalHotkeyEvent evt);
/// hotkeyManager is a reference to the raw hotkey manager, which a 3rd party manages.
UGlobalHotkeys* hotkeyManager;
UGlobalHotkeys* m_hotkeyManager = nullptr;
};

View File

@ -30,9 +30,8 @@ TrayManager::TrayManager(QWidget * parent, DatabaseConnection* db)
: QDialog(parent)
, db(db)
, screenshotTool(new Screenshot(this))
, hotkeyManager(new HotkeyManager(this))
, updateCheckTimer(new QTimer(this))
, settingsWindow(new Settings(hotkeyManager, this))
, settingsWindow(new Settings(this))
, evidenceManagerWindow(new EvidenceManager(this->db, this))
, creditsWindow(new Credits(this))
, importWindow(new PortingDialog(PortingDialog::Import, this->db, this))
@ -43,7 +42,7 @@ TrayManager::TrayManager(QWidget * parent, DatabaseConnection* db)
, allOperationActions(this)
{
hotkeyManager->updateHotkeys();
HotkeyManager::updateHotkeys();
updateCheckTimer->start(MS_IN_DAY); // every day
buildUi();
@ -106,11 +105,11 @@ void TrayManager::wireUi() {
&TrayManager::onScreenshotCaptured);
// connect to hotkey signals
connect(hotkeyManager, &HotkeyManager::clipboardHotkeyPressed, this,
connect(HotkeyManager::get(), &HotkeyManager::clipboardHotkeyPressed, this,
&TrayManager::captureClipboardActionTriggered);
connect(hotkeyManager, &HotkeyManager::captureAreaHotkeyPressed, this,
connect(HotkeyManager::get(), &HotkeyManager::captureAreaHotkeyPressed, this,
&TrayManager::captureAreaActionTriggered);
connect(hotkeyManager, &HotkeyManager::captureWindowHotkeyPressed, this,
connect(HotkeyManager::get(), &HotkeyManager::captureWindowHotkeyPressed, this,
&TrayManager::captureWindowActionTriggered);
// connect to network signals

View File

@ -14,7 +14,6 @@
#include "forms/porting/porting_dialog.h"
#include "forms/settings/settings.h"
#include "helpers/screenshot.h"
#include "hotkeymanager.h"
#include "forms/add_operation/createoperation.h"
#ifndef QT_NO_SYSTEMTRAYICON
@ -91,7 +90,6 @@ class TrayManager : public QDialog {
inline static const int MS_IN_DAY = 86400000;
QString _recordErrorTitle = tr("Unable to Record Evidence");
DatabaseConnection *db = nullptr;
HotkeyManager *hotkeyManager = nullptr;
Screenshot *screenshotTool = nullptr;
QTimer *updateCheckTimer = nullptr;
MessageType currentTrayMessage = NO_ACTION;