Skip to content

Commit ff0fcb7

Browse files
authored
Merge pull request #715 from cgalpin/Scripting-Interface-enhancements
Scripting Interface Enhancements
2 parents da5e502 + f6c2da8 commit ff0fcb7

File tree

4 files changed

+130
-5
lines changed

4 files changed

+130
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ Makefile
4545
SavvyCAN.pro.qtds
4646
.xcode
4747
SavvyCAN.xcodeproj
48+
.vscode

scriptingwindow.cpp

+89
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ ScriptingWindow::ScriptingWindow(const QVector<CANFrame> *frames, QWidget *paren
4141
connect(ui->btnRecompile, &QAbstractButton::pressed, this, &ScriptingWindow::recompileScript);
4242
connect(ui->btnRemoveScript, &QAbstractButton::pressed, this, &ScriptingWindow::deleteCurrentScript);
4343
connect(ui->btnRevertScript, &QAbstractButton::pressed, this, &ScriptingWindow::revertScript);
44+
connect(ui->btnReloadScript, &QAbstractButton::pressed, this, &ScriptingWindow::reloadScript);
4445
connect(ui->btnSaveScript, &QAbstractButton::pressed, this, &ScriptingWindow::saveScript);
46+
connect(ui->btnSaveAsScript, &QAbstractButton::pressed, this, &ScriptingWindow::saveAsScript);
4547
connect(ui->btnClearLog, &QAbstractButton::pressed, this, &ScriptingWindow::clickedLogClear);
48+
connect(ui->btnSaveLog, &QAbstractButton::pressed, this, &ScriptingWindow::saveLog);
4649
connect(ui->listLoadedScripts, &QListWidget::currentRowChanged, this, &ScriptingWindow::changeCurrentScript);
4750
connect(ui->tableVariables, SIGNAL(cellChanged(int,int)), this, SLOT(updatedValue(int, int)));
4851

@@ -301,6 +304,21 @@ void ScriptingWindow::refreshSourceWindow()
301304
}
302305

303306
void ScriptingWindow::saveScript()
307+
{
308+
QFile *outFile = new QFile(currentScript->filePath);
309+
310+
if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text))
311+
{
312+
delete outFile;
313+
return;
314+
}
315+
outFile->write(editor->toPlainText().toUtf8());
316+
currentScript->scriptText = editor->toPlainText();
317+
outFile->close();
318+
delete outFile;
319+
}
320+
321+
void ScriptingWindow::saveAsScript()
304322
{
305323
QString filename;
306324
QFileDialog dialog(this);
@@ -358,6 +376,38 @@ void ScriptingWindow::revertScript()
358376
}
359377
}
360378

379+
void ScriptingWindow::reloadScript()
380+
{
381+
QMessageBox msgBox;
382+
msgBox.setText("Are you sure you'd like to reload from disk?");
383+
msgBox.setInformativeText("Really do it?");
384+
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
385+
msgBox.setDefaultButton(QMessageBox::Cancel);
386+
int ret = msgBox.exec();
387+
switch (ret)
388+
{
389+
case QMessageBox::Yes:
390+
{
391+
// get the latest version from disk and set in the editor/state
392+
QFile scriptFile(currentScript->filePath);
393+
if (scriptFile.open(QIODevice::ReadOnly | QIODevice::Text))
394+
{
395+
QString contents = scriptFile.readAll();
396+
scriptFile.close();
397+
editor->setPlainText(contents);
398+
currentScript->scriptText = contents;
399+
currentScript->compileScript();
400+
}
401+
break;
402+
}
403+
case QMessageBox::No:
404+
break;
405+
default:
406+
// should never be reached
407+
break;
408+
}
409+
}
410+
361411
void ScriptingWindow::recompileScript()
362412
{
363413
if (currentScript)
@@ -373,6 +423,45 @@ void ScriptingWindow::clickedLogClear()
373423
elapsedTime.start();
374424
}
375425

426+
void ScriptingWindow::saveLog()
427+
{
428+
QString filename;
429+
QFileDialog dialog(this);
430+
QSettings settings;
431+
432+
QStringList filters;
433+
filters.append(QString(tr("Log File (*.log)")));
434+
435+
dialog.setDirectory(settings.value("ScriptingWindow/LoadSaveDirectory", dialog.directory().path()).toString());
436+
dialog.setFileMode(QFileDialog::AnyFile);
437+
dialog.setNameFilters(filters);
438+
dialog.setViewMode(QFileDialog::Detail);
439+
dialog.setAcceptMode(QFileDialog::AcceptSave);
440+
441+
if (dialog.exec() == QDialog::Accepted)
442+
{
443+
filename = dialog.selectedFiles()[0];
444+
if (!filename.contains('.')) filename += ".log";
445+
if (dialog.selectedNameFilter() == filters[0])
446+
{
447+
QFile *outFile = new QFile(filename);
448+
449+
if (!outFile->open(QIODevice::WriteOnly | QIODevice::Text))
450+
{
451+
delete outFile;
452+
return;
453+
}
454+
int c = ui->listLog ->count();
455+
for (int row = 0; row < c; row++) {
456+
outFile->write(ui->listLog->item(row)->data(Qt::DisplayRole).toString().toUtf8() + "\n");
457+
}
458+
outFile->close();
459+
delete outFile;
460+
settings.setValue("ScriptingWindow/LoadSaveDirectory", dialog.directory().path());
461+
}
462+
}
463+
}
464+
376465
void ScriptingWindow::log(QString text)
377466
{
378467
ScriptContainer *cont = qobject_cast<ScriptContainer*>(sender());

scriptingwindow.h

+3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ private slots:
3737
void deleteCurrentScript();
3838
void refreshSourceWindow();
3939
void saveScript();
40+
void saveAsScript();
4041
void revertScript();
42+
void reloadScript();
4143
void recompileScript();
4244
void changeCurrentScript();
4345
void newFrames(const CANConnection*, const QVector<CANFrame>&);
@@ -49,6 +51,7 @@ private slots:
4951
void closeEvent(QCloseEvent *event);
5052
void readSettings();
5153
void writeSettings();
54+
void saveLog();
5255
bool eventFilter(QObject *obj, QEvent *event);
5356

5457
Ui::ScriptingWindow *ui;

ui/scriptingwindow.ui

+37-5
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,27 @@
108108
</property>
109109
</widget>
110110
</item>
111+
<item>
112+
<widget class="QPushButton" name="btnSaveAsScript">
113+
<property name="text">
114+
<string>Sa&amp;ve As</string>
115+
</property>
116+
</widget>
117+
</item>
111118
<item>
112119
<widget class="QPushButton" name="btnRevertScript">
113120
<property name="text">
114121
<string>&amp;Revert</string>
115122
</property>
116123
</widget>
117124
</item>
125+
<item>
126+
<widget class="QPushButton" name="btnReloadScript">
127+
<property name="text">
128+
<string>R&amp;eload</string>
129+
</property>
130+
</widget>
131+
</item>
118132
<item>
119133
<widget class="QPushButton" name="btnRecompile">
120134
<property name="text">
@@ -151,11 +165,26 @@
151165
</widget>
152166
</item>
153167
<item>
154-
<widget class="QPushButton" name="btnClearLog">
155-
<property name="text">
156-
<string>Clear Log &amp;Window</string>
157-
</property>
158-
</widget>
168+
<layout class="QHBoxLayout" name="horizontalLayout_log">
169+
<property name="alignment">
170+
<set>Qt::AlignCenter</set>
171+
</property>
172+
<property name="contentsMargins">10, 0, 10, 0</property>
173+
<item>
174+
<widget class="QPushButton" name="btnClearLog">
175+
<property name="text">
176+
<string>Clear Log &amp;Window</string>
177+
</property>
178+
</widget>
179+
</item>
180+
<item>
181+
<widget class="QPushButton" name="btnSaveLog">
182+
<property name="text">
183+
<string>Save L&amp;og</string>
184+
</property>
185+
</widget>
186+
</item>
187+
</layout>
159188
</item>
160189
</layout>
161190
</item>
@@ -169,10 +198,13 @@
169198
<tabstop>listLoadedScripts</tabstop>
170199
<tabstop>tableVariables</tabstop>
171200
<tabstop>btnSaveScript</tabstop>
201+
<tabstop>btnSaveAsScript</tabstop>
172202
<tabstop>btnRevertScript</tabstop>
203+
<tabstop>btnReloadScript</tabstop>
173204
<tabstop>btnRecompile</tabstop>
174205
<tabstop>cbAutoScroll</tabstop>
175206
<tabstop>btnClearLog</tabstop>
207+
<tabstop>btnSaveLog</tabstop>
176208
</tabstops>
177209
<resources/>
178210
<connections/>

0 commit comments

Comments
 (0)