QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsrunprocess.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsrunprocess.h
3
4 A class that runs an external program
5
6 -------------------
7 begin : Jan 2005
8 copyright : (C) 2005 by Gavin Macaulay
9 email : gavin at macaulay dot co dot nz
10 ***************************************************************************/
11
12/***************************************************************************
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 ***************************************************************************/
20
21#ifndef QGSRUNPROCESS_H
22#define QGSRUNPROCESS_H
23
24#include <QObject>
25
26#if QT_CONFIG(process)
27#include <QProcess>
28#endif
29
30#include <QThread>
31
32#include "qgis_core.h"
33#include "qgis_sip.h"
34
35class QgsFeedback;
37
48class CORE_EXPORT QgsRunProcess: public QObject SIP_NODEFAULTCTORS
49{
50 Q_OBJECT
51
52 public:
53 // This class deletes itself, so to ensure that it is only created
54 // using new, the Named Constructor Idiom is used, and one needs to
55 // use the create() static function to get an instance of this class.
56
57 // The action argument contains string with the command.
58 // If capture is true, the standard output and error from the process
59 // will be sent to QgsMessageOutput - usually a dialog box.
60 static QgsRunProcess *create( const QString &action, bool capture ) SIP_FACTORY
61 { return new QgsRunProcess( action, capture ); }
62
72 static QStringList splitCommand( const QString &command );
73
74 private:
75 QgsRunProcess( const QString &action, bool capture ) SIP_FORCE;
76 ~QgsRunProcess() override SIP_FORCE;
77
78#if QT_CONFIG(process)
79 // Deletes the instance of the class
80 void die();
81
82 std::unique_ptr<QProcess> mProcess;
83 QgsMessageOutput *mOutput = nullptr;
84 QString mCommand;
85
86 public slots:
87 void stdoutAvailable();
88 void stderrAvailable();
89 void processError( QProcess::ProcessError );
90 void processExit( int, QProcess::ExitStatus );
91 void dialogGone();
92#endif // !(QT_CONFIG(process)
93};
94
95#if QT_CONFIG(process)
96
110class CORE_EXPORT QgsBlockingProcess : public QObject
111{
112 Q_OBJECT
113
114 public:
115
121 QgsBlockingProcess( const QString &program, const QStringList &arguments );
122
123#ifndef SIP_RUN
124
128 void setStdOutHandler( const std::function< void( const QByteArray & ) > &handler ) { mStdoutHandler = handler; }
129#else
130
134 void setStdOutHandler( SIP_PYCALLABLE / AllowNone / );
135 % MethodCode
136 Py_BEGIN_ALLOW_THREADS
137
138 sipCpp->setStdOutHandler( [a0]( const QByteArray &arg )
139 {
140 SIP_BLOCK_THREADS
141 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QByteArray, NULL ) );
142 SIP_UNBLOCK_THREADS
143 } );
144
145 Py_END_ALLOW_THREADS
146 % End
147#endif
148
149#ifndef SIP_RUN
150
154 void setStdErrHandler( const std::function< void( const QByteArray & ) > &handler ) { mStderrHandler = handler; }
155#else
156
160 void setStdErrHandler( SIP_PYCALLABLE / AllowNone / );
161 % MethodCode
162 Py_BEGIN_ALLOW_THREADS
163
164 sipCpp->setStdErrHandler( [a0]( const QByteArray &arg )
165 {
166 SIP_BLOCK_THREADS
167 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QByteArray, NULL ) );
168 SIP_UNBLOCK_THREADS
169 } );
170
171 Py_END_ALLOW_THREADS
172 % End
173#endif
174
182 int run( QgsFeedback *feedback = nullptr );
183
187 QProcess::ExitStatus exitStatus() const;
188
194 QProcess::ProcessError processError() const;
195
196 private:
197
198 QString mProcess;
199 QStringList mArguments;
200 std::function< void( const QByteArray & ) > mStdoutHandler;
201 std::function< void( const QByteArray & ) > mStderrHandler;
202
203 QProcess::ExitStatus mExitStatus = QProcess::NormalExit;
204 QProcess::ProcessError mProcessError = QProcess::UnknownError;
205};
206
207#endif // QT_CONFIG(process)
208
210#ifndef SIP_RUN
211
212class ProcessThread : public QThread
213{
214 Q_OBJECT
215
216 public:
217 ProcessThread( const std::function<void()> &function, QObject *parent = nullptr )
218 : QThread( parent )
219 , mFunction( function )
220 {
221 }
222
223 void run() override
224 {
225 mFunction();
226 }
227
228 private:
229 std::function<void()> mFunction;
230};
231
232#endif
234
235
236#endif
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Interface for showing messages from QGIS in GUI independent way.
Executes an external program/script.
static QgsRunProcess * create(const QString &action, bool capture)
#define SIP_FORCE
Definition qgis_sip.h:139
#define SIP_FACTORY
Definition qgis_sip.h:84
#define SIP_NODEFAULTCTORS
Definition qgis_sip.h:109