Skip to content

Commit

Permalink
Various changes to support use and loading of multiple scripts in the
Browse files Browse the repository at this point in the history
scripting system.
  • Loading branch information
collin80 committed Sep 1, 2017
1 parent 290af20 commit 4b61ea2
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 44 deletions.
35 changes: 16 additions & 19 deletions scriptcontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,23 @@ ScriptContainer::ScriptContainer()
canHelper = new CANScriptHelper(&scriptEngine);
isoHelper = new ISOTPScriptHelper(&scriptEngine);
udsHelper = new UDSScriptHelper(&scriptEngine);
elapsedTime.start();
connect(&timer, SIGNAL(timeout()), this, SLOT(tick()));
}

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
{
Expand Down Expand Up @@ -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)
Expand All @@ -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());
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions scriptcontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <QTimer>
#include <qlistwidget.h>

class ScriptingWindow;

class CANScriptHelper: public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -75,7 +77,7 @@ class ScriptContainer : public QObject

public:
ScriptContainer();
void setLogWidget(QListWidget *list);
void setScriptWindow(ScriptingWindow *win);

QString fileName;
QString filePath;
Expand All @@ -86,6 +88,9 @@ public slots:
void setTickInterval(QJSValue interval);
void log(QJSValue logString);

signals:
void sendLog(QString text);

private slots:
void tick();

Expand All @@ -95,11 +100,10 @@ private slots:
QJSValue setupFunction;
QJSValue tickFunction;
QTimer timer;
QListWidget *logWidget;
ScriptingWindow *window;
CANScriptHelper *canHelper;
ISOTPScriptHelper *isoHelper;
UDSScriptHelper *udsHelper;
QElapsedTimer elapsedTime;
};

#endif // SCRIPTCONTAINER_H
40 changes: 35 additions & 5 deletions scriptingwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions scriptingwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class ScriptingWindow : public QDialog
void showEvent(QShowEvent*);
~ScriptingWindow();

public slots:
void log(QString text);

private slots:
void loadNewScript();
void createNewScript();
Expand All @@ -32,6 +35,7 @@ private slots:
void saveScript();
void revertScript();
void recompileScript();
void changeCurrentScript();
void newFrames(const CANConnection*, const QVector<CANFrame>&);
void clickedLogClear();

Expand All @@ -45,6 +49,7 @@ private slots:
QList<ScriptContainer *> scripts;
ScriptContainer *currentScript;
const QVector<CANFrame> *modelFrames;
QElapsedTimer elapsedTime;
};

#endif // SCRIPTINGWINDOW_H
58 changes: 41 additions & 17 deletions ui/scriptingwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,64 @@
<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">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Loaded Scripts</string>
<string>Public Variables:</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="listLoadedScripts"/>
<widget class="QTableWidget" name="tableVariables"/>
</item>
<item>
<widget class="QPushButton" name="btnLoadScript">
<widget class="QLabel" name="label">
<property name="text">
<string>Load New Script</string>
<string>Loaded Scripts</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnNewScript">
<property name="text">
<string>Create New Script</string>
</property>
</widget>
<widget class="QListWidget" name="listLoadedScripts"/>
</item>
<item>
<widget class="QPushButton" name="btnRemoveScript">
<property name="text">
<string>Remove Current Script</string>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="btnNewScript">
<property name="text">
<string>New</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnRemoveScript">
<property name="text">
<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">
Expand Down Expand Up @@ -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">
Expand Down

0 comments on commit 4b61ea2

Please sign in to comment.