LuaEngine imporved

This commit is contained in:
Syping 2018-08-26 22:40:45 +02:00
parent 7ae82a4e32
commit 3d920f6168
5 changed files with 293 additions and 105 deletions

View file

@ -21,75 +21,75 @@ local mainWindow
local dialogLineEdit
function main()
mainWindow = createMainWindow()
local mainWidget = createCentralWidget(mainWindow)
local mainLayout = createLayout(VerticalLayout, mainWidget)
mainWindow = createMainWindow()
local mainWidget = createCentralWidget(mainWindow)
local mainLayout = createLayout(VerticalLayout, mainWidget)
local menuBar = createMenuBar(mainWindow)
local menuFile = createMenu("File", menuBar)
local menuEntryExit = createMenuEntry("Exit", menuFile)
connect(menuEntryExit, "triggered()", "closeWindow")
local menuHelp = createMenu("Help", menuBar)
local menuEntryAbout = createMenuEntry("About LuaEngine", menuHelp)
connect(menuEntryAbout, "triggered()", "showAboutBox")
local menuBar = createMenuBar(mainWindow)
local menuFile = createMenu("File", menuBar)
local menuEntryExit = createMenuEntry("Exit", menuFile)
connect(menuEntryExit, "triggered()", "closeWindow")
local menuHelp = createMenu("Help", menuBar)
local menuEntryAbout = createMenuEntry("About LuaEngine", menuHelp)
connect(menuEntryAbout, "triggered()", "showAboutBox")
local labelLayout = createLayout(HorizontalLayout, mainLayout)
local appLabel1 = createLabel("LuaEngine greets!", mainWidget)
layoutAddWidget(labelLayout, appLabel1)
local appLabel2 = createLabel("..and not only one time!", mainWidget)
layoutAddWidget(labelLayout, appLabel2)
local labelLayout = createLayout(HorizontalLayout, mainLayout)
local appLabel1 = createLabel("LuaEngine greets!", mainWidget)
layoutAddWidget(labelLayout, appLabel1)
local appLabel2 = createLabel("..and not only one time!", mainWidget)
layoutAddWidget(labelLayout, appLabel2)
local checkBox1 = createCheckBox("Want to check me?", mainWidget)
layoutAddWidget(mainLayout, checkBox1)
local checkBox1 = createCheckBox("Want to check me?", mainWidget)
layoutAddWidget(mainLayout, checkBox1)
local pushButton1 = createPushButton("Press me hard please!", mainWidget)
layoutAddWidget(mainLayout, pushButton1)
connect(pushButton1, "clicked()", "showDialog")
local pushButton1 = createPushButton("Press me hard please!", mainWidget)
layoutAddWidget(mainLayout, pushButton1)
connect(pushButton1, "clicked()", "showDialog")
setWidgetFixedSize(mainWindow)
showWidget(mainWindow, ShowNormal)
return GuiExecuted
setWidgetFixedSize(mainWindow)
showWidget(mainWindow, ShowNormal)
return GuiExecuted
end
function closeWindow()
closeWidget(mainWindow)
closeWidget(mainWindow)
end
function showAboutBox(menuEntry)
showMessageBox(InfoMessageBox, "You triggered the About Box!", "LuaEngine", mainWindow)
showMessageBox(InfoMessageBox, "You triggered the About Box!", "LuaEngine", mainWindow)
end
function showDialog(pushButton)
local dialog = createDialog("LuaEngine", mainWindow)
local dialogLayout = createLayout(VerticalLayout, dialog)
local dialogLabel = createLabel("Yes, you have open a Dialog!", dialog)
layoutAddWidget(dialogLayout, dialogLabel)
dialogLineEdit = createLineEdit("", dialog)
layoutAddWidget(dialogLayout, dialogLineEdit)
local buttonLayout = createLayout(HorizontalLayout, dialogLayout)
createSpacerItem(SizePolicyExpanding, SizePolicyMinimum, buttonLayout)
local dialogButton = createPushButton("Close", dialog)
layoutAddWidget(buttonLayout, dialogButton)
connect(dialogButton, "clicked()", "closeDialog")
setWidgetFixedSize(dialog)
executeWidget(dialog)
delete(dialog, DeleteInstant)
local dialog = createDialog("LuaEngine", mainWindow)
local dialogLayout = createLayout(VerticalLayout, dialog)
local dialogLabel = createLabel("Yes, you have open a Dialog!", dialog)
layoutAddWidget(dialogLayout, dialogLabel)
dialogLineEdit = createLineEdit("", dialog)
layoutAddWidget(dialogLayout, dialogLineEdit)
local buttonLayout = createLayout(HorizontalLayout, dialogLayout)
createSpacerItem(SizePolicyExpanding, SizePolicyMinimum, buttonLayout)
local dialogButton = createPushButton("Close", dialog)
layoutAddWidget(buttonLayout, dialogButton)
connect(dialogButton, "clicked()", "closeDialog")
setWidgetFixedSize(dialog)
executeWidget(dialog)
delete(dialog, DeleteInstant)
end
function closeDialog(pushButton)
disconnect(pushButton, "clicked()")
if not (getWidgetText(dialogLineEdit) == "") then
showMessageBox(InfoMessageBox, "You typed: " .. getWidgetText(dialogLineEdit), "LuaEngine", getParent(pushButton))
end
closeWidget(getParent(pushButton))
disconnect(pushButton, "clicked()")
if not (getWidgetText(dialogLineEdit) == "") then
showMessageBox(InfoMessageBox, "You typed: " .. getWidgetText(dialogLineEdit), "LuaEngine", getParent(pushButton))
end
closeWidget(getParent(pushButton))
end
function testMessageBoxes()
showMessageBox(InfoMessageBox, "LuaEngine Test")
local returnCode = showMessageBox(QuestionMessageBox, "Do you press Yes or No?")
if (returnCode == true) then
showMessageBox(InfoMessageBox, "You have pressed Yes!")
else
showMessageBox(InfoMessageBox, "You have pressed No!")
end
showMessageBox(InfoMessageBox, "LuaEngine Test")
local returnCode = showMessageBox(QuestionMessageBox, "Do you press Yes or No?")
if (returnCode == true) then
showMessageBox(InfoMessageBox, "You have pressed Yes!")
else
showMessageBox(InfoMessageBox, "You have pressed No!")
end
end

View file

@ -22,29 +22,95 @@ local mainWindow
local textEditor
function main()
mainWindow = createMainWindow()
local mainWidget = createCentralWidget(mainWindow)
local mainLayout = createLayout(VerticalLayout, mainWidget)
setLayoutMargins(mainLayout, 0, 0, 0, 0)
mainWindow = createMainWindow("LE Text Editor")
local mainWidget = createCentralWidget(mainWindow)
local mainLayout = createLayout(VerticalLayout, mainWidget)
setLayoutMargins(mainLayout, 0, 0, 0, 0)
local menuBar = createMenuBar(mainWindow)
local menuFile = createMenu("File", menuBar)
local menuEntryNew = createMenuEntry("New", menuFile)
local menuEntryOpen = createMenuEntry("Open...", menuFile)
connect(menuEntrySave, "triggered()", "editorOpen")
local menuEntrySave = createMenuEntry("Save...", menuFile)
connect(menuEntrySave, "triggered()", "editorSave")
local menuEntrySaveAs = createMenuEntry("Save as...", menuFile)
connect(menuEntrySaveAs, "triggered()", "editorSaveAs")
local menuEntryExit = createMenuEntry("Exit", menuFile)
connect(menuEntryExit, "triggered()", "editorClose")
local menuHelp = createMenu("Help", menuBar)
local menuEntryAbout = createMenuEntry("About LE Text Editor", menuHelp)
connect(menuEntryAbout, "triggered()", "editorAboutBox")
local menuBar = createMenuBar(mainWindow)
local menuFile = createMenu("&File", menuBar)
local menuEntryNew = createMenuEntry("&New", menuFile)
setKeySequence(menuEntryNew, "Ctrl+N")
connect(menuEntryNew, "triggered()", "editorNew")
local menuEntryOpen = createMenuEntry("&Open...", menuFile)
setKeySequence(menuEntryOpen, "Ctrl+O")
connect(menuEntryOpen, "triggered()", "editorOpen")
local menuEntrySave = createMenuEntry("&Save...", menuFile)
setKeySequence(menuEntrySave, "Ctrl+S")
connect(menuEntrySave, "triggered()", "editorSave")
local menuEntrySaveAs = createMenuEntry("Save &as...", menuFile)
connect(menuEntrySaveAs, "triggered()", "editorSaveAs")
createMenuSeparator(menuFile)
local menuEntryExit = createMenuEntry("&Exit", menuFile)
setKeySequence(menuEntryExit, "Alt+F4")
connect(menuEntryExit, "triggered()", "editorClose")
local menuHelp = createMenu("&Help", menuBar)
local menuEntryAbout = createMenuEntry("&About LE Text Editor", menuHelp)
setKeySequence(menuEntryAbout, "Ctrl+P")
connect(menuEntryAbout, "triggered()", "editorAboutBox")
textEditor = createTextEdit("", mainWidget)
layoutAddWidget(mainLayout, textEditor)
textEditor = createPlainTextEdit("", mainWidget)
layoutAddWidget(mainLayout, textEditor)
showWidget(mainWindow, ShowNormal)
return GuiExecuted
showWidget(mainWindow, ShowNormal)
return GuiExecuted
end
function editorNew()
setWidgetText(textEditor, "")
currentFile = nil
end
function editorOpen()
local selectedFile = showFileDialog(OpenFileDialog, "Open...", "All files (*)", mainWindow)
if not (selectedFile == nil) then
local file = io.open(selectedFile, "r")
if not (file == nil) then
local content = file:read("*a")
setWidgetText(textEditor, content)
file:close()
currentFile = selectedFile
else
showMessageBox(WarningMessageBox, "Failed to open file " .. selectedFile .. "!", "Open...", mainWindow)
end
end
end
function editorSave()
if not (currentFile == nil) then
local file = io.open(currentFile, "w")
if not (file == nil) then
editorSaveFile(file)
else
showMessageBox(WarningMessageBox, "Failed to save file " .. currentFile .. "!", "Save...", mainWindow)
end
else
editorSaveAs()
end
end
function editorSaveAs()
local selectedFile = showFileDialog(SaveFileDialog, "Save...", "All files (*)", mainWindow)
if not (selectedFile == nil) then
local file = io.open(selectedFile, "w")
if not (file == nil) then
editorSaveFile(file)
currentFile = selectedFile
else
showMessageBox(WarningMessageBox, "Failed to save file " .. selectedFile .. "!", "Save as...", mainWindow)
end
end
end
function editorSaveFile(file)
file:write(getWidgetText(textEditor))
file:close()
end
function editorAboutBox()
showMessageBox(InfoMessageBox, "<h4>LE Text Editor</h4>A simple Text Editor made in Lua Engine", "About LE Text Editor", mainWindow)
end
function editorClose()
closeWidget(mainWindow)
end

View file

@ -101,6 +101,13 @@ QListWidget::item:selected {
background-color: rgb(255, 255, 255);
color: rgb(215, 90, 0);
}
QPlainTextEdit {
background-color: rgb(215, 120, 50);
color: rgb(255, 255, 255);
selection-background-color: rgb(255, 255, 255);
selection-color: rgb(215, 90, 0);
border: 1px solid white;
}
QPushButton {
border-style: ridge;
background-color: rgb(215, 70, 25);

View file

@ -16,6 +16,7 @@
*****************************************************************************/
#include "LuaEngineGui.h"
#include <QPlainTextEdit>
#include <QMainWindow>
#include <QPushButton>
#include <QMessageBox>
@ -25,6 +26,7 @@
#include <QSpacerItem>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QSizePolicy>
#include <QEventLoop>
#include <QTextEdit>
@ -47,7 +49,13 @@ LuaEngineGui::LuaEngineGui(QObject *parent, bool loadBaseLibraries) : LuaEngine(
void LuaEngineGui::pushClass(lua_State *L_p)
{
// MessageBox
// File Dialog
pushFunction(L_p, "showFileDialog", showFileDialog);
pushVariant(L_p, "OpenFileDialog", 0);
pushVariant(L_p, "SaveFileDialog", 1);
pushVariant(L_p, "OpenFolderDialog", 2);
// Message Box
pushFunction(L_p, "showMessageBox", showMessageBox);
pushVariant(L_p, "InfoMessageBox", 0);
pushVariant(L_p, "WarningMessageBox", 1);
@ -91,6 +99,9 @@ void LuaEngineGui::pushClass(lua_State *L_p)
// Line Edit
pushFunction(L_p, "createLineEdit", createLineEdit);
// Key Sequence
pushFunction(L_p, "setKeySequence", setKeySequence);
// Main Window
pushFunction(L_p, "createMainWindow", createMainWindow);
pushFunction(L_p, "createCentralWidget", createCentralWidget);
@ -99,6 +110,10 @@ void LuaEngineGui::pushClass(lua_State *L_p)
pushFunction(L_p, "createMenu", createMenu);
pushFunction(L_p, "createMenuBar", createMenuBar);
pushFunction(L_p, "createMenuEntry", createMenuEntry);
pushFunction(L_p, "createMenuSeparator", createMenuSeparator);
// Plain Text Edit
pushFunction(L_p, "createPlainTextEdit", createPlainTextEdit);
// Push Button
pushFunction(L_p, "createPushButton", createPushButton);
@ -106,7 +121,7 @@ void LuaEngineGui::pushClass(lua_State *L_p)
// Spacer Item
pushFunction(L_p, "createSpacerItem", createSpacerItem);
// Line Edit
// Text Edit
pushFunction(L_p, "createTextEdit", createTextEdit);
// Size Policy
@ -126,25 +141,73 @@ void LuaEngineGui::pushClass(LuaEngine *luaEngine)
pushClass(luaEngine->luaState());
}
int LuaEngineGui::showMessageBox(lua_State *L_p)
int LuaEngineGui::showFileDialog(lua_State *L_p)
{
QVariantList args = getArguments(L_p);
if (args.length() >= 2) {
QWidget *parent = nullptr;
QString boxMessage = args.at(1).toString();
QString boxTitle = "LuaEngine";
if (args.length() >= 3) {
boxTitle = args.at(2).toString();
if (args.length() >= 4) {
if ((QMetaType::Type)args.at(3).type() == QMetaType::Void || (QMetaType::Type)args.at(3).type() == QMetaType::VoidStar) {
if (((QObject*)qvariant_cast<void*>(args.at(3)))->inherits("QWidget")) {
parent = (QWidget*)qvariant_cast<void*>(args.at(3));
QWidget *parent = nullptr;
QString dialogTitle = "LuaEngine";
QString fileFormats = "All files (*)";
QFileDialog::FileMode fileMode = QFileDialog::ExistingFile;
QFileDialog::Options fileOptions = QFileDialog::DontUseNativeDialog;
QFileDialog::AcceptMode fileAcceptMode = QFileDialog::AcceptOpen;
if (getArgumentCount(L_p) >= 1) {
switch (getVariant(L_p, 1).toInt()) {
case 1:
fileMode = QFileDialog::AnyFile;
fileAcceptMode = QFileDialog::AcceptSave;
break;
case 2:
fileMode = QFileDialog::Directory;
fileOptions = QFileDialog::ShowDirsOnly | QFileDialog::DontUseNativeDialog;
break;
}
if (getArgumentCount(L_p) >= 2) {
dialogTitle = getVariant(L_p, 2).toString();
if (getArgumentCount(L_p) >= 3) {
fileFormats = getVariant(L_p, 3).toString();
if (getArgumentCount(L_p) >= 4) {
void *pointer = getPointer(L_p, 4);
if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer;
}
}
}
}
}
QFileDialog fileDialog(parent);
fileDialog.setWindowFlag(Qt::WindowContextHelpButtonHint, false);
fileDialog.setWindowTitle(dialogTitle);
fileDialog.setAcceptMode(fileAcceptMode);
fileDialog.setNameFilter(fileFormats);
fileDialog.setFileMode(fileMode);
fileDialog.setOptions(fileOptions);
if (fileDialog.exec()) {
int returnInt = 0;
switch (args.at(0).toInt()) {
for (const QString &fileName : fileDialog.selectedFiles()) {
returnInt++;
pushVariant(L_p, fileName);
}
return returnInt;
}
return 0;
}
int LuaEngineGui::showMessageBox(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 2) {
QWidget *parent = nullptr;
QString boxMessage = getVariant(L_p, 2).toString();
QString boxTitle = "LuaEngine";
if (getArgumentCount(L_p) >= 3) {
boxTitle = getVariant(L_p, 3).toString();
if (getArgumentCount(L_p) >= 4) {
void *pointer = getPointer(L_p, 4);
if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer;
}
}
}
int returnInt = 0;
switch (getVariant(L_p, 1).toInt()) {
case 1:
QMessageBox::warning(parent, boxTitle, boxMessage);
break;
@ -153,11 +216,7 @@ int LuaEngineGui::showMessageBox(lua_State *L_p)
break;
case 3:
{
bool returnCode = false;
if (QMessageBox::Yes == QMessageBox::question(parent, boxTitle, boxMessage)) {
returnCode = true;
}
pushVariant(L_p, returnCode);
pushVariant(L_p, (QMessageBox::Yes == QMessageBox::question(parent, boxTitle, boxMessage)));
returnInt = 1;
}
break;
@ -255,7 +314,6 @@ int LuaEngineGui::showWidget(lua_State *L_p)
return 0;
}
int LuaEngineGui::setLayoutMargins(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 5) {
@ -267,6 +325,20 @@ int LuaEngineGui::setLayoutMargins(lua_State *L_p)
}
return 0;
}
int LuaEngineGui::setKeySequence(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 2) {
void *pointer = getPointer(L_p, 1);
if (pointer != NULL && ((QObject*)pointer)->inherits("QAction")) {
((QAction*)pointer)->setShortcut(QKeySequence::fromString(getVariant(L_p, 2).toString()));
((QAction*)pointer)->setShortcutContext(Qt::ApplicationShortcut);
return 0;
}
}
return 0;
}
int LuaEngineGui::setWidgetFixedSize(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 1) {
@ -320,8 +392,11 @@ int LuaEngineGui::setWidgetText(lua_State *L_p)
else if (((QObject*)pointer)->inherits("QLineEdit")) {
((QLineEdit*)pointer)->setText(getVariant(L_p, 2).toString());
}
else if (((QObject*)pointer)->inherits("QPlainTextEdit")) {
((QPlainTextEdit*)pointer)->setPlainText(getVariant(L_p, 2).toString());
}
else if (((QObject*)pointer)->inherits("QTextEdit")) {
((QTextEdit*)pointer)->setText(getVariant(L_p, 2).toString());
((QTextEdit*)pointer)->setHtml(getVariant(L_p, 2).toString());
}
else if (((QObject*)pointer)->inherits("QWidget")) {
((QWidget*)pointer)->setWindowTitle(getVariant(L_p, 2).toString());
@ -420,6 +495,7 @@ int LuaEngineGui::createDialog(lua_State *L_p)
}
}
QDialog *dialog = new QDialog(parent);
dialog->setWindowFlag(Qt::WindowContextHelpButtonHint, false);
dialog->setObjectName(nameForPointer(dialog));
dialog->setWindowTitle(windowTitle);
pushPointer(L_p, dialog);
@ -584,6 +660,39 @@ int LuaEngineGui::createMenuEntry(lua_State *L_p)
return 0;
}
int LuaEngineGui::createMenuSeparator(lua_State *L_p)
{
if (getArgumentCount(L_p) >= 1) {
void *pointer = getPointer(L_p, 1);
if (pointer != NULL && ((QObject*)pointer)->inherits("QMenu")) {
QAction *action = ((QMenu*)pointer)->addSeparator();
pushPointer(L_p, action);
return 1;
}
}
return 0;
}
int LuaEngineGui::createPlainTextEdit(lua_State *L_p)
{
QWidget *parent = nullptr;
QString editText = "LuaEngine";
if (getArgumentCount(L_p) >= 1) {
editText = getVariant(L_p, 1).toString();
if (getArgumentCount(L_p) >= 2) {
void *pointer = getPointer(L_p, 2);
if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer;
}
}
}
QPlainTextEdit *textEdit = new QPlainTextEdit(parent);
textEdit->setObjectName(nameForPointer(textEdit));
textEdit->setPlainText(editText);
pushPointer(L_p, textEdit);
return 1;
}
int LuaEngineGui::createPushButton(lua_State *L_p)
{
QVariantList args = getArguments(L_p);
@ -632,22 +741,20 @@ int LuaEngineGui::createSpacerItem(lua_State *L_p)
int LuaEngineGui::createTextEdit(lua_State *L_p)
{
QVariantList args = getArguments(L_p);
QWidget *parent = nullptr;
QString editText = "LuaEngine";
if (args.length() >= 1) {
editText = 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<void*>(args.at(1)))->inherits("QWidget")) {
parent = (QWidget*)qvariant_cast<void*>(args.at(1));
}
if (getArgumentCount(L_p) >= 1) {
editText = getVariant(L_p, 1).toString();
if (getArgumentCount(L_p) >= 2) {
void *pointer = getPointer(L_p, 2);
if (pointer != NULL && ((QObject*)pointer)->inherits("QWidget")) {
parent = (QWidget*)pointer;
}
}
}
QTextEdit *textEdit = new QTextEdit(parent);
textEdit->setObjectName(nameForPointer(textEdit));
textEdit->setText(editText);
textEdit->setHtml(editText);
pushPointer(L_p, textEdit);
return 1;
}
@ -681,8 +788,12 @@ int LuaEngineGui::getWidgetText(lua_State *L_p)
pushVariant(L_p, ((QLineEdit*)pointer)->text());
return 1;
}
else if (((QObject*)pointer)->inherits("QPlainTextEdit")) {
pushVariant(L_p, ((QPlainTextEdit*)pointer)->toPlainText());
return 1;
}
else if (((QObject*)pointer)->inherits("QTextEdit")) {
pushVariant(L_p, ((QTextEdit*)pointer)->toPlainText());
pushVariant(L_p, ((QTextEdit*)pointer)->toHtml());
return 1;
}
else if (((QObject*)pointer)->inherits("QWidget")) {

View file

@ -30,11 +30,13 @@ public:
LuaEngineGui(QObject *parent = nullptr, bool loadBaseLibraries = true);
static void pushClass(lua_State *L_p);
static void pushClass(LuaEngine *luaEngine);
static int showFileDialog(lua_State *L_p);
static int showMessageBox(lua_State *L_p);
static int closeWidget(lua_State *L_p);
static int executeWidget(lua_State *L_p);
static int showWidget(lua_State *L_p);
static int setLayoutMargins(lua_State *L_p);
static int setKeySequence(lua_State *L_p);
static int setWidgetFixedSize(lua_State *L_p);
static int setWidgetLayout(lua_State *L_p);
static int setWidgetText(lua_State *L_p);
@ -50,6 +52,8 @@ public:
static int createMenu(lua_State *L_p);
static int createMenuBar(lua_State *L_p);
static int createMenuEntry(lua_State *L_p);
static int createMenuSeparator(lua_State *L_p);
static int createPlainTextEdit(lua_State *L_p);
static int createPushButton(lua_State *L_p);
static int createSpacerItem(lua_State *L_p);
static int createTextEdit(lua_State *L_p);