musredit 1.0.0
Loading...
Searching...
No Matches
PFitOutputHandler.cpp
Go to the documentation of this file.
1/****************************************************************************
2
3 PFitOutputHandler.cpp
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8*****************************************************************************/
9
10/***************************************************************************
11 * Copyright (C) 2009-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
43
44#include <QTimer>
45
46#include <QtDebug>
47
48#include "PFitOutputHandler.h"
49
50//----------------------------------------------------------------------------------------------------
76PFitOutputHandler::PFitOutputHandler(QString workingDirectory, QVector<QString> &cmd)
77{
78 if (cmd.empty())
79 return;
80
81 // Layout
82 fVbox = std::make_unique<QVBoxLayout>( this );
83//Qt.3x fVbox->resize(800, 500);
84 fOutput = std::make_unique<QPlainTextEdit>();
85 fOutput->setMaximumBlockCount(1000);
86 fVbox->addWidget(fOutput.get());
87 fOutput->setMinimumSize(800, 455);
88 fOutput->setReadOnly(true);
89 connect( fOutput.get(), SIGNAL( destroyed() ), this, SLOT( quitButtonPressed() ) );
90 fQuitButton = std::make_unique<QPushButton>( tr("Fitting...") );
91 fVbox->addWidget(fQuitButton.get());
92 connect( fQuitButton.get(), SIGNAL( clicked() ), this, SLOT( quitButtonPressed() ) );
93 resize( 800, 500 );
94 fQuitButton->setFocus();
95
96 // QProcess related code
97 fProc = std::make_unique<QProcess>( this );
98
99 // make sure that the system environment variables are properly set
100 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
101#if defined(Q_OS_DARWIN)
102 env.insert("DYLD_LIBRARY_PATH", env.value("ROOTSYS") + "/lib:/usr/local/lib:" + env.value("DYLD_LIBRARY_PATH"));
103#else
104 env.insert("LD_LIBRARY_PATH", env.value("ROOTSYS") + "/lib:" + env.value("LD_LIBRARY_PATH"));
105#endif
106 fProc->setProcessEnvironment(env);
107 fProc->setWorkingDirectory(workingDirectory);
108
109 // Set up the command and arguments.
110 QString program = cmd[0];
111 QStringList arguments;
112 for (int i=1; i<cmd.size(); i++)
113 arguments << cmd[i];
114
115 connect( fProc.get(), SIGNAL( readyReadStandardOutput() ), this, SLOT( readFromStdOut() ) );
116 connect( fProc.get(), SIGNAL( readyReadStandardError() ), this, SLOT( readFromStdErr() ) );
117 connect( fProc.get(), SIGNAL( finished(int, QProcess::ExitStatus) ), this, SLOT( processDone(int, QProcess::ExitStatus) ) );
118
119 fProc->start(program, arguments);
120 if ( !fProc->waitForStarted() ) {
121 // error handling
122 QString msg(tr("Could not execute the output command: ")+cmd[0]);
123 QMessageBox::critical( nullptr, tr("FATAL ERROR"), msg, QMessageBox::Close );
124 done(0);
125 }
126 fProcPID = fProc->processId();
127}
128
129//----------------------------------------------------------------------------------------------------
145{
146 if (fProc->state() == QProcess::Running) {
147 fProc->terminate();
148 if (!fProc->waitForFinished()) {
149 qDebug() << "fProc still running, will call kill." << Qt::endl;
150 fProc->kill();
151 }
152 fProc->waitForFinished();
153 }
154 if (fProc->state() == QProcess::Running) {
155 QString cmd = "kill -9 "+ QString("%1").arg(fProcPID);
156 QString msg = "fProc still running even after Qt kill, will try system kill cmd: "+cmd;
157 qDebug() << msg << Qt::endl;
158 system(cmd.toLatin1());
159 }
160}
161
162//----------------------------------------------------------------------------------------------------
175{
176 // Read and process the data.
177 // Bear in mind that the data might be output in chunks.
178 fOutput->appendPlainText( fProc->readAllStandardOutput() );
179}
180
181//----------------------------------------------------------------------------------------------------
194{
195 // Read and process the data.
196 // Bear in mind that the data might be output in chunks.
197 fOutput->appendPlainText( fProc->readAllStandardError() );
198}
199
200//----------------------------------------------------------------------------------------------------
215void PFitOutputHandler::processDone(int exitCode, QProcess::ExitStatus exitStatus)
216{
217 if ((exitStatus == QProcess::CrashExit) && (exitCode != 0)) {
218 qDebug() << "**ERROR** PFitOutputHandler::processDone: exitCode = " << exitCode << Qt::endl;
219 }
220 fQuitButton->setText("Done");
221}
222
223//----------------------------------------------------------------------------------------------------
238{
239 // if the fitting is still taking place, kill it
240 if (fProc->state() == QProcess::Running) {
241 fProc->terminate();
242 if (!fProc->waitForFinished()) {
243 fProc->kill();
244 }
245 }
246
247 accept();
248}
249
250//----------------------------------------------------------------------------------------------------
251// END
252//----------------------------------------------------------------------------------------------------
Dialog for displaying musrfit fitting process output.
virtual ~PFitOutputHandler()
Destructor - ensures the musrfit process is terminated.
qint64 fProcPID
Process ID of the running musrfit process.
virtual void processDone(int exitCode, QProcess::ExitStatus exitStatus)
Slot called when the musrfit process finishes.
PFitOutputHandler(QString workingDirectory, QVector< QString > &cmd)
Constructs the fit output handler dialog and starts the musrfit process.
virtual void readFromStdOut()
Slot to read and display standard output from musrfit.
std::unique_ptr< QPlainTextEdit > fOutput
Read-only text widget displaying musrfit output (max 1000 lines).
std::unique_ptr< QProcess > fProc
QProcess object managing the musrfit execution.
std::unique_ptr< QVBoxLayout > fVbox
Vertical layout manager for dialog widgets.
virtual void readFromStdErr()
Slot to read and display standard error from musrfit.
std::unique_ptr< QPushButton > fQuitButton
Button showing "Fitting..." during fit, "Done" after completion.
virtual void quitButtonPressed()
Slot called when the Quit/Done button is pressed.