strawberry-audio-player-win.../src/globalshortcuts/globalshortcutgrabber.cpp

123 lines
3.1 KiB
C++
Raw Permalink Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
#include <QDialog>
#include <QWidget>
#include <QString>
#include <QLabel>
#include <QKeySequence>
2020-02-09 02:29:35 +01:00
#include <QDialogButtonBox>
#include <QEvent>
#include <QShowEvent>
#include <QHideEvent>
#include <QKeyEvent>
2018-02-27 18:06:05 +01:00
#include "globalshortcutgrabber.h"
#include "ui_globalshortcutgrabber.h"
GlobalShortcutGrabber::GlobalShortcutGrabber(QWidget *parent)
2021-07-11 07:40:57 +02:00
: QDialog(parent),
ui_(new Ui::GlobalShortcutGrabber),
wrapper_(nullptr) {
2021-06-20 23:53:28 +02:00
2018-02-27 18:06:05 +01:00
ui_->setupUi(this);
modifier_keys_ << Qt::Key_Shift << Qt::Key_Control << Qt::Key_Meta << Qt::Key_Alt << Qt::Key_AltGr;
2021-01-26 16:48:04 +01:00
QObject::connect(ui_->buttonBox, &QDialogButtonBox::accepted, this, &GlobalShortcutGrabber::Accepted);
QObject::connect(ui_->buttonBox, &QDialogButtonBox::rejected, this, &GlobalShortcutGrabber::Rejected);
2018-02-27 18:06:05 +01:00
}
GlobalShortcutGrabber::~GlobalShortcutGrabber() {
delete ui_;
}
QKeySequence GlobalShortcutGrabber::GetKey(const QString &name) {
ui_->label_shortcut->setText(tr("Press a key combination to use for %1...").arg(name));
ui_->label_key->clear();
2018-02-27 18:06:05 +01:00
ret_ = QKeySequence();
if (exec() == QDialog::Rejected) return QKeySequence();
return ret_;
}
void GlobalShortcutGrabber::showEvent(QShowEvent *e) {
grabKeyboard();
QDialog::showEvent(e);
}
void GlobalShortcutGrabber::hideEvent(QHideEvent *e) {
releaseKeyboard();
QDialog::hideEvent(e);
}
void GlobalShortcutGrabber::grabKeyboard() {
2018-07-01 22:26:46 +02:00
#ifdef Q_OS_MACOS
2018-02-27 18:06:05 +01:00
SetupMacEventHandler();
#endif
QDialog::grabKeyboard();
}
void GlobalShortcutGrabber::releaseKeyboard() {
2018-07-01 22:26:46 +02:00
#ifdef Q_OS_MACOS
2018-02-27 18:06:05 +01:00
TeardownMacEventHandler();
#endif
QDialog::releaseKeyboard();
}
bool GlobalShortcutGrabber::event(QEvent *e) {
if (e->type() == QEvent::ShortcutOverride) {
QKeyEvent *ke = static_cast<QKeyEvent*>(e);
2021-08-23 21:21:08 +02:00
if (modifier_keys_.contains(ke->key())) {
2021-10-30 02:21:29 +02:00
ret_ = QKeySequence(static_cast<int>(ke->modifiers()));
2021-08-23 21:21:08 +02:00
}
else {
2021-10-30 02:21:29 +02:00
ret_ = QKeySequence(static_cast<int>(ke->modifiers() | ke->key()));
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
UpdateText();
if (!modifier_keys_.contains(ke->key())) accept();
return true;
}
return QDialog::event(e);
}
void GlobalShortcutGrabber::UpdateText() {
ui_->label_key->setText(QLatin1String("<b>") + ret_.toString(QKeySequence::NativeText) + QLatin1String("</b>"));
}
void GlobalShortcutGrabber::Accepted() {
accept();
}
void GlobalShortcutGrabber::Rejected() {
if (ui_->label_key->text().isEmpty()) reject();
2018-02-27 18:06:05 +01:00
}