1 #include "processdialog.h"
2 #include <QVBoxLayout>
3 #include <QPushButton>
4 #include <QLabel>
5 #include <QDebug>
6 
7 /**
8  * @brief Creates dialog and connects it to process
9  * @param proc process
10  * @param procName name of program that is running in process
11  * @param parent parent widget
12  */
ProcessDialog(QProcess * proc,const QString & procName,QWidget * parent)13 ProcessDialog::ProcessDialog(QProcess* proc, const QString &procName,
14                              QWidget *parent) : QDialog(parent) {
15 
16   // Saves process
17   this->procPtr = proc;
18 
19   // Size
20   this->setMinimumSize(420, 320);
21   this->setWindowTitle(tr("Custom Action"));
22 
23   // Text
24   QString text = tr("<h2>Custom action '%1' is running ...</h2>");
25 
26   // Create widgets
27   output = new QListWidget(this);
28 
29   QWidget *labelContainer = new QWidget(this);
30   QHBoxLayout *labelLayout = new QHBoxLayout(labelContainer);
31 
32   QLabel *labelIcon = new QLabel(this);
33   labelIcon->setPixmap(QIcon::fromTheme("applications-system").pixmap(QSize(48, 48)));
34   labelIcon->setMinimumSize(QSize(48, 48));
35   labelIcon->setMaximumSize(QSize(48, 48));
36   QLabel* labelText = new QLabel(text.arg(procName), this);
37   labelLayout->addWidget(labelIcon);
38   labelLayout->addWidget(labelText);
39 
40   QPushButton* btnClear = new QPushButton(tr("Clear"), this);
41   QPushButton* btnClose = new QPushButton(tr("Hide"), this);
42   QPushButton* btnTerminate = new QPushButton(tr("Abort"), this);
43 
44   // Create buttons layout
45   QHBoxLayout* layoutBtns = new QHBoxLayout();
46   layoutBtns->setSpacing(10);
47   layoutBtns->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding));
48   layoutBtns->addWidget(btnClear);
49   layoutBtns->addWidget(btnClose);
50   layoutBtns->addWidget(btnTerminate);
51 
52   // Label properties
53   labelText->setWordWrap(true);
54 
55   // Create main layout
56   QVBoxLayout* layout = new QVBoxLayout(this);
57   layout->addWidget(labelContainer);
58   layout->addItem(new QSpacerItem(0, 10));
59   //layout->addWidget(new QLabel(tr("Output:"), this));
60   layout->addWidget(output);
61   layout->addItem(layoutBtns);
62 
63   // Connect to process
64   connect(procPtr, SIGNAL(started()), SLOT(onProcStarted()));
65   connect(procPtr, SIGNAL(finished(int)), SLOT(onProcFinished()));
66   connect(procPtr, SIGNAL(readyReadStandardOutput()), SLOT(onProcStdOut()));
67   //connect(procPtr, SIGNAL(readyRead()), SLOT(onProcStdOut()));
68   connect(btnTerminate, SIGNAL(clicked()), procPtr, SLOT(terminate()));
69   connect(btnClear, SIGNAL(clicked()), output, SLOT(clear()));
70   connect(btnClose, SIGNAL(clicked()), SLOT(hide()));
71 }
72 //---------------------------------------------------------------------------
73 
74 /**
75  * @brief Reaction on process finish
76  */
onProcFinished()77 void ProcessDialog::onProcFinished() {
78   //this->hide();
79     //qDebug() << "proc finished";
80   this->deleteLater();
81 }
82 //---------------------------------------------------------------------------
83 
84 /**
85  * @brief Reaction on process start
86  */
onProcStarted()87 void ProcessDialog::onProcStarted() {
88     //qDebug() << "proc started";
89   this->show();
90 }
91 //---------------------------------------------------------------------------
92 
93 /**
94  * @brief Reaction on process standard output
95  */
onProcStdOut()96 void ProcessDialog::onProcStdOut() {
97   QProcess* p = qobject_cast<QProcess*>(sender());
98   QString text = QString::fromLocal8Bit(p->readAllStandardOutput());
99   output->addItem(new QListWidgetItem(text, output));
100   output->scrollToBottom();
101 }
102 //---------------------------------------------------------------------------
103