-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscripteditor.cpp
More file actions
88 lines (70 loc) · 2.52 KB
/
scripteditor.cpp
File metadata and controls
88 lines (70 loc) · 2.52 KB
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
#include "scripteditor.h"
#include "ui_scripteditor.h"
#include "util.h"
#include <QWebChannel>
#include <QFileInfo>
#include <fstream>
ScriptEditor::ScriptEditor(QWidget *parent) :
QDialog(parent),
ui(new Ui::ScriptEditor)
{
ui->setupUi(this);
this->ui->webEngineView->page()->setBackgroundColor(Qt::transparent);
this->ui->webEngineView->setUrl(QUrl::fromLocalFile(QFileInfo("js/code_editor/lint.html").absoluteFilePath()));
this->ui->webEngineView->setZoomFactor(0.5);
}
ScriptEditor::~ScriptEditor()
{
delete ui;
}
void ScriptEditor::load_script(QString script_file){
QString script;
std::ifstream t(script_file.toStdString());
if(!t){
qDebug() << "error file";
}
else{
std::string str;
t.seekg(0, std::ios::end);
str.reserve(t.tellg());
t.seekg(0, std::ios::beg);
str.assign((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
script = QString::fromStdString(str).replace("\n","\\n").replace("'","\\'");
}
this->ui->webEngineView->page()->runJavaScript("editor.setValue('" + script + "');");
this->ui->lineEdit_script_file_name->setText(script_file);
}
void ScriptEditor::save_script(QString script_content){
if(Util::fileExists(this->ui->lineEdit_script_file_name->text())){
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, tr("Overwrite"), tr("There exists a file with this name. Should it be overwritten?"), QMessageBox::Yes|QMessageBox::No);
if (reply != QMessageBox::Yes) {
return;
}
}
std::ofstream script_file(this->ui->lineEdit_script_file_name->text().toStdString());
if (script_file.is_open())
{
script_file << script_content.toStdString();
script_file.close();
}
else{
qDebug() << "Unable to save script";
Util::show_msgBox(tr("Unable to save script"));
}
qDebug() << "Saved Script: " << this->ui->lineEdit_script_file_name->text();
accept();
}
void ScriptEditor::on_pushButton_save_script_clicked()
{
this->ui->webEngineView->page()->runJavaScript("editor.getValue()", [this](const QVariant &v) { this->save_script(v.toString());});
}
void ScriptEditor::on_pushButton_discard_script_clicked()
{
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, tr("Discard Changes"), tr("Are you sure you want to discard all changes?"), QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes) {
reject();
}
}