Merge pull request #3791 from Kewlan/hotkey-config-plus
configuration: Add Restore Default and Clear options to hotkeys
This commit is contained in:
		| @@ -12,7 +12,6 @@ | ||||
| #include "input_common/main.h" | ||||
| #include "input_common/udp/client.h" | ||||
| #include "yuzu/configuration/config.h" | ||||
| #include "yuzu/uisettings.h" | ||||
|  | ||||
| Config::Config() { | ||||
|     // TODO: Don't hardcode the path; let the frontend decide where to put the config files. | ||||
| @@ -212,12 +211,13 @@ const std::array<int, Settings::NativeKeyboard::NumKeyboardMods> Config::default | ||||
| // This must be in alphabetical order according to action name as it must have the same order as | ||||
| // UISetting::values.shortcuts, which is alphabetically ordered. | ||||
| // clang-format off | ||||
| const std::array<UISettings::Shortcut, 15> default_hotkeys{{ | ||||
| const std::array<UISettings::Shortcut, 15> Config::default_hotkeys{{ | ||||
|     {QStringLiteral("Capture Screenshot"),       QStringLiteral("Main Window"), {QStringLiteral("Ctrl+P"), Qt::ApplicationShortcut}}, | ||||
|     {QStringLiteral("Change Docked Mode"),       QStringLiteral("Main Window"), {QStringLiteral("F10"), Qt::ApplicationShortcut}}, | ||||
|     {QStringLiteral("Continue/Pause Emulation"), QStringLiteral("Main Window"), {QStringLiteral("F4"), Qt::WindowShortcut}}, | ||||
|     {QStringLiteral("Decrease Speed Limit"),     QStringLiteral("Main Window"), {QStringLiteral("-"), Qt::ApplicationShortcut}}, | ||||
|     {QStringLiteral("Exit yuzu"),                QStringLiteral("Main Window"), {QStringLiteral("Ctrl+Q"), Qt::WindowShortcut}}, | ||||
|     {QStringLiteral("Exit Fullscreen"),          QStringLiteral("Main Window"), {QStringLiteral("Esc"), Qt::WindowShortcut}}, | ||||
|     {QStringLiteral("Exit yuzu"),                QStringLiteral("Main Window"), {QStringLiteral("Ctrl+Q"), Qt::WindowShortcut}}, | ||||
|     {QStringLiteral("Fullscreen"),               QStringLiteral("Main Window"), {QStringLiteral("F11"), Qt::WindowShortcut}}, | ||||
|     {QStringLiteral("Increase Speed Limit"),     QStringLiteral("Main Window"), {QStringLiteral("+"), Qt::ApplicationShortcut}}, | ||||
|     {QStringLiteral("Load Amiibo"),              QStringLiteral("Main Window"), {QStringLiteral("F2"), Qt::ApplicationShortcut}}, | ||||
| @@ -227,7 +227,6 @@ const std::array<UISettings::Shortcut, 15> default_hotkeys{{ | ||||
|     {QStringLiteral("Toggle Filter Bar"),        QStringLiteral("Main Window"), {QStringLiteral("Ctrl+F"), Qt::WindowShortcut}}, | ||||
|     {QStringLiteral("Toggle Speed Limit"),       QStringLiteral("Main Window"), {QStringLiteral("Ctrl+Z"), Qt::ApplicationShortcut}}, | ||||
|     {QStringLiteral("Toggle Status Bar"),        QStringLiteral("Main Window"), {QStringLiteral("Ctrl+S"), Qt::WindowShortcut}}, | ||||
|     {QStringLiteral("Change Docked Mode"),       QStringLiteral("Main Window"), {QStringLiteral("F10"), Qt::ApplicationShortcut}}, | ||||
| }}; | ||||
| // clang-format on | ||||
|  | ||||
|   | ||||
| @@ -9,6 +9,7 @@ | ||||
| #include <string> | ||||
| #include <QVariant> | ||||
| #include "core/settings.h" | ||||
| #include "yuzu/uisettings.h" | ||||
|  | ||||
| class QSettings; | ||||
|  | ||||
| @@ -26,6 +27,7 @@ public: | ||||
|         default_mouse_buttons; | ||||
|     static const std::array<int, Settings::NativeKeyboard::NumKeyboardKeys> default_keyboard_keys; | ||||
|     static const std::array<int, Settings::NativeKeyboard::NumKeyboardMods> default_keyboard_mods; | ||||
|     static const std::array<UISettings::Shortcut, 15> default_hotkeys; | ||||
|  | ||||
| private: | ||||
|     void ReadValues(); | ||||
|   | ||||
| @@ -2,10 +2,12 @@ | ||||
| // Licensed under GPLv2 or any later version | ||||
| // Refer to the license.txt file included. | ||||
|  | ||||
| #include <QMenu> | ||||
| #include <QMessageBox> | ||||
| #include <QStandardItemModel> | ||||
| #include "core/settings.h" | ||||
| #include "ui_configure_hotkeys.h" | ||||
| #include "yuzu/configuration/config.h" | ||||
| #include "yuzu/configuration/configure_hotkeys.h" | ||||
| #include "yuzu/hotkeys.h" | ||||
| #include "yuzu/util/sequence_dialog/sequence_dialog.h" | ||||
| @@ -19,6 +21,9 @@ ConfigureHotkeys::ConfigureHotkeys(QWidget* parent) | ||||
|     model->setColumnCount(3); | ||||
|  | ||||
|     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) | ||||
| @@ -27,6 +32,10 @@ ConfigureHotkeys::ConfigureHotkeys(QWidget* parent) | ||||
|     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); | ||||
|  | ||||
|     RetranslateUI(); | ||||
| } | ||||
|  | ||||
| @@ -71,7 +80,6 @@ void ConfigureHotkeys::Configure(QModelIndex index) { | ||||
|     } | ||||
|  | ||||
|     index = index.sibling(index.row(), 1); | ||||
|     auto* const model = ui->hotkey_list->model(); | ||||
|     const auto previous_key = model->data(index); | ||||
|  | ||||
|     SequenceDialog hotkey_dialog{this}; | ||||
| @@ -81,31 +89,33 @@ 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)); | ||||
|     } | ||||
| } | ||||
|  | ||||
| bool ConfigureHotkeys::IsUsedKey(QKeySequence key_sequence) const { | ||||
|     for (int r = 0; r < model->rowCount(); r++) { | ||||
| std::pair<bool, QString> ConfigureHotkeys::IsUsedKey(QKeySequence key_sequence) const { | ||||
|     for (int r = 0; r < model->rowCount(); ++r) { | ||||
|         const QStandardItem* const parent = model->item(r, 0); | ||||
|  | ||||
|         for (int r2 = 0; r2 < parent->rowCount(); r2++) { | ||||
|         for (int r2 = 0; r2 < parent->rowCount(); ++r2) { | ||||
|             const QStandardItem* const key_seq_item = parent->child(r2, 1); | ||||
|             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 true; | ||||
|                 return std::make_pair(true, parent->child(r2, 0)->text()); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     return false; | ||||
|     return std::make_pair(false, QString()); | ||||
| } | ||||
|  | ||||
| void ConfigureHotkeys::ApplyConfiguration(HotkeyRegistry& registry) { | ||||
| @@ -128,3 +138,55 @@ 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, 1)->setText(Config::default_hotkeys[r2].shortcut.first); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| 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, 1)->setText(tr("")); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| void ConfigureHotkeys::PopupContextMenu(const QPoint& menu_location) { | ||||
|     QModelIndex index = ui->hotkey_list->indexAt(menu_location); | ||||
|     if (!index.parent().isValid()) { | ||||
|         return; | ||||
|     } | ||||
|  | ||||
|     const auto selected = index.sibling(index.row(), 1); | ||||
|     QMenu context_menu; | ||||
|  | ||||
|     QAction* restore_default = context_menu.addAction(tr("Restore Default")); | ||||
|     QAction* clear = context_menu.addAction(tr("Clear")); | ||||
|  | ||||
|     connect(restore_default, &QAction::triggered, [this, selected] { | ||||
|         const QKeySequence& default_key_sequence = QKeySequence::fromString( | ||||
|             Config::default_hotkeys[selected.row()].shortcut.first, QKeySequence::NativeText); | ||||
|         const auto [key_sequence_used, used_action] = IsUsedKey(default_key_sequence); | ||||
|  | ||||
|         if (key_sequence_used && | ||||
|             default_key_sequence != QKeySequence(model->data(selected).toString())) { | ||||
|  | ||||
|             QMessageBox::warning( | ||||
|                 this, tr("Conflicting Key Sequence"), | ||||
|                 tr("The default key sequence is already assigned to: %1").arg(used_action)); | ||||
|         } else { | ||||
|             model->setData(selected, default_key_sequence.toString(QKeySequence::NativeText)); | ||||
|         } | ||||
|     }); | ||||
|     connect(clear, &QAction::triggered, [this, selected] { model->setData(selected, tr("")); }); | ||||
|  | ||||
|     context_menu.exec(ui->hotkey_list->viewport()->mapToGlobal(menu_location)); | ||||
| } | ||||
|   | ||||
| @@ -35,7 +35,11 @@ private: | ||||
|     void RetranslateUI(); | ||||
|  | ||||
|     void Configure(QModelIndex index); | ||||
|     bool IsUsedKey(QKeySequence key_sequence) const; | ||||
|     std::pair<bool, QString> IsUsedKey(QKeySequence key_sequence) const; | ||||
|  | ||||
|     void RestoreDefaults(); | ||||
|     void ClearAll(); | ||||
|     void PopupContextMenu(const QPoint& menu_location); | ||||
|  | ||||
|     std::unique_ptr<Ui::ConfigureHotkeys> ui; | ||||
|  | ||||
|   | ||||
| @@ -6,8 +6,8 @@ | ||||
|    <rect> | ||||
|     <x>0</x> | ||||
|     <y>0</y> | ||||
|     <width>363</width> | ||||
|     <height>388</height> | ||||
|     <width>439</width> | ||||
|     <height>510</height> | ||||
|    </rect> | ||||
|   </property> | ||||
|   <property name="windowTitle"> | ||||
| @@ -15,7 +15,7 @@ | ||||
|   </property> | ||||
|   <layout class="QVBoxLayout" name="verticalLayout"> | ||||
|    <item> | ||||
|     <layout class="QVBoxLayout" name="verticalLayout_2"> | ||||
|     <layout class="QHBoxLayout" name="horizontalLayout"> | ||||
|      <item> | ||||
|       <widget class="QLabel" name="label_2"> | ||||
|        <property name="text"> | ||||
| @@ -23,6 +23,37 @@ | ||||
|        </property> | ||||
|       </widget> | ||||
|      </item> | ||||
|      <item> | ||||
|       <spacer name="horizontalSpacer"> | ||||
|        <property name="orientation"> | ||||
|         <enum>Qt::Horizontal</enum> | ||||
|        </property> | ||||
|        <property name="sizeHint" stdset="0"> | ||||
|         <size> | ||||
|          <width>40</width> | ||||
|          <height>20</height> | ||||
|         </size> | ||||
|        </property> | ||||
|       </spacer> | ||||
|      </item> | ||||
|      <item> | ||||
|       <widget class="QPushButton" name="button_clear_all"> | ||||
|        <property name="text"> | ||||
|         <string>Clear All</string> | ||||
|        </property> | ||||
|       </widget> | ||||
|      </item> | ||||
|      <item> | ||||
|       <widget class="QPushButton" name="button_restore_defaults"> | ||||
|        <property name="text"> | ||||
|         <string>Restore Defaults</string> | ||||
|        </property> | ||||
|       </widget> | ||||
|      </item> | ||||
|     </layout> | ||||
|    </item> | ||||
|    <item> | ||||
|     <layout class="QVBoxLayout" name="verticalLayout_2"> | ||||
|      <item> | ||||
|       <widget class="QTreeView" name="hotkey_list"> | ||||
|        <property name="editTriggers"> | ||||
| @@ -39,4 +70,4 @@ | ||||
|  </widget> | ||||
|  <resources/> | ||||
|  <connections/> | ||||
| </ui> | ||||
| </ui> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 bunnei
					bunnei