From 63692a012feb874e5ba2056a59c8afa89e303cb5 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Mon, 27 Apr 2020 01:05:01 +1000 Subject: [PATCH] Qt: Add "New..." to input profile save button This will ensure the profile gets saved to the correct location. --- src/duckstation-qt/portsettingswidget.cpp | 63 +++++++++++++------ src/duckstation-qt/qthostinterface.h | 4 ++ src/frontend-common/common_host_interface.cpp | 5 ++ src/frontend-common/common_host_interface.h | 3 + 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/duckstation-qt/portsettingswidget.cpp b/src/duckstation-qt/portsettingswidget.cpp index 31403bd58..aa5979577 100644 --- a/src/duckstation-qt/portsettingswidget.cpp +++ b/src/duckstation-qt/portsettingswidget.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -336,15 +337,6 @@ void PortSettingsWidget::onLoadProfileClicked() const auto profile_names = m_host_interface->getInputProfileList(); QMenu menu; - for (const auto& [name, path] : profile_names) - { - QAction* action = menu.addAction(QString::fromStdString(name)); - QString path_qstr = QString::fromStdString(path); - connect(action, &QAction::triggered, [this, path_qstr]() { m_host_interface->applyInputProfile(path_qstr); }); - } - - if (!profile_names.empty()) - menu.addSeparator(); QAction* browse = menu.addAction(tr("Browse...")); connect(browse, &QAction::triggered, [this]() { @@ -354,6 +346,16 @@ void PortSettingsWidget::onLoadProfileClicked() m_host_interface->applyInputProfile(path); }); + if (!profile_names.empty()) + menu.addSeparator(); + + for (const auto& [name, path] : profile_names) + { + QAction* action = menu.addAction(QString::fromStdString(name)); + QString path_qstr = QString::fromStdString(path); + connect(action, &QAction::triggered, [this, path_qstr]() { m_host_interface->applyInputProfile(path_qstr); }); + } + menu.exec(QCursor::pos()); } @@ -362,6 +364,38 @@ void PortSettingsWidget::onSaveProfileClicked() const auto profile_names = m_host_interface->getInputProfileList(); QMenu menu; + + QAction* new_action = menu.addAction(tr("New...")); + connect(new_action, &QAction::triggered, [this]() { + QString name = QInputDialog::getText(QtUtils::GetRootWidget(this), tr("Enter Input Profile Name"), + tr("Enter Input Profile Name")); + if (name.isEmpty()) + { + QMessageBox::critical(QtUtils::GetRootWidget(this), tr("Error"), + tr("No name entered, input profile was not saved.")); + return; + } + + m_host_interface->saveInputProfile(m_host_interface->getPathForInputProfile(name)); + }); + + QAction* browse = menu.addAction(tr("Browse...")); + connect(browse, &QAction::triggered, [this]() { + QString path = QFileDialog::getSaveFileName(QtUtils::GetRootWidget(this), tr("Select path to input profile ini"), + QString(), tr(INPUT_PROFILE_FILTER)); + if (path.isEmpty()) + { + QMessageBox::critical(QtUtils::GetRootWidget(this), tr("Error"), + tr("No path selected, input profile was not saved.")); + return; + } + + m_host_interface->saveInputProfile(path); + }); + + if (!profile_names.empty()) + menu.addSeparator(); + for (const auto& [name, path] : profile_names) { QAction* action = menu.addAction(QString::fromStdString(name)); @@ -369,16 +403,5 @@ void PortSettingsWidget::onSaveProfileClicked() connect(action, &QAction::triggered, [this, path_qstr]() { m_host_interface->saveInputProfile(path_qstr); }); } - if (!profile_names.empty()) - menu.addSeparator(); - - QAction* browse = menu.addAction(tr("Browse...")); - connect(browse, &QAction::triggered, [this]() { - QString path = - QFileDialog::getSaveFileName(this, tr("Select path to input profile ini"), QString(), tr(INPUT_PROFILE_FILTER)); - if (!path.isEmpty()) - m_host_interface->saveInputProfile(path); - }); - menu.exec(QCursor::pos()); } diff --git a/src/duckstation-qt/qthostinterface.h b/src/duckstation-qt/qthostinterface.h index e28fb763c..6a8afb6b8 100644 --- a/src/duckstation-qt/qthostinterface.h +++ b/src/duckstation-qt/qthostinterface.h @@ -73,6 +73,10 @@ public: /// Fills menu with save state info and handlers. void populateGameListContextMenu(const char* game_code, QWidget* parent_window, QMenu* menu); + ALWAYS_INLINE QString getPathForInputProfile(const QString& name) const + { + return QString::fromStdString(GetPathForInputProfile(name.toUtf8().constData())); + } ALWAYS_INLINE std::vector> getInputProfileList() const { return GetInputProfileList(); diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index c55d1662e..1e96d3e8e 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -874,6 +874,11 @@ void CommonHostInterface::RegisterSaveStateHotkeys() } } +std::string CommonHostInterface::GetPathForInputProfile(const char* name) const +{ + return GetUserDirectoryRelativePath("inputprofiles/%s.ini", name); +} + std::vector> CommonHostInterface::GetInputProfileList() const { FileSystem::FindResultsArray results; diff --git a/src/frontend-common/common_host_interface.h b/src/frontend-common/common_host_interface.h index 1d72d5f4d..df3303c02 100644 --- a/src/frontend-common/common_host_interface.h +++ b/src/frontend-common/common_host_interface.h @@ -92,6 +92,9 @@ protected: /// Reloads the input map from config. Callable from controller interface. virtual void UpdateInputMap() = 0; + /// Returns a path where an input profile with the specified name would be saved. + std::string GetPathForInputProfile(const char* name) const; + /// Returns a list of all input profiles. first - name, second - path std::vector> GetInputProfileList() const;