Permalink
Browse files

Add UI dialog to choose files in Lua

  • Loading branch information...
feragon committed Jun 21, 2017
1 parent f226485 commit abba6eb482bd1fc0e1c09ac687001519b46bdea8
View
@@ -13,12 +13,12 @@ LuaInterface::~LuaInterface() {
}
void LuaInterface::initLua() {
luaL_openlibs(_L);
luaOpenQtBridge(_L);
auto lcLua = lc::LCLua(_L);
lcLua.addLuaLibs();
lcLua.importLCKernel();
luaOpenQtBridge(_L);
LuaIntf::Lua::setGlobal(_L, "luaInterface", this);
QString luaFile = QCoreApplication::applicationDirPath() + "/path.lua";
@@ -29,7 +29,7 @@ void LuaInterface::initLua() {
lua_pop(_L, 1);
}
_pluginManager.loadPlugins();
_pluginManager.loadPlugins(&LuaInterface::openFileDialog);
}
bool LuaInterface::luaConnect(
@@ -109,4 +109,21 @@ std::vector<std::string> LuaInterface::pluginList(const char* path) {
}
return plugins;
}
}
FILE* LuaInterface::openFileDialog(bool isOpening, const char* description, const char* mode) {
QString path;
if(isOpening) {
path = QFileDialog::getOpenFileName(nullptr, (std::string("Open ") + description).c_str());
}
else {
path = QFileDialog::getSaveFileName(nullptr, (std::string("Save ") + description).c_str());
}
if(path.isEmpty()) {
return nullptr;
}
return fopen(path.toStdString().c_str(), mode);
}
View
@@ -94,6 +94,8 @@ class LuaInterface {
*/
LuaIntf::LuaState luaState();
static FILE* openFileDialog(bool isOpening, const char* description, const char* mode);
private:
LuaIntf::LuaState _L;
std::vector<LuaQObject_SPtr> _luaQObjects;
@@ -2,6 +2,7 @@
#include "ui_luascript.h"
#include <lclua.h>
#include <luainterface.h>
LuaScript::LuaScript(QMdiArea* mdiArea, CliCommand* cliCommand) :
ui(new Ui::LuaScript),
@@ -21,7 +22,7 @@ void LuaScript::on_luaRun_clicked() {
auto luaState = LuaIntf::LuaState::newState();
auto lcLua = lc::LCLua(luaState);
lcLua.setF_openFileDialog(&LuaInterface::openFileDialog);
lcLua.addLuaLibs();
lcLua.importLCKernel();
lcLua.setDocument(mdiChild->document());
@@ -1,6 +1,2 @@
print(LC_interface)
file = openFile("/tmp/a", "r+")
actual = file:read(1)
actual = actual .. "Test"
print(actual)
file:write(actual)
file = openFileDialog(true, "Plugin config", "r")
View
@@ -17,7 +17,8 @@ static const luaL_Reg loadedlibs[] = {
};
LCLua::LCLua(lua_State* L) :
_L(L) {
_L(L),
_f_openFileDialog(nullptr) {
}
@@ -42,6 +43,15 @@ void LCLua::addLuaLibs() {
return write(file, content);
})
.endClass();
if(_f_openFileDialog == nullptr) {
LuaBinding(_L).addFunction("openFileDialog", []() {
return (FILE*) nullptr;
});
}
else {
LuaBinding(_L).addFunction("openFileDialog", _f_openFileDialog);
}
}
void LCLua::setDocument(lc::Document_SPtr document) {
@@ -77,4 +87,8 @@ std::string LCLua::read(FILE* file, const size_t len) {
void LCLua::write(FILE* file, const char* content) {
fwrite(content, sizeof(char), strlen(content), file);
}
}
void LCLua::setF_openFileDialog(FILE* (* f_openFileDialog)(bool, const char*, const char*)) {
_f_openFileDialog = f_openFileDialog;
}
View
@@ -19,16 +19,23 @@ namespace lc {
public:
LCLua(lua_State* L);
/**
* @brief Bind Lua functions and others functions required by LibreCAD
* @param f_openFileDialog Function which asks user to choose a file
*/
void addLuaLibs();
void importLCKernel();
void setDocument(lc::Document_SPtr document);
std::string runString(const char* code);
void setF_openFileDialog(FILE* (* f_openFileDialog)(bool, const char*, const char*));
static FILE* openFile(const char* path, const char* mode);
static std::string read(FILE* file, const size_t len);
static void write(FILE* file, const char* content);
private:
lua_State* _L;
FILE* (*_f_openFileDialog)(bool, const char*, const char*);
};
}
@@ -1,14 +1,13 @@
#include "pluginmanager.h"
#include "lclua.h"
#include <dirent.h>
#include <iostream>
lc::PluginManager::PluginManager(const char* interface) :
_interface(interface) {
}
void lc::PluginManager::loadPlugins() {
void lc::PluginManager::loadPlugins(FILE* (*f_openFileDialog)(bool, const char*, const char*)) {
const char* path = "../lcUILua/plugins/"; //TODO: get path
DIR* dir;
@@ -20,7 +19,7 @@ void lc::PluginManager::loadPlugins() {
continue;
}
loadPlugin((std::string(path) + ent->d_name + "/plugin.lua").c_str());
loadPlugin((std::string(path) + ent->d_name + "/plugin.lua").c_str(), f_openFileDialog);
}
closedir(dir);
}
@@ -29,9 +28,10 @@ void lc::PluginManager::loadPlugins() {
}
}
void lc::PluginManager::loadPlugin(const char* file) {
void lc::PluginManager::loadPlugin(const char* file, FILE* (*f_openFileDialog)(bool, const char*, const char*)) {
auto state = LuaIntf::LuaState::newState();
auto lcLua = LCLua(state);
lcLua.setF_openFileDialog(f_openFileDialog);
lcLua.addLuaLibs();
lcLua.importLCKernel();
@@ -11,10 +11,15 @@ namespace lc {
class PluginManager {
public:
PluginManager(const char* interface);
void loadPlugins();
/**
* @brief Load the Lua plugins
* @param f_openFileDialog Function which ask the user to choose a file, this might be moved later
*/
void loadPlugins(FILE* (*f_openFileDialog)(bool, const char*, const char*));
private:
void loadPlugin(const char* file);
void loadPlugin(const char* file, FILE* (*f_openFileDialog)(bool, const char*, const char*));
const char* _interface;
};
View
@@ -69,6 +69,21 @@ cairo_status_t write_func (void * closure, const unsigned char *data, unsigned i
return CAIRO_STATUS_SUCCESS;
}
static FILE* openFileDialog(bool isOpening, const char* description, const char* mode) {
std::string path;
if(isOpening) {
std::cout << "Enter path to open " << description << ": ";
}
else {
std::cout << "Enter path to save " << description << ": ";
}
std::cin >> path;
return fopen(path.c_str(), mode);
}
int main(int argc, char** argv) {
int width = DEFAULT_IMAGE_WIDTH;
int height = DEFAULT_IMAGE_HEIGHT;
@@ -164,11 +179,12 @@ int main(int argc, char** argv) {
// Render Lua Code
lc::PluginManager pluginManager("cli");
pluginManager.loadPlugins();
pluginManager.loadPlugins(&openFileDialog);
auto luaState = LuaIntf::LuaState::newState();
auto lcLua = lc::LCLua(luaState);
lcLua.setF_openFileDialog(&openFileDialog);
lcLua.addLuaLibs();
lcLua.importLCKernel();
lcLua.setDocument(_document);

0 comments on commit abba6eb

Please sign in to comment.