Qt: Add "New..." to input profile save button

This will ensure the profile gets saved to the correct location.
This commit is contained in:
Connor McLaughlin 2020-04-27 01:05:01 +10:00
parent a8c43b0c8c
commit 63692a012f
4 changed files with 55 additions and 20 deletions

View file

@ -10,6 +10,7 @@
#include <QtGui/QCursor> #include <QtGui/QCursor>
#include <QtGui/QKeyEvent> #include <QtGui/QKeyEvent>
#include <QtWidgets/QFileDialog> #include <QtWidgets/QFileDialog>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QMenu> #include <QtWidgets/QMenu>
#include <QtWidgets/QMessageBox> #include <QtWidgets/QMessageBox>
@ -336,15 +337,6 @@ void PortSettingsWidget::onLoadProfileClicked()
const auto profile_names = m_host_interface->getInputProfileList(); const auto profile_names = m_host_interface->getInputProfileList();
QMenu menu; 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...")); QAction* browse = menu.addAction(tr("Browse..."));
connect(browse, &QAction::triggered, [this]() { connect(browse, &QAction::triggered, [this]() {
@ -354,6 +346,16 @@ void PortSettingsWidget::onLoadProfileClicked()
m_host_interface->applyInputProfile(path); 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()); menu.exec(QCursor::pos());
} }
@ -362,6 +364,38 @@ void PortSettingsWidget::onSaveProfileClicked()
const auto profile_names = m_host_interface->getInputProfileList(); const auto profile_names = m_host_interface->getInputProfileList();
QMenu menu; 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) for (const auto& [name, path] : profile_names)
{ {
QAction* action = menu.addAction(QString::fromStdString(name)); 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); }); 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()); menu.exec(QCursor::pos());
} }

View file

@ -73,6 +73,10 @@ public:
/// Fills menu with save state info and handlers. /// Fills menu with save state info and handlers.
void populateGameListContextMenu(const char* game_code, QWidget* parent_window, QMenu* menu); 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<std::pair<std::string, std::string>> getInputProfileList() const ALWAYS_INLINE std::vector<std::pair<std::string, std::string>> getInputProfileList() const
{ {
return GetInputProfileList(); return GetInputProfileList();

View file

@ -874,6 +874,11 @@ void CommonHostInterface::RegisterSaveStateHotkeys()
} }
} }
std::string CommonHostInterface::GetPathForInputProfile(const char* name) const
{
return GetUserDirectoryRelativePath("inputprofiles/%s.ini", name);
}
std::vector<std::pair<std::string, std::string>> CommonHostInterface::GetInputProfileList() const std::vector<std::pair<std::string, std::string>> CommonHostInterface::GetInputProfileList() const
{ {
FileSystem::FindResultsArray results; FileSystem::FindResultsArray results;

View file

@ -92,6 +92,9 @@ protected:
/// Reloads the input map from config. Callable from controller interface. /// Reloads the input map from config. Callable from controller interface.
virtual void UpdateInputMap() = 0; 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 /// Returns a list of all input profiles. first - name, second - path
std::vector<std::pair<std::string, std::string>> GetInputProfileList() const; std::vector<std::pair<std::string, std::string>> GetInputProfileList() const;