Various changes to support use and loading of multiple scripts in the
scripting system.
This commit is contained in:
+16
-19
@@ -9,7 +9,6 @@ ScriptContainer::ScriptContainer()
|
||||
canHelper = new CANScriptHelper(&scriptEngine);
|
||||
isoHelper = new ISOTPScriptHelper(&scriptEngine);
|
||||
udsHelper = new UDSScriptHelper(&scriptEngine);
|
||||
elapsedTime.start();
|
||||
connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
|
||||
}
|
||||
|
||||
@@ -17,19 +16,16 @@ void ScriptContainer::compileScript()
|
||||
{
|
||||
QJSValue result = scriptEngine.evaluate(scriptText, fileName);
|
||||
|
||||
if (logWidget)
|
||||
{
|
||||
logWidget->addItem("");
|
||||
logWidget->addItem("Compiling script " + fileName);
|
||||
}
|
||||
emit sendLog("Compiling script...");
|
||||
|
||||
if (result.isError() && logWidget)
|
||||
if (result.isError())
|
||||
{
|
||||
logWidget->addItem("SCRIPT EXCEPTION!");
|
||||
logWidget->addItem("Line: " + result.property("lineNumber").toString());
|
||||
logWidget->addItem(result.property("message").toString());
|
||||
logWidget->addItem("Stack:");
|
||||
logWidget->addItem(result.property("stack").toString());
|
||||
|
||||
emit sendLog("SCRIPT EXCEPTION!");
|
||||
emit sendLog("Line: " + result.property("lineNumber").toString());
|
||||
emit sendLog(result.property("message").toString());
|
||||
emit sendLog("Stack:");
|
||||
emit sendLog(result.property("stack").toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -60,23 +56,24 @@ void ScriptContainer::compileScript()
|
||||
QJSValue res = setupFunction.call();
|
||||
if (res.isError())
|
||||
{
|
||||
logWidget->addItem("Error in setup function on line " + res.property("lineNumber").toString());
|
||||
logWidget->addItem(res.property("message").toString());
|
||||
emit sendLog("Error in setup function on line " + res.property("lineNumber").toString());
|
||||
emit sendLog(res.property("message").toString());
|
||||
}
|
||||
}
|
||||
if (tickFunction.isCallable()) qDebug() << "tick exists";
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptContainer::setLogWidget(QListWidget *list)
|
||||
void ScriptContainer::setScriptWindow(ScriptingWindow *win)
|
||||
{
|
||||
logWidget = list;
|
||||
window = win;
|
||||
connect(this, &ScriptContainer::sendLog, window, &ScriptingWindow::log);
|
||||
}
|
||||
|
||||
void ScriptContainer::log(QJSValue logString)
|
||||
{
|
||||
QString val = logString.toString();
|
||||
logWidget->addItem(QString::number(elapsedTime.elapsed()) + ": " + val);
|
||||
emit sendLog(val);
|
||||
}
|
||||
|
||||
void ScriptContainer::setTickInterval(QJSValue interval)
|
||||
@@ -99,8 +96,8 @@ void ScriptContainer::tick()
|
||||
QJSValue res = tickFunction.call();
|
||||
if (res.isError())
|
||||
{
|
||||
logWidget->addItem("Error in tick function on line " + res.property("lineNumber").toString());
|
||||
logWidget->addItem(res.property("message").toString());
|
||||
emit sendLog("Error in tick function on line " + res.property("lineNumber").toString());
|
||||
emit sendLog(res.property("message").toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-3
@@ -12,6 +12,8 @@
|
||||
#include <QTimer>
|
||||
#include <qlistwidget.h>
|
||||
|
||||
class ScriptingWindow;
|
||||
|
||||
class CANScriptHelper: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -75,7 +77,7 @@ class ScriptContainer : public QObject
|
||||
|
||||
public:
|
||||
ScriptContainer();
|
||||
void setLogWidget(QListWidget *list);
|
||||
void setScriptWindow(ScriptingWindow *win);
|
||||
|
||||
QString fileName;
|
||||
QString filePath;
|
||||
@@ -86,6 +88,9 @@ public slots:
|
||||
void setTickInterval(QJSValue interval);
|
||||
void log(QJSValue logString);
|
||||
|
||||
signals:
|
||||
void sendLog(QString text);
|
||||
|
||||
private slots:
|
||||
void tick();
|
||||
|
||||
@@ -95,11 +100,10 @@ private:
|
||||
QJSValue setupFunction;
|
||||
QJSValue tickFunction;
|
||||
QTimer timer;
|
||||
QListWidget *logWidget;
|
||||
ScriptingWindow *window;
|
||||
CANScriptHelper *canHelper;
|
||||
ISOTPScriptHelper *isoHelper;
|
||||
UDSScriptHelper *udsHelper;
|
||||
QElapsedTimer elapsedTime;
|
||||
};
|
||||
|
||||
#endif // SCRIPTCONTAINER_H
|
||||
|
||||
+35
-5
@@ -32,9 +32,11 @@ ScriptingWindow::ScriptingWindow(const QVector<CANFrame> *frames, QWidget *paren
|
||||
connect(ui->btnRevertScript, &QAbstractButton::pressed, this, &ScriptingWindow::revertScript);
|
||||
connect(ui->btnSaveScript, &QAbstractButton::pressed, this, &ScriptingWindow::saveScript);
|
||||
connect(ui->btnClearLog, &QAbstractButton::pressed, this, &ScriptingWindow::clickedLogClear);
|
||||
connect(ui->listLoadedScripts, &QListWidget::currentRowChanged, this, &ScriptingWindow::changeCurrentScript);
|
||||
|
||||
connect(CANConManager::getInstance(), &CANConManager::framesReceived, this, &ScriptingWindow::newFrames);
|
||||
|
||||
elapsedTime.start();
|
||||
}
|
||||
|
||||
ScriptingWindow::~ScriptingWindow()
|
||||
@@ -91,6 +93,19 @@ void ScriptingWindow::showEvent(QShowEvent* event)
|
||||
QDialog::showEvent(event);
|
||||
}
|
||||
|
||||
void ScriptingWindow::changeCurrentScript()
|
||||
{
|
||||
int sel = ui->listLoadedScripts->currentRow();
|
||||
if (sel < 0) return;
|
||||
|
||||
if (currentScript) currentScript->scriptText = editor->toPlainText();
|
||||
|
||||
ScriptContainer *container = scripts.at(sel);
|
||||
currentScript = container;
|
||||
editor->setPlainText(container->scriptText);
|
||||
editor->setEnabled(true);
|
||||
}
|
||||
|
||||
void ScriptingWindow::loadNewScript()
|
||||
{
|
||||
QString filename;
|
||||
@@ -124,12 +139,12 @@ void ScriptingWindow::loadNewScript()
|
||||
container->fileName = justFileName;
|
||||
container->filePath = filename;
|
||||
container->scriptText = contents;
|
||||
container->setLogWidget(ui->listLog);
|
||||
container->setScriptWindow(this);
|
||||
container->compileScript();
|
||||
scripts.append(container);
|
||||
currentScript = container;
|
||||
editor->setPlainText(container->scriptText);
|
||||
editor->setEnabled(true);
|
||||
|
||||
ui->listLoadedScripts->setCurrentRow(ui->listLoadedScripts->count() - 1);
|
||||
changeCurrentScript();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,7 +159,7 @@ void ScriptingWindow::createNewScript()
|
||||
container->fileName = "UNNAMED_" + QString::number((qrand() % 10000)) + ".js";
|
||||
container->filePath = QString();
|
||||
container->scriptText = QString();
|
||||
container->setLogWidget(ui->listLog);
|
||||
container->setScriptWindow(this);
|
||||
scripts.append(container);
|
||||
ui->listLoadedScripts->addItem(container->fileName);
|
||||
currentScript = container;
|
||||
@@ -209,4 +224,19 @@ void ScriptingWindow::recompileScript()
|
||||
void ScriptingWindow::clickedLogClear()
|
||||
{
|
||||
ui->listLog->clear();
|
||||
elapsedTime.start();
|
||||
}
|
||||
|
||||
void ScriptingWindow::log(QString text)
|
||||
{
|
||||
ScriptContainer *cont = qobject_cast<ScriptContainer*>(sender());
|
||||
if (cont != NULL)
|
||||
ui->listLog->addItem(QString::number(elapsedTime.elapsed()) + "(" + cont->fileName + "): " + text);
|
||||
else
|
||||
ui->listLog->addItem(QString::number(elapsedTime.elapsed()) + ": " + text);
|
||||
|
||||
if (ui->cbAutoScroll->isChecked())
|
||||
{
|
||||
ui->listLog->scrollToBottom();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,9 @@ public:
|
||||
void showEvent(QShowEvent*);
|
||||
~ScriptingWindow();
|
||||
|
||||
public slots:
|
||||
void log(QString text);
|
||||
|
||||
private slots:
|
||||
void loadNewScript();
|
||||
void createNewScript();
|
||||
@@ -32,6 +35,7 @@ private slots:
|
||||
void saveScript();
|
||||
void revertScript();
|
||||
void recompileScript();
|
||||
void changeCurrentScript();
|
||||
void newFrames(const CANConnection*, const QVector<CANFrame>&);
|
||||
void clickedLogClear();
|
||||
|
||||
@@ -45,6 +49,7 @@ private:
|
||||
QList<ScriptContainer *> scripts;
|
||||
ScriptContainer *currentScript;
|
||||
const QVector<CANFrame> *modelFrames;
|
||||
QElapsedTimer elapsedTime;
|
||||
};
|
||||
|
||||
#endif // SCRIPTINGWINDOW_H
|
||||
|
||||
+34
-10
@@ -13,9 +13,22 @@
|
||||
<property name="windowTitle">
|
||||
<string>Scripting Interface</string>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,4">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,4">
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_4">
|
||||
<property name="text">
|
||||
<string>Public Variables:</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTableWidget" name="tableVariables"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
@@ -30,30 +43,34 @@
|
||||
<widget class="QListWidget" name="listLoadedScripts"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnLoadScript">
|
||||
<property name="text">
|
||||
<string>Load New Script</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnNewScript">
|
||||
<property name="text">
|
||||
<string>Create New Script</string>
|
||||
<string>New</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnRemoveScript">
|
||||
<property name="text">
|
||||
<string>Remove Current Script</string>
|
||||
<string>Del</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnLoadScript">
|
||||
<property name="text">
|
||||
<string>Load</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,0,0,4,0">
|
||||
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,0,0,4,0,0">
|
||||
<item>
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
@@ -102,6 +119,13 @@
|
||||
<item>
|
||||
<widget class="QListWidget" name="listLog"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QCheckBox" name="cbAutoScroll">
|
||||
<property name="text">
|
||||
<string>Auto Scroll Log Window</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnClearLog">
|
||||
<property name="text">
|
||||
|
||||
Reference in New Issue
Block a user