/* * Strawberry Music Player * This file was part of Clementine. * Copyright 2010, David Sansome * * 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 . * */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include "globalshortcutgrabber.h" #include "ui_globalshortcutgrabber.h" GlobalShortcutGrabber::GlobalShortcutGrabber(QWidget *parent) : QDialog(parent), ui_(new Ui::GlobalShortcutGrabber), wrapper_(nullptr) { ui_->setupUi(this); modifier_keys_ << Qt::Key_Shift << Qt::Key_Control << Qt::Key_Meta << Qt::Key_Alt << Qt::Key_AltGr; QObject::connect(ui_->buttonBox, &QDialogButtonBox::accepted, this, &GlobalShortcutGrabber::Accepted); QObject::connect(ui_->buttonBox, &QDialogButtonBox::rejected, this, &GlobalShortcutGrabber::Rejected); } 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(); 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() { #ifdef Q_OS_MACOS SetupMacEventHandler(); #endif QDialog::grabKeyboard(); } void GlobalShortcutGrabber::releaseKeyboard() { #ifdef Q_OS_MACOS TeardownMacEventHandler(); #endif QDialog::releaseKeyboard(); } bool GlobalShortcutGrabber::event(QEvent *e) { if (e->type() == QEvent::ShortcutOverride) { QKeyEvent *ke = static_cast(e); if (modifier_keys_.contains(ke->key())) { ret_ = QKeySequence(static_cast(ke->modifiers())); } else { ret_ = QKeySequence(static_cast(ke->modifiers() | ke->key())); } UpdateText(); if (!modifier_keys_.contains(ke->key())) accept(); return true; } return QDialog::event(e); } void GlobalShortcutGrabber::UpdateText() { ui_->label_key->setText(QLatin1String("") + ret_.toString(QKeySequence::NativeText) + QLatin1String("")); } void GlobalShortcutGrabber::Accepted() { accept(); } void GlobalShortcutGrabber::Rejected() { if (ui_->label_key->text().isEmpty()) reject(); }