QGIS API Documentation 3.41.0-Master (3291e9ff48b)
Loading...
Searching...
No Matches
qgsprocessingfeedback.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingfeedback.cpp
3 -------------------------
4 begin : June 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "moc_qgsprocessingfeedback.cpp"
20#include "qgsgeos.h"
22#include "qgsmessagelog.h"
23#include <ogr_api.h>
24#include <gdal_version.h>
25#include <proj.h>
26
27#ifdef HAVE_PDAL_QGIS
28#include <pdal/pdal.hpp>
29#endif
30
32 : mLogFeedback( logFeedback )
33{
34
35}
36
37void QgsProcessingFeedback::setProgressText( const QString &text )
38{
39 mHtmlLog.append( text.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) + QStringLiteral( "<br/>" ) );
40 mTextLog.append( text + '\n' );
41}
42
43void QgsProcessingFeedback::log( const QString &htmlMessage, const QString &textMessage )
44{
45 constexpr int MESSAGE_COUNT_LIMIT = 10000;
46 // Avoid logging too many messages, which might blow memory.
47 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
48 return;
49 ++mMessageLoggedCount;
50 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
51 {
52 mHtmlLog.append( QStringLiteral( "<span style=\"color:red\">%1</span><br/>" ).arg( tr( "Message log truncated" ) ) );
53 mTextLog.append( tr( "Message log truncated" ) + '\n' );
54 }
55 else
56 {
57 mHtmlLog.append( htmlMessage );
58 mTextLog.append( textMessage );
59 }
60}
61
62
63void QgsProcessingFeedback::reportError( const QString &error, bool )
64{
65 if ( mLogFeedback )
66 QgsMessageLog::logMessage( error, tr( "Processing" ), Qgis::MessageLevel::Critical );
67
68 log( QStringLiteral( "<span style=\"color:red\">%1</span><br/>" ).arg( error.toHtmlEscaped() ).replace( '\n', QLatin1String( "<br>" ) ),
69 error + '\n' );
70}
71
72void QgsProcessingFeedback::pushWarning( const QString &warning )
73{
74 if ( mLogFeedback )
75 QgsMessageLog::logMessage( warning, tr( "Processing" ), Qgis::MessageLevel::Warning );
76
77 log( QStringLiteral( "<span style=\"color:#b85a20;\">%1</span><br/>" ).arg( warning.toHtmlEscaped() ).replace( '\n', QLatin1String( "<br>" ) ) + QStringLiteral( "<br/>" ),
78 warning + '\n' );
79}
80
81void QgsProcessingFeedback::pushInfo( const QString &info )
82{
83 if ( mLogFeedback )
84 QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
85
86 mHtmlLog.append( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) + QStringLiteral( "<br/>" ) );
87 mTextLog.append( info + '\n' );
88}
89
90void QgsProcessingFeedback::pushFormattedMessage( const QString &html, const QString &text )
91{
92 if ( mLogFeedback )
93 QgsMessageLog::logMessage( text, tr( "Processing" ), Qgis::MessageLevel::Info );
94
95 mHtmlLog.append( html + QStringLiteral( "<br/>" ) );
96 mTextLog.append( text + '\n' );
97}
98
99void QgsProcessingFeedback::pushCommandInfo( const QString &info )
100{
101 if ( mLogFeedback )
102 QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
103
104 log( QStringLiteral( "<code>%1</code><br/>" ).arg( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) ),
105 info + '\n' );
106}
107
108void QgsProcessingFeedback::pushDebugInfo( const QString &info )
109{
110 if ( mLogFeedback )
111 QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
112
113 log( QStringLiteral( "<span style=\"color:#777\">%1</span><br/>" ).arg( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) ),
114 info + '\n' );
115}
116
117void QgsProcessingFeedback::pushConsoleInfo( const QString &info )
118{
119 if ( mLogFeedback )
120 QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
121
122 log( QStringLiteral( "<code style=\"color:#777\">%1</code><br/>" ).arg( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) ),
123 info + '\n' );
124}
125
127{
128 pushDebugInfo( tr( "QGIS version: %1" ).arg( Qgis::version() ) );
129 if ( QString( Qgis::devVersion() ) != QLatin1String( "exported" ) )
130 {
131 pushDebugInfo( tr( "QGIS code revision: %1" ).arg( Qgis::devVersion() ) );
132 }
133 pushDebugInfo( tr( "Qt version: %1" ).arg( qVersion() ) );
134 pushDebugInfo( tr( "Python version: %1" ).arg( PYTHON_VERSION ) );
135 pushDebugInfo( tr( "GDAL version: %1" ).arg( GDALVersionInfo( "RELEASE_NAME" ) ) );
136 pushDebugInfo( tr( "GEOS version: %1" ).arg( GEOSversion() ) );
137
138 const PJ_INFO info = proj_info();
139 pushDebugInfo( tr( "PROJ version: %1" ).arg( info.release ) );
140
141#ifdef HAVE_PDAL_QGIS
142#if PDAL_VERSION_MAJOR_INT > 1 || (PDAL_VERSION_MAJOR_INT == 1 && PDAL_VERSION_MINOR_INT >= 7)
143 pushDebugInfo( tr( "PDAL version: %1" ).arg( QString::fromStdString( pdal::Config::fullVersionString() ) ) );
144#else
145 pushDebugInfo( tr( "PDAL version: %1" ).arg( QString::fromStdString( pdal::GetFullVersionString() ) ) );
146#endif
147#endif
148
149 if ( provider && !provider->versionInfo().isEmpty() )
150 {
151 pushDebugInfo( tr( "%1 version: %2" ).arg( provider->name(), provider->versionInfo() ) );
152 }
153}
154
156{
157 if ( results.empty() )
158 return;
159
160 pushInfo( tr( "Results:" ) );
161
162 const QList< const QgsProcessingOutputDefinition * > outputs = algorithm->outputDefinitions();
163 for ( const QgsProcessingOutputDefinition *output : outputs )
164 {
165 const QString outputName = output->name();
166 if ( outputName == QLatin1String( "CHILD_RESULTS" ) || outputName == QLatin1String( "CHILD_INPUTS" ) )
167 continue;
168
169 if ( !results.contains( outputName ) )
170 continue;
171
172 bool ok = false;
173 const QString textValue = output->valueAsString( results.value( output->name() ), context, ok );
174 const QString formattedValue = output->valueAsFormattedString( results.value( output->name() ), context, ok );
175 if ( ok )
176 {
177 pushFormattedMessage( QStringLiteral( "<code>&nbsp;&nbsp;%1: %2</code>" ).arg( output->name(), formattedValue ),
178 QStringLiteral( " %1: %2" ).arg( output->name(), textValue ) );
179 }
180 }
181}
182
184{
185 return mHtmlLog;
186}
187
189{
190 return mTextLog;
191}
192
193
195 : mChildSteps( childAlgorithmCount )
196 , mFeedback( feedback )
197{
198 connect( mFeedback, &QgsFeedback::canceled, this, &QgsFeedback::cancel, Qt::DirectConnection );
199 connect( this, &QgsFeedback::progressChanged, this, &QgsProcessingMultiStepFeedback::updateOverallProgress );
200}
201
203{
204 mCurrentStep = step;
205 mFeedback->setProgress( 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps );
206}
207
209{
210 mFeedback->setProgressText( text );
211}
212
213void QgsProcessingMultiStepFeedback::reportError( const QString &error, bool fatalError )
214{
215 mFeedback->reportError( error, fatalError );
216}
217
219{
220 mFeedback->pushWarning( warning );
221}
222
224{
225 mFeedback->pushInfo( info );
226}
227
229{
230 mFeedback->pushCommandInfo( info );
231}
232
234{
235 mFeedback->pushDebugInfo( info );
236}
237
239{
240 mFeedback->pushConsoleInfo( info );
241}
242
243void QgsProcessingMultiStepFeedback::pushFormattedMessage( const QString &html, const QString &text )
244{
245 mFeedback->pushFormattedMessage( html, text );
246}
247
249{
250 return mFeedback->htmlLog();
251}
252
254{
255 return mFeedback->textLog();
256}
257
258void QgsProcessingMultiStepFeedback::updateOverallProgress( double progress )
259{
260 const double baseProgress = 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps;
261 const double currentAlgorithmProgress = progress / mChildSteps;
262 mFeedback->setProgress( baseProgress + currentAlgorithmProgress );
263}
264
static QString version()
Version string.
Definition qgis.cpp:259
@ Warning
Warning message.
Definition qgis.h:156
@ Critical
Critical/error message.
Definition qgis.h:157
@ Info
Information message.
Definition qgis.h:155
static QString devVersion()
The development version.
Definition qgis.cpp:276
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
void canceled()
Internal routines can connect to this signal if they use event loop.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
double progress() const
Returns the current progress reported by the feedback object.
Definition qgsfeedback.h:77
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Abstract base class for processing algorithms.
QgsProcessingOutputDefinitions outputDefinitions() const
Returns an ordered list of output definitions utilized by the algorithm.
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
virtual void pushCommandInfo(const QString &info)
Pushes an informational message containing a command from the algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
void pushVersionInfo(const QgsProcessingProvider *provider=nullptr)
Pushes a summary of the QGIS (and underlying library) version information to the log.
virtual QString textLog() const
Returns the plain text contents of the log, which contains all messages pushed to the feedback object...
QgsProcessingFeedback(bool logFeedback=true)
Constructor for QgsProcessingFeedback.
void pushFormattedResults(const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap &results)
Pushes a summary of the execution results to the log.
virtual QString htmlLog() const
Returns the HTML formatted contents of the log, which contains all messages pushed to the feedback ob...
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
virtual void pushFormattedMessage(const QString &html, const QString &text)
Pushes a pre-formatted message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
virtual void pushConsoleInfo(const QString &info)
Pushes a console feedback message from the algorithm.
virtual void setProgressText(const QString &text)
Sets a progress report text string.
QgsProcessingMultiStepFeedback(int steps, QgsProcessingFeedback *feedback)
Constructor for QgsProcessingMultiStepFeedback, for a process with the specified number of steps.
void pushDebugInfo(const QString &info) override
Pushes an informational message containing debugging helpers from the algorithm.
void pushFormattedMessage(const QString &html, const QString &text) override
Pushes a pre-formatted message from the algorithm.
void pushConsoleInfo(const QString &info) override
Pushes a console feedback message from the algorithm.
void pushInfo(const QString &info) override
Pushes a general informational message from the algorithm.
void pushWarning(const QString &warning) override
Pushes a warning informational message from the algorithm.
void setProgressText(const QString &text) override
Sets a progress report text string.
void setCurrentStep(int step)
Sets the step which is being executed.
void reportError(const QString &error, bool fatalError=false) override
Reports that the algorithm encountered an error while executing.
QString textLog() const override
Returns the plain text contents of the log, which contains all messages pushed to the feedback object...
void pushCommandInfo(const QString &info) override
Pushes an informational message containing a command from the algorithm.
QString htmlLog() const override
Returns the HTML formatted contents of the log, which contains all messages pushed to the feedback ob...
Base class for the definition of processing outputs.
Abstract base class for processing providers.
virtual QString versionInfo() const
Returns a version information string for the provider, or an empty string if this is not applicable (...
virtual QString name() const =0
Returns the provider name, which is used to describe the provider within the GUI.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call