Gui: Try to load Theme Parameters from file

This creates logic that tries to load user parameters from file as the
priority and pushes back older mechanism to lower layer. Older way of
loading theme parameters should be preserved until we release first 1.1
RC.
This commit is contained in:
Kacper Donat
2025-08-16 12:51:16 +02:00
parent db3e96ebf0
commit fd502ce94f
3 changed files with 52 additions and 9 deletions

View File

@@ -141,6 +141,7 @@
#include "QtWidgets.h"
#include <OverlayManager.h>
#include <ParamHandler.h>
#include <Base/ServiceProvider.h>
#ifdef BUILD_TRACY_FRAME_PROFILER
@@ -381,20 +382,48 @@ struct PyMethodDef FreeCADGui_methods[] = {
void Application::initStyleParameterManager()
{
static ParamHandlers handlers;
const auto deduceParametersFilePath = []() -> std::string {
const auto hMainWindowGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow");
if (const std::string& path = hMainWindowGrp->GetASCII("ThemeStyleParametersFile");
!path.empty()) {
return path;
}
return fmt::format("qss:parameters/{}.yaml", hMainWindowGrp->GetASCII("Theme", "Classic"));
};
auto themeParametersSource = new StyleParameters::YamlParameterSource(
deduceParametersFilePath(),
{.name = QT_TR_NOOP("Theme Parameters"),
.options = StyleParameters::ParameterSourceOption::UserEditable});
handlers.addDelayedHandler(
"BaseApp/Preferences/MainWindow",
{"ThemeStyleParametersFiles", "Theme"},
[themeParametersSource, deduceParametersFilePath](ParameterGrp::handle) {
themeParametersSource->changeFilePath(deduceParametersFilePath());
});
Base::registerServiceImplementation<StyleParameters::ParameterSource>(
new StyleParameters::BuiltInParameterSource({.name = QT_TR_NOOP("Built-in Parameters")}));
Base::registerServiceImplementation<StyleParameters::ParameterSource>(
new StyleParameters::UserParameterSource(
App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Themes/Tokens"),
{.name = QT_TR_NOOP("Theme Parameters"),
.options = StyleParameters::ParameterSourceOption::UserEditable}));
// todo: left for compatibility with older theme versions, to be removed before release
Base::registerServiceImplementation<StyleParameters::ParameterSource>(
new StyleParameters::UserParameterSource(
App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Themes/UserTokens"),
{.name = QT_TR_NOOP("Theme Parameters - Fallback"),
.options = StyleParameters::ParameterSourceOption::ReadOnly}));
Base::registerServiceImplementation<StyleParameters::ParameterSource>(themeParametersSource);
Base::registerServiceImplementation<StyleParameters::ParameterSource>(
new StyleParameters::UserParameterSource(
App::GetApplication().GetParameterGroupByPath(
"User parameter:BaseApp/Preferences/Themes/UserParameters"),
{.name = QT_TR_NOOP("User Parameters"),
.options = StyleParameters::ParameterSource::UserEditable}));

View File

@@ -223,7 +223,17 @@ void UserParameterSource::remove(const std::string& name)
YamlParameterSource::YamlParameterSource(const std::string& filePath, const Metadata& metadata)
: ParameterSource(metadata)
, filePath(filePath)
{
changeFilePath(filePath);
}
void YamlParameterSource::changeFilePath(const std::string& path)
{
this->filePath = path;
reload();
}
void YamlParameterSource::reload()
{
QFile file(QString::fromStdString(filePath));
@@ -240,6 +250,7 @@ YamlParameterSource::YamlParameterSource(const std::string& filePath, const Meta
std::string content = in.readAll().toStdString();
YAML::Node root = YAML::Load(content);
parameters.clear();
for (auto it = root.begin(); it != root.end(); ++it) {
auto key = it->first.as<std::string>();
auto value = it->second.as<std::string>();
@@ -288,7 +299,7 @@ void YamlParameterSource::flush()
QFile file(QString::fromStdString(filePath));
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
FC_TRACE("StyleParameters: Unable to open file " << filePath);
FC_WARN("StyleParameters: Unable to open file " << filePath);
return;
}

View File

@@ -403,6 +403,9 @@ public:
*/
explicit YamlParameterSource(const std::string& filePath, const Metadata& metadata = {});
void changeFilePath(const std::string& path);
void reload();
std::list<Parameter> all() const override;
std::optional<Parameter> get(const std::string& name) const override;
void define(const Parameter& param) override;