1// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#include "processdialog.h"
6
7#include <QDebug>
8
9ProcessDialog::ProcessDialog(QWidget *parent, Qt::WindowFlags f)
10 : QDialog (parent, f)
11 , progressBar(new QProgressBar)
12 , textBrowser(new QTextBrowser)
13 , vLayout(new QVBoxLayout)
14{
15 setWindowTitle(__FUNCTION__);
16 setMinimumSize(600, 400);
17
18 vLayout->addWidget(textBrowser);
19 vLayout->addWidget(progressBar);
20 setLayout(vLayout);
21
22 QObject::connect(&process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
23 this, &ProcessDialog::doFinished);
24 QObject::connect(&process, &QProcess::readyReadStandardOutput,
25 this, [&](){
26 auto data = process.readAllStandardOutput();
27 this->doShowStdOut(data);
28 });
29 QObject::connect(&process, &QProcess::readyReadStandardError,
30 this, [&](){
31 auto data = process.readAllStandardError();
32 this->doShowStdErr(data);
33 });
34}
35
36ProcessDialog::~ProcessDialog()
37{
38 process.kill();
39}
40
41void ProcessDialog::setProgram(const QString &program)
42{
43 process.setProgram(program);
44}
45
46QString ProcessDialog::program() const
47{
48 return process.program();
49}
50
51void ProcessDialog::setArguments(const QStringList &args)
52{
53 process.setArguments(args);
54}
55
56QStringList ProcessDialog::arguments()
57{
58 return process.arguments();
59}
60
61void ProcessDialog::setWorkingDirectory(const QString &workDir)
62{
63 process.setWorkingDirectory(workDir);
64}
65
66QString ProcessDialog::workDirectory() const
67{
68 return process.workingDirectory();
69}
70
71void ProcessDialog::setEnvironment(const QStringList &env)
72{
73 process.setEnvironment(env);
74}
75
76int ProcessDialog::exec()
77{
78 process.start();
79 return QDialog::exec();
80}
81
82void ProcessDialog::doShowStdErr(const QByteArray &array)
83{
84 textBrowser->append(array);
85}
86
87void ProcessDialog::doShowStdOut(const QByteArray &array)
88{
89 textBrowser->append(array);
90}
91
92void ProcessDialog::doFinished(int exitCode, QProcess::ExitStatus status)
93{
94 this->close();
95 qInfo() << exitCode << status;
96}
97
98void ProcessDialog::doShowProgress(int current, int count)
99{
100 progressBar->setRange(0, count);
101 progressBar->setValue(current);
102}
103