Add ability to set script parameters that can be changed and monitored
in realtime from SavvyCAN.
This commit is contained in:
@@ -102,6 +102,60 @@ void ScriptContainer::tick()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptContainer::addParameter(QJSValue name)
|
||||||
|
{
|
||||||
|
scriptParams.append(name.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptContainer::updateValuesTable(QTableWidget *widget)
|
||||||
|
{
|
||||||
|
QString valu;
|
||||||
|
|
||||||
|
if (widget->columnCount() == 0)
|
||||||
|
{
|
||||||
|
widget->insertColumn(0);
|
||||||
|
widget->insertColumn(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (QString paramName, scriptParams)
|
||||||
|
{
|
||||||
|
valu = scriptEngine.globalObject().property(paramName).toString();
|
||||||
|
qDebug() << paramName << " - " << valu;
|
||||||
|
bool found = false;
|
||||||
|
for (int i = 0; i < widget->rowCount(); i++)
|
||||||
|
{
|
||||||
|
if (widget->item(i, 0)->text().compare(paramName) == 0)
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
if (!widget->item(i, 1)->isSelected())
|
||||||
|
{
|
||||||
|
widget->item(i,1)->setText(valu);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
int row = widget->rowCount();
|
||||||
|
widget->insertRow(widget->rowCount());
|
||||||
|
QTableWidgetItem *item;
|
||||||
|
item = new QTableWidgetItem();
|
||||||
|
item->setText(paramName);
|
||||||
|
item->setFlags(Qt::ItemIsEnabled);
|
||||||
|
widget->setItem(row, 0, item);
|
||||||
|
item = new QTableWidgetItem();
|
||||||
|
item->setText(valu);
|
||||||
|
widget->setItem(row, 1, item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptContainer::updateParameter(QString name, QString value)
|
||||||
|
{
|
||||||
|
qDebug() << name << " * " << value;
|
||||||
|
QJSValue val(value);
|
||||||
|
scriptEngine.globalObject().setProperty(name, val);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -83,10 +83,17 @@ public:
|
|||||||
QString filePath;
|
QString filePath;
|
||||||
QString scriptText;
|
QString scriptText;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Anything registered as a public slot on this class and that takes either no arguments
|
||||||
|
or only QJSValue arguments will be callable from scripts via host.XXX calls. So, have at it, champ.
|
||||||
|
*/
|
||||||
public slots:
|
public slots:
|
||||||
void compileScript();
|
void compileScript();
|
||||||
void setTickInterval(QJSValue interval);
|
void setTickInterval(QJSValue interval);
|
||||||
void log(QJSValue logString);
|
void log(QJSValue logString);
|
||||||
|
void addParameter(QJSValue name);
|
||||||
|
void updateValuesTable(QTableWidget *widget);
|
||||||
|
void updateParameter(QString name, QString value);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void sendLog(QString text);
|
void sendLog(QString text);
|
||||||
@@ -104,6 +111,7 @@ private:
|
|||||||
CANScriptHelper *canHelper;
|
CANScriptHelper *canHelper;
|
||||||
ISOTPScriptHelper *isoHelper;
|
ISOTPScriptHelper *isoHelper;
|
||||||
UDSScriptHelper *udsHelper;
|
UDSScriptHelper *udsHelper;
|
||||||
|
QVector<QString> scriptParams;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SCRIPTCONTAINER_H
|
#endif // SCRIPTCONTAINER_H
|
||||||
|
|||||||
+36
-1
@@ -33,12 +33,16 @@ ScriptingWindow::ScriptingWindow(const QVector<CANFrame> *frames, QWidget *paren
|
|||||||
connect(ui->btnSaveScript, &QAbstractButton::pressed, this, &ScriptingWindow::saveScript);
|
connect(ui->btnSaveScript, &QAbstractButton::pressed, this, &ScriptingWindow::saveScript);
|
||||||
connect(ui->btnClearLog, &QAbstractButton::pressed, this, &ScriptingWindow::clickedLogClear);
|
connect(ui->btnClearLog, &QAbstractButton::pressed, this, &ScriptingWindow::clickedLogClear);
|
||||||
connect(ui->listLoadedScripts, &QListWidget::currentRowChanged, this, &ScriptingWindow::changeCurrentScript);
|
connect(ui->listLoadedScripts, &QListWidget::currentRowChanged, this, &ScriptingWindow::changeCurrentScript);
|
||||||
|
connect(ui->tableVariables, SIGNAL(cellChanged(int,int)), this, SLOT(updatedValue(int, int)));
|
||||||
|
|
||||||
connect(CANConManager::getInstance(), &CANConManager::framesReceived, this, &ScriptingWindow::newFrames);
|
connect(CANConManager::getInstance(), &CANConManager::framesReceived, this, &ScriptingWindow::newFrames);
|
||||||
|
|
||||||
|
connect(&valuesTimer, SIGNAL(timeout()), this, SLOT(valuesTimerElapsed()));
|
||||||
|
|
||||||
currentScript = NULL;
|
currentScript = NULL;
|
||||||
|
|
||||||
elapsedTime.start();
|
elapsedTime.start();
|
||||||
|
valuesTimer.start(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptingWindow::~ScriptingWindow()
|
ScriptingWindow::~ScriptingWindow()
|
||||||
@@ -62,6 +66,22 @@ void ScriptingWindow::newFrames(const CANConnection* pConn, const QVector<CANFra
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptingWindow::updatedValue(int row, int col)
|
||||||
|
{
|
||||||
|
QTableWidgetItem *nameItem = ui->tableVariables->item(row, 0);
|
||||||
|
QTableWidgetItem *valItem = ui->tableVariables->item(row, 1);
|
||||||
|
|
||||||
|
if (!valItem) return;
|
||||||
|
if (col == 0) return;
|
||||||
|
if (!valItem->isSelected()) return; // don't record updates not from a user edited cell
|
||||||
|
|
||||||
|
if (nameItem && valItem)
|
||||||
|
{
|
||||||
|
QString name = nameItem->text();
|
||||||
|
QString val = valItem->text();
|
||||||
|
emit updatedParameter(name, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptingWindow::closeEvent(QCloseEvent *event)
|
void ScriptingWindow::closeEvent(QCloseEvent *event)
|
||||||
{
|
{
|
||||||
@@ -100,12 +120,27 @@ void ScriptingWindow::changeCurrentScript()
|
|||||||
int sel = ui->listLoadedScripts->currentRow();
|
int sel = ui->listLoadedScripts->currentRow();
|
||||||
if (sel < 0) return;
|
if (sel < 0) return;
|
||||||
|
|
||||||
if (currentScript) currentScript->scriptText = editor->toPlainText();
|
if (currentScript) {
|
||||||
|
currentScript->scriptText = editor->toPlainText();
|
||||||
|
disconnect(this, SIGNAL(updateValueTable(QTableWidget*)), currentScript, SLOT(updateValuesTable(QTableWidget*)));
|
||||||
|
disconnect(this, SIGNAL(updatedParameter(QString,QString)), currentScript, SLOT(updateParameter(QString,QString)));
|
||||||
|
}
|
||||||
|
|
||||||
ScriptContainer *container = scripts.at(sel);
|
ScriptContainer *container = scripts.at(sel);
|
||||||
currentScript = container;
|
currentScript = container;
|
||||||
editor->setPlainText(container->scriptText);
|
editor->setPlainText(container->scriptText);
|
||||||
editor->setEnabled(true);
|
editor->setEnabled(true);
|
||||||
|
ui->tableVariables->clear();
|
||||||
|
connect(this, SIGNAL(updateValueTable(QTableWidget*)), currentScript, SLOT(updateValuesTable(QTableWidget*)));
|
||||||
|
connect(this, SIGNAL(updatedParameter(QString,QString)), currentScript, SLOT(updateParameter(QString,QString)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScriptingWindow::valuesTimerElapsed()
|
||||||
|
{
|
||||||
|
if (currentScript)
|
||||||
|
{
|
||||||
|
emit updateValueTable(ui->tableVariables);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ScriptingWindow::loadNewScript()
|
void ScriptingWindow::loadNewScript()
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ public:
|
|||||||
public slots:
|
public slots:
|
||||||
void log(QString text);
|
void log(QString text);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void updateValueTable(QTableWidget *widget);
|
||||||
|
void updatedParameter(QString name, QString value);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void loadNewScript();
|
void loadNewScript();
|
||||||
void createNewScript();
|
void createNewScript();
|
||||||
@@ -38,6 +42,8 @@ private slots:
|
|||||||
void changeCurrentScript();
|
void changeCurrentScript();
|
||||||
void newFrames(const CANConnection*, const QVector<CANFrame>&);
|
void newFrames(const CANConnection*, const QVector<CANFrame>&);
|
||||||
void clickedLogClear();
|
void clickedLogClear();
|
||||||
|
void valuesTimerElapsed();
|
||||||
|
void updatedValue(int row, int col);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void closeEvent(QCloseEvent *event);
|
void closeEvent(QCloseEvent *event);
|
||||||
@@ -50,6 +56,7 @@ private:
|
|||||||
ScriptContainer *currentScript;
|
ScriptContainer *currentScript;
|
||||||
const QVector<CANFrame> *modelFrames;
|
const QVector<CANFrame> *modelFrames;
|
||||||
QElapsedTimer elapsedTime;
|
QElapsedTimer elapsedTime;
|
||||||
|
QTimer valuesTimer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SCRIPTINGWINDOW_H
|
#endif // SCRIPTINGWINDOW_H
|
||||||
|
|||||||
Reference in New Issue
Block a user