add widgets automatic in layout

This commit is contained in:
Syping 2019-09-25 14:23:45 +02:00
parent 33c32e096d
commit c51239a3fa
6 changed files with 212 additions and 4 deletions

View file

@ -41,6 +41,3 @@ OTHER_FILES += \
app.lua app.lua
win32: RC_FILE = app.rc win32: RC_FILE = app.rc
ANDROID_EXTRA_LIBS += $$OUT_PWD/../luaengine/libluaengine.so $$OUT_PWD/../luaenginegui/libluaenginegui.so
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android

View file

@ -537,6 +537,7 @@ int LuaEngineGui::createCentralWidget(lua_State *L_p)
int LuaEngineGui::createCheckBox(lua_State *L_p) int LuaEngineGui::createCheckBox(lua_State *L_p)
{ {
QLayout *layout = nullptr;
QWidget *parent = nullptr; QWidget *parent = nullptr;
QString labelText = "LuaEngine"; QString labelText = "LuaEngine";
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
@ -549,11 +550,21 @@ int LuaEngineGui::createCheckBox(lua_State *L_p)
else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) { else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer; 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); QCheckBox *checkBox = new QCheckBox(parent);
checkBox->setObjectName(nameForPointer(checkBox)); checkBox->setObjectName(nameForPointer(checkBox));
checkBox->setText(labelText); checkBox->setText(labelText);
if (layout != nullptr) {
layout->addWidget(checkBox);
}
pushPointer(L_p, checkBox); pushPointer(L_p, checkBox);
return 1; return 1;
} }
@ -585,6 +596,7 @@ int LuaEngineGui::createDialog(lua_State *L_p)
int LuaEngineGui::createLabel(lua_State *L_p) int LuaEngineGui::createLabel(lua_State *L_p)
{ {
QLayout *layout = nullptr;
QWidget *parent = nullptr; QWidget *parent = nullptr;
QString labelText = "LuaEngine"; QString labelText = "LuaEngine";
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
@ -597,11 +609,21 @@ int LuaEngineGui::createLabel(lua_State *L_p)
else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) { else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer; 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); QLabel *label = new QLabel(parent);
label->setObjectName(nameForPointer(label)); label->setObjectName(nameForPointer(label));
label->setText(labelText); label->setText(labelText);
if (layout != nullptr) {
layout->addWidget(label);
}
pushPointer(L_p, label); pushPointer(L_p, label);
return 1; return 1;
} }
@ -649,6 +671,7 @@ int LuaEngineGui::createLayout(lua_State *L_p)
int LuaEngineGui::createLineEdit(lua_State *L_p) int LuaEngineGui::createLineEdit(lua_State *L_p)
{ {
QLayout *layout = nullptr;
QWidget *parent = nullptr; QWidget *parent = nullptr;
QString editText = "LuaEngine"; QString editText = "LuaEngine";
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
@ -661,11 +684,21 @@ int LuaEngineGui::createLineEdit(lua_State *L_p)
else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) { else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer; 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); QLineEdit *lineEdit = new QLineEdit(parent);
lineEdit->setObjectName(nameForPointer(lineEdit)); lineEdit->setObjectName(nameForPointer(lineEdit));
lineEdit->setText(editText); lineEdit->setText(editText);
if (layout != nullptr) {
layout->addWidget(lineEdit);
}
pushPointer(L_p, lineEdit); pushPointer(L_p, lineEdit);
return 1; return 1;
} }
@ -772,6 +805,7 @@ int LuaEngineGui::createMenuSeparator(lua_State *L_p)
int LuaEngineGui::createPlainTextEdit(lua_State *L_p) int LuaEngineGui::createPlainTextEdit(lua_State *L_p)
{ {
QLayout *layout = nullptr;
QWidget *parent = nullptr; QWidget *parent = nullptr;
QString editText = "LuaEngine"; QString editText = "LuaEngine";
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
@ -784,17 +818,28 @@ int LuaEngineGui::createPlainTextEdit(lua_State *L_p)
else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) { else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer; 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); QPlainTextEdit *textEdit = new QPlainTextEdit(parent);
textEdit->setObjectName(nameForPointer(textEdit)); textEdit->setObjectName(nameForPointer(textEdit));
textEdit->setPlainText(editText); textEdit->setPlainText(editText);
if (layout != nullptr) {
layout->addWidget(textEdit);
}
pushPointer(L_p, textEdit); pushPointer(L_p, textEdit);
return 1; return 1;
} }
int LuaEngineGui::createPushButton(lua_State *L_p) int LuaEngineGui::createPushButton(lua_State *L_p)
{ {
QLayout *layout = nullptr;
QWidget *parent = nullptr; QWidget *parent = nullptr;
QString buttonText = "LuaEngine"; QString buttonText = "LuaEngine";
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
@ -807,17 +852,28 @@ int LuaEngineGui::createPushButton(lua_State *L_p)
else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) { else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer; 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); QPushButton *pushButton = new QPushButton(parent);
pushButton->setObjectName(nameForPointer(pushButton)); pushButton->setObjectName(nameForPointer(pushButton));
pushButton->setText(buttonText); pushButton->setText(buttonText);
if (layout != nullptr) {
layout->addWidget(pushButton);
}
pushPointer(L_p, pushButton); pushPointer(L_p, pushButton);
return 1; return 1;
} }
int LuaEngineGui::createRadioButton(lua_State *L_p) int LuaEngineGui::createRadioButton(lua_State *L_p)
{ {
QLayout *layout = nullptr;
QWidget *parent = nullptr; QWidget *parent = nullptr;
QString labelText = "LuaEngine"; QString labelText = "LuaEngine";
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
@ -830,11 +886,21 @@ int LuaEngineGui::createRadioButton(lua_State *L_p)
else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) { else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer; 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); QRadioButton *radioButton = new QRadioButton(parent);
radioButton->setObjectName(nameForPointer(radioButton)); radioButton->setObjectName(nameForPointer(radioButton));
radioButton->setText(labelText); radioButton->setText(labelText);
if (layout != nullptr) {
layout->addWidget(radioButton);
}
pushPointer(L_p, radioButton); pushPointer(L_p, radioButton);
return 1; return 1;
} }
@ -865,6 +931,7 @@ int LuaEngineGui::createSpacerItem(lua_State *L_p)
int LuaEngineGui::createTextEdit(lua_State *L_p) int LuaEngineGui::createTextEdit(lua_State *L_p)
{ {
QLayout *layout = nullptr;
QWidget *parent = nullptr; QWidget *parent = nullptr;
QString editText = "LuaEngine"; QString editText = "LuaEngine";
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
@ -877,11 +944,21 @@ int LuaEngineGui::createTextEdit(lua_State *L_p)
else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) { else if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer; 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); QTextEdit *textEdit = new QTextEdit(parent);
textEdit->setObjectName(nameForPointer(textEdit)); textEdit->setObjectName(nameForPointer(textEdit));
textEdit->setHtml(editText); textEdit->setHtml(editText);
if (layout != nullptr) {
layout->addWidget(textEdit);
}
pushPointer(L_p, textEdit); pushPointer(L_p, textEdit);
return 1; return 1;
} }
@ -972,6 +1049,30 @@ int LuaEngineGui::getWidgetText(lua_State *L_p)
return 0; 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 LuaEngineGui::nameForPointer(void *pointer)
{ {
QString nameStorage; QString nameStorage;

View file

@ -67,6 +67,7 @@ public:
private: private:
lua_State *L; lua_State *L;
static QString nameForPointer(void *pointer); static QString nameForPointer(void *pointer);
static QWidget* windowForObject(QObject *object);
}; };
#endif // LUAENGINEGUI_H #endif // LUAENGINEGUI_H

View file

@ -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

73
src/luaenginerun/main.cpp Normal file
View file

@ -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;
}
}

View file

@ -20,4 +20,5 @@ CONFIG += ordered
SUBDIRS += luaengine \ SUBDIRS += luaengine \
luaenginegui \ luaenginegui \
luaengineapp luaengineapp \
luaenginerun