From 1606544cca7ae0861319212080fb96c140c20e65 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Thu, 22 Aug 2024 14:25:58 +0200 Subject: [PATCH] Lua: Fix simple types Adds support for either { 1, 2 } or {x = 1, y = 2} syntax. Change-Id: I2975f87ec7e4d683bfb52af05ca502c82abb780e Reviewed-by: Cristian Adam --- src/plugins/lua/bindings/gui.cpp | 10 ++-- src/plugins/lua/luaqttypes.cpp | 83 ++++++++++++++++++++++++-------- 2 files changed, 65 insertions(+), 28 deletions(-) diff --git a/src/plugins/lua/bindings/gui.cpp b/src/plugins/lua/bindings/gui.cpp index 02b82e58b98..33c478fcda6 100644 --- a/src/plugins/lua/bindings/gui.cpp +++ b/src/plugins/lua/bindings/gui.cpp @@ -111,13 +111,9 @@ void setProperties(std::unique_ptr &item, const sol::table &children, QObject } if constexpr (hasSetSize::value) { - sol::optional size = children.get>("size"); - if (size) { - if (size->size() == 2) - item->setSize(size->get(1), size->get(2)); - else - throw sol::error("size must have exactly two elements"); - } + sol::optional size = children.get>("size"); + if (size) + item->setSize(size->width(), size->height()); } if constexpr (hasOnTextChanged::value) { diff --git a/src/plugins/lua/luaqttypes.cpp b/src/plugins/lua/luaqttypes.cpp index 840482f4837..2ae21361673 100644 --- a/src/plugins/lua/luaqttypes.cpp +++ b/src/plugins/lua/luaqttypes.cpp @@ -49,11 +49,22 @@ QRect sol_lua_get(sol::types, lua_State *L, int index, sol::stack::record { sol::state_view lua(L); sol::table table = sol::stack::get(L, index, tracking); - return QRect(table.get_or("x", 0), - table.get_or("y", 0), - table.get_or("width", 0), - table.get_or("height", 0)); + if (table.size() != 0 && table.size() != 2 && table.size() != 4) + throw sol::error("Expected table to have 'x', 'y', 'width' and 'height' or 2 (pos and " + "size) or 4 elements"); + if (table.size() == 0) + return QRect( + table.get("x"), + table.get("y"), + table.get("width"), + table.get("height")); + if (table.size() == 2) + return QRect(table.get(1), table.get(2)); + + if (table.size() == 4) + return QRect(table.get(1), table.get(2), table.get(3), table.get(4)); } + int sol_lua_push(sol::types, lua_State *L, const QRect &value) { sol::state_view lua(L); @@ -69,14 +80,19 @@ bool sol_lua_check(sol::types, std::function handler, sol::stack::record &tracking) { - return sol::stack::check(L, index, handler, tracking); + return sol::stack::check(L, index, handler, tracking); } + QSize sol_lua_get(sol::types, lua_State *L, int index, sol::stack::record &tracking) { sol::state_view lua(L); sol::table table = sol::stack::get(L, index, tracking); - return QSize(table.get_or("width", 0), - table.get_or("height", 0)); + if (table.size() != 0 && table.size() != 2) + throw sol::error("Expected table to have 'width' and 'height' or 2 elements"); + if (table.size() == 0) + return QSize(table.get("width"), table.get("height")); + + return QSize(table.get(1), table.get(2)); } int sol_lua_push(sol::types, lua_State *L, const QSize &value) { @@ -99,8 +115,11 @@ QPoint sol_lua_get(sol::types, lua_State *L, int index, sol::stack::reco { sol::state_view lua(L); sol::table table = sol::stack::get(L, index, tracking); - return QPoint(table.get_or("x", 0), - table.get_or("y", 0)); + if (table.size() != 0 && table.size() != 2) + throw sol::error("Expected table to have 'x' and 'y' or 2 elements"); + if (table.size() == 0) + return QPoint(table.get("x"), table.get("y")); + return QPoint(table.get(1), table.get(2)); } int sol_lua_push(sol::types, lua_State *L, const QPoint &value) { @@ -123,10 +142,20 @@ QRectF sol_lua_get(sol::types, lua_State *L, int index, sol::stack::reco { sol::state_view lua(L); sol::table table = sol::stack::get(L, index, tracking); - return QRectF(table.get_or("x", 0.0), - table.get_or("y", 0.0), - table.get_or("width", 0.0), - table.get_or("height", 0.0)); + if (table.size() != 0 && table.size() != 2 && table.size() != 4) + throw sol::error("Expected table to have 'x', 'y', 'width' and 'height' or 2 (pos and " + "size) or 4 elements"); + if (table.size() == 0) { + return QRectF( + table.get("x"), + table.get("y"), + table.get("width"), + table.get("height")); + } + if (table.size() == 2) + return QRectF(table.get(1), table.get(2)); + + return QRectF(table.get(1), table.get(2), table.get(3), table.get(4)); } int sol_lua_push(sol::types, lua_State *L, const QRectF &value) { @@ -149,8 +178,11 @@ QSizeF sol_lua_get(sol::types, lua_State *L, int index, sol::stack::reco { sol::state_view lua(L); sol::table table = sol::stack::get(L, index, tracking); - return QSizeF(table.get_or("width", 0.0), - table.get_or("height", 0.0)); + if (table.size() != 0 && table.size() != 2) + throw sol::error("Expected table to have 'width' and 'height' or 2 elements"); + if (table.size() == 0) + return QSizeF(table.get("width"), table.get("height")); + return QSizeF(table.get(1), table.get(2)); } int sol_lua_push(sol::types, lua_State *L, const QSizeF &value) { @@ -173,8 +205,11 @@ QPointF sol_lua_get(sol::types, lua_State *L, int index, sol::stack::re { sol::state_view lua(L); sol::table table = sol::stack::get(L, index, tracking); - return QPointF(table.get_or("x", 0.0), - table.get_or("y", 0.0)); + if (table.size() != 0 && table.size() != 2) + throw sol::error("Expected table to have 'x' and 'y' or 2 elements"); + if (table.size() == 0) + return QPointF(table.get("x"), table.get("y")); + return QPointF(table.get(1), table.get(2)); } int sol_lua_push(sol::types, lua_State *L, const QPointF &value) { @@ -197,10 +232,16 @@ QColor sol_lua_get(sol::types, lua_State *L, int index, sol::stack::reco { sol::state_view lua(L); sol::table table = sol::stack::get(L, index, tracking); - return QColor(table.get_or("red", 0), - table.get_or("green", 0), - table.get_or("blue", 0), - table.get_or("alpha", 255)); + if (table.size() != 0 && table.size() != 4) + throw sol::error("Expected table to have 0 or 4 elements"); + if (table.size() == 0) { + return QColor( + table.get("red"), + table.get("green"), + table.get("blue"), + table.get("alpha")); + } + return QColor(table.get(1), table.get(2), table.get(3), table.get(4)); } int sol_lua_push(sol::types, lua_State *L, const QColor &value) {