QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
18 #include "qgsprocessingfeedback.h"
19 #include "qgsgeos.h"
20 #include "qgsprocessingprovider.h"
21 #include <ogr_api.h>
22 #include <gdal_version.h>
23 #include <proj.h>
24 
25 #ifdef HAVE_PDAL_QGIS
26 #include <pdal/pdal.hpp>
27 #endif
28 
30  : mLogFeedback( logFeedback )
31 {
32 
33 }
34 
36 {
37 }
38 
39 void QgsProcessingFeedback::log( const QString &htmlMessage, const QString &textMessage )
40 {
41  constexpr int MESSAGE_COUNT_LIMIT = 10000;
42  // Avoid logging too many messages, which might blow memory.
43  if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
44  return;
45  ++mMessageLoggedCount;
46  if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
47  {
48  mHtmlLog.append( QStringLiteral( "<span style=\"color:red\">%1</span><br/>" ).arg( tr( "Message log truncated" ) ) );
49  mTextLog.append( tr( "Message log truncated" ) + '\n' );
50  }
51  else
52  {
53  mHtmlLog.append( htmlMessage );
54  mTextLog.append( textMessage );
55  }
56 }
57 
58 
59 void QgsProcessingFeedback::reportError( const QString &error, bool )
60 {
61  if ( mLogFeedback )
62  QgsMessageLog::logMessage( error, tr( "Processing" ), Qgis::MessageLevel::Critical );
63 
64  log( QStringLiteral( "<span style=\"color:red\">%1</span><br/>" ).arg( error.toHtmlEscaped() ).replace( '\n', QLatin1String( "<br>" ) ),
65  error + '\n' );
66 }
67 
68 void QgsProcessingFeedback::pushWarning( const QString &warning )
69 {
70  if ( mLogFeedback )
71  QgsMessageLog::logMessage( warning, tr( "Processing" ), Qgis::MessageLevel::Warning );
72 
73  log( QStringLiteral( "<span style=\"color:#b85a20;\">%1</span><br/>" ).arg( warning.toHtmlEscaped() ).replace( '\n', QLatin1String( "<br>" ) ) + QStringLiteral( "<br/>" ),
74  warning + '\n' );
75 }
76 
77 void QgsProcessingFeedback::pushInfo( const QString &info )
78 {
79  if ( mLogFeedback )
80  QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
81 
82  mHtmlLog.append( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) + QStringLiteral( "<br/>" ) );
83  mTextLog.append( info + '\n' );
84 }
85 
86 void QgsProcessingFeedback::pushCommandInfo( const QString &info )
87 {
88  if ( mLogFeedback )
89  QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
90 
91  log( QStringLiteral( "<code>%1</code><br/>" ).arg( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) ),
92  info + '\n' );
93 }
94 
95 void QgsProcessingFeedback::pushDebugInfo( const QString &info )
96 {
97  if ( mLogFeedback )
98  QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
99 
100  log( QStringLiteral( "<span style=\"color:#777\">%1</span><br/>" ).arg( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) ),
101  info + '\n' );
102 }
103 
104 void QgsProcessingFeedback::pushConsoleInfo( const QString &info )
105 {
106  if ( mLogFeedback )
107  QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::MessageLevel::Info );
108 
109  log( QStringLiteral( "<code style=\"color:#777\">%1</code><br/>" ).arg( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) ),
110  info + '\n' );
111 }
112 
114 {
115  pushDebugInfo( tr( "QGIS version: %1" ).arg( Qgis::version() ) );
116  if ( QString( Qgis::devVersion() ) != QLatin1String( "exported" ) )
117  {
118  pushDebugInfo( tr( "QGIS code revision: %1" ).arg( Qgis::devVersion() ) );
119  }
120  pushDebugInfo( tr( "Qt version: %1" ).arg( qVersion() ) );
121  pushDebugInfo( tr( "Python version: %1" ).arg( PYTHON_VERSION ) );
122  pushDebugInfo( tr( "GDAL version: %1" ).arg( GDALVersionInfo( "RELEASE_NAME" ) ) );
123  pushDebugInfo( tr( "GEOS version: %1" ).arg( GEOSversion() ) );
124 
125  const PJ_INFO info = proj_info();
126  pushDebugInfo( tr( "PROJ version: %1" ).arg( info.release ) );
127 
128 #ifdef HAVE_PDAL_QGIS
129 #if PDAL_VERSION_MAJOR_INT > 1 || (PDAL_VERSION_MAJOR_INT == 1 && PDAL_VERSION_MINOR_INT >= 7)
130  pushDebugInfo( tr( "PDAL version: %1" ).arg( QString::fromStdString( pdal::Config::fullVersionString() ) ) );
131 #else
132  pushDebugInfo( tr( "PDAL version: %1" ).arg( QString::fromStdString( pdal::GetFullVersionString() ) ) );
133 #endif
134 #endif
135 
136  if ( provider && !provider->versionInfo().isEmpty() )
137  {
138  pushDebugInfo( tr( "%1 version: %2" ).arg( provider->name(), provider->versionInfo() ) );
139  }
140 }
141 
143 {
144  return mHtmlLog;
145 }
146 
148 {
149  return mTextLog;
150 }
151 
152 
154  : mChildSteps( childAlgorithmCount )
155  , mFeedback( feedback )
156 {
157  connect( mFeedback, &QgsFeedback::canceled, this, &QgsFeedback::cancel, Qt::DirectConnection );
158  connect( this, &QgsFeedback::progressChanged, this, &QgsProcessingMultiStepFeedback::updateOverallProgress );
159 }
160 
162 {
163  mCurrentStep = step;
164  mFeedback->setProgress( 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps );
165 }
166 
168 {
169  mFeedback->setProgressText( text );
170 }
171 
172 void QgsProcessingMultiStepFeedback::reportError( const QString &error, bool fatalError )
173 {
174  mFeedback->reportError( error, fatalError );
175 }
176 
177 void QgsProcessingMultiStepFeedback::pushWarning( const QString &warning )
178 {
179  mFeedback->pushWarning( warning );
180 }
181 
182 void QgsProcessingMultiStepFeedback::pushInfo( const QString &info )
183 {
184  mFeedback->pushInfo( info );
185 }
186 
188 {
189  mFeedback->pushCommandInfo( info );
190 }
191 
193 {
194  mFeedback->pushDebugInfo( info );
195 }
196 
198 {
199  mFeedback->pushConsoleInfo( info );
200 }
201 
203 {
204  return mFeedback->htmlLog();
205 }
206 
208 {
209  return mFeedback->textLog();
210 }
211 
212 void QgsProcessingMultiStepFeedback::updateOverallProgress( double progress )
213 {
214  const double baseProgress = 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps;
215  const double currentAlgorithmProgress = progress / mChildSteps;
216  mFeedback->setProgress( baseProgress + currentAlgorithmProgress );
217 }
218 
static QString version()
Version string.
Definition: qgis.cpp:277
static QString devVersion()
The development version.
Definition: qgis.cpp:294
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.
double progress() const SIP_HOLDGIL
Returns the current progress reported by the feedback object.
Definition: qgsfeedback.h:80
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
Definition: qgsfeedback.h:108
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:63
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).
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.
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 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 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...
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.