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"
|
2011-08-14 01:07:10 +02:00
|
|
|
#include "globalshortcutssettingspage.h"
|
|
|
|
#include "ui_globalshortcutssettingspage.h"
|
2010-05-17 02:31:39 +02:00
|
|
|
#include "core/globalshortcuts.h"
|
2011-11-09 13:59:25 +01:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/utilities.h"
|
2011-08-14 01:07:10 +02:00
|
|
|
#include "ui/iconloader.h"
|
|
|
|
#include "ui/settingsdialog.h"
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2018-03-17 07:16:49 +01:00
|
|
|
#include <QAction>
|
2010-05-17 00:22:51 +02:00
|
|
|
#include <QKeyEvent>
|
2010-05-17 02:31:39 +02:00
|
|
|
#include <QMessageBox>
|
2011-08-14 01:07:10 +02:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSettings>
|
2013-11-11 19:22:52 +01:00
|
|
|
#include <QShortcut>
|
2011-08-14 01:07:10 +02:00
|
|
|
#include <QtDebug>
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
GlobalShortcutsSettingsPage::GlobalShortcutsSettingsPage(SettingsDialog* dialog)
|
2014-02-07 16:34:20 +01:00
|
|
|
: SettingsPage(dialog),
|
|
|
|
ui_(new Ui_GlobalShortcutsSettingsPage),
|
|
|
|
initialised_(false),
|
|
|
|
grabber_(new GlobalShortcutGrabber) {
|
2010-05-17 00:22:51 +02:00
|
|
|
ui_->setupUi(this);
|
|
|
|
ui_->shortcut_options->setEnabled(false);
|
2015-04-11 22:52:31 +02:00
|
|
|
ui_->list->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
|
2015-10-14 03:01:08 +02:00
|
|
|
setWindowIcon(IconLoader::Load("input-keyboard", IconLoader::Base));
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2010-06-23 18:27:23 +02:00
|
|
|
settings_.beginGroup(GlobalShortcuts::kSettingsGroup);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(ui_->list,
|
|
|
|
SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
|
|
|
|
SLOT(ItemClicked(QTreeWidgetItem*)));
|
2010-06-23 18:27:23 +02:00
|
|
|
connect(ui_->radio_none, SIGNAL(clicked()), SLOT(NoneClicked()));
|
|
|
|
connect(ui_->radio_default, SIGNAL(clicked()), SLOT(DefaultClicked()));
|
|
|
|
connect(ui_->radio_custom, SIGNAL(clicked()), SLOT(ChangeClicked()));
|
|
|
|
connect(ui_->change, SIGNAL(clicked()), SLOT(ChangeClicked()));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(ui_->gnome_open, SIGNAL(clicked()),
|
|
|
|
SLOT(OpenGnomeKeybindingProperties()));
|
2010-06-23 18:27:23 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GlobalShortcutsSettingsPage::~GlobalShortcutsSettingsPage() { delete ui_; }
|
2010-05-17 02:31:39 +02:00
|
|
|
|
2015-02-18 18:03:05 +01:00
|
|
|
bool GlobalShortcutsSettingsPage::IsEnabled() const { return true; }
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void GlobalShortcutsSettingsPage::Load() {
|
|
|
|
GlobalShortcuts* manager = dialog()->global_shortcuts_manager();
|
|
|
|
|
|
|
|
if (!initialised_) {
|
|
|
|
initialised_ = true;
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(ui_->mac_open, SIGNAL(clicked()), manager,
|
|
|
|
SLOT(ShowMacAccessibilityDialog()));
|
2011-08-14 01:07:10 +02:00
|
|
|
|
|
|
|
if (!manager->IsGsdAvailable()) {
|
|
|
|
ui_->gnome_container->hide();
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const GlobalShortcuts::Shortcut& s : manager->shortcuts().values()) {
|
2011-08-14 01:07:10 +02:00
|
|
|
Shortcut shortcut;
|
|
|
|
shortcut.s = s;
|
|
|
|
shortcut.key = s.action->shortcut();
|
2014-02-07 16:34:20 +01:00
|
|
|
shortcut.item = new QTreeWidgetItem(
|
|
|
|
ui_->list, QStringList() << s.action->text()
|
|
|
|
<< s.action->shortcut().toString(
|
|
|
|
QKeySequence::NativeText));
|
2011-08-14 01:07:10 +02:00
|
|
|
shortcut.item->setData(0, Qt::UserRole, s.id);
|
|
|
|
shortcuts_[s.id] = shortcut;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_->list->sortItems(0, Qt::AscendingOrder);
|
|
|
|
ItemClicked(ui_->list->topLevelItem(0));
|
|
|
|
}
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Shortcut& s : shortcuts_.values()) {
|
2010-05-17 13:06:50 +02:00
|
|
|
SetShortcut(s.s.id, s.s.action->shortcut());
|
|
|
|
}
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
bool use_gnome = settings_.value("use_gnome", true).toBool();
|
2010-06-23 18:27:23 +02:00
|
|
|
if (ui_->gnome_container->isVisibleTo(this)) {
|
2010-05-17 13:06:50 +02:00
|
|
|
ui_->gnome_checkbox->setChecked(use_gnome);
|
|
|
|
}
|
2010-06-18 01:11:51 +02:00
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
ui_->mac_container->setVisible(!manager->IsMacAccessibilityEnabled());
|
2010-05-17 00:22:51 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void GlobalShortcutsSettingsPage::SetShortcut(const QString& id,
|
|
|
|
const QKeySequence& key) {
|
2010-05-17 00:22:51 +02:00
|
|
|
Shortcut& shortcut = shortcuts_[id];
|
|
|
|
|
|
|
|
shortcut.key = key;
|
|
|
|
shortcut.item->setText(1, key.toString(QKeySequence::NativeText));
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void GlobalShortcutsSettingsPage::Save() {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Shortcut& s : shortcuts_.values()) {
|
2010-05-17 13:06:50 +02:00
|
|
|
s.s.action->setShortcut(s.key);
|
2013-11-11 19:22:52 +01:00
|
|
|
s.s.shortcut->setKey(s.key);
|
2010-05-17 13:06:50 +02:00
|
|
|
settings_.setValue(s.s.id, s.key.toString());
|
2010-05-17 00:22:51 +02:00
|
|
|
}
|
2010-05-17 13:06:50 +02:00
|
|
|
|
|
|
|
settings_.setValue("use_gnome", ui_->gnome_checkbox->isChecked());
|
2010-06-23 18:27:23 +02:00
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
dialog()->global_shortcuts_manager()->ReloadSettings();
|
2010-05-17 00:22:51 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void GlobalShortcutsSettingsPage::ItemClicked(QTreeWidgetItem* item) {
|
2010-05-17 00:22:51 +02:00
|
|
|
current_id_ = item->data(0, Qt::UserRole).toString();
|
|
|
|
Shortcut& shortcut = shortcuts_[current_id_];
|
|
|
|
|
|
|
|
// Enable options
|
|
|
|
ui_->shortcut_options->setEnabled(true);
|
2014-02-07 16:34:20 +01:00
|
|
|
ui_->shortcut_options->setTitle(
|
|
|
|
tr("Shortcut for %1").arg(shortcut.s.action->text()));
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
if (shortcut.key == shortcut.s.default_key)
|
2010-05-17 00:22:51 +02:00
|
|
|
ui_->radio_default->setChecked(true);
|
|
|
|
else if (shortcut.key.isEmpty())
|
|
|
|
ui_->radio_none->setChecked(true);
|
|
|
|
else
|
|
|
|
ui_->radio_custom->setChecked(true);
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void GlobalShortcutsSettingsPage::NoneClicked() {
|
2010-05-17 00:22:51 +02:00
|
|
|
SetShortcut(current_id_, QKeySequence());
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void GlobalShortcutsSettingsPage::DefaultClicked() {
|
2010-05-17 13:06:50 +02:00
|
|
|
SetShortcut(current_id_, shortcuts_[current_id_].s.default_key);
|
2010-05-17 00:22:51 +02:00
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void GlobalShortcutsSettingsPage::ChangeClicked() {
|
|
|
|
GlobalShortcuts* manager = dialog()->global_shortcuts_manager();
|
|
|
|
manager->Unregister();
|
2010-05-17 13:06:50 +02:00
|
|
|
QKeySequence key = grabber_->GetKey(shortcuts_[current_id_].s.action->text());
|
2011-08-14 01:07:10 +02:00
|
|
|
manager->Register();
|
2010-07-10 21:17:21 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (key.isEmpty()) return;
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2010-07-10 21:17:21 +02:00
|
|
|
// Check if this key sequence is used by any other actions
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& id : shortcuts_.keys()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (shortcuts_[id].key == key) SetShortcut(id, QKeySequence());
|
2010-07-10 21:17:21 +02:00
|
|
|
}
|
|
|
|
|
2010-05-17 00:22:51 +02:00
|
|
|
ui_->radio_custom->setChecked(true);
|
|
|
|
SetShortcut(current_id_, key);
|
|
|
|
}
|
2010-05-17 02:31:39 +02:00
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
void GlobalShortcutsSettingsPage::OpenGnomeKeybindingProperties() {
|
2010-05-17 02:31:39 +02:00
|
|
|
if (!QProcess::startDetached("gnome-keybinding-properties")) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!QProcess::startDetached("gnome-control-center", QStringList()
|
|
|
|
<< "keyboard")) {
|
2011-10-14 23:07:01 +02:00
|
|
|
QMessageBox::warning(this, "Error",
|
2014-02-07 16:34:20 +01:00
|
|
|
tr("The \"%1\" command could not be started.")
|
|
|
|
.arg("gnome-keybinding-properties"));
|
2011-10-14 23:07:01 +02:00
|
|
|
}
|
2010-05-17 02:31:39 +02:00
|
|
|
}
|
|
|
|
}
|