dolphin/Source/Core/DolphinQt/Config/ConfigControls/ConfigUserPath.cpp
Maximilian Mader 514beb2081
DolphinQt: Add an optional default value to ConfigUserPath constructor
The optional default value is used to implement `ConfigUserPath::Reset()`.
Since `ConfigUserPath` must not be empty, there was no easy way to reset
these paths to their default value.

With this change users of `ConfigUserPath` can now set the default path.
At the moment users of `ConfigUserPath` must place a reset button and
connect a callback to `ConfigUserPath::Reset` on their own.
2026-04-21 15:47:41 +02:00

79 lines
2.3 KiB
C++

// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "DolphinQt/Config/ConfigControls/ConfigUserPath.h"
#include <QLineEdit>
#include <QString>
#include "Common/FileUtil.h"
#include "DolphinQt/Config/ConfigControls/ConfigText.h"
#include "DolphinQt/QtUtils/ModalMessageBox.h"
ConfigUserPath::ConfigUserPath(const unsigned int dir_index,
const Config::Info<std::string>& setting)
: ConfigUserPath(dir_index, setting, "", nullptr)
{
}
ConfigUserPath::ConfigUserPath(const unsigned int dir_index,
const Config::Info<std::string>& setting,
const std::string default_value)
: ConfigUserPath(dir_index, setting, default_value, nullptr)
{
}
ConfigUserPath::ConfigUserPath(const unsigned int dir_index,
const Config::Info<std::string>& setting,
const std::string default_value, Config::Layer* layer)
: ConfigText(setting, layer), m_dir_index(dir_index), m_default_value(default_value)
{
OnConfigChanged();
connect(this, &QLineEdit::editingFinished, this, &ConfigUserPath::Update);
}
// Display the effective path: if Config has a value, use it; otherwise fall back to UserPath.
// Config values only serve to initialize UserPath at startup, an empty config meaning "use the
// current UserPath".
void ConfigUserPath::RefreshText()
{
const std::string config_value = ReadValue(m_setting);
const std::string userpath_value = File::GetUserPath(m_dir_index);
const QString text = QString::fromStdString(config_value.empty() ? userpath_value : config_value);
setText(text);
}
void ConfigUserPath::Update()
{
const QString new_text = text().trimmed();
if (new_text.isEmpty())
{
ModalMessageBox::warning(this, tr("Empty Value"),
tr("This field cannot be left empty. Please enter a value."));
RefreshText();
return;
}
const std::string value = new_text.toStdString();
File::SetUserPath(m_dir_index, value);
SaveValue(m_setting, value);
}
void ConfigUserPath::OnConfigChanged()
{
RefreshText();
}
void ConfigUserPath::Reset()
{
if (m_default_value.empty())
return;
File::SetUserPath(m_dir_index, m_default_value);
SaveValue(m_setting, m_default_value);
RefreshText();
}