citra_qt: Improvements to hotkeys and ui state management (#6224)
This commit is contained in:
@ -2,31 +2,40 @@
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <QMenu>
|
||||
#include <QMessageBox>
|
||||
#include <QStandardItemModel>
|
||||
#include "citra_qt/configuration/config.h"
|
||||
#include "citra_qt/configuration/configure_hotkeys.h"
|
||||
#include "citra_qt/hotkeys.h"
|
||||
#include "citra_qt/util/sequence_dialog/sequence_dialog.h"
|
||||
#include "common/settings.h"
|
||||
#include "ui_configure_hotkeys.h"
|
||||
|
||||
constexpr int name_column = 0;
|
||||
constexpr int hotkey_column = 1;
|
||||
|
||||
ConfigureHotkeys::ConfigureHotkeys(QWidget* parent)
|
||||
: QWidget(parent), ui(std::make_unique<Ui::ConfigureHotkeys>()) {
|
||||
ui->setupUi(this);
|
||||
setFocusPolicy(Qt::ClickFocus);
|
||||
|
||||
model = new QStandardItemModel(this);
|
||||
model->setColumnCount(3);
|
||||
model->setHorizontalHeaderLabels({tr("Action"), tr("Hotkey"), tr("Context")});
|
||||
model->setColumnCount(2);
|
||||
model->setHorizontalHeaderLabels({tr("Action"), tr("Hotkey")});
|
||||
|
||||
connect(ui->hotkey_list, &QTreeView::doubleClicked, this, &ConfigureHotkeys::Configure);
|
||||
connect(ui->hotkey_list, &QTreeView::customContextMenuRequested, this,
|
||||
&ConfigureHotkeys::PopupContextMenu);
|
||||
ui->hotkey_list->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||
ui->hotkey_list->setModel(model);
|
||||
|
||||
// TODO(Kloen): Make context configurable as well (hiding the column for now)
|
||||
ui->hotkey_list->hideColumn(2);
|
||||
ui->hotkey_list->setColumnWidth(0, 250);
|
||||
ui->hotkey_list->resizeColumnToContents(hotkey_column);
|
||||
|
||||
ui->hotkey_list->setColumnWidth(0, 200);
|
||||
ui->hotkey_list->resizeColumnToContents(1);
|
||||
connect(ui->button_restore_defaults, &QPushButton::clicked, this,
|
||||
&ConfigureHotkeys::RestoreDefaults);
|
||||
connect(ui->button_clear_all, &QPushButton::clicked, this, &ConfigureHotkeys::ClearAll);
|
||||
}
|
||||
|
||||
ConfigureHotkeys::~ConfigureHotkeys() = default;
|
||||
@ -55,9 +64,11 @@ void ConfigureHotkeys::Populate(const HotkeyRegistry& registry) {
|
||||
QStandardItem* action = new QStandardItem(hotkey.first);
|
||||
QStandardItem* keyseq =
|
||||
new QStandardItem(hotkey.second.keyseq.toString(QKeySequence::NativeText));
|
||||
QStandardItem* controller_keyseq = new QStandardItem(hotkey.second.controller_keyseq);
|
||||
action->setEditable(false);
|
||||
keyseq->setEditable(false);
|
||||
parent_item->appendRow({action, keyseq});
|
||||
controller_keyseq->setEditable(false);
|
||||
parent_item->appendRow({action, keyseq, controller_keyseq});
|
||||
}
|
||||
model->appendRow(parent_item);
|
||||
}
|
||||
@ -74,8 +85,9 @@ void ConfigureHotkeys::Configure(QModelIndex index) {
|
||||
return;
|
||||
}
|
||||
|
||||
index = index.sibling(index.row(), 1);
|
||||
auto* const model = ui->hotkey_list->model();
|
||||
// Swap to the hotkey column
|
||||
index = index.sibling(index.row(), hotkey_column);
|
||||
|
||||
const auto previous_key = model->data(index);
|
||||
|
||||
SequenceDialog hotkey_dialog{this};
|
||||
@ -85,26 +97,50 @@ void ConfigureHotkeys::Configure(QModelIndex index) {
|
||||
if (return_code == QDialog::Rejected || key_sequence.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
const auto [key_sequence_used, used_action] = IsUsedKey(key_sequence);
|
||||
|
||||
if (IsUsedKey(key_sequence) && key_sequence != QKeySequence(previous_key.toString())) {
|
||||
QMessageBox::warning(this, tr("Conflicting Key Sequence"),
|
||||
tr("The entered key sequence is already assigned to another hotkey."));
|
||||
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));
|
||||
} else {
|
||||
model->setData(index, key_sequence.toString(QKeySequence::NativeText));
|
||||
EmitHotkeysChanged();
|
||||
}
|
||||
}
|
||||
|
||||
bool ConfigureHotkeys::IsUsedKey(QKeySequence key_sequence) const {
|
||||
return input_keys_list.contains(key_sequence) || GetUsedKeyList().contains(key_sequence);
|
||||
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());
|
||||
}
|
||||
|
||||
void ConfigureHotkeys::ApplyConfiguration(HotkeyRegistry& registry) {
|
||||
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++) {
|
||||
QStandardItem* action = parent->child(key_column_id, 0);
|
||||
QStandardItem* keyseq = parent->child(key_column_id, 1);
|
||||
const QStandardItem* action = parent->child(key_column_id, name_column);
|
||||
const QStandardItem* keyseq = parent->child(key_column_id, hotkey_column);
|
||||
for (auto& [group, sub_actions] : registry.hotkey_groups) {
|
||||
if (group != parent->text())
|
||||
continue;
|
||||
@ -120,6 +156,61 @@ void ConfigureHotkeys::ApplyConfiguration(HotkeyRegistry& registry) {
|
||||
registry.SaveHotkeys();
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
void ConfigureHotkeys::RetranslateUI() {
|
||||
ui->retranslateUi(this);
|
||||
}
|
||||
|
Reference in New Issue
Block a user