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 | |
9 | ProcessDialog::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 | |
36 | ProcessDialog::~ProcessDialog() |
37 | { |
38 | process.kill(); |
39 | } |
40 | |
41 | void ProcessDialog::setProgram(const QString &program) |
42 | { |
43 | process.setProgram(program); |
44 | } |
45 | |
46 | QString ProcessDialog::program() const |
47 | { |
48 | return process.program(); |
49 | } |
50 | |
51 | void ProcessDialog::setArguments(const QStringList &args) |
52 | { |
53 | process.setArguments(args); |
54 | } |
55 | |
56 | QStringList ProcessDialog::arguments() |
57 | { |
58 | return process.arguments(); |
59 | } |
60 | |
61 | void ProcessDialog::setWorkingDirectory(const QString &workDir) |
62 | { |
63 | process.setWorkingDirectory(workDir); |
64 | } |
65 | |
66 | QString ProcessDialog::workDirectory() const |
67 | { |
68 | return process.workingDirectory(); |
69 | } |
70 | |
71 | void ProcessDialog::setEnvironment(const QStringList &env) |
72 | { |
73 | process.setEnvironment(env); |
74 | } |
75 | |
76 | int ProcessDialog::exec() |
77 | { |
78 | process.start(); |
79 | return QDialog::exec(); |
80 | } |
81 | |
82 | void ProcessDialog::doShowStdErr(const QByteArray &array) |
83 | { |
84 | textBrowser->append(array); |
85 | } |
86 | |
87 | void ProcessDialog::doShowStdOut(const QByteArray &array) |
88 | { |
89 | textBrowser->append(array); |
90 | } |
91 | |
92 | void ProcessDialog::doFinished(int exitCode, QProcess::ExitStatus status) |
93 | { |
94 | this->close(); |
95 | qInfo() << exitCode << status; |
96 | } |
97 | |
98 | void ProcessDialog::doShowProgress(int current, int count) |
99 | { |
100 | progressBar->setRange(0, count); |
101 | progressBar->setValue(current); |
102 | } |
103 | |