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/custom_keyseq_edit/singlestrokekeysequenceedit.h"
#include "components/loading_button/loadingbutton.h" #include "components/loading_button/loadingbutton.h"
Settings::Settings(HotkeyManager *hotkeyManager, QWidget *parent) Settings::Settings(QWidget *parent)
: AShirtDialog(parent, AShirtDialog::commonWindowFlags) : AShirtDialog(parent, AShirtDialog::commonWindowFlags)
, hotkeyManager(hotkeyManager)
, connStatusLabel(new QLabel(this)) , connStatusLabel(new QLabel(this))
, eviRepoTextBox(new QLineEdit(this)) , eviRepoTextBox(new QLineEdit(this))
, accessKeyTextBox(new QLineEdit(this)) , accessKeyTextBox(new QLineEdit(this))
@ -163,7 +162,7 @@ void Settings::checkForDuplicateShortcuts(const QKeySequence& keySequence, QKeyS
void Settings::showEvent(QShowEvent *evt) { void Settings::showEvent(QShowEvent *evt) {
QDialog::showEvent(evt); QDialog::showEvent(evt);
hotkeyManager->disableHotkeys(); HotkeyManager::disableHotkeys();
eviRepoTextBox->setFocus(); //setting focus to prevent retaining focus for macs 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" // 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) { void Settings::closeEvent(QCloseEvent *event) {
onSaveClicked(); onSaveClicked();
hotkeyManager->enableHotkeys(); HotkeyManager::enableHotkeys();
QDialog::closeEvent(event); QDialog::closeEvent(event);
} }
void Settings::onCancelClicked() { void Settings::onCancelClicked() {
hotkeyManager->enableHotkeys(); HotkeyManager::enableHotkeys();
reject(); reject();
} }
@ -211,7 +210,7 @@ void Settings::onSaveClicked() {
AppConfig::setValue(CONFIG::SHORTCUT_CAPTUREWINDOW, captureWindowShortcutTextBox->keySequence().toString()); AppConfig::setValue(CONFIG::SHORTCUT_CAPTUREWINDOW, captureWindowShortcutTextBox->keySequence().toString());
AppConfig::setValue(CONFIG::SHORTCUT_CAPTURECLIPBOARD, captureClipboardShortcutTextBox->keySequence().toString()); AppConfig::setValue(CONFIG::SHORTCUT_CAPTURECLIPBOARD, captureClipboardShortcutTextBox->keySequence().toString());
hotkeyManager->updateHotkeys(); HotkeyManager::updateHotkeys();
close(); close();
} }

View File

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

View File

@ -107,7 +107,9 @@ bool UGlobalHotkeys::registerHotkey(const UKeySequence &keySeq, size_t id)
void UGlobalHotkeys::unregisterHotkey(size_t id) void UGlobalHotkeys::unregisterHotkey(size_t id)
{ {
#if defined(Q_OS_WIN) || defined(Q_OS_LINUX) #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 #endif
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
UnregisterHotKey(nullptr, id); UnregisterHotKey(nullptr, id);

View File

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

View File

@ -13,12 +13,9 @@
*/ */
class HotkeyManager : public QObject { class HotkeyManager : public QObject {
Q_OBJECT Q_OBJECT
/// GlobalHotkeyEvent provides names for all possible application-global hotkeys
public: public:
HotkeyManager(QObject *parent = nullptr);
~HotkeyManager();
/// GlobalHotkeyEvent provides names for all possible application-global hotkeys
enum GlobalHotkeyEvent { enum GlobalHotkeyEvent {
// Reserving 1 (UGlobalHotkey default) // Reserving 1 (UGlobalHotkey default)
ACTION_CAPTURE_AREA = 2, ACTION_CAPTURE_AREA = 2,
@ -26,23 +23,26 @@ class HotkeyManager : public QObject {
ACTION_CAPTURE_CLIPBOARD = 4, 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. * @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 binding A string representing the actual command (e.g. Alt+f1)
* @param evt A GlobalHotkeyEvent specifying what should happen when a key is pressed. * @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 /// unregisterKey removes the handling specified for the given event. Safe to call even if
/// no key has been registered. /// no key has been registered.
void unregisterKey(GlobalHotkeyEvent evt); static void unregisterKey(GlobalHotkeyEvent evt);
/// disableHotkeys removes all of the keybindings. /// 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 /// enableHotkeys "restores" all of the currently set hotkeys. This acts as the counterpoint to
/// disableHotkeys, but functionally is identical to updateHotKeys. /// disableHotkeys, but functionally is identical to updateHotKeys.
void enableHotkeys(); static void enableHotkeys();
signals: signals:
/// clipboardHotkeyPressed signals when the ACTION_CAPTURE_CLIPBOARD event has been triggered. /// clipboardHotkeyPressed signals when the ACTION_CAPTURE_CLIPBOARD event has been triggered.
@ -55,13 +55,17 @@ class HotkeyManager : public QObject {
public slots: public slots:
/// updateHotkeys retrives AppConfig data to set known global hotkeys. Removes _all_ (Application) /// updateHotkeys retrives AppConfig data to set known global hotkeys. Removes _all_ (Application)
/// hotkeys when called. /// hotkeys when called.
void updateHotkeys(); static void updateHotkeys();
private slots: private slots:
/// hotkeyTriggered provides a slot for interacting with the underlying UGlobalHotkey manager. /// hotkeyTriggered provides a slot for interacting with the underlying UGlobalHotkey manager.
void hotkeyTriggered(size_t hotkeyIndex); static void hotkeyTriggered(size_t hotkeyIndex);
private: 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. /// 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) : QDialog(parent)
, db(db) , db(db)
, screenshotTool(new Screenshot(this)) , screenshotTool(new Screenshot(this))
, hotkeyManager(new HotkeyManager(this))
, updateCheckTimer(new QTimer(this)) , updateCheckTimer(new QTimer(this))
, settingsWindow(new Settings(hotkeyManager, this)) , settingsWindow(new Settings(this))
, evidenceManagerWindow(new EvidenceManager(this->db, this)) , evidenceManagerWindow(new EvidenceManager(this->db, this))
, creditsWindow(new Credits(this)) , creditsWindow(new Credits(this))
, importWindow(new PortingDialog(PortingDialog::Import, this->db, this)) , importWindow(new PortingDialog(PortingDialog::Import, this->db, this))
@ -43,7 +42,7 @@ TrayManager::TrayManager(QWidget * parent, DatabaseConnection* db)
, allOperationActions(this) , allOperationActions(this)
{ {
hotkeyManager->updateHotkeys(); HotkeyManager::updateHotkeys();
updateCheckTimer->start(MS_IN_DAY); // every day updateCheckTimer->start(MS_IN_DAY); // every day
buildUi(); buildUi();
@ -106,11 +105,11 @@ void TrayManager::wireUi() {
&TrayManager::onScreenshotCaptured); &TrayManager::onScreenshotCaptured);
// connect to hotkey signals // connect to hotkey signals
connect(hotkeyManager, &HotkeyManager::clipboardHotkeyPressed, this, connect(HotkeyManager::get(), &HotkeyManager::clipboardHotkeyPressed, this,
&TrayManager::captureClipboardActionTriggered); &TrayManager::captureClipboardActionTriggered);
connect(hotkeyManager, &HotkeyManager::captureAreaHotkeyPressed, this, connect(HotkeyManager::get(), &HotkeyManager::captureAreaHotkeyPressed, this,
&TrayManager::captureAreaActionTriggered); &TrayManager::captureAreaActionTriggered);
connect(hotkeyManager, &HotkeyManager::captureWindowHotkeyPressed, this, connect(HotkeyManager::get(), &HotkeyManager::captureWindowHotkeyPressed, this,
&TrayManager::captureWindowActionTriggered); &TrayManager::captureWindowActionTriggered);
// connect to network signals // connect to network signals

View File

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