LibreCAD
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
luainterface.cpp
Go to the documentation of this file.
1 #include <lclua.h>
2 #include <managers/luacustomentitymanager.h>
3 #include "luainterface.h"
4 
6  _L(LuaIntf::LuaState::newState()),
7  _pluginManager(_L, "gui") {
8 }
9 
11  _luaQObjects.clear();
12  _operations.clear();
13  _events.clear();
14 
15  lc::LuaCustomEntityManager::getInstance().removePlugins();
16 
17  _L.close();
18 }
19 
21  auto lcLua = lc::LCLua(_L);
22  lcLua.setF_openFileDialog(&LuaInterface::openFileDialog);
23  lcLua.addLuaLibs();
24  lcLua.importLCKernel();
25 
27 
28  LuaIntf::Lua::setGlobal(_L, "luaInterface", this);
29 
30  QString luaFile = QCoreApplication::applicationDirPath() + "/path.lua";
31  int s = _L.doFile(luaFile.toStdString().c_str());
32 
33  if (s != 0) {
34  std::cout << lua_tostring(_L, -1) << std::endl;
35  lua_pop(_L, 1);
36  }
37 
38  _pluginManager.loadPlugins();
39 }
40 
42  QObject* sender,
43  std::string signalName,
44  LuaIntf::LuaRef slot)
45 {
46  int signalId = sender->metaObject()->indexOfSignal(signalName.c_str());
47 
48  if(signalId < 0) {
49  std::cout << "No such signal " << signalName << std::endl;
50  }
51  else {
52  auto lqo = std::make_shared<LuaQObject>(sender);
53  _luaQObjects.push_back(lqo);
54 
55  auto connected = lqo->connect(signalId, slot);
56 
58 
59  return connected;
60  }
61 
62  return false;
63 }
64 
65 std::shared_ptr<QWidget> LuaInterface::loadUiFile(const char* fileName) {
66  QUiLoader uiLoader;
67  QFile file(fileName);
68  file.open(QFile::ReadOnly);
69 
70  std::shared_ptr<QWidget> widget(uiLoader.load(&file));
71 
72  file.close();
73 
74  return widget;
75 }
76 
78  _luaQObjects.erase(std::remove_if(_luaQObjects.begin(),
79  _luaQObjects.end(),
80  [](LuaQObject_SPtr lqo){
81  return !lqo->valid();
82  }),
83  _luaQObjects.end());
84 }
85 
86 bool LuaInterface::qtConnect(QObject *sender, std::string signalName, QObject *receiver, std::string slotName) {
87  int signalId = sender->metaObject()->indexOfSignal(signalName.c_str());
88  if(signalId < 0) {
89  std::cout << "No such signal " << signalName << std::endl;
90  }
91 
92  int slotId = receiver->metaObject()->indexOfSlot(slotName.c_str());
93  if(slotId < 0) {
94  std::cout << "No such slot " << slotName << std::endl;
95  }
96 
97  return QMetaObject::connect(sender, signalId, receiver, slotId);
98 }
99 
100 void LuaInterface::hideUI(bool hidden) {
101  LuaIntf::Lua::setGlobal(_L, "hideUI", hidden);
102 }
103 
104 LuaIntf::LuaState LuaInterface::luaState() {
105  return _L;
106 }
107 
108 std::vector<std::string> LuaInterface::pluginList(const char* path) {
109  std::vector<std::string> plugins;
110  QDir dir(path);
111 
112  auto list = dir.entryList(QDir::Filter::Dirs | QDir::Filter::NoDotAndDotDot);
113  for(auto dir : list) {
114  plugins.push_back(dir.toStdString());
115  }
116 
117  return plugins;
118 }
119 
120 FILE* LuaInterface::openFileDialog(bool isOpening, const char* description, const char* mode) {
121  QString path;
122 
123  if(isOpening) {
124  path = QFileDialog::getOpenFileName(nullptr, (std::string("Open ") + description).c_str());
125  }
126  else {
127  path = QFileDialog::getSaveFileName(nullptr, (std::string("Save ") + description).c_str());
128  }
129 
130  if(path.isEmpty()) {
131  return nullptr;
132  }
133 
134  return fopen(path.toStdString().c_str(), mode);
135 }
136 
137 LuaIntf::LuaRef LuaInterface::operation(unsigned int windowID) {
138  if(_operations.find(windowID) != _operations.end()) {
139  return _operations[windowID];
140  }
141 
142  return LuaIntf::LuaRef();
143 }
144 
145 void LuaInterface::setOperation(unsigned int windowID, LuaIntf::LuaRef operation) {
146  _operations[windowID] = operation;
147 }
148 
149 void LuaInterface::registerEvent(const std::string& event, LuaIntf::LuaRef callback) {
150  if(callback.isTable() && !callback.has("onEvent")) {
151  return;
152  }
153 
154  _events[event].push_back(callback);
155 }
156 
157 void LuaInterface::deleteEvent(const std::string& event, LuaIntf::LuaRef callback) {
158  auto it = std::find(_events[event].begin(), _events[event].end(), callback);
159 
160  if(it != _events[event].end()) {
161  _events[event].erase(it);
162  }
163 }
164 
165 void LuaInterface::triggerEvent(const std::string& event, LuaIntf::LuaRef args) {
166  auto events = _events[event];
167  for(auto eventCallback : events) {
168  if(eventCallback.isFunction()) {
169  eventCallback(event, args);
170  }
171  else if(eventCallback.isTable()) {
172  eventCallback.get("onEvent").call(eventCallback, event, args);
173  }
174  }
175 }
lc::PluginManager _pluginManager
Definition: luainterface.h:109
static FILE * openFileDialog(bool isOpening, const char *description, const char *mode)
void luaOpenQtBridge(lua_State *L)
Definition: qtbridge.cpp:10
static std::shared_ptr< QWidget > loadUiFile(const char *fileName)
Load Qt widget from .ui file.
LuaIntf::LuaRef operation(unsigned int windowID)
void cleanInvalidQObject()
Remove all connections that aren't valid anymore.
LuaIntf::LuaState luaState()
Returns current Lua state. This is used for unit tests.
bool luaConnect(QObject *sender, std::string signalName, LuaIntf::LuaRef slot)
Connect Qt signal with Lua function.
LuaIntf::LuaState _L
Definition: luainterface.h:107
LuaInterface()
Create Lua instance.
Definition: luainterface.cpp:5
std::vector< LuaQObject_SPtr > _luaQObjects
Definition: luainterface.h:108
void triggerEvent(const std::string &event, LuaIntf::LuaRef args)
bool qtConnect(QObject *sender, std::string signalName, QObject *receiver, std::string slotName)
Connect Qt signal with Qt slot.
void setOperation(unsigned int windowID, LuaIntf::LuaRef)
void initLua()
Read and execute Lua files.
void registerEvent(const std::string &event, LuaIntf::LuaRef callback)
std::map< unsigned int, LuaIntf::LuaRef > _operations
Definition: luainterface.h:110
void deleteEvent(const std::string &event, LuaIntf::LuaRef callback)
std::map< std::string, std::vector< LuaIntf::LuaRef > > _events
Definition: luainterface.h:111
std::vector< std::string > pluginList(const char *path)
Return a list of plugins.
void hideUI(bool hidden)
Hide the window. It needs to be used before initLua(), this is used in unit tests.