31#include <QApplication>
34QgsRunProcess::QgsRunProcess(
const QString &action,
bool capture )
43 QStringList arguments = QProcess::splitCommand( action );
44 const QString command = arguments.value( 0 );
45 if ( !arguments.isEmpty() )
46 arguments.removeFirst();
48 mProcess =
new QProcess;
52 connect( mProcess, &QProcess::errorOccurred,
this, &QgsRunProcess::processError );
53 connect( mProcess, &QProcess::readyReadStandardOutput,
this, &QgsRunProcess::stdoutAvailable );
54 connect( mProcess, &QProcess::readyReadStandardError,
this, &QgsRunProcess::stderrAvailable );
58 connect( mProcess,
static_cast < void ( QProcess::* )(
int, QProcess::ExitStatus )
>( &QProcess::finished ),
this, &QgsRunProcess::processExit );
63 mOutput->setTitle( action );
65 mOutput->showMessage(
false );
68 QObject *mOutputObj =
dynamic_cast<QObject *
>( mOutput );
71 connect( mOutputObj, &QObject::destroyed,
this, &QgsRunProcess::dialogGone );
75 mProcess->start( command, arguments );
79 if ( ! QProcess::startDetached( command, arguments ) )
81 QMessageBox::critical(
nullptr, tr(
"Action" ),
82 tr(
"Unable to run command\n%1" ).arg( action ),
83 QMessageBox::Ok, Qt::NoButton );
91QgsRunProcess::~QgsRunProcess()
96void QgsRunProcess::die()
102void QgsRunProcess::stdoutAvailable()
104 const QByteArray bytes( mProcess->readAllStandardOutput() );
105 QTextCodec *codec = QTextCodec::codecForLocale();
106 const QString line( codec->toUnicode( bytes ) );
109 mOutput->appendMessage( line );
112void QgsRunProcess::stderrAvailable()
114 const QByteArray bytes( mProcess->readAllStandardOutput() );
115 QTextCodec *codec = QTextCodec::codecForLocale();
116 const QString line( codec->toUnicode( bytes ) );
119 mOutput->appendMessage(
"<font color=red>" + line +
"</font>" );
122void QgsRunProcess::processExit(
int, QProcess::ExitStatus )
133 mOutput->appendMessage(
"<b>" + tr(
"Done" ) +
"</b>" );
142void QgsRunProcess::dialogGone()
153 disconnect( mProcess, &QProcess::errorOccurred,
this, &QgsRunProcess::processError );
154 disconnect( mProcess, &QProcess::readyReadStandardOutput,
this, &QgsRunProcess::stdoutAvailable );
155 disconnect( mProcess, &QProcess::readyReadStandardError,
this, &QgsRunProcess::stderrAvailable );
156 disconnect( mProcess,
static_cast < void ( QProcess::* )(
int, QProcess::ExitStatus )
>( &QProcess::finished ),
this, &QgsRunProcess::processExit );
161void QgsRunProcess::processError( QProcess::ProcessError err )
163 if ( err == QProcess::FailedToStart )
178 return QProcess::splitCommand( command );
181QgsRunProcess::QgsRunProcess(
const QString &action,
bool )
187QgsRunProcess::~QgsRunProcess()
193 return QStringList();
202#if QT_CONFIG(process)
203QgsBlockingProcess::QgsBlockingProcess(
const QString &process,
const QStringList &arguments )
205 , mProcess( process )
206 , mArguments( arguments )
211int QgsBlockingProcess::run(
QgsFeedback *feedback )
213 const bool requestMadeFromMainThread = QThread::currentThread() == QCoreApplication::instance()->thread();
216 QProcess::ExitStatus exitStatus = QProcess::NormalExit;
217 QProcess::ProcessError error = QProcess::UnknownError;
219 const std::function<void()> runFunction = [
this, &result, &exitStatus, &error, feedback]()
226 const QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
227 p.setProcessEnvironment( env );
233 connect( qApp, &QCoreApplication::aboutToQuit, &loop, &QEventLoop::quit, Qt::DirectConnection );
247 connect( &p, qOverload< int, QProcess::ExitStatus >( &QProcess::finished ),
this, [&loop, &result, &exitStatus](
int res, QProcess::ExitStatus st )
252 }, Qt::DirectConnection );
254 connect( &p, &QProcess::readyReadStandardOutput, &p, [&p,
this]
256 const QByteArray ba = p.readAllStandardOutput();
257 mStdoutHandler( ba );
259 connect( &p, &QProcess::readyReadStandardError, &p, [&p,
this]
261 const QByteArray ba = p.readAllStandardError();
262 mStderrHandler( ba );
264 p.start( mProcess, mArguments, QProcess::Unbuffered | QProcess::ReadWrite );
265 if ( !p.waitForStarted() )
268 exitStatus = QProcess::NormalExit;
276 mStdoutHandler( p.readAllStandardOutput() );
277 mStderrHandler( p.readAllStandardError() );
280 if ( requestMadeFromMainThread )
282 std::unique_ptr<ProcessThread> processThread = std::make_unique<ProcessThread>( runFunction );
283 processThread->start();
285 processThread->wait();
292 mExitStatus = exitStatus;
293 mProcessError = error;
297QProcess::ExitStatus QgsBlockingProcess::exitStatus()
const
302QProcess::ProcessError QgsBlockingProcess::processError()
const
304 return mProcessError;
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
void canceled()
Internal routines can connect to this signal if they use event loop.
Interface for showing messages from QGIS in GUI independent way.
static QgsMessageOutput * createMessageOutput()
function that returns new class derived from QgsMessageOutput (don't forget to delete it then if show...
virtual void setMessage(const QString &message, MessageType msgType)=0
Sets message, it won't be displayed until.
static QStringList splitCommand(const QString &command)
Splits the string command into a list of tokens, and returns the list.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)