| 1 | #include "progress_dialog.h" |
| 2 | |
| 3 | ProgressDialog::ProgressDialog(const QString &title, const QString &operation, |
| 4 | const QString &message, QProcess *process, |
| 5 | QWidget *parent, bool close, bool trim) |
| 6 | : QDialog(parent) { |
| 7 | ui.setupUi(this); |
| 8 | resize(width(), 0); |
| 9 | |
| 10 | setWindowTitle(title); |
| 11 | ui.labelOperation->setText(operation); |
| 12 | ui.labelInfo->setText(message); |
| 13 | |
| 14 | ui.output->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); |
| 15 | ui.output->setVisible(false); |
| 16 | |
| 17 | QObject::connect(ui.buttonBox, &QDialogButtonBox::rejected, this, |
| 18 | &QDialog::reject); |
| 19 | |
| 20 | QObject::connect(ui.buttonShowOutput, &QPushButton::toggled, this, |
| 21 | [=](bool checked) { |
| 22 | ui.output->setVisible(checked); |
| 23 | ui.buttonShowOutput->setArrowType( |
| 24 | checked ? Qt::DownArrow : Qt::RightArrow); |
| 25 | if (!checked) { |
| 26 | adjustSize(); |
| 27 | } |
| 28 | }); |
| 29 | |
| 30 | QObject::connect(process, |
| 31 | static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>( |
| 32 | &QProcess::finished), |
| 33 | this, [=](int code, QProcess::ExitStatus status) { |
| 34 | if (status == QProcess::NormalExit && code == 0) { |
| 35 | if (close) { |
| 36 | emit accept(); |
| 37 | } |
| 38 | } else { |
| 39 | ui.buttonShowOutput->setChecked(true); |
| 40 | ui.buttonBox->setEnabled(true); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | QObject::connect(process, &QProcess::readyRead, this, [=]() { |
| 45 | QString output = process->readAll(); |
| 46 | if (trim) { |
| 47 | output = output.trimmed(); |
| 48 | } |
| 49 | ui.output->appendPlainText(output); |
| 50 | emit outputAvailable(output); |
| 51 | }); |
| 52 | |
| 53 | process->setProcessChannelMode(QProcess::MergedChannels); |
| 54 | process->start(QIODevice::ReadOnly); |
| 55 | } |
| 56 | |
| 57 | ProgressDialog::~ProgressDialog() {} |
| 58 | |
| 59 | void ProgressDialog::expand() { ui.buttonShowOutput->setChecked(true); } |
| 60 | |
| 61 | void ProgressDialog::allowToClose() { ui.buttonBox->setEnabled(true); } |
| 62 | // |
| 63 | // QString ProgressDialog::getOutput() const |
| 64 | //{ |
| 65 | // return ui.output->toPlainText(); |
| 66 | //} |