Lua: Fix Layout.Span & Tab constructor

* Allows to use Span {...} or Span(...) syntax.
* Same for Tab {...} or Tab(...).
* Fixes some documentation issues.
* Adds "withFormAlignment()" and "spacing()"

Change-Id: I05ec0823617e99384e309f34a4861e29045eff94
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-05-29 10:06:25 +02:00
parent e7cf251abe
commit 997f0fb624
2 changed files with 90 additions and 61 deletions

View File

@@ -137,12 +137,52 @@ std::unique_ptr<T> constructWidgetType(const sol::table &children, QObject *guar
return item;
}
std::unique_ptr<Tab> constructTabFromTable(const sol::table &children)
{
if (children.size() != 2)
throw sol::error("Tab must have exactly two children");
auto tabName = children[1];
if (tabName.get_type() != sol::type::string)
throw sol::error("Tab name (first argument) must be a string");
const auto &layout = children[2];
if (!layout.is<Layout *>())
throw sol::error("Tab child (second argument) must be a Layout");
std::unique_ptr<Tab> item = std::make_unique<Tab>(tabName, *layout.get<Layout *>());
return item;
}
std::unique_ptr<Tab> constructTab(const QString &tabName, const Layout &layout)
{
std::unique_ptr<Tab> item = std::make_unique<Tab>(tabName, layout);
return item;
}
std::unique_ptr<Span> constructSpanFromTable(const sol::table &children)
{
if (children.size() != 2)
throw sol::error("Span must have exactly two children");
auto spanSize = children[1];
if (spanSize.get_type() != sol::type::number)
throw sol::error("Span size (first argument) must be a number");
const auto &layout = children[2];
if (!layout.is<Layout *>())
throw sol::error("Span child (second argument) must be a Layout");
std::unique_ptr<Span> item = std::make_unique<Span>(spanSize, *layout.get<Layout *>());
return item;
}
std::unique_ptr<Span> constructSpan(int n, const Layout &layout)
{
std::unique_ptr<Span> item = std::make_unique<Span>(n, layout);
return item;
}
std::unique_ptr<TabWidget> constructTabWidget(const sol::table &children, QObject *guard)
{
std::unique_ptr<TabWidget> item(new TabWidget({}));
@@ -183,7 +223,7 @@ void addLayoutModule()
sol::table layout = l.create_table();
layout.new_usertype<Span>(
"Span", sol::call_constructor, sol::constructors<Span(int n, const Layout::I &item)>());
"Span", sol::call_constructor, sol::factories(&constructSpan, &constructSpanFromTable));
layout.new_usertype<Space>("Space", sol::call_constructor, sol::constructors<Space(int)>());
@@ -268,7 +308,7 @@ void addLayoutModule()
layout.new_usertype<Tab>(
"Tab",
sol::call_constructor,
sol::factories(&constructTab),
sol::factories(&constructTab, &constructTabFromTable),
sol::base_classes,
sol::bases<Widget, Object, Thing>());
@@ -327,6 +367,8 @@ void addLayoutModule()
layout["hr"] = &hr;
layout["noMargin"] = &noMargin;
layout["normalMargin"] = &normalMargin;
layout["withFormAlignment"] = &withFormAlignment;
layout["spacing"] = &spacing;
return layout;
});

View File

@@ -2,98 +2,99 @@
local layout = {}
---The base class of all layout items
---@class LayoutItem
layout.LayoutItem = {
---Attaches the layout to the specified widget
---@param widget QWidget
attachTo = function(widget) end
}
---The base class of all layout classes
---@class Object
layout.Layout = {}
---The base class of all layout classes
---@class Layout : Object
layout.Layout = {}
---The base class of all widget classes, an empty widget itself.
---@class Widget : Object
Widget = {}
---@param children Layout
---@return Widget
function layout.Widget(children) end
---Column layout
---@class Column : LayoutItem
---@class Column : Layout
local column = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return Column
function layout.Column(children) end
---A group box with a title
---@class Group : LayoutItem
---@class Group : Widget
local group = {}
---@return Group
function layout.Group(children) end
---Row layout
---@class Row : LayoutItem
---@class Row : Layout
local row = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return Row
function layout.Row(children) end
---Flow layout
---@class Flow : LayoutItem
---@class Flow : Layout
local flow = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return Flow
function layout.Flow(children) end
---Grid layout
---@class Grid : LayoutItem
---@class Grid : Layout
local grid = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return Grid
function layout.Grid(children) end
---Form layout
---@class Form : LayoutItem
---@class Form : Layout
local form = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return Form
function layout.Form(children) end
---An empty widget
---@class Widget : LayoutItem
local widget = {}
---@param children LayoutItem|string|BaseAspect|function
---@return Widget
function layout.Widget(children) end
---A stack of multiple widgets
---@class Stack : LayoutItem
---@class Stack : Widget
local stack = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return Stack
function layout.Stack(children) end
---A Tab widget
---@class Tab : LayoutItem
---@class Tab : Widget
local tab = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return Tab
function layout.Tab(children) end
---A Multiline text edit
---@class TextEdit : LayoutItem
---@class TextEdit : Widget
local textEdit = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return TextEdit
function layout.TextEdit(children) end
---A PushButton
---@class PushButton : LayoutItem
---@class PushButton : Widget
local pushButton = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return PushButton
function layout.PushButton(children) end
@@ -106,37 +107,41 @@ local label = {}
function layout.Label(children) end
---A SpinBox
---@class SpinBox : LayoutItem
---@class SpinBox : Widget
local spinBox = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return SpinBox
function layout.SpinBox(children) end
---A Splitter
---@class Splitter : LayoutItem
---@class Splitter : Widget
local splitter = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return Splitter
function layout.Splitter(children) end
---A Toolbar
---@class ToolBar : LayoutItem
---@class ToolBar : Widget
local toolBar = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return ToolBar
function layout.ToolBar(children) end
---A TabWidget
---@class TabWidget : LayoutItem
---@class TabWidget : Widget
local tabWidget = {}
---@param children LayoutItem|string|BaseAspect|function
---@param children Layout|string|BaseAspect|function
---@return TabWidget
function layout.TabWidget(children) end
---@param name string
---@param child Layout|string|BaseAspect|function
---@return TabWidget
function layout.TabWidget(name, child) end
---A "Line break" in the layout
function layout.br() end
@@ -155,33 +160,15 @@ function layout.noMargin() end
---Sets the margin of the layout to the default value
function layout.normalMargin() end
---Sets the margin of the layout to a custom value
function layout.customMargin(left, top, right, bottom) end
---Sets the alignment of the layout to "Form"
function layout.withFormAlignment() end
---Sets the title of the parent object if possible
function layout.title(text) end
---Sets the text of the parent object if possible
function layout.text(text) end
---Sets the tooltip of the parent object if possible
function layout.tooltip(text) end
---Sets the size of the parent object if possible
function layout.resize(width, height) end
---Sets the stretch of the column at `index`
function layout.columnStretch(index, stretch) end
---Sets the spacing of the layout
function layout.spacing(spacing) end
---Sets the window title of the parent object if possible
function layout.windowTitle(text) end
---Sets the field growth policy of the layout
function layout.fieldGrowthPolicy(policy) end