2018-05-22 19:30:36 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2023-02-04 18:06:20 +00:00
|
|
|
#include <QMenu>
|
2018-05-22 19:30:36 +00:00
|
|
|
#include <QMessageBox>
|
2018-12-30 10:09:16 +00:00
|
|
|
#include <QStandardItemModel>
|
2023-02-04 18:06:20 +00:00
|
|
|
#include "citra_qt/configuration/config.h"
|
2018-05-22 19:30:36 +00:00
|
|
|
#include "citra_qt/configuration/configure_hotkeys.h"
|
|
|
|
#include "citra_qt/hotkeys.h"
|
|
|
|
#include "citra_qt/util/sequence_dialog/sequence_dialog.h"
|
2022-12-08 11:27:25 +00:00
|
|
|
#include "common/settings.h"
|
2018-05-22 19:30:36 +00:00
|
|
|
#include "ui_configure_hotkeys.h"
|
|
|
|
|
2023-02-04 18:06:20 +00:00
|
|
|
constexpr int name_column = 0;
|
|
|
|
constexpr int hotkey_column = 1;
|
|
|
|
|
2018-05-22 19:30:36 +00:00
|
|
|
ConfigureHotkeys::ConfigureHotkeys(QWidget* parent)
|
|
|
|
: QWidget(parent), ui(std::make_unique<Ui::ConfigureHotkeys>()) {
|
|
|
|
ui->setupUi(this);
|
|
|
|
setFocusPolicy(Qt::ClickFocus);
|
|
|
|
|
|
|
|
model = new QStandardItemModel(this);
|
2023-02-04 18:06:20 +00:00
|
|
|
model->setColumnCount(2);
|
|
|
|
model->setHorizontalHeaderLabels({tr("Action"), tr("Hotkey")});
|
2018-05-22 19:30:36 +00:00
|
|
|
|
|
|
|
connect(ui->hotkey_list, &QTreeView::doubleClicked, this, &ConfigureHotkeys::Configure);
|
2023-02-04 18:06:20 +00:00
|
|
|
connect(ui->hotkey_list, &QTreeView::customContextMenuRequested, this,
|
|
|
|
&ConfigureHotkeys::PopupContextMenu);
|
|
|
|
ui->hotkey_list->setContextMenuPolicy(Qt::CustomContextMenu);
|
2018-05-22 19:30:36 +00:00
|
|
|
ui->hotkey_list->setModel(model);
|
|
|
|
|
2023-02-04 18:06:20 +00:00
|
|
|
ui->hotkey_list->setColumnWidth(0, 250);
|
|
|
|
ui->hotkey_list->resizeColumnToContents(hotkey_column);
|
2018-05-22 19:30:36 +00:00
|
|
|
|
2023-02-04 18:06:20 +00:00
|
|
|
connect(ui->button_restore_defaults, &QPushButton::clicked, this,
|
|
|
|
&ConfigureHotkeys::RestoreDefaults);
|
|
|
|
connect(ui->button_clear_all, &QPushButton::clicked, this, &ConfigureHotkeys::ClearAll);
|
2018-05-22 19:30:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 10:09:16 +00:00
|
|
|
ConfigureHotkeys::~ConfigureHotkeys() = default;
|
2018-05-22 19:30:36 +00:00
|
|
|
|
|
|
|
void ConfigureHotkeys::EmitHotkeysChanged() {
|
|
|
|
emit HotkeysChanged(GetUsedKeyList());
|
|
|
|
}
|
|
|
|
|
2019-04-09 23:35:52 +00:00
|
|
|
QList<QKeySequence> ConfigureHotkeys::GetUsedKeyList() const {
|
2018-05-22 19:30:36 +00:00
|
|
|
QList<QKeySequence> list;
|
|
|
|
for (int r = 0; r < model->rowCount(); r++) {
|
|
|
|
QStandardItem* parent = model->item(r, 0);
|
|
|
|
for (int r2 = 0; r2 < parent->rowCount(); r2++) {
|
|
|
|
QStandardItem* keyseq = parent->child(r2, 1);
|
|
|
|
list << QKeySequence::fromString(keyseq->text(), QKeySequence::NativeText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::Populate(const HotkeyRegistry& registry) {
|
|
|
|
for (const auto& group : registry.hotkey_groups) {
|
|
|
|
QStandardItem* parent_item = new QStandardItem(group.first);
|
|
|
|
parent_item->setEditable(false);
|
|
|
|
for (const auto& hotkey : group.second) {
|
|
|
|
QStandardItem* action = new QStandardItem(hotkey.first);
|
|
|
|
QStandardItem* keyseq =
|
|
|
|
new QStandardItem(hotkey.second.keyseq.toString(QKeySequence::NativeText));
|
2023-02-04 18:06:20 +00:00
|
|
|
QStandardItem* controller_keyseq = new QStandardItem(hotkey.second.controller_keyseq);
|
2018-05-22 19:30:36 +00:00
|
|
|
action->setEditable(false);
|
|
|
|
keyseq->setEditable(false);
|
2023-02-04 18:06:20 +00:00
|
|
|
controller_keyseq->setEditable(false);
|
|
|
|
parent_item->appendRow({action, keyseq, controller_keyseq});
|
2018-05-22 19:30:36 +00:00
|
|
|
}
|
|
|
|
model->appendRow(parent_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->hotkey_list->expandAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::OnInputKeysChanged(QList<QKeySequence> new_key_list) {
|
|
|
|
input_keys_list = new_key_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::Configure(QModelIndex index) {
|
2019-04-09 23:47:18 +00:00
|
|
|
if (!index.parent().isValid()) {
|
2018-05-22 19:30:36 +00:00
|
|
|
return;
|
2019-04-09 23:47:18 +00:00
|
|
|
}
|
2018-05-22 19:30:36 +00:00
|
|
|
|
2023-02-04 18:06:20 +00:00
|
|
|
// Swap to the hotkey column
|
|
|
|
index = index.sibling(index.row(), hotkey_column);
|
|
|
|
|
2019-04-09 23:50:14 +00:00
|
|
|
const auto previous_key = model->data(index);
|
2018-05-22 19:30:36 +00:00
|
|
|
|
2019-04-10 00:06:45 +00:00
|
|
|
SequenceDialog hotkey_dialog{this};
|
2018-05-22 19:30:36 +00:00
|
|
|
|
2019-04-09 23:50:59 +00:00
|
|
|
const int return_code = hotkey_dialog.exec();
|
|
|
|
const auto key_sequence = hotkey_dialog.GetSequence();
|
2019-04-09 23:50:14 +00:00
|
|
|
if (return_code == QDialog::Rejected || key_sequence.isEmpty()) {
|
2018-05-22 19:30:36 +00:00
|
|
|
return;
|
2019-04-09 23:50:14 +00:00
|
|
|
}
|
2023-02-04 18:06:20 +00:00
|
|
|
const auto [key_sequence_used, used_action] = IsUsedKey(key_sequence);
|
2018-05-22 19:30:36 +00:00
|
|
|
|
2023-02-04 18:06:20 +00:00
|
|
|
if (key_sequence_used && key_sequence != QKeySequence(previous_key.toString())) {
|
|
|
|
QMessageBox::warning(
|
|
|
|
this, tr("Conflicting Key Sequence"),
|
|
|
|
tr("The entered key sequence is already assigned to: %1").arg(used_action));
|
2018-05-22 19:30:36 +00:00
|
|
|
} else {
|
|
|
|
model->setData(index, key_sequence.toString(QKeySequence::NativeText));
|
|
|
|
EmitHotkeysChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-04 18:06:20 +00:00
|
|
|
std::pair<bool, QString> ConfigureHotkeys::IsUsedKey(QKeySequence key_sequence) const {
|
|
|
|
if (key_sequence == QKeySequence::fromString(QStringLiteral(""), QKeySequence::NativeText)) {
|
|
|
|
return std::make_pair(false, QString());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (input_keys_list.contains(key_sequence)) {
|
|
|
|
return std::make_pair(true, tr("A 3ds button"));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int r = 0; r < model->rowCount(); ++r) {
|
|
|
|
const QStandardItem* const parent = model->item(r, 0);
|
|
|
|
|
|
|
|
for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
|
|
|
|
const QStandardItem* const key_seq_item = parent->child(r2, hotkey_column);
|
|
|
|
const auto key_seq_str = key_seq_item->text();
|
|
|
|
const auto key_seq = QKeySequence::fromString(key_seq_str, QKeySequence::NativeText);
|
|
|
|
|
|
|
|
if (key_sequence == key_seq) {
|
|
|
|
return std::make_pair(true, parent->child(r2, 0)->text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(false, QString());
|
2018-05-22 19:30:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 04:39:23 +00:00
|
|
|
void ConfigureHotkeys::ApplyConfiguration(HotkeyRegistry& registry) {
|
2018-05-22 19:30:36 +00:00
|
|
|
for (int key_id = 0; key_id < model->rowCount(); key_id++) {
|
|
|
|
QStandardItem* parent = model->item(key_id, 0);
|
|
|
|
for (int key_column_id = 0; key_column_id < parent->rowCount(); key_column_id++) {
|
2023-02-04 18:06:20 +00:00
|
|
|
const QStandardItem* action = parent->child(key_column_id, name_column);
|
|
|
|
const QStandardItem* keyseq = parent->child(key_column_id, hotkey_column);
|
2018-12-12 15:44:27 +00:00
|
|
|
for (auto& [group, sub_actions] : registry.hotkey_groups) {
|
|
|
|
if (group != parent->text())
|
|
|
|
continue;
|
|
|
|
for (auto& [action_name, hotkey] : sub_actions) {
|
|
|
|
if (action_name != action->text())
|
|
|
|
continue;
|
|
|
|
hotkey.keyseq = QKeySequence(keyseq->text());
|
2018-05-22 19:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registry.SaveHotkeys();
|
|
|
|
}
|
|
|
|
|
2023-02-04 18:06:20 +00:00
|
|
|
void ConfigureHotkeys::RestoreDefaults() {
|
|
|
|
for (int r = 0; r < model->rowCount(); ++r) {
|
|
|
|
const QStandardItem* parent = model->item(r, 0);
|
|
|
|
|
|
|
|
for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
|
|
|
|
model->item(r, 0)
|
|
|
|
->child(r2, hotkey_column)
|
|
|
|
->setText(Config::default_hotkeys[r2].shortcut.keyseq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::ClearAll() {
|
|
|
|
for (int r = 0; r < model->rowCount(); ++r) {
|
|
|
|
const QStandardItem* parent = model->item(r, 0);
|
|
|
|
|
|
|
|
for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
|
|
|
|
model->item(r, 0)->child(r2, hotkey_column)->setText(QString{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::PopupContextMenu(const QPoint& menu_location) {
|
|
|
|
const auto index = ui->hotkey_list->indexAt(menu_location);
|
|
|
|
if (!index.parent().isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMenu context_menu;
|
|
|
|
QAction* restore_default = context_menu.addAction(tr("Restore Default"));
|
|
|
|
QAction* clear = context_menu.addAction(tr("Clear"));
|
|
|
|
|
|
|
|
const auto hotkey_index = index.sibling(index.row(), hotkey_column);
|
|
|
|
connect(restore_default, &QAction::triggered,
|
|
|
|
[this, hotkey_index] { RestoreHotkey(hotkey_index); });
|
|
|
|
connect(clear, &QAction::triggered,
|
|
|
|
[this, hotkey_index] { model->setData(hotkey_index, QString{}); });
|
|
|
|
|
|
|
|
context_menu.exec(ui->hotkey_list->viewport()->mapToGlobal(menu_location));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::RestoreHotkey(QModelIndex index) {
|
|
|
|
const QKeySequence& default_key_sequence = QKeySequence::fromString(
|
|
|
|
Config::default_hotkeys[index.row()].shortcut.keyseq, QKeySequence::NativeText);
|
|
|
|
const auto [key_sequence_used, used_action] = IsUsedKey(default_key_sequence);
|
|
|
|
|
|
|
|
if (key_sequence_used && default_key_sequence != QKeySequence(model->data(index).toString())) {
|
|
|
|
QMessageBox::warning(
|
|
|
|
this, tr("Conflicting Key Sequence"),
|
|
|
|
tr("The default key sequence is already assigned to: %1").arg(used_action));
|
|
|
|
} else {
|
|
|
|
model->setData(index, default_key_sequence.toString(QKeySequence::NativeText));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 04:39:23 +00:00
|
|
|
void ConfigureHotkeys::RetranslateUI() {
|
2018-05-22 19:30:36 +00:00
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|