From c51239a3fa773d7a02e1153c69ad81e263a91e09 Mon Sep 17 00:00:00 2001
From: Syping <schiedelrafael@keppe.org>
Date: Wed, 25 Sep 2019 14:23:45 +0200
Subject: [PATCH] add widgets automatic in layout

---
 src/luaengineapp/luaengineapp.pro           |   3 -
 src/luaenginegui/luaengine/LuaEngineGui.cpp | 101 ++++++++++++++++++++
 src/luaenginegui/luaengine/LuaEngineGui.h   |   1 +
 src/luaenginerun/luaenginerun.pro           |  35 +++++++
 src/luaenginerun/main.cpp                   |  73 ++++++++++++++
 src/src.pro                                 |   3 +-
 6 files changed, 212 insertions(+), 4 deletions(-)
 create mode 100644 src/luaenginerun/luaenginerun.pro
 create mode 100644 src/luaenginerun/main.cpp

diff --git a/src/luaengineapp/luaengineapp.pro b/src/luaengineapp/luaengineapp.pro
index cd71be9..b28d509 100644
--- a/src/luaengineapp/luaengineapp.pro
+++ b/src/luaengineapp/luaengineapp.pro
@@ -41,6 +41,3 @@ OTHER_FILES += \
     app.lua
 
 win32: RC_FILE = app.rc
-
-ANDROID_EXTRA_LIBS += $$OUT_PWD/../luaengine/libluaengine.so $$OUT_PWD/../luaenginegui/libluaenginegui.so
-ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
diff --git a/src/luaenginegui/luaengine/LuaEngineGui.cpp b/src/luaenginegui/luaengine/LuaEngineGui.cpp
index d692158..801a936 100644
--- a/src/luaenginegui/luaengine/LuaEngineGui.cpp
+++ b/src/luaenginegui/luaengine/LuaEngineGui.cpp
@@ -537,6 +537,7 @@ int LuaEngineGui::createCentralWidget(lua_State *L_p)
 
 int LuaEngineGui::createCheckBox(lua_State *L_p)
 {
+    QLayout *layout = nullptr;
     QWidget *parent = nullptr;
     QString labelText = "LuaEngine";
     if (getArgumentCount(L_p) >= 1) {
@@ -549,11 +550,21 @@ int LuaEngineGui::createCheckBox(lua_State *L_p)
             else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
                 parent = (QWidget*)pointer;
             }
+            else if (pointer != NULL && ((QObject*)pointer)->inherits("QLayout")) {
+                QWidget *widget = windowForObject((QObject*)pointer);
+                if (widget != nullptr) {
+                    layout = (QLayout*)pointer;
+                    parent = widget;
+                }
+            }
         }
     }
     QCheckBox *checkBox = new QCheckBox(parent);
     checkBox->setObjectName(nameForPointer(checkBox));
     checkBox->setText(labelText);
+    if (layout != nullptr) {
+        layout->addWidget(checkBox);
+    }
     pushPointer(L_p, checkBox);
     return 1;
 }
@@ -585,6 +596,7 @@ int LuaEngineGui::createDialog(lua_State *L_p)
 
 int LuaEngineGui::createLabel(lua_State *L_p)
 {
+    QLayout *layout = nullptr;
     QWidget *parent = nullptr;
     QString labelText = "LuaEngine";
     if (getArgumentCount(L_p) >= 1) {
@@ -597,11 +609,21 @@ int LuaEngineGui::createLabel(lua_State *L_p)
             else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
                 parent = (QWidget*)pointer;
             }
+            else if (pointer != NULL && ((QObject*)pointer)->inherits("QLayout")) {
+                QWidget *widget = windowForObject((QObject*)pointer);
+                if (widget != nullptr) {
+                    layout = (QLayout*)pointer;
+                    parent = widget;
+                }
+            }
         }
     }
     QLabel *label = new QLabel(parent);
     label->setObjectName(nameForPointer(label));
     label->setText(labelText);
+    if (layout != nullptr) {
+        layout->addWidget(label);
+    }
     pushPointer(L_p, label);
     return 1;
 }
@@ -649,6 +671,7 @@ int LuaEngineGui::createLayout(lua_State *L_p)
 
 int LuaEngineGui::createLineEdit(lua_State *L_p)
 {
+    QLayout *layout = nullptr;
     QWidget *parent = nullptr;
     QString editText = "LuaEngine";
     if (getArgumentCount(L_p) >= 1) {
@@ -661,11 +684,21 @@ int LuaEngineGui::createLineEdit(lua_State *L_p)
             else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
                 parent = (QWidget*)pointer;
             }
+            else if (pointer != NULL && ((QObject*)pointer)->inherits("QLayout")) {
+                QWidget *widget = windowForObject((QObject*)pointer);
+                if (widget != nullptr) {
+                    layout = (QLayout*)pointer;
+                    parent = widget;
+                }
+            }
         }
     }
     QLineEdit *lineEdit = new QLineEdit(parent);
     lineEdit->setObjectName(nameForPointer(lineEdit));
     lineEdit->setText(editText);
+    if (layout != nullptr) {
+        layout->addWidget(lineEdit);
+    }
     pushPointer(L_p, lineEdit);
     return 1;
 }
@@ -772,6 +805,7 @@ int LuaEngineGui::createMenuSeparator(lua_State *L_p)
 
 int LuaEngineGui::createPlainTextEdit(lua_State *L_p)
 {
+    QLayout *layout = nullptr;
     QWidget *parent = nullptr;
     QString editText = "LuaEngine";
     if (getArgumentCount(L_p) >= 1) {
@@ -784,17 +818,28 @@ int LuaEngineGui::createPlainTextEdit(lua_State *L_p)
             else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
                 parent = (QWidget*)pointer;
             }
+            else if (pointer != NULL && ((QObject*)pointer)->inherits("QLayout")) {
+                QWidget *widget = windowForObject((QObject*)pointer);
+                if (widget != nullptr) {
+                    layout = (QLayout*)pointer;
+                    parent = widget;
+                }
+            }
         }
     }
     QPlainTextEdit *textEdit = new QPlainTextEdit(parent);
     textEdit->setObjectName(nameForPointer(textEdit));
     textEdit->setPlainText(editText);
+    if (layout != nullptr) {
+        layout->addWidget(textEdit);
+    }
     pushPointer(L_p, textEdit);
     return 1;
 }
 
 int LuaEngineGui::createPushButton(lua_State *L_p)
 {
+    QLayout *layout = nullptr;
     QWidget *parent = nullptr;
     QString buttonText = "LuaEngine";
     if (getArgumentCount(L_p) >= 1) {
@@ -807,17 +852,28 @@ int LuaEngineGui::createPushButton(lua_State *L_p)
             else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
                 parent = (QWidget*)pointer;
             }
+            else if (pointer != NULL && ((QObject*)pointer)->inherits("QLayout")) {
+                QWidget *widget = windowForObject((QObject*)pointer);
+                if (widget != nullptr) {
+                    layout = (QLayout*)pointer;
+                    parent = widget;
+                }
+            }
         }
     }
     QPushButton *pushButton = new QPushButton(parent);
     pushButton->setObjectName(nameForPointer(pushButton));
     pushButton->setText(buttonText);
+    if (layout != nullptr) {
+        layout->addWidget(pushButton);
+    }
     pushPointer(L_p, pushButton);
     return 1;
 }
 
 int LuaEngineGui::createRadioButton(lua_State *L_p)
 {
+    QLayout *layout = nullptr;
     QWidget *parent = nullptr;
     QString labelText = "LuaEngine";
     if (getArgumentCount(L_p) >= 1) {
@@ -830,11 +886,21 @@ int LuaEngineGui::createRadioButton(lua_State *L_p)
             else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
                 parent = (QWidget*)pointer;
             }
+            else if (pointer != NULL && ((QObject*)pointer)->inherits("QLayout")) {
+                QWidget *widget = windowForObject((QObject*)pointer);
+                if (widget != nullptr) {
+                    layout = (QLayout*)pointer;
+                    parent = widget;
+                }
+            }
         }
     }
     QRadioButton *radioButton = new QRadioButton(parent);
     radioButton->setObjectName(nameForPointer(radioButton));
     radioButton->setText(labelText);
+    if (layout != nullptr) {
+        layout->addWidget(radioButton);
+    }
     pushPointer(L_p, radioButton);
     return 1;
 }
@@ -865,6 +931,7 @@ int LuaEngineGui::createSpacerItem(lua_State *L_p)
 
 int LuaEngineGui::createTextEdit(lua_State *L_p)
 {
+    QLayout *layout = nullptr;
     QWidget *parent = nullptr;
     QString editText = "LuaEngine";
     if (getArgumentCount(L_p) >= 1) {
@@ -877,11 +944,21 @@ int LuaEngineGui::createTextEdit(lua_State *L_p)
             else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
                 parent = (QWidget*)pointer;
             }
+            else if (pointer != NULL && ((QObject*)pointer)->inherits("QLayout")) {
+                QWidget *widget = windowForObject((QObject*)pointer);
+                if (widget != nullptr) {
+                    layout = (QLayout*)pointer;
+                    parent = widget;
+                }
+            }
         }
     }
     QTextEdit *textEdit = new QTextEdit(parent);
     textEdit->setObjectName(nameForPointer(textEdit));
     textEdit->setHtml(editText);
+    if (layout != nullptr) {
+        layout->addWidget(textEdit);
+    }
     pushPointer(L_p, textEdit);
     return 1;
 }
@@ -972,6 +1049,30 @@ int LuaEngineGui::getWidgetText(lua_State *L_p)
     return 0;
 }
 
+QWidget* LuaEngineGui::windowForObject(QObject *object)
+{
+    bool isWindow = false;
+    QObject *w_object = object;
+    while (!isWindow) {
+        if (w_object->inherits("QDialog") || w_object->inherits("QMainWindow")) {
+            isWindow = true;
+        }
+        else {
+            QObject *parent = w_object->parent();
+            if (parent != NULL) {
+                w_object = parent;
+            }
+            else {
+                break;
+            }
+        }
+    }
+    if (isWindow) {
+        return (QWidget*)w_object;
+    }
+    return nullptr;
+}
+
 QString LuaEngineGui::nameForPointer(void *pointer)
 {
     QString nameStorage;
diff --git a/src/luaenginegui/luaengine/LuaEngineGui.h b/src/luaenginegui/luaengine/LuaEngineGui.h
index 9863bbd..8e91752 100644
--- a/src/luaenginegui/luaengine/LuaEngineGui.h
+++ b/src/luaenginegui/luaengine/LuaEngineGui.h
@@ -67,6 +67,7 @@ public:
 private:
     lua_State *L;
     static QString nameForPointer(void *pointer);
+    static QWidget* windowForObject(QObject *object);
 };
 
 #endif // LUAENGINEGUI_H
diff --git a/src/luaenginerun/luaenginerun.pro b/src/luaenginerun/luaenginerun.pro
new file mode 100644
index 0000000..24e2138
--- /dev/null
+++ b/src/luaenginerun/luaenginerun.pro
@@ -0,0 +1,35 @@
+#/*****************************************************************************
+#* luaEngine Lua Engine for Qt
+#* Copyright (C) 2019 Syping
+#*
+#* Licensed under the Apache License, Version 2.0 (the "License");
+#* you may not use this file except in compliance with the License.
+#* You may obtain a copy of the License at
+#*
+#*     http://www.apache.org/licenses/LICENSE-2.0
+#*
+#* Unless required by applicable law or agreed to in writing, software
+#* distributed under the License is distributed on an "AS IS" BASIS,
+#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#* See the License for the specific language governing permissions and
+#* limitations under the License.
+#*****************************************************************************/
+
+QT += core gui widgets
+TARGET = luaengine
+CONFIG += c++11
+
+static: DEFINES += LUAENGINE_STATIC
+
+CONFIG(debug, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengine/debug -lluaengine -L$$OUT_PWD/../luaenginegui/debug -lluaenginegui
+CONFIG(release, debug|release): win32: LIBS += -L$$OUT_PWD/../luaengine/release -lluaengine -L$$OUT_PWD/../luaenginegui/release -lluaenginegui
+unix: LIBS += -L$$OUT_PWD/../luaengine -lluaengine -L$$OUT_PWD/../luaenginegui -lluaenginegui
+win32: LIBS += -luser32
+
+INCLUDEPATH += \
+    ../luaengine/lua \
+    ../luaengine/luaengine \
+    ../luaenginegui/luaengine
+
+SOURCES += \
+    main.cpp
diff --git a/src/luaenginerun/main.cpp b/src/luaenginerun/main.cpp
new file mode 100644
index 0000000..4a160cf
--- /dev/null
+++ b/src/luaenginerun/main.cpp
@@ -0,0 +1,73 @@
+/*****************************************************************************
+* luaEngine Lua Engine for Qt
+* Copyright (C) 2018-2019 Syping
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*****************************************************************************/
+
+#include "LuaEngineGui.h"
+#include <QApplication>
+#include <QFont>
+#include <QFile>
+#include <QDebug>
+
+int main(int argc, char *argv[])
+{
+    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
+    QApplication a(argc, argv);
+
+#ifdef Q_OS_WIN
+#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
+    if (QSysInfo::windowsVersion() >= 0x0080)
+    {
+        a.setFont(QApplication::font("QMenu"));
+    }
+#endif
+#endif
+
+    QVariantList arguments;
+    for (const QString &argument : a.arguments()) {
+        arguments << QVariant::fromValue(argument);
+    }
+
+    if (arguments.length() >= 2) {
+        arguments.removeFirst();
+        QFile luaScript(arguments.first().toString());
+
+        LuaEngineGui luaEngineGui;
+        luaEngineGui.executeLuaScript(&luaScript);
+
+        if (luaEngineGui.executeLuaFunction("main", arguments, true)) {
+            QVariant variant = luaEngineGui.returnVariant();
+            if (variant.type() == QVariant::Int || variant.type() == QVariant::LongLong) {
+                return variant.toInt();
+            }
+            else if (variant.type() == QVariant::String) {
+                if (variant.toString() == "GuiExecuted") {
+                    return a.exec();
+                }
+                else {
+                    return 1;
+                }
+            }
+            else {
+                return 1;
+            }
+        }
+
+        return 1;
+    }
+    else {
+        return 1;
+    }
+}
diff --git a/src/src.pro b/src/src.pro
index 6e737e4..ebacd52 100644
--- a/src/src.pro
+++ b/src/src.pro
@@ -20,4 +20,5 @@ CONFIG += ordered
 
 SUBDIRS += luaengine \
     luaenginegui \
-    luaengineapp
+    luaengineapp \
+    luaenginerun