forked from collin80/SavvyCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptingwindow.cpp
379 lines (320 loc) · 11.4 KB
/
scriptingwindow.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "scriptingwindow.h"
#include "ui_scriptingwindow.h"
#include <QFile>
#include <QFileDialog>
#include <QSettings>
#include "connections/canconmanager.h"
#include "helpwindow.h"
ScriptingWindow::ScriptingWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent),
ui(new Ui::ScriptingWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::Window);
editor = new JSEdit();
editor->setFrameShape(JSEdit::NoFrame);
editor->setWordWrapMode(QTextOption::NoWrap);
editor->setEnabled(false);
editor->setFont(QFont("Monospace", 12));
editor->show();
//Show whitespaces
QTextOption option = editor->document()->defaultTextOption();
option.setFlags(QTextOption::ShowTabsAndSpaces);
editor->document()->setDefaultTextOption(option);
ui->verticalLayout->insertWidget(2,editor, 10);
readSettings();
modelFrames = frames;
connect(ui->btnLoadScript, &QAbstractButton::pressed, this, &ScriptingWindow::loadNewScript);
connect(ui->btnNewScript, &QAbstractButton::pressed, this, &ScriptingWindow::createNewScript);
connect(ui->btnRecompile, &QAbstractButton::pressed, this, &ScriptingWindow::recompileScript);
connect(ui->btnRemoveScript, &QAbstractButton::pressed, this, &ScriptingWindow::deleteCurrentScript);
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(ui->tableVariables, SIGNAL(cellChanged(int,int)), this, SLOT(updatedValue(int, int)));
connect(CANConManager::getInstance(), &CANConManager::framesReceived, this, &ScriptingWindow::newFrames);
connect(&valuesTimer, SIGNAL(timeout()), this, SLOT(valuesTimerElapsed()));
currentScript = nullptr;
elapsedTime.start();
valuesTimer.start(1000);
ui->tableVariables->insertColumn(0);
ui->tableVariables->insertColumn(1);
}
ScriptingWindow::~ScriptingWindow()
{
delete editor;
delete ui;
}
void ScriptingWindow::newFrames(const CANConnection* pConn, const QVector<CANFrame>& pFrames)
{
/*FIXME: name of the probe and bus should be checked */
Q_UNUSED(pConn);
Q_UNUSED(pFrames);
/*for (int j = 0; j < scripts.length(); j++)
{
foreach(const CANFrame& frame, pFrames)
{
//scripts[j]->gotFrame(frame);
}
}*/
}
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::showEvent(QShowEvent* event)
{
QDialog::showEvent(event);
installEventFilter(this);
}
void ScriptingWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
removeEventFilter(this);
writeSettings();
}
bool ScriptingWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::KeyRelease) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
switch (keyEvent->key())
{
case Qt::Key_F1:
HelpWindow::getRef()->showHelp("scriptingwindow.md");
break;
}
return true;
} else {
// standard event processing
return QObject::eventFilter(obj, event);
}
return false;
}
void ScriptingWindow::readSettings()
{
QSettings settings;
if (settings.value("Main/SaveRestorePositions", false).toBool())
{
resize(settings.value("ScriptingWindow/WindowSize", QSize(860, 650)).toSize());
move(Utility::constrainedWindowPos(settings.value("ScriptingWindow/WindowPos", QPoint(100, 100)).toPoint()));
}
}
void ScriptingWindow::writeSettings()
{
QSettings settings;
if (settings.value("Main/SaveRestorePositions", false).toBool())
{
settings.setValue("ScriptingWindow/WindowSize", size());
settings.setValue("ScriptingWindow/WindowPos", pos());
}
}
void ScriptingWindow::changeCurrentScript()
{
ui->tableVariables->clear();
for (int i = 0; i < ui->tableVariables->rowCount(); i++) ui->tableVariables->removeRow(0);
QStringList labels;
labels.append("Parameter");
labels.append("Value");
ui->tableVariables->setHorizontalHeaderLabels(labels);
int sel = ui->listLoadedScripts->currentRow();
if (sel < 0) return;
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);
currentScript = container;
editor->setPlainText(container->scriptText);
editor->setEnabled(true);
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()
{
QString filename;
QFileDialog dialog;
QSettings settings;
ScriptContainer *container;
QStringList filters;
filters.append(QString(tr("Javascript File (*.js)")));
dialog.setDirectory(settings.value("ScriptingWindow/LoadSaveDirectory", dialog.directory().path()).toString());
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilters(filters);
dialog.setViewMode(QFileDialog::Detail);
if (dialog.exec() == QDialog::Accepted)
{
filename = dialog.selectedFiles()[0];
if (dialog.selectedNameFilter() == filters[0])
{
QFile scriptFile(filename);
if (scriptFile.open(QIODevice::ReadOnly))
{
QString contents = scriptFile.readAll();
scriptFile.close();
QStringList fileList = filename.split('/');
QString justFileName = fileList[fileList.length() - 1];
ui->listLoadedScripts->addItem(justFileName);
container = new ScriptContainer();
container->fileName = justFileName;
container->filePath = filename;
container->scriptText = contents;
container->setScriptWindow(this);
container->compileScript();
scripts.append(container);
ui->listLoadedScripts->setCurrentRow(ui->listLoadedScripts->count() - 1);
changeCurrentScript();
settings.setValue("ScriptingWindow/LoadSaveDirectory", dialog.directory().path());
}
}
}
}
void ScriptingWindow::createNewScript()
{
ScriptContainer *container;
container = new ScriptContainer();
container->fileName = "UNNAMED_" + QString::number((qrand() % 10000)) + ".js";
container->filePath = QString();
container->scriptText = QString();
container->setScriptWindow(this);
scripts.append(container);
ui->listLoadedScripts->addItem(container->fileName);
ui->listLoadedScripts->setCurrentRow(ui->listLoadedScripts->count() - 1);
changeCurrentScript();
}
void ScriptingWindow::deleteCurrentScript()
{
ScriptContainer* thisScript;
int sel = ui->listLoadedScripts->currentRow();
if (sel < 0) return;
QMessageBox msgBox;
msgBox.setText("Really remove script?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::No);
int ret = msgBox.exec();
switch (ret)
{
case QMessageBox::Yes:
ui->listLoadedScripts->takeItem(sel);
thisScript = scripts.at(sel);
scripts.removeAt(sel);
delete thisScript; //causes a seg fault. Seems to be due to currently running javascript code. No idea how to stop code from running
thisScript = nullptr;
currentScript = nullptr;
if (ui->listLoadedScripts->count() > 0)
{
ui->listLoadedScripts->setCurrentRow(0);
changeCurrentScript();
}
else
{
editor->setPlainText("");
editor->setEnabled(false);
}
break;
case QMessageBox::No:
break;
default:
// should never be reached
break;
}
}
void ScriptingWindow::refreshSourceWindow()
{
editor->setPlainText(currentScript->scriptText);
}
void ScriptingWindow::saveScript()
{
QString filename;
QFileDialog dialog(this);
QSettings settings;
QStringList filters;
filters.append(QString(tr("Javascript File (*.js)")));
dialog.setDirectory(settings.value("ScriptingWindow/LoadSaveDirectory", dialog.directory().path()).toString());
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setNameFilters(filters);
dialog.setViewMode(QFileDialog::Detail);
dialog.setAcceptMode(QFileDialog::AcceptSave);
if (dialog.exec() == QDialog::Accepted)
{
filename = dialog.selectedFiles()[0];
if (!filename.contains('.')) filename += ".js";
if (dialog.selectedNameFilter() == filters[0])
{
QFile *outFile = new QFile(filename);
if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text))
{
delete outFile;
return;
}
outFile->write(editor->toPlainText().toUtf8());
outFile->close();
delete outFile;
settings.setValue("ScriptingWindow/LoadSaveDirectory", dialog.directory().path());
}
}
}
void ScriptingWindow::revertScript()
{
QMessageBox msgBox;
msgBox.setText("Are you sure you'd like to revert?");
msgBox.setInformativeText("Really do it?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Cancel);
int ret = msgBox.exec();
switch (ret)
{
case QMessageBox::Yes:
//just grab the last stored copy of the script (last compiled version) and replace current text with that text
editor->setPlainText(currentScript->scriptText);
break;
case QMessageBox::No:
break;
default:
// should never be reached
break;
}
}
void ScriptingWindow::recompileScript()
{
if (currentScript)
{
currentScript->scriptText = editor->toPlainText();
currentScript->compileScript();
}
}
void ScriptingWindow::clickedLogClear()
{
ui->listLog->clear();
elapsedTime.start();
}
void ScriptingWindow::log(QString text)
{
ScriptContainer *cont = qobject_cast<ScriptContainer*>(sender());
if (cont != nullptr)
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();
}
}