2010-05-17 00:22:51 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-17 00:22:51 +02:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "globalshortcutgrabber.h"
|
|
|
|
#include "ui_globalshortcutgrabber.h"
|
|
|
|
|
|
|
|
#include <QtDebug>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
|
|
|
|
GlobalShortcutGrabber::GlobalShortcutGrabber(QWidget *parent)
|
|
|
|
: QDialog(parent),
|
|
|
|
ui_(new Ui::GlobalShortcutGrabber)
|
|
|
|
{
|
|
|
|
ui_->setupUi(this);
|
2010-06-17 15:59:32 +02:00
|
|
|
|
|
|
|
modifier_keys_ << Qt::Key_Shift << Qt::Key_Control << Qt::Key_Meta
|
|
|
|
<< Qt::Key_Alt << Qt::Key_AltGr;
|
2010-05-17 00:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GlobalShortcutGrabber::~GlobalShortcutGrabber() {
|
|
|
|
delete ui_;
|
|
|
|
}
|
|
|
|
|
|
|
|
QKeySequence GlobalShortcutGrabber::GetKey(const QString &name) {
|
|
|
|
ui_->label->setText(tr("Press a key combination to use for %1...").arg(name));
|
|
|
|
ui_->combo->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);
|
|
|
|
}
|
|
|
|
|
2011-09-01 15:10:30 +02:00
|
|
|
void GlobalShortcutGrabber::grabKeyboard() {
|
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
SetupMacEventHandler();
|
|
|
|
#endif
|
|
|
|
QDialog::grabKeyboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcutGrabber::releaseKeyboard() {
|
|
|
|
#ifdef Q_OS_DARWIN
|
|
|
|
TeardownMacEventHandler();
|
|
|
|
#endif
|
|
|
|
QDialog::releaseKeyboard();
|
|
|
|
}
|
|
|
|
|
2010-05-17 00:22:51 +02:00
|
|
|
bool GlobalShortcutGrabber::event(QEvent* e) {
|
|
|
|
if (e->type() == QEvent::ShortcutOverride) {
|
|
|
|
QKeyEvent* ke = static_cast<QKeyEvent*>(e);
|
|
|
|
|
2010-06-17 15:59:32 +02:00
|
|
|
if (modifier_keys_.contains(ke->key()))
|
2010-05-17 00:22:51 +02:00
|
|
|
ret_ = QKeySequence(ke->modifiers());
|
|
|
|
else
|
|
|
|
ret_ = QKeySequence(ke->modifiers() | ke->key());
|
|
|
|
|
2011-09-01 15:10:30 +02:00
|
|
|
UpdateText();
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2010-06-17 15:59:32 +02:00
|
|
|
if (!modifier_keys_.contains(ke->key()))
|
2010-05-17 00:22:51 +02:00
|
|
|
accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return QDialog::event(e);
|
|
|
|
}
|
|
|
|
|
2011-09-01 15:10:30 +02:00
|
|
|
void GlobalShortcutGrabber::UpdateText() {
|
|
|
|
ui_->combo->setText("<b>" + ret_.toString(QKeySequence::NativeText) + "</b>");
|
|
|
|
}
|
|
|
|
|
2010-05-17 00:22:51 +02:00
|
|
|
|