QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 { return new QgsRunProcess( action, capture ); }
61
71 static QStringList splitCommand( const QString &command );
72
73 private:
74 QgsRunProcess( const QString &action, bool capture ) SIP_FORCE;
75 ~QgsRunProcess() override SIP_FORCE;
76
77#if QT_CONFIG( process )
78 // Deletes the instance of the class
79 void die();
80
81 std::unique_ptr<QProcess> mProcess;
82 QgsMessageOutput *mOutput = nullptr;
83 QString mCommand;
84
85 public slots:
86 void stdoutAvailable();
87 void stderrAvailable();
88 void processError( QProcess::ProcessError );
89 void processExit( int, QProcess::ExitStatus );
90 void dialogGone();
91#endif // !(QT_CONFIG(process)
92};
93
94#if QT_CONFIG( process )
95
109class CORE_EXPORT QgsBlockingProcess : public QObject
110{
111 Q_OBJECT
112
113 public:
119 QgsBlockingProcess( const QString &program, const QStringList &arguments );
120
121#ifndef SIP_RUN
122
126 void setStdOutHandler( const std::function< void( const QByteArray & ) > &handler ) { mStdoutHandler = handler; }
127#else
128 // clang-format off
129
133 void setStdOutHandler( SIP_PYCALLABLE / AllowNone / );
134 % MethodCode
135 Py_BEGIN_ALLOW_THREADS
136
137 sipCpp->setStdOutHandler( [a0]( const QByteArray &arg )
138 {
139 SIP_BLOCK_THREADS
140 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QByteArray, NULL ) );
141 SIP_UNBLOCK_THREADS
142 } );
143
144 Py_END_ALLOW_THREADS
145 % End
146// clang-format on
147#endif
148
149#ifndef SIP_RUN
150
154 void setStdErrHandler( const std::function< void( const QByteArray & ) > &handler ) { mStderrHandler = handler; }
155#else
156 // clang-format off
157
161 void setStdErrHandler( SIP_PYCALLABLE / AllowNone / );
162 % MethodCode
163 Py_BEGIN_ALLOW_THREADS
164
165 sipCpp->setStdErrHandler( [a0]( const QByteArray &arg )
166 {
167 SIP_BLOCK_THREADS
168 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QByteArray, NULL ) );
169 SIP_UNBLOCK_THREADS
170 } );
171
172 Py_END_ALLOW_THREADS
173 % End
174// clang-format on
175#endif
176
184 int run( QgsFeedback *feedback = nullptr );
185
189 QProcess::ExitStatus exitStatus() const;
190
196 QProcess::ProcessError processError() const;
197
198 private:
199 QString mProcess;
200 QStringList mArguments;
201 std::function< void( const QByteArray & ) > mStdoutHandler;
202 std::function< void( const QByteArray & ) > mStderrHandler;
203
204 QProcess::ExitStatus mExitStatus = QProcess::NormalExit;
205 QProcess::ProcessError mProcessError = QProcess::UnknownError;
206};
207
208#endif // QT_CONFIG(process)
209
211#ifndef SIP_RUN
212
213class ProcessThread : public QThread
214{
215 Q_OBJECT
216
217 public:
218 ProcessThread( const std::function<void()> &function, QObject *parent = nullptr )
219 : QThread( parent )
220 , mFunction( function )
221 {}
222
223 void run() override { mFunction(); }
224
225 private:
226 std::function<void()> mFunction;
227};
228
229#endif
231
232
233#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:138
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_NODEFAULTCTORS
Definition qgis_sip.h:108