From ba207a1c8fd3e8ca83fc30e382b46067cc3a88a8 Mon Sep 17 00:00:00 2001 From: Syping Date: Thu, 23 Aug 2018 16:44:11 +0200 Subject: [PATCH] layout + button --- src/luaengineapp/app.lua | 18 ++++- src/luaenginegui/luaengine/LuaEngineGui.cpp | 80 ++++++++++++++++----- src/luaenginegui/luaengine/LuaEngineGui.h | 4 +- 3 files changed, 83 insertions(+), 19 deletions(-) diff --git a/src/luaengineapp/app.lua b/src/luaengineapp/app.lua index 9070341..0d9c9e4 100644 --- a/src/luaengineapp/app.lua +++ b/src/luaengineapp/app.lua @@ -25,8 +25,10 @@ local menuFile local menuHelp local menuEntryExit local menuEntryAbout +local labelLayout local appLabel1 local appLabel2 +local pushButton1 function main() mainWindow = createMainWindow() @@ -43,11 +45,19 @@ function main() mainLayout = createLayout(VerticalLayout, mainWidget) + labelLayout = createLayout(HorizontalLayout, mainLayout) + layoutAddLayout(mainLayout, labelLayout) + appLabel1 = createLabel("LuaEngine greets!", mainWidget) - layoutAddWidget(mainLayout, appLabel1) + layoutAddWidget(labelLayout, appLabel1) appLabel2 = createLabel("..and not only one time!", mainWidget) - layoutAddWidget(mainLayout, appLabel2) + layoutAddWidget(labelLayout, appLabel2) + + pushButton1 = createPushButton("Press me hard please!", mainWidget) + layoutAddWidget(mainLayout, pushButton1) + + connect(pushButton1, "clicked()", "showHarderBox") showWidget(mainWindow, ShowNormal) @@ -62,6 +72,10 @@ function showAboutBox(menuEntry) showMessageBox(InfoMessageBox, "You triggered the About Box!", "LuaEngine", mainWindow) end +function showHarderBox(pushButton) + showMessageBox(InfoMessageBox, "Harder pls!", "LuaEngine", mainWindow) +end + function testMessageBoxes() showMessageBox(InfoMessageBox, "LuaEngine Test") local returnCode = showMessageBox(QuestionMessageBox, "Do you press Yes or No?") diff --git a/src/luaenginegui/luaengine/LuaEngineGui.cpp b/src/luaenginegui/luaengine/LuaEngineGui.cpp index 4c5fef7..2892057 100644 --- a/src/luaenginegui/luaengine/LuaEngineGui.cpp +++ b/src/luaenginegui/luaengine/LuaEngineGui.cpp @@ -17,6 +17,7 @@ #include "LuaEngineGui.h" #include +#include #include #include #include @@ -61,8 +62,9 @@ void LuaEngineGui::pushClass(lua_State *L_p) // Label pushFunction(L_p, "createLabel", createLabel); - // Label + // Layout pushFunction(L_p, "createLayout", createLayout); + pushFunction(L_p, "layoutAddLayout", layoutAddLayout); pushFunction(L_p, "layoutAddWidget", layoutAddWidget); pushVariant(L_p, "HorizontalLayout", 0); pushVariant(L_p, "VerticalLayout", 1); @@ -75,6 +77,9 @@ void LuaEngineGui::pushClass(lua_State *L_p) pushFunction(L_p, "createMenu", createMenu); pushFunction(L_p, "createMenuBar", createMenuBar); pushFunction(L_p, "createMenuEntry", createMenuEntry); + + // Push Button + pushFunction(L_p, "createPushButton", createPushButton); } void LuaEngineGui::pushClass(LuaEngine *luaEngine) @@ -172,6 +177,27 @@ int LuaEngineGui::showWidget(lua_State *L_p) return 0; } +int LuaEngineGui::layoutAddLayout(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 2) { + void *pointer = getPointer(L_p, 1); + if (pointer != NULL && ((QObject*)pointer)->inherits("QLayout")) { + void *l_pointer = getPointer(L_p, 2); + if (l_pointer!= NULL && ((QObject*)l_pointer)->inherits("QLayout")) { + if (((QObject*)pointer)->inherits("QVBoxLayout")) { + QVBoxLayout *layout = (QVBoxLayout*)pointer; + layout->addLayout((QLayout*)l_pointer); + } + else if (((QObject*)pointer)->inherits("QHBoxLayout")) { + QHBoxLayout *layout = (QHBoxLayout*)pointer; + layout->addLayout((QLayout*)l_pointer); + } + } + } + } + return 0; +} + int LuaEngineGui::layoutAddWidget(lua_State *L_p) { if (getArgumentCount(L_p) >= 2) { @@ -187,6 +213,21 @@ int LuaEngineGui::layoutAddWidget(lua_State *L_p) return 0; } +int LuaEngineGui::createCentralWidget(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 1) { + void *pointer = getPointer(L_p, 1); + if (pointer != NULL && ((QObject*)pointer)->inherits("QMainWindow")) { + QWidget *centralWidget = new QWidget((QWidget*)pointer); + centralWidget->setObjectName(nameForPointer(centralWidget)); + ((QMainWindow*)pointer)->setCentralWidget(centralWidget); + pushPointer(L_p, centralWidget); + return 1; + } + } + return 0; +} + int LuaEngineGui::createLabel(lua_State *L_p) { QVariantList args = getArguments(L_p); @@ -263,21 +304,6 @@ int LuaEngineGui::createMainWindow(lua_State *L_p) return 1; } -int LuaEngineGui::createCentralWidget(lua_State *L_p) -{ - if (getArgumentCount(L_p) >= 1) { - void *pointer = getPointer(L_p, 1); - if (pointer != NULL && ((QObject*)pointer)->inherits("QMainWindow")) { - QWidget *centralWidget = new QWidget((QWidget*)pointer); - centralWidget->setObjectName(nameForPointer(centralWidget)); - ((QMainWindow*)pointer)->setCentralWidget(centralWidget); - pushPointer(L_p, centralWidget); - return 1; - } - } - return 0; -} - int LuaEngineGui::createMenu(lua_State *L_p) { if (getArgumentCount(L_p) >= 2) { @@ -332,6 +358,28 @@ int LuaEngineGui::createMenuEntry(lua_State *L_p) return 0; } +int LuaEngineGui::createPushButton(lua_State *L_p) +{ + QVariantList args = getArguments(L_p); + QWidget *parent = nullptr; + QString buttonText = "LuaEngine"; + if (args.length() >= 1) { + buttonText = args.at(0).toString(); + if (args.length() >= 2) { + if ((QMetaType::Type)args.at(1).type() == QMetaType::Void || (QMetaType::Type)args.at(1).type() == QMetaType::VoidStar) { + if (((QObject*)qvariant_cast(args.at(1)))->inherits("QWidget")) { + parent = (QWidget*)qvariant_cast(args.at(1)); + } + } + } + } + QPushButton *pushButton = new QPushButton(parent); + pushButton->setObjectName(nameForPointer(pushButton)); + pushButton->setText(buttonText); + pushPointer(L_p, pushButton); + return 1; +} + int LuaEngineGui::setWidgetLayout(lua_State *L_p) { if (getArgumentCount(L_p) >= 2) { diff --git a/src/luaenginegui/luaengine/LuaEngineGui.h b/src/luaenginegui/luaengine/LuaEngineGui.h index 4c6f756..dbaae57 100644 --- a/src/luaenginegui/luaengine/LuaEngineGui.h +++ b/src/luaenginegui/luaengine/LuaEngineGui.h @@ -33,14 +33,16 @@ public: static int showMessageBox(lua_State *L_p); static int closeWidget(lua_State *L_p); static int showWidget(lua_State *L_p); + static int layoutAddLayout(lua_State *L_p); static int layoutAddWidget(lua_State *L_p); + static int createCentralWidget(lua_State *L_p); static int createLabel(lua_State *L_p); static int createLayout(lua_State *L_p); static int createMainWindow(lua_State *L_p); - static int createCentralWidget(lua_State *L_p); static int createMenu(lua_State *L_p); static int createMenuBar(lua_State *L_p); static int createMenuEntry(lua_State *L_p); + static int createPushButton(lua_State *L_p); static int setWidgetLayout(lua_State *L_p); private: