-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolvingwidget.cpp
More file actions
112 lines (92 loc) · 3.51 KB
/
Copy pathsolvingwidget.cpp
File metadata and controls
112 lines (92 loc) · 3.51 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "solvingwidget.h"
#include "ui_solvingwidget.h"
SolvingWidget::SolvingWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::SolvingWidget)
{
ui->setupUi(this);
//compileErrorWidget.hide();
//codeOutputWidget.hide();
connect(ui->Compile_Buttion, &QPushButton::clicked, this, &SolvingWidget::compileCode);
// Init Compiler process
compiler = new CompilerProcess(this);
connect(compiler, &CompilerProcess::outputReceived,
this, &SolvingWidget::handleCompileOutput);
connect(compiler, &CompilerProcess::outputReceived,
this, &SolvingWidget::handleCompileError);
connect(compiler, &CompilerProcess::compileFinished,
this, &SolvingWidget::compilationFinished);
// Code Syntax Highlight
CodeSyntaxHighlighter *highlighter = new CodeSyntaxHighlighter(ui->CodeEditor_TextEdit->document());
}
SolvingWidget::~SolvingWidget()
{
delete ui;
}
void SolvingWidget::compileCode()
{
ui->Compile_Buttion->setEnabled(false);
QString sourceCode = ui->CodeEditor_TextEdit->toPlainText();
if (sourceCode.trimmed().isEmpty())
{
// ui->Output_Display_TextBrowser->append("Error: No Input");
ui->Compile_Buttion->setEnabled(true);
return;
}
// Start
compiler->compile(sourceCode);
}
void SolvingWidget::handleCompileOutput(const QString &output)
{
//ui->Output_Display_TextBrowser->append(output);
}
void SolvingWidget::handleCompileError(const QString &output)
{
compileErrorWidget.setErrorMessage(output);
}
void SolvingWidget::compilationFinished(int exitCode, QProcess::ExitStatus exitStatus, const QString &tempFilePath)
{
ui->Compile_Buttion->setEnabled(true);
while (ui->Code_Output_VerticalLayout->count() > 0)
{
QWidget *widget = ui->Code_Output_VerticalLayout->itemAt(0)->widget();
if (widget)
{
ui->Code_Output_VerticalLayout->removeWidget(widget);
widget->hide();
}
}
if (exitStatus == QProcess::NormalExit && exitCode == 0)
{
// If compilation success, execute it
//ui->Output_Display_TextBrowser->append("<span style='color: green'>編譯成功!</span>");
qDebug() << "編譯成功";
QString outputPath = QFileInfo(tempFilePath).path() + "/output.exe";
QProcess *process = new QProcess(this);
// Merge stdout and stderr
process->setProcessChannelMode(QProcess::MergedChannels);
// TODO: 連接 readyRead 信號,持續讀取執行的輸出並將之加入到 TextBrowser
connect(process, &QProcess::readyRead, [=]() {
QByteArray output = process->readAll();
codeOutputWidget.setUserOutput(QString::fromLocal8Bit(output));
});
// Delete this process after execution
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus exitStatus) {
//ui->Output_Display_TextBrowser->append(QString("<span style='color: blue'>執行結束,退出碼:%1</span>").arg(exitCode));
process->deleteLater();
});
// Start
process->start(outputPath);
ui->Code_Output_VerticalLayout->addWidget(&codeOutputWidget);
codeOutputWidget.show();
}
else
{
ui->Code_Output_VerticalLayout->addWidget(&compileErrorWidget);
qDebug() << "編譯失敗";
compileErrorWidget.show();
}
// Remove temporary file
QFile::remove(tempFilePath);
}