-
Notifications
You must be signed in to change notification settings - Fork 1
/
consolewidget.cpp
112 lines (87 loc) · 2.76 KB
/
consolewidget.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
#include "consolewidget.h"
#include "ui_consolewidget.h"
#include "constants.h"
#include "configuration.h"
#include "environmentvariablesdialog.h"
ConsoleWidget::ConsoleWidget(QWidget *parent) :
QDialog(parent),
ui(new Ui::ConsoleWidget)
{
ui->setupUi(this);
connect(ui->title, SIGNAL(textChanged(QString)), this, SLOT(titleTextEditChanged(const QString &)));
connect(&process, SIGNAL(readyRead()), this, SLOT(printProcessOutput()));
connect(&process, SIGNAL(started()), this, SLOT(processStarted()));
connect(&process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)));
}
ConsoleWidget::ConsoleWidget(const QString &tabName, QWidget *parent) :
ConsoleWidget::ConsoleWidget(parent)
{
Configuration &configuration = Configuration::getInstance();
QString command;
QString workingDirectory;
configuration.load(tabName, command, workingDirectory);
ui->title->setText(tabName);
ui->command->setText(command);
ui->workingDirectory->setText(workingDirectory);
}
ConsoleWidget::~ConsoleWidget()
{
delete ui;
}
void ConsoleWidget::titleTextEditChanged(const QString &newTitle)
{
emit titleUpdated(newTitle, property(TAB_INDEX).toInt());
}
void ConsoleWidget::toggleCommand()
{
if(process.state() == QProcess::NotRunning)
{
executeCommand();
}
else
{
process.close();
process.waitForFinished(10000);
}
}
void ConsoleWidget::printProcessOutput(){
while(process.bytesAvailable() > 0){
QByteArray bLine = process.readLine().trimmed();
QString line(bLine);
if(!line.isEmpty()){
ui->output->appendPlainText(line);
}
}
}
void ConsoleWidget::executeCommand()
{
Configuration &configuration = Configuration::getInstance();
QStringList env;
ui->output->clear();
ui->commandBtn->setText("Starting...");
configuration.save(ui->title->text(), ui->command->text(), ui->workingDirectory->text());
configuration.getEnvironment(env, ui->title->text());
process.setWorkingDirectory(ui->workingDirectory->text());
process.setEnvironment(env);
process.start(ui->command->text());
process.waitForStarted(10000);
}
void ConsoleWidget::processStarted()
{
int id = process.processId();
ui->processIdLabel->setText(QString::number(id));
ui->commandBtn->setText("Stop");
emit started(property(TAB_INDEX).toInt());
}
void ConsoleWidget::processFinished(int,QProcess::ExitStatus)
{
ui->processIdLabel->setText("");
ui->commandBtn->setText("Execute");
emit stopped(property(TAB_INDEX).toInt());
}
void ConsoleWidget::openEnvVarDialog()
{
EnvironmentVariablesDialog dialog(ui->title->text(), this);
dialog.setModal(true);
dialog.exec();
}