musredit 1.0.0
Loading...
Searching...
No Matches
PDumpOutputHandler.cpp
Go to the documentation of this file.
1/****************************************************************************
2
3 PDumpOutputHandler.cpp
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8*****************************************************************************/
9
10/***************************************************************************
11 * Copyright (C) 2012-2026 by Andreas Suter *
12 * andreas.suter@psi.ch *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
29
42
43#include <QTimer>
44
45#include <QtDebug>
46
47#include "PDumpOutputHandler.h"
48
49//----------------------------------------------------------------------------------------------------
72{
73 if (cmd.empty())
74 return;
75
76 // Layout
77 fVbox = std::make_unique<QVBoxLayout>( this );
78 fOutput = std::make_unique<QTextEdit>();
79 fVbox->addWidget(fOutput.get());
80 fOutput->setMinimumSize(600, 755);
81 fOutput->setReadOnly(true);
82 connect( fOutput.get(), SIGNAL( destroyed() ), this, SLOT( quitButtonPressed() ) );
83 fQuitButton = std::make_unique<QPushButton>( tr("Quit") );
84 fVbox->addWidget(fQuitButton.get());
85 connect( fQuitButton.get(), SIGNAL( clicked() ), this, SLOT( quitButtonPressed() ) );
86 resize( 600, 800 );
87 fQuitButton->setFocus();
88
89 // QProcess related code
90 fProc = std::make_unique<QProcess>( this );
91
92 // make sure that the system environment variables are properly set
93 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
94#if defined(Q_OS_DARWIN)
95 env.insert("DYLD_LIBRARY_PATH", env.value("ROOTSYS") + "/lib:/usr/local/lib:" + env.value("DYLD_LIBRARY_PATH"));
96#else
97 env.insert("LD_LIBRARY_PATH", env.value("ROOTSYS") + "/lib:" + env.value("LD_LIBRARY_PATH"));
98#endif
99 fProc->setProcessEnvironment(env);
100
101 // Set up the command and arguments.
102 QString program = cmd[0];
103 QStringList arguments;
104 for (int i=1; i<cmd.size(); i++)
105 arguments << cmd[i];
106
107 connect( fProc.get(), SIGNAL( readyReadStandardOutput() ), this, SLOT( readFromStdOut() ) );
108 connect( fProc.get(), SIGNAL( readyReadStandardError() ), this, SLOT( readFromStdErr() ) );
109
110
111 fProc->start(program, arguments);
112 if ( !fProc->waitForStarted() ) {
113 // error handling
114 QString msg(tr("Could not execute the output command: ")+cmd[0]);
115 QMessageBox::critical( nullptr, tr("FATAL ERROR"), msg, QMessageBox::Close );
116 done(0);
117 }
118
119 fProcPID = fProc->processId();
120}
121
122//----------------------------------------------------------------------------------------------------
137{
138 if (fProc->state() == QProcess::Running) {
139 fProc->terminate();
140 if (!fProc->waitForFinished()) {
141 qDebug() << "fProc still running, will call kill." << Qt::endl;
142 fProc->kill();
143 }
144 fProc->waitForFinished();
145 }
146 if (fProc->state() == QProcess::Running) {
147 QString cmd = "kill -9 "+ QString("%1").arg(fProcPID);
148 QString msg = "fProc still running even after Qt kill, will try system kill cmd: "+cmd;
149 qDebug() << msg << Qt::endl;
150 system(cmd.toLatin1());
151 }
152}
153
154//----------------------------------------------------------------------------------------------------
164{
165 // Read and process the data.
166 // Bear in mind that the data might be output in chunks.
167 fOutput->append( fProc->readAllStandardOutput() );
168}
169
170//----------------------------------------------------------------------------------------------------
183{
184 // Read and process the data.
185 // Bear in mind that the data might be output in chunks.
186 fOutput->append( fProc->readAllStandardError() );
187}
188
189//----------------------------------------------------------------------------------------------------
202{
203 // if the fitting is still taking place, kill it
204 if (fProc->state() == QProcess::Running) {
205 fProc->terminate();
206 if (!fProc->waitForFinished()) {
207 fProc->kill();
208 }
209 }
210
211 accept();
212}
213
214//----------------------------------------------------------------------------------------------------
215// END
216//----------------------------------------------------------------------------------------------------
Dialog for displaying dump_header command output.
std::unique_ptr< QTextEdit > fOutput
Read-only text widget displaying command output.
std::unique_ptr< QPushButton > fQuitButton
Button to close dialog and terminate process.
virtual void readFromStdErr()
Slot to read and display standard error from the process.
virtual void readFromStdOut()
Slot to read and display standard output from the process.
virtual ~PDumpOutputHandler()
Destructor - ensures the dump_header process is terminated.
qint64 fProcPID
Process ID of the running dump_header command.
virtual void quitButtonPressed()
Slot called when the Quit button is pressed.
std::unique_ptr< QVBoxLayout > fVbox
Vertical layout manager for dialog widgets.
PDumpOutputHandler(QVector< QString > &cmd)
Constructs the dump output handler dialog and starts the command.
std::unique_ptr< QProcess > fProc
QProcess object managing the dump_header execution.