Qt: Persist shortcut configurations to a settings file
@@ -12,6 +12,7 @@ - Debugger: Add CLI functions for writing to memory
- Better audio resampling via blip-buf - Game Pak overrides dialog for setting savetype and sensor values - Support for games using the tilt sensor + - Remappable shortcuts for keyboard and gamepad Bugfixes: - Qt: Fix issue with set frame sizes being the wrong height - Qt: Fix emulator crashing when full screen if a game is not running
@@ -143,6 +143,17 @@ QString ConfigController::getOption(const char* key) const {
return QString(GBAConfigGetValue(&m_config, key)); } +QVariant ConfigController::getQtOption(const QString& key, const QString& group) const { + if (!group.isNull()) { + m_settings->beginGroup(group); + } + QVariant value = m_settings->value(key); + if (!group.isNull()) { + m_settings->endGroup(); + } + return value; +} + void ConfigController::setOption(const char* key, bool value) { GBAConfigSetIntValue(&m_config, key, value); QString optionName(key);@@ -182,6 +193,16 @@ return;
} QString stringValue(value.toString()); setOption(key, stringValue.toLocal8Bit().constData()); +} + +void ConfigController::setQtOption(const QString& key, const QVariant& value, const QString& group) { + if (!group.isNull()) { + m_settings->beginGroup(group); + } + m_settings->setValue(key, value); + if (!group.isNull()) { + m_settings->endGroup(); + } } QList<QString> ConfigController::getMRU() const {
@@ -70,6 +70,8 @@ void updateOption(const char* key);
QString getOption(const char* key) const; + QVariant getQtOption(const QString& key, const QString& group = QString()) const; + QList<QString> getMRU() const; void setMRU(const QList<QString>& mru);@@ -79,6 +81,7 @@ void setOption(const char* key, int value);
void setOption(const char* key, unsigned value); void setOption(const char* key, const char* value); void setOption(const char* key, const QVariant& value); + void setQtOption(const QString& key, const QVariant& value, const QString& group = QString()); void write();
@@ -5,6 +5,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "ShortcutController.h" +#include "ConfigController.h" #include "GamepadButtonEvent.h" #include <QAction>@@ -16,7 +17,12 @@
ShortcutController::ShortcutController(QObject* parent) : QAbstractItemModel(parent) , m_rootMenu(nullptr) + , m_config(nullptr) { +} + +void ShortcutController::setConfigController(ConfigController* controller) { + m_config = controller; } QVariant ShortcutController::data(const QModelIndex& index, int role) const {@@ -99,6 +105,9 @@ beginInsertRows(parent, smenu->items().count(), smenu->items().count());
smenu->addAction(action, name); endInsertRows(); ShortcutItem* item = &smenu->items().last(); + if (m_config) { + loadShortcuts(item); + } emit dataChanged(createIndex(smenu->items().count() - 1, 0, item), createIndex(smenu->items().count() - 1, 2, item)); }@@ -114,6 +123,9 @@ beginInsertRows(parent, smenu->items().count(), smenu->items().count());
smenu->addFunctions(qMakePair(press, release), shortcut, visibleName, name); endInsertRows(); ShortcutItem* item = &smenu->items().last(); + if (m_config) { + loadShortcuts(item); + } m_heldKeys[shortcut] = item; emit dataChanged(createIndex(smenu->items().count() - 1, 0, item), createIndex(smenu->items().count() - 1, 2, item)); }@@ -176,6 +188,9 @@ }
m_heldKeys[keySequence] = item; } item->setShortcut(keySequence); + if (m_config) { + m_config->setQtOption(item->name(), keySequence.toString(), KEY_SECTION); + } emit dataChanged(createIndex(index.row(), 0, index.internalPointer()), createIndex(index.row(), 2, index.internalPointer())); }@@ -194,6 +209,9 @@ if (oldButton >= 0) {
m_buttons.take(oldButton); } m_buttons[button] = item; + if (m_config) { + m_config->setQtOption(item->name(), button, BUTTON_SECTION); + } emit dataChanged(createIndex(index.row(), 0, index.internalPointer()), createIndex(index.row(), 2, index.internalPointer())); }@@ -249,6 +267,30 @@ event->accept();
return true; } return false; +} + +void ShortcutController::loadShortcuts(ShortcutItem* item) { + QVariant shortcut = m_config->getQtOption(item->name(), KEY_SECTION); + if (!shortcut.isNull()) { + QKeySequence keySequence(shortcut.toString()); + if (item->functions().first) { + QKeySequence oldShortcut = item->shortcut(); + if (!oldShortcut.isEmpty()) { + m_heldKeys.take(oldShortcut); + } + m_heldKeys[keySequence] = item; + } + item->setShortcut(keySequence); + } + QVariant button = m_config->getQtOption(item->name(), BUTTON_SECTION); + if (!button.isNull()) { + int oldButton = item->button(); + item->setButton(button.toInt()); + if (oldButton >= 0) { + m_buttons.take(oldButton); + } + m_buttons[button.toInt()] = item; + } } QKeySequence ShortcutController::keyEventToSequence(const QKeyEvent* event) {
@@ -18,10 +18,15 @@ class QString;
namespace QGBA { +class ConfigController; + class ShortcutController : public QAbstractItemModel { Q_OBJECT private: + constexpr static const char* const KEY_SECTION = "shortcutKey"; + constexpr static const char* const BUTTON_SECTION = "shortcutButton"; + class ShortcutItem { public: typedef QPair<std::function<void ()>, std::function<void ()>> Functions;@@ -66,6 +71,8 @@
public: ShortcutController(QObject* parent = nullptr); + void setConfigController(ConfigController* controller); + virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;@@ -91,10 +98,14 @@ bool eventFilter(QObject*, QEvent*) override;
private: static QKeySequence keyEventToSequence(const QKeyEvent*); + + void loadShortcuts(ShortcutItem*); + ShortcutItem m_rootMenu; QMap<QMenu*, ShortcutItem*> m_menuMap; QMap<int, ShortcutItem*> m_buttons; QMap<QKeySequence, ShortcutItem*> m_heldKeys; + ConfigController* m_config; }; }
@@ -96,6 +96,7 @@
m_logView->setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL); m_fpsTimer.setInterval(FPS_TIMER_INTERVAL); + m_shortcutController->setConfigController(m_config); setupMenu(menuBar()); }