luaengineapp/src/luaengineapp/edit.lua
2018-09-15 02:53:35 +02:00

134 lines
4.7 KiB
Lua

--[[
******************************************************************************
* luaEngine Lua Engine for Qt
* Copyright (C) 2018 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.
******************************************************************************
--]]
local currentFile
local mainWindow
local textEditor
function main()
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)
setMenuShortcut(menuEntryNew, "Ctrl+N")
connect(menuEntryNew, "triggered()", "editorNew")
local menuEntryOpen = createMenuEntry("&Open...", menuFile)
setMenuShortcut(menuEntryOpen, "Ctrl+O")
connect(menuEntryOpen, "triggered()", "editorOpen")
local menuEntrySave = createMenuEntry("&Save...", menuFile)
setMenuShortcut(menuEntrySave, "Ctrl+S")
connect(menuEntrySave, "triggered()", "editorSave")
local menuEntrySaveAs = createMenuEntry("Save &as...", menuFile)
connect(menuEntrySaveAs, "triggered()", "editorSaveAs")
createMenuSeparator(menuFile)
local menuEntryExit = createMenuEntry("&Exit", menuFile)
setMenuShortcut(menuEntryExit, "Alt+F4")
connect(menuEntryExit, "triggered()", "editorClose")
local menuHelp = createMenu("&Help", menuBar)
local menuEntryAbout = createMenuEntry("&About LE Text Editor", menuHelp)
setMenuShortcut(menuEntryAbout, "Ctrl+P")
connect(menuEntryAbout, "triggered()", "editorAboutBox")
textEditor = createPlainTextEdit("", mainWidget)
layoutAddWidget(mainLayout, textEditor)
setWidgetSize(mainWindow, 650, 450)
showWidget(mainWindow, ShowDefault)
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 as...", "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()
local dialog = createDialog("About LE Text Editor", mainWindow)
local dialogLayout = createLayout(VerticalLayout, dialog)
local dialogLabel = createLabel("<h4>LE Text Editor</h4>A simple Text Editor made in Lua Engine", dialog)
layoutAddWidget(dialogLayout, dialogLabel)
local buttonLayout = createLayout(HorizontalLayout, dialogLayout)
createSpacerItem(SizePolicyExpanding, SizePolicyMinimum, buttonLayout)
local dialogButton = createPushButton("&OK", dialog)
layoutAddWidget(buttonLayout, dialogButton)
connect(dialogButton, "clicked()", "closeDialog")
setWidgetFixed(dialog)
executeWidget(dialog)
delete(dialog, DeleteInstant)
end
function editorClose()
closeWidget(mainWindow)
end
function closeDialog(pushButton)
disconnect(pushButton, "clicked()")
closeWidget(getWindow(pushButton))
end