Skip to content

Commit

Permalink
Remove "projectMSDL_UI.properties file
Browse files Browse the repository at this point in the history
Having three configuration files is just too confusing. Users will rarely need to edit the configuration file by hand, so one "projectMSDL.properties" file in the user's app data dir will suffice.
  • Loading branch information
kblaschke committed Mar 20, 2024
1 parent cded180 commit ab65aeb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 107 deletions.
21 changes: 9 additions & 12 deletions src/ProjectMSDLApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void ProjectMSDLApplication::initialize(Poco::Util::Application& self)

try
{
//if (loadConfiguration(PRIO_DEFAULT) == 0)
if (loadConfiguration(PRIO_DEFAULT) == 0)
{
// The file may be located in the ../Resources bundle dir on macOS, elsewhere relative
// to the executable or within an absolute path.
Expand Down Expand Up @@ -75,28 +75,25 @@ void ProjectMSDLApplication::initialize(Poco::Util::Application& self)
// Try to load user's custom configuration file.
Poco::Path userConfigurationFile = userConfigurationDir;
userConfigurationFile.setFileName(configFileName);
if (Poco::File(userConfigurationFile).exists())
{
loadConfiguration(userConfigurationFile.toString(), PRIO_DEFAULT - 10);
}

// Lastly, load user's UI configuration file into a separate instance.
// This is used to save any config changes made via the UI, so it can be reset independently.
userConfigurationFile.setFileName(config().getString("application.baseName") + "_UI.properties");
if (!Poco::File(userConfigurationFile).exists())
{
Poco::File(userConfigurationDir).createDirectories();
Poco::File(userConfigurationFile).createFile();
}
Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> uiConfiguration = new Poco::Util::PropertyFileConfiguration(userConfigurationFile.toString());
config().add(uiConfiguration, PRIO_DEFAULT - 20);
getSubsystem<ProjectMGUI>().UIConfiguration(uiConfiguration);
Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> userConfiguration = new Poco::Util::PropertyFileConfiguration(userConfigurationFile.toString());
config().add(userConfiguration, PRIO_DEFAULT - 10);

// Pass the config data to the UI
getSubsystem<ProjectMGUI>().UserConfiguration(userConfiguration);
}
catch (Poco::Exception& ex)
{
poco_error_f1(logger(), "Failed to load/create user configuration file: %s", ex.displayText());
}

// Add another layer on top for temporary command-line parameter overrides
config().add(new Poco::Util::MapConfiguration(), PRIO_DEFAULT - 30);

Application::initialize(self);
}

Expand Down
8 changes: 4 additions & 4 deletions src/gui/ProjectMGUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ void ProjectMGUI::uninitialize()
_glContext = nullptr;
}

void ProjectMGUI::UIConfiguration(Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> config)
void ProjectMGUI::UserConfiguration(Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> config)
{
_uiConfig = config;
_userConfiguration = std::move(config);
}

Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> ProjectMGUI::UIConfiguration()
Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> ProjectMGUI::UserConfiguration()
{
return _uiConfig;
return _userConfiguration;
}

void ProjectMGUI::CommandLineConfiguration(Poco::AutoPtr<Poco::Util::MapConfiguration> config)
Expand Down
14 changes: 7 additions & 7 deletions src/gui/ProjectMGUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ class ProjectMGUI : public Poco::Util::Subsystem
void uninitialize() override;

/**
* @brief Sets the UI configuration file.
* @param config The properties file instance which stores the UI settings.
* @brief Sets the current user's configuration file.
* @param config The properties file instance which stores the settings for the current user.
*/
void UIConfiguration(Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> config);
void UserConfiguration(Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> config);

/**
* @brief Returns the UI configuration file.
* @return The properties file instance which stores the UI settings.
* @brief Returns the user configuration file.
* @return The properties file instance which stores the settings for the current user.
*/
Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> UIConfiguration();
Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> UserConfiguration();

/**
* @brief Sets the command line override map.
Expand Down Expand Up @@ -129,7 +129,7 @@ class ProjectMGUI : public Poco::Util::Subsystem

ProjectMWrapper* _projectMWrapper{nullptr};

Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> _uiConfig; //!< The UI configuration, used to store/reset changes made in the UI's settings dialog.
Poco::AutoPtr<Poco::Util::PropertyFileConfiguration> _userConfiguration; //!< The current user's configuration, used to store/reset changes made in the UI's settings dialog.
Poco::AutoPtr<Poco::Util::MapConfiguration> _commandLineOverrides; //!< The command-line override settings.

Poco::NObserver<ProjectMGUI, DisplayToastNotification> _displayToastNotificationObserver{*this, &ProjectMGUI::DisplayToastNotificationHandler};
Expand Down
120 changes: 36 additions & 84 deletions src/gui/SettingsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ void SettingsWindow::Draw()

if (ImGui::BeginTabItem("projectM"))
{
if (ImGui::BeginTable("projectM", 3, tableFlags))
if (ImGui::BeginTable("projectM", 4, tableFlags))
{
ImGui::TableSetupColumn("##desc", ImGuiTableColumnFlags_WidthFixed, .0f);
ImGui::TableSetupColumn("##setting", ImGuiTableColumnFlags_WidthStretch, .0f);
ImGui::TableSetupColumn("##reset", ImGuiTableColumnFlags_WidthFixed, 100.0f);
ImGui::TableSetupColumn("##override", ImGuiTableColumnFlags_WidthFixed, .0f);

ImGui::TableNextRow();
LabelWithTooltip("Preset Path", "Path to search for preset files if no playlist is loaded.");
Expand Down Expand Up @@ -146,20 +147,13 @@ void SettingsWindow::PathSetting(const std::string& property)
{
ImGui::TableSetColumnIndex(1);

auto path = Poco::Util::Application::instance().config().getString(property, "");
auto path = _gui.UserConfiguration()->getString(property, "");
char pathBuffer[2048]{};
strncpy(pathBuffer, path.c_str(), std::min<size_t>(2047, path.size()));

bool isOverridden{false};
if (_gui.CommandLineConfiguration()->has(property))
{
isOverridden = true;
ImGui::BeginDisabled();
}

if (ImGui::InputText(std::string("##path_" + property).c_str(), &pathBuffer[0], IM_ARRAYSIZE(pathBuffer), ImGuiInputTextFlags_EnterReturnsTrue))
{
_gui.UIConfiguration()->setString(property, std::string(pathBuffer));
_gui.UserConfiguration()->setString(property, std::string(pathBuffer));
}

ImGui::SameLine();
Expand All @@ -170,140 +164,98 @@ void SettingsWindow::PathSetting(const std::string& property)
}
ImGui::PopID();

if (isOverridden)
ResetButton(property);

if (_gui.CommandLineConfiguration()->has(property))
{
ImGui::EndDisabled();
OverriddenSettingMarker();
}
else
{
ResetButton(property);
}
}


void SettingsWindow::BooleanSetting(const std::string& property, bool defaultValue)
{
ImGui::TableSetColumnIndex(1);

auto value = Poco::Util::Application::instance().config().getBool(property, defaultValue);

bool isOverridden{false};
if (_gui.CommandLineConfiguration()->has(property))
{
isOverridden = true;
ImGui::BeginDisabled();
}
auto value = _gui.UserConfiguration()->getBool(property, defaultValue);

if (ImGui::Checkbox(std::string("##boolean_" + property).c_str(), &value))
{
_gui.UIConfiguration()->setBool(property, value);
_gui.UserConfiguration()->setBool(property, value);
}

if (isOverridden)
ResetButton(property);

if (_gui.CommandLineConfiguration()->has(property))
{
ImGui::EndDisabled();
OverriddenSettingMarker();
}
else
{
ResetButton(property);
}
}

void SettingsWindow::IntegerSetting(const std::string& property, int defaultValue, int min, int max)
{
ImGui::TableSetColumnIndex(1);

auto value = Poco::Util::Application::instance().config().getInt(property, defaultValue);

bool isOverridden{false};
if (_gui.CommandLineConfiguration()->has(property))
{
isOverridden = true;
ImGui::BeginDisabled();
}
auto value = _gui.UserConfiguration()->getInt(property, defaultValue);

if (ImGui::SliderInt(std::string("##integer_" + property).c_str(), &value, min, max))
{
_gui.UIConfiguration()->setInt(property, value);
_gui.UserConfiguration()->setInt(property, value);
}

if (isOverridden)
ResetButton(property);

if (_gui.CommandLineConfiguration()->has(property))
{
ImGui::EndDisabled();
OverriddenSettingMarker();
}
else
{
ResetButton(property);
}
}

void SettingsWindow::IntegerSettingVec(const std::string& property1, const std::string& property2, int defaultValue1, int defaultValue2, int min, int max)
{
ImGui::TableSetColumnIndex(1);

int values[2] = {
Poco::Util::Application::instance().config().getInt(property1, defaultValue1),
Poco::Util::Application::instance().config().getInt(property2, defaultValue2)};

bool isOverridden{false};
if (_gui.CommandLineConfiguration()->has(property1) || _gui.CommandLineConfiguration()->has(property2))
{
isOverridden = true;
ImGui::BeginDisabled();
}
_gui.UserConfiguration()->getInt(property1, defaultValue1),
_gui.UserConfiguration()->getInt(property2, defaultValue2)};

if (ImGui::SliderInt2(std::string("##integer_" + property1 + property2).c_str(), values, min, max))
{
_gui.UIConfiguration()->setInt(property1, values[0]);
_gui.UIConfiguration()->setInt(property2, values[1]);
_gui.UserConfiguration()->setInt(property1, values[0]);
_gui.UserConfiguration()->setInt(property2, values[1]);
}

if (isOverridden)
ResetButton(property1, property2);

if (_gui.CommandLineConfiguration()->has(property1) || _gui.CommandLineConfiguration()->has(property2))
{
ImGui::EndDisabled();
OverriddenSettingMarker();
}
else
{
ResetButton(property1, property2);
}

}

void SettingsWindow::DoubleSetting(const std::string& property, double defaultValue, double min, double max)
{
ImGui::TableSetColumnIndex(1);

auto value = static_cast<float>(Poco::Util::Application::instance().config().getDouble(property, defaultValue));

bool isOverridden{false};
if (_gui.CommandLineConfiguration()->has(property))
{
isOverridden = true;
ImGui::BeginDisabled();
}
auto value = static_cast<float>(_gui.UserConfiguration()->getDouble(property, defaultValue));

if (ImGui::SliderFloat(std::string("##double_" + property).c_str(), &value, static_cast<float>(min), static_cast<float>(max)))
{
_gui.UIConfiguration()->setDouble(property, value);
_gui.UserConfiguration()->setDouble(property, value);
}

if (isOverridden)
ResetButton(property);

if (_gui.CommandLineConfiguration()->has(property))
{
ImGui::EndDisabled();
OverriddenSettingMarker();
}
else
{
ResetButton(property);
}
}

void SettingsWindow::ResetButton(const std::string& property1, const std::string& property2)
{
if (!_gui.UIConfiguration()->has(property1) && (property2.empty() || !_gui.UIConfiguration()->has(property2)))
if (!_gui.UserConfiguration()->has(property1) && (property2.empty() || !_gui.UserConfiguration()->has(property2)))
{
return;
}
Expand All @@ -313,25 +265,25 @@ void SettingsWindow::ResetButton(const std::string& property1, const std::string
ImGui::PushID(std::string(property1 + property2 + "_ResetButton").c_str());
if (ImGui::Button("Reset"))
{
_gui.UIConfiguration()->remove(property1);
_gui.UserConfiguration()->remove(property1);
if (!property2.empty())
{
_gui.UIConfiguration()->remove(property2);
_gui.UserConfiguration()->remove(property2);
}
}
ImGui::PopID();
}

void SettingsWindow::OverriddenSettingMarker()
{
ImGui::TableSetColumnIndex(2);
ImGui::TableSetColumnIndex(3);

ImGui::TextColored(ImVec4(1, 0, 0, 1), "[Locked]");
ImGui::TextColored(ImVec4(1, 0, 0, 1), "[!]");
if (ImGui::IsItemHovered())
{
ImGui::BeginTooltip();
ImGui::TextUnformatted("Value set via command line argument.");
ImGui::TextUnformatted("Can only be changed if not overridden.");
ImGui::TextUnformatted("It will only be used if not overridden.");
ImGui::EndTooltip();
}
}

0 comments on commit ab65aeb

Please sign in to comment.