From f1c7e42aa361c85ecfed91fdd76680d6f76c991d Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Tue, 20 Aug 2024 10:26:03 +0200 Subject: [PATCH] Lua: Add Splitter arguments Change-Id: I51c33621bc424a65ae5aa14dd8340534477e96f3 Reviewed-by: Cristian Adam --- src/plugins/lua/bindings/gui.cpp | 20 ++++++++++++++++++++ src/plugins/lua/meta/gui.lua | 23 ++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/plugins/lua/bindings/gui.cpp b/src/plugins/lua/bindings/gui.cpp index cefa8e3bd57..59f07d4e2bf 100644 --- a/src/plugins/lua/bindings/gui.cpp +++ b/src/plugins/lua/bindings/gui.cpp @@ -245,6 +245,18 @@ std::unique_ptr constructSplitter(const sol::table &children) std::unique_ptr item(new Splitter({})); constructWidget(item, children); + if (const auto &orientation = children.get>("orientation")) { + if (*orientation == "horizontal") + item->setOrientation(Qt::Horizontal); + else if (*orientation == "vertical") + item->setOrientation(Qt::Vertical); + else + throw sol::error(QString("Invalid orientation: %1").arg(*orientation).toStdString()); + } + + if (const auto collapsible = children.get>("collapsible")) + item->setChildrenCollapsible(*collapsible); + for (size_t i = 1; i <= children.size(); ++i) { const auto &child = children[i]; if (child.is()) { @@ -256,6 +268,14 @@ std::unique_ptr constructSplitter(const sol::table &children) << " (expected Layout or Widget)"; } } + + if (const auto &stretchFactors = children.get>("stretchFactors")) { + for (const auto &kv : *stretchFactors) { + if (kv.second.get_type() != sol::type::number) + throw sol::error("Stretch factors must be numbers"); + item->setStretchFactor(kv.first.as() - 1, kv.second.as()); + } + } return item; } diff --git a/src/plugins/lua/meta/gui.lua b/src/plugins/lua/meta/gui.lua index c241af5f158..d07f5d78fc8 100644 --- a/src/plugins/lua/meta/gui.lua +++ b/src/plugins/lua/meta/gui.lua @@ -17,14 +17,17 @@ gui.widget = {} ---@alias LayoutChild string|BaseAspect|Layout|Widget|function ---@alias LayoutChildren LayoutChild[] ----@class (exact) WidgetOptions +---@class (exact) BaseWidgetOptions +---@field size? integer[] Two integers, representing the width and height of the widget. +---@field windowFlags? WindowType[] The window flags of the widget. +gui.baseWidgetOptions = {} + +---@class (exact) WidgetOptions : BaseWidgetOptions +---@field title? string The title of the widget, if applicable. ---@field onTextChanged? function The function to be called when the text of the widget changes, if applicable. ---@field onClicked? function The function to be called when the widget is clicked, if applicable. ---@field text? string The text of the widget, if applicable. ----@field title? string The title of the widget, if applicable. ---@field value? integer The value of the widget, if applicable. ----@field size? integer[] Two integers, representing the width and height of the widget. ----@field windowFlags? WindowType[] The window flags of the widget. ---@field [1]? Layout The layout of the widget, if applicable. gui.widgetOptions = {} @@ -144,7 +147,17 @@ function gui.SpinBox(options) end ---@class Splitter : Widget local splitter = {} ----@param options WidgetOptions +---@alias Orientation "horizontal"|"vertical" + +---@class (exact) SplitterOptions : BaseWidgetOptions +---@field orientation? Orientation The orientation of the splitter. (default: "vertical") +---@field childrenCollapsible? boolean A boolean, representing whether the children are collapsible. (default: true) +---@field stretchFactors? integer[] A list of integers, representing the stretch factors of the children. (default: {1, ...}) +---@field size? integer[] Two integers, representing the width and height of the widget. +---@field [integer] Layout | Widget The splits. +gui.splitterOptions = {} + +---@param options SplitterOptions ---@return Splitter function gui.Splitter(options) end