From 1c39164d89739f6775e07bbcf3d25747022f055c Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Wed, 18 Sep 2024 08:40:49 +0200 Subject: [PATCH] Lua: Create simple ExtensionOptionsPage Allows Simple extensions to easily piggy-back off of the Extension Manager settings category. Change-Id: I955120fe54d42ac93a74e2d306fc580956d53e35 Reviewed-by: Alessandro Portale --- src/plugins/lua/bindings/settings.cpp | 98 ++++++++++++++++++--------- src/plugins/lua/meta/settings.lua | 10 +++ 2 files changed, 77 insertions(+), 31 deletions(-) diff --git a/src/plugins/lua/bindings/settings.cpp b/src/plugins/lua/bindings/settings.cpp index 38450cdbf69..e74f93d7cdb 100644 --- a/src/plugins/lua/bindings/settings.cpp +++ b/src/plugins/lua/bindings/settings.cpp @@ -288,24 +288,27 @@ sol::usertype addTypedAspect(sol::table &lua, const QString &name) void setupSettingsModule() { - registerProvider("Settings", [](sol::state_view l) -> sol::object { - sol::table settings = l.create_table(); + registerProvider("Settings", [](sol::state_view lua) -> sol::object { + const ScriptPluginSpec *pluginSpec = lua.get("PluginSpec"); + + sol::table settings = lua.create_table(); settings.new_usertype("Aspect", "apply", &BaseAspect::apply); - settings.new_usertype("AspectContainer", - "create", - &aspectContainerCreate, - "apply", - &LuaAspectContainer::apply, - sol::meta_function::index, - &LuaAspectContainer::dynamic_get, - sol::meta_function::new_index, - &LuaAspectContainer::dynamic_set, - sol::meta_function::length, - &LuaAspectContainer::size, - sol::base_classes, - sol::bases()); + settings.new_usertype( + "AspectContainer", + "create", + &aspectContainerCreate, + "apply", + &LuaAspectContainer::apply, + sol::meta_function::index, + &LuaAspectContainer::dynamic_get, + sol::meta_function::new_index, + &LuaAspectContainer::dynamic_set, + sol::meta_function::length, + &LuaAspectContainer::size, + sol::base_classes, + sol::bases()); addTypedAspect(settings, "BoolAspect"); addTypedAspect(settings, "ColorAspect"); @@ -515,13 +518,12 @@ void setupSettingsModule() options, [](AspectList *aspect, const std::string &key, const sol::object &value) { if (key == "createItemFunction") { - aspect->setCreateItemFunction([func = value.as()]() - -> std::shared_ptr { - auto res = safe_call>( - func); - QTC_ASSERT_EXPECTED(res, return nullptr); - return *res; - }); + aspect->setCreateItemFunction( + [func = value.as()]() -> std::shared_ptr { + auto res = safe_call>(func); + QTC_ASSERT_EXPECTED(res, return nullptr); + return *res; + }); } else if (key == "onItemAdded") { aspect->setItemAddedCallback([func = value.as()]( std::shared_ptr item) { @@ -558,19 +560,42 @@ void setupSettingsModule() sol::base_classes, sol::bases()); + class ExtensionOptionsPage : public Core::IOptionsPage + { + public: + ExtensionOptionsPage(const ScriptPluginSpec *spec, AspectContainer *container) + { + setId(Id::fromString(QString("Extension.%2").arg(spec->id))); + setCategory(Id("ExtensionManager")); + + setDisplayName(spec->name); + + if (container->isAutoApply()) + throw sol::error("AspectContainer must have autoApply set to false"); + + setSettingsProvider([container]() { return container; }); + } + }; + class OptionsPage : public Core::IOptionsPage { public: - OptionsPage(const sol::table &options) + OptionsPage(const ScriptPluginSpec *spec, const sol::table &options) { - setId(Id::fromString(options.get("id"))); + setId( + Id::fromString(QString("%1.%2").arg(spec->id).arg(options.get("id")))); + setCategory(Id::fromString( + QString("%1.%2").arg(spec->id).arg(options.get("categoryId")))); + setDisplayName(options.get("displayName")); - setCategory(Id::fromString(options.get("categoryId"))); setDisplayCategory(options.get("displayCategory")); + const FilePath catIcon = options.get>("categoryIconPath") .value_or(FilePath::fromUserInput( options.get_or("categoryIconPath", {}))); + setCategoryIconPath(catIcon); + AspectContainer *container = options.get("aspectContainer"); if (container->isAutoApply()) throw sol::error("AspectContainer must have autoApply set to false"); @@ -582,28 +607,39 @@ void setupSettingsModule() settings.new_usertype( "OptionsPage", "create", - [](const sol::table &options) { return std::make_unique(options); }, + [pluginSpec](const sol::table &options) { + return std::make_unique(pluginSpec, options); + }, "show", [](OptionsPage *page) { Core::ICore::showOptionsDialog(page->id()); }); + settings.new_usertype( + "ExtensionOptionsPage", + "create", + [pluginSpec](AspectContainer *container) { + return std::make_unique(pluginSpec, container); + }, + "show", + [](ExtensionOptionsPage *page) { Core::ICore::showOptionsDialog(page->id()); }); + // clang-format off - settings["StringDisplayStyle"] = l.create_table_with( + settings["StringDisplayStyle"] = lua.create_table_with( "Label", StringAspect::DisplayStyle::LabelDisplay, "LineEdit", StringAspect::DisplayStyle::LineEditDisplay, "TextEdit", StringAspect::DisplayStyle::TextEditDisplay, "PasswordLineEdit", StringAspect::DisplayStyle::PasswordLineEditDisplay ); - settings["SelectionDisplayStyle"] = l.create_table_with( + settings["SelectionDisplayStyle"] = lua.create_table_with( "RadioButtons", SelectionAspect::DisplayStyle::RadioButtons, "ComboBox", SelectionAspect::DisplayStyle::ComboBox ); - settings["CheckBoxPlacement"] = l.create_table_with( + settings["CheckBoxPlacement"] = lua.create_table_with( "Top", CheckBoxPlacement::Top, "Right", CheckBoxPlacement::Right ); - settings["Kind"] = l.create_table_with( + settings["Kind"] = lua.create_table_with( "ExistingDirectory", PathChooser::Kind::ExistingDirectory, "Directory", PathChooser::Kind::Directory, "File", PathChooser::Kind::File, @@ -612,7 +648,7 @@ void setupSettingsModule() "Command", PathChooser::Kind::Command, "Any", PathChooser::Kind::Any ); - settings["LabelPlacement"] = l.create_table_with( + settings["LabelPlacement"] = lua.create_table_with( "AtCheckBox", BoolAspect::LabelPlacement::AtCheckBox, "Compact", BoolAspect::LabelPlacement::Compact, "InExtraLabel", BoolAspect::LabelPlacement::InExtraLabel diff --git a/src/plugins/lua/meta/settings.lua b/src/plugins/lua/meta/settings.lua index f2652947a25..837af4c0d64 100644 --- a/src/plugins/lua/meta/settings.lua +++ b/src/plugins/lua/meta/settings.lua @@ -218,4 +218,14 @@ function settings.OptionsPage.create(options) end ---Shows options page. function settings.OptionsPage:show() end +---@class ExtensionOptionsPage +settings.extensionOptionsPage = {} + +---Create an ExtensionOptionsPage. +---@param aspectContainer AspectContainer +---@return ExtensionOptionsPage +function settings.extensionOptionsPage.create(aspectContainer) end + +---Show the options page. +function settings.extensionOptionsPage:show() end return settings