-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluascriptcontroller.cpp
More file actions
262 lines (211 loc) · 8.97 KB
/
luascriptcontroller.cpp
File metadata and controls
262 lines (211 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "luascriptcontroller.h"
#include "luascriptadapter.h"
#include "model/luaeditormodelitem.h"
#include "model/apimodel.h"
#include <QDebug>
LuaScriptController::LuaScriptController(QQmlApplicationEngine* qmlEngine, QSharedPointer<LuaScriptAdapter> ptrLuaScriptAdapter, QObject* parent)
: QObject{parent},
qmlEngine(qmlEngine),
ptrLuaScriptAdapter(ptrLuaScriptAdapter),
luaScriptQmlAdapter(LuaScriptQmlAdapter::instance()),
luaEditorModel(LuaEditorModel::instance()),
luaScriptQmlComponent(Q_NULLPTR),
luaEditorContainer(Q_NULLPTR)
{
connect(luaEditorModel, &LuaEditorModel::signal_requestAddLuaScript, this, &LuaScriptController::slot_createLuaScript);
connect(luaEditorModel, &LuaEditorModel::signal_requestRemoveLuaScript, this, &LuaScriptController::slot_removeLuaScript);
connect(luaEditorModel, &LuaEditorModel::signal_requestSaveLuaScript, this, &LuaScriptController::slot_saveLuaScript);
connect(ptrLuaScriptAdapter.data(), &LuaScriptAdapter::signal_luaScriptSaved, this, &LuaScriptController::slot_luaScriptSaved);
connect(this->luaScriptQmlAdapter, &LuaScriptQmlAdapter::signal_requestSyntaxCheck, ptrLuaScriptAdapter.data(), &LuaScriptAdapter::checkSyntax);
connect(ptrLuaScriptAdapter.data(), &LuaScriptAdapter::signal_luaApiPrepareInitial, this, &LuaScriptController::prepareLuaApi);
connect(this->luaScriptQmlAdapter, &LuaScriptQmlAdapter::signal_requestSetLuaApi, this, &LuaScriptController::prepareLuaApi);
// Connecting backend response to QML
connect(ptrLuaScriptAdapter.data(), &LuaScriptAdapter::signal_syntaxCheckResult, this->luaScriptQmlAdapter, &LuaScriptQmlAdapter::syntaxCheckResult);
connect(ptrLuaScriptAdapter.data(), &LuaScriptAdapter::signal_runtimeError, this->luaScriptQmlAdapter, &LuaScriptQmlAdapter::signal_runtimeErrorResult);
connect(ptrLuaScriptAdapter.data(), &LuaScriptAdapter::signal_luaApiPrepareResult, this->luaScriptQmlAdapter, &LuaScriptQmlAdapter::luaApiPreparationResult);
}
LuaScriptController::~LuaScriptController()
{
// Removes child pin qml objects first (don't make the lose their parent!)
for (const auto& luaEditorQml : this->luaEditorQmls)
{
luaEditorQml->deleteLater();
}
this->luaEditorQmls.clear();
}
void LuaScriptController::slot_createLuaScript(const QString& filePathName)
{
if (Q_NULLPTR == this->ptrLuaScriptAdapter)
{
return;
}
QString tempFilePathName = filePathName;
tempFilePathName = tempFilePathName.replace("\\", "/");
// Try creating a node model and associate it with backendmodel, let it be filled and configured
QPair<int, LuaEditorModelItem*> resultData = this->ptrLuaScriptAdapter->createLuaScript(tempFilePathName);
LuaEditorModelItem* luaEditorModelItem = resultData.second;
if (Q_NULLPTR == luaEditorModelItem)
{
if (resultData.first >= 0)
{
// There is already this lua script, so send event with index to set current tab.
this->luaScriptQmlAdapter->changeTab(resultData.first);
}
return;
}
QQmlEngine::setObjectOwnership(luaEditorModel, QQmlEngine::CppOwnership);
LuaEditorQml* luaEditorQml = this->createNewLuaEditorQml();
if (Q_NULLPTR == luaEditorQml)
{
return;
}
this->luaEditorQmls.append(luaEditorQml);
// this->luaEditorContainer->setProperty("currentIndex", this->luaEditorQmls.size());
// luaEditorQml->setProperty("filePathName", tempFilePathName);
luaEditorQml->setModel(luaEditorModelItem);
// connect(luaEditorQml, &LuaEditorQml::currentTextChanged, ApiModel::instance(), &ApiModel::onCurrentTextChanged);
this->luaEditorModel->addLuaScript(luaEditorModelItem);
// Sets the new script as active one
this->luaScriptQmlAdapter->changeTab(this->luaEditorModel->getSize() - 1);
}
void LuaScriptController::slot_removeLuaScript(int index)
{
LuaEditorQml* luaEditorQml = Q_NULLPTR;
#if 0
for (const auto& currentLuaEditorQml : this->luaEditorQmls)
{
if (currentLuaEditorQml->property("filePathName") == filePathName)
{
luaEditorQml = currentLuaEditorQml;
break;
}
}
#endif
luaEditorQml = this->luaEditorQmls.at(index);
if (Q_NULLPTR == luaEditorQml)
{
qDebug() << "Error on removing lua script";
return;
}
LuaEditorModelItem* luaEditorModelItem = luaEditorQml->model();
bool success = this->ptrLuaScriptAdapter->removeLuaScript(luaEditorModelItem->getFilePathName());
if (false == success)
{
qDebug() << "Error on removing lua script for file path name: " << luaEditorModelItem->getFilePathName();
return;
}
luaEditorQml->deleteLater();
this->luaEditorQmls.removeOne(luaEditorQml);
this->luaEditorModel->removeLuaScript(luaEditorModelItem);
}
void LuaScriptController::slot_saveLuaScript(const QString& filePathName, const QString& content)
{
bool success = this->ptrLuaScriptAdapter->saveLuaScript(filePathName, content);
if (false == success)
{
qDebug() << "Error on saving lua script for file path name: " << filePathName;
return;
}
}
void LuaScriptController::slot_luaScriptSaved()
{
this->luaEditorModel->luaScriptSaved();
}
void LuaScriptController::slot_sendLuaScriptError(const QString& filePathName, const QString& errorMessage, int line, int start, int end)
{
// If there is no such lua script so far, create it first and then send the error
bool success = this->ptrLuaScriptAdapter->sendLuaScriptRuntimeError(filePathName, errorMessage, line, start, end);
if (false == success)
{
this->slot_createLuaScript(filePathName);
this->ptrLuaScriptAdapter->sendLuaScriptRuntimeError(filePathName, errorMessage, line, start, end);
}
}
void LuaScriptController::prepareLuaApi(const QString& filePathName, bool parseSilent)
{
const auto& apiData = this->ptrLuaScriptAdapter->prepareLuaApi(filePathName, parseSilent);
if (true == apiData.empty())
{
qDebug() << "Error parsing lua api for file path name: " << filePathName;
return;
}
ApiModel::instance()->setApiData(apiData);
}
LuaEditorQml* LuaScriptController::createNewLuaEditorQml(void)
{
if (Q_NULLPTR == this->luaScriptQmlComponent)
{
if (Q_NULLPTR == this->qmlEngine)
{
return Q_NULLPTR;
}
if (true == this->qmlEngine->rootObjects().isEmpty())
{
return Q_NULLPTR;
}
this->luaScriptQmlComponent = new QQmlComponent(this->qmlEngine, QUrl(QStringLiteral("qrc:/qml_files/LuaEditor.qml")), this);
}
// Find the root object (the ApplicationWindow)
this->luaEditorContainer = this->qmlEngine->rootObjects()[0]->findChild<QQuickItem*>("luaEditorContainer");
if (Q_NULLPTR == this->luaEditorContainer)
{
qWarning() << "Could not find luaEditorContainer!";
return Q_NULLPTR;
}
else
{
qDebug() << "luaEditorContainer found!";
}
// Initialize node qml item
LuaEditorQml* luaEditorQml;
QObject* object = this->luaScriptQmlComponent->create();
luaEditorQml = qobject_cast<LuaEditorQml*>(object);
if (Q_NULLPTR != luaEditorQml)
{
QQmlEngine::setObjectOwnership(luaEditorQml, QQmlEngine::CppOwnership);
}
else
{
qDebug() << "Error on creating LuaEditorQml";
for (auto& error : this->luaScriptQmlComponent->errors())
{
qDebug() << error.description();
}
return Q_NULLPTR;
}
luaEditorQml->setParentItem(this->luaEditorContainer);
luaEditorQml->setParent(this->qmlEngine);
luaEditorQml->setWidth(this->luaEditorContainer->width() - 12);
// luaEditorQml->setHeight(1000.0f);
return luaEditorQml;
}
QQuickItem *LuaScriptController::findChildRecursive(QObject* parent, const QString& childName)
{
// Check if the current parent is valid
if (Q_NULLPTR == parent)
{
return Q_NULLPTR;
}
// Iterate through all children of the parent
for (QObject* child : parent->children())
{
QQuickItem* childItem = qobject_cast<QQuickItem*>(child);
if (childItem)
{
// Print the current child name for debugging
qDebug() << "Checking child:" << childItem->objectName() << ", type:" << childItem->metaObject()->className();
// Check if the child matches the desired name
if (childItem->objectName() == childName)
{
return childItem;
}
// Recursively search in the child
QQuickItem* foundChild = this->findChildRecursive(childItem, childName);
if (Q_NULLPTR != foundChild)
{
return foundChild;
}
}
}
return Q_NULLPTR;
}