| 1 | /* |
| 2 | * Copyright (C) 2019 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "GlobalHotKeyHandler.h" |
| 21 | |
| 22 | GlobalHotKeyHandler::GlobalHotKeyHandler( |
| 23 | const QList<CaptureModes> &supportedCaptureModes, |
| 24 | const QSharedPointer<IPlatformChecker> &platformChecker, |
| 25 | const QSharedPointer<IConfig> &config) : |
| 26 | mSupportedCaptureModes(supportedCaptureModes), |
| 27 | mConfig(config), |
| 28 | mPlatformChecker(platformChecker) |
| 29 | { |
| 30 | connect(mConfig.data(), &IConfig::hotKeysChanged, this, &GlobalHotKeyHandler::setupHotKeys); |
| 31 | |
| 32 | setupHotKeys(); |
| 33 | } |
| 34 | |
| 35 | GlobalHotKeyHandler::~GlobalHotKeyHandler() |
| 36 | { |
| 37 | removeHotKeys(); |
| 38 | } |
| 39 | |
| 40 | void GlobalHotKeyHandler::removeHotKeys() |
| 41 | { |
| 42 | mGlobalHotKeys.clear(); |
| 43 | } |
| 44 | |
| 45 | void GlobalHotKeyHandler::setupHotKeys() |
| 46 | { |
| 47 | removeHotKeys(); |
| 48 | if(mConfig->globalHotKeysEnabled()) { |
| 49 | createHotKey(mConfig->rectAreaHotKey(), CaptureModes::RectArea); |
| 50 | createHotKey(mConfig->lastRectAreaHotKey(), CaptureModes::LastRectArea); |
| 51 | createHotKey(mConfig->fullScreenHotKey(), CaptureModes::FullScreen); |
| 52 | createHotKey(mConfig->currentScreenHotKey(), CaptureModes::CurrentScreen); |
| 53 | createHotKey(mConfig->activeWindowHotKey(), CaptureModes::ActiveWindow); |
| 54 | createHotKey(mConfig->windowUnderCursorHotKey(), CaptureModes::WindowUnderCursor); |
| 55 | createHotKey(mConfig->portalHotKey(), CaptureModes::Portal); |
| 56 | |
| 57 | auto actions = mConfig->actions(); |
| 58 | for (const auto& action : actions) { |
| 59 | createHotKey(action); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void GlobalHotKeyHandler::createHotKey(const QKeySequence &keySequence, CaptureModes captureMode) |
| 65 | { |
| 66 | if(mSupportedCaptureModes.contains(captureMode) && !keySequence.isEmpty()) { |
| 67 | auto hotKey = QSharedPointer<GlobalHotKey>(new GlobalHotKey(QApplication::instance(), keySequence, mPlatformChecker)); |
| 68 | connect(hotKey.data(), &GlobalHotKey::pressed, [this, captureMode](){ emit captureTriggered(captureMode); }); |
| 69 | mGlobalHotKeys.append(hotKey); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void GlobalHotKeyHandler::createHotKey(const Action &action) |
| 74 | { |
| 75 | auto isGlobal = action.isGlobalShortcut(); |
| 76 | auto isShortcutSet = !action.shortcut().isEmpty(); |
| 77 | auto isPostProcessingOnlyAction = !action.isCaptureEnabled(); |
| 78 | auto isRequestedCaptureSupported = action.isCaptureEnabled() && mSupportedCaptureModes.contains(action.captureMode()); |
| 79 | if(isShortcutSet && isGlobal && (isPostProcessingOnlyAction || isRequestedCaptureSupported)) { |
| 80 | auto hotKey = QSharedPointer<GlobalHotKey>(new GlobalHotKey(QApplication::instance(), action.shortcut(), mPlatformChecker)); |
| 81 | connect(hotKey.data(), &GlobalHotKey::pressed, [this, action](){ emit actionTriggered(action); }); |
| 82 | mGlobalHotKeys.append(hotKey); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void GlobalHotKeyHandler::setEnabled(bool enabled) |
| 87 | { |
| 88 | if(enabled) { |
| 89 | setupHotKeys(); |
| 90 | } else { |
| 91 | removeHotKeys(); |
| 92 | } |
| 93 | } |