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

@ -22,29 +22,95 @@ local mainWindow
local textEditor local textEditor
function main() function main()
mainWindow = createMainWindow() mainWindow = createMainWindow("LE Text Editor")
local mainWidget = createCentralWidget(mainWindow) local mainWidget = createCentralWidget(mainWindow)
local mainLayout = createLayout(VerticalLayout, mainWidget) local mainLayout = createLayout(VerticalLayout, mainWidget)
setLayoutMargins(mainLayout, 0, 0, 0, 0) setLayoutMargins(mainLayout, 0, 0, 0, 0)
local menuBar = createMenuBar(mainWindow) local menuBar = createMenuBar(mainWindow)
local menuFile = createMenu("File", menuBar) local menuFile = createMenu("&File", menuBar)
local menuEntryNew = createMenuEntry("New", menuFile) local menuEntryNew = createMenuEntry("&New", menuFile)
local menuEntryOpen = createMenuEntry("Open...", menuFile) setKeySequence(menuEntryNew, "Ctrl+N")
connect(menuEntrySave, "triggered()", "editorOpen") connect(menuEntryNew, "triggered()", "editorNew")
local menuEntrySave = createMenuEntry("Save...", menuFile) 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") connect(menuEntrySave, "triggered()", "editorSave")
local menuEntrySaveAs = createMenuEntry("Save as...", menuFile) local menuEntrySaveAs = createMenuEntry("Save &as...", menuFile)
connect(menuEntrySaveAs, "triggered()", "editorSaveAs") connect(menuEntrySaveAs, "triggered()", "editorSaveAs")
local menuEntryExit = createMenuEntry("Exit", menuFile) createMenuSeparator(menuFile)
local menuEntryExit = createMenuEntry("&Exit", menuFile)
setKeySequence(menuEntryExit, "Alt+F4")
connect(menuEntryExit, "triggered()", "editorClose") connect(menuEntryExit, "triggered()", "editorClose")
local menuHelp = createMenu("Help", menuBar) local menuHelp = createMenu("&Help", menuBar)
local menuEntryAbout = createMenuEntry("About LE Text Editor", menuHelp) local menuEntryAbout = createMenuEntry("&About LE Text Editor", menuHelp)
setKeySequence(menuEntryAbout, "Ctrl+P")
connect(menuEntryAbout, "triggered()", "editorAboutBox") connect(menuEntryAbout, "triggered()", "editorAboutBox")
textEditor = createTextEdit("", mainWidget) textEditor = createPlainTextEdit("", mainWidget)
layoutAddWidget(mainLayout, textEditor) layoutAddWidget(mainLayout, textEditor)
showWidget(mainWindow, ShowNormal) showWidget(mainWindow, ShowNormal)
return GuiExecuted return GuiExecuted
end 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); background-color: rgb(255, 255, 255);
color: rgb(215, 90, 0); 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 { QPushButton {
border-style: ridge; border-style: ridge;
background-color: rgb(215, 70, 25); background-color: rgb(215, 70, 25);

View file

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

View file

@ -30,11 +30,13 @@ public:
LuaEngineGui(QObject *parent = nullptr, bool loadBaseLibraries = true); LuaEngineGui(QObject *parent = nullptr, bool loadBaseLibraries = true);
static void pushClass(lua_State *L_p); static void pushClass(lua_State *L_p);
static void pushClass(LuaEngine *luaEngine); static void pushClass(LuaEngine *luaEngine);
static int showFileDialog(lua_State *L_p);
static int showMessageBox(lua_State *L_p); static int showMessageBox(lua_State *L_p);
static int closeWidget(lua_State *L_p); static int closeWidget(lua_State *L_p);
static int executeWidget(lua_State *L_p); static int executeWidget(lua_State *L_p);
static int showWidget(lua_State *L_p); static int showWidget(lua_State *L_p);
static int setLayoutMargins(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 setWidgetFixedSize(lua_State *L_p);
static int setWidgetLayout(lua_State *L_p); static int setWidgetLayout(lua_State *L_p);
static int setWidgetText(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 createMenu(lua_State *L_p);
static int createMenuBar(lua_State *L_p); static int createMenuBar(lua_State *L_p);
static int createMenuEntry(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 createPushButton(lua_State *L_p);
static int createSpacerItem(lua_State *L_p); static int createSpacerItem(lua_State *L_p);
static int createTextEdit(lua_State *L_p); static int createTextEdit(lua_State *L_p);