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
|
|
|
|
|
|
|
#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>
|
|
|
|
#include <QtDebug>
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
GlobalShortcutsSettingsPage::GlobalShortcutsSettingsPage(SettingsDialog* dialog)
|
|
|
|
: SettingsPage(dialog),
|
|
|
|
ui_(new Ui_GlobalShortcutsSettingsPage),
|
|
|
|
initialised_(false),
|
2010-05-17 00:22:51 +02:00
|
|
|
grabber_(new GlobalShortcutGrabber)
|
|
|
|
{
|
|
|
|
ui_->setupUi(this);
|
|
|
|
ui_->shortcut_options->setEnabled(false);
|
|
|
|
ui_->list->header()->setResizeMode(QHeaderView::ResizeToContents);
|
2011-08-14 01:07:10 +02:00
|
|
|
setWindowIcon(IconLoader::Load("input-keyboard"));
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2010-06-23 18:27:23 +02:00
|
|
|
settings_.beginGroup(GlobalShortcuts::kSettingsGroup);
|
|
|
|
|
|
|
|
connect(ui_->list, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), SLOT(ItemClicked(QTreeWidgetItem*)));
|
|
|
|
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()));
|
|
|
|
connect(ui_->gnome_open, SIGNAL(clicked()), SLOT(OpenGnomeKeybindingProperties()));
|
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
GlobalShortcutsSettingsPage::~GlobalShortcutsSettingsPage() {
|
|
|
|
delete ui_;
|
|
|
|
}
|
2010-05-17 02:31:39 +02:00
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
bool GlobalShortcutsSettingsPage::IsEnabled() const {
|
|
|
|
#ifdef Q_OS_MAC
|
2011-11-09 13:59:25 +01:00
|
|
|
qLog(Debug) << Utilities::GetMacVersion();
|
|
|
|
if (Utilities::GetMacVersion() < 6) { // Leopard and earlier.
|
2011-08-14 01:07:10 +02:00
|
|
|
return false;
|
2010-05-17 13:06:50 +02:00
|
|
|
}
|
2011-08-14 01:07:10 +02:00
|
|
|
#endif
|
2010-05-17 00:22:51 +02:00
|
|
|
|
2011-08-14 01:07:10 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
connect(ui_->mac_open, SIGNAL(clicked()), manager, SLOT(ShowMacAccessibilityDialog()));
|
|
|
|
|
|
|
|
if (!manager->IsGsdAvailable()) {
|
|
|
|
ui_->gnome_container->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const GlobalShortcuts::Shortcut& s, manager->shortcuts().values()) {
|
|
|
|
Shortcut shortcut;
|
|
|
|
shortcut.s = s;
|
|
|
|
shortcut.key = s.action->shortcut();
|
|
|
|
shortcut.item = new QTreeWidgetItem(ui_->list,
|
|
|
|
QStringList() << s.action->text()
|
|
|
|
<< s.action->shortcut().toString(QKeySequence::NativeText));
|
|
|
|
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
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
foreach (const Shortcut& s, shortcuts_.values()) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-08-14 01:07:10 +02: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() {
|
2010-05-17 13:06:50 +02:00
|
|
|
foreach (const Shortcut& s, shortcuts_.values()) {
|
|
|
|
s.s.action->setShortcut(s.key);
|
|
|
|
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);
|
2010-05-17 13:06:50 +02: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
|
|
|
|
2010-05-17 00:22:51 +02:00
|
|
|
if (key.isEmpty())
|
|
|
|
return;
|
|
|
|
|
2010-07-10 21:17:21 +02:00
|
|
|
// Check if this key sequence is used by any other actions
|
|
|
|
foreach (const QString& id, shortcuts_.keys()) {
|
|
|
|
if (shortcuts_[id].key == key)
|
|
|
|
SetShortcut(id, QKeySequence());
|
|
|
|
}
|
|
|
|
|
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")) {
|
2011-10-14 23:07:01 +02:00
|
|
|
if (!QProcess::startDetached("gnome-control-center",
|
|
|
|
QStringList() << "keyboard")) {
|
|
|
|
QMessageBox::warning(this, "Error",
|
|
|
|
tr("The \"%1\" command could not be started.")
|
|
|
|
.arg("gnome-keybinding-properties"));
|
|
|
|
}
|
2010-05-17 02:31:39 +02:00
|
|
|
}
|
|
|
|
}
|