QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:76
QgsProcessingFeedback::setProgressText
virtual void setProgressText(const QString &text)
Sets a progress report text string.
Definition: qgsprocessingfeedback.cpp:35
QgsProcessingFeedback::QgsProcessingFeedback
QgsProcessingFeedback(bool logFeedback=true)
Constructor for QgsProcessingFeedback.
Definition: qgsprocessingfeedback.cpp:29
Qgis::version
static QString version()
Version string.
Definition: qgis.cpp:277
QgsProcessingFeedback::pushCommandInfo
virtual void pushCommandInfo(const QString &info)
Pushes an informational message containing a command from the algorithm.
Definition: qgsprocessingfeedback.cpp:86
QgsProcessingMultiStepFeedback::pushWarning
void pushWarning(const QString &warning) override
Pushes a warning informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:177
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsFeedback::canceled
void canceled()
Internal routines can connect to this signal if they use event loop.
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:77
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:59
QgsProcessingProvider
Abstract base class for processing providers.
Definition: qgsprocessingprovider.h:35
QgsProcessingMultiStepFeedback::reportError
void reportError(const QString &error, bool fatalError=false) override
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:172
QgsProcessingFeedback::textLog
virtual QString textLog() const
Returns the plain text contents of the log, which contains all messages pushed to the feedback object...
Definition: qgsprocessingfeedback.cpp:147
QgsFeedback::cancel
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
Definition: qgsfeedback.h:121
QgsProcessingProvider::versionInfo
virtual QString versionInfo() const
Returns a version information string for the provider, or an empty string if this is not applicable (...
Definition: qgsprocessingprovider.cpp:59
QgsProcessingMultiStepFeedback::pushConsoleInfo
void pushConsoleInfo(const QString &info) override
Pushes a console feedback message from the algorithm.
Definition: qgsprocessingfeedback.cpp:197
QgsProcessingFeedback::pushConsoleInfo
virtual void pushConsoleInfo(const QString &info)
Pushes a console feedback message from the algorithm.
Definition: qgsprocessingfeedback.cpp:104
QgsProcessingMultiStepFeedback::htmlLog
QString htmlLog() const override
Returns the HTML formatted contents of the log, which contains all messages pushed to the feedback ob...
Definition: qgsprocessingfeedback.cpp:202
QgsProcessingProvider::name
virtual QString name() const =0
Returns the provider name, which is used to describe the provider within the GUI.
QgsProcessingMultiStepFeedback::QgsProcessingMultiStepFeedback
QgsProcessingMultiStepFeedback(int steps, QgsProcessingFeedback *feedback)
Constructor for QgsProcessingMultiStepFeedback, for a process with the specified number of steps.
Definition: qgsprocessingfeedback.cpp:153
QgsProcessingFeedback::pushVersionInfo
void pushVersionInfo(const QgsProcessingProvider *provider=nullptr)
Pushes a summary of the QGIS (and underlying library) version information to the log.
Definition: qgsprocessingfeedback.cpp:113
qgsprocessingfeedback.h
QgsMessageLog::logMessage
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).
Definition: qgsmessagelog.cpp:27
QgsFeedback::progressChanged
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
QgsProcessingMultiStepFeedback::pushDebugInfo
void pushDebugInfo(const QString &info) override
Pushes an informational message containing debugging helpers from the algorithm.
Definition: qgsprocessingfeedback.cpp:192
QgsProcessingFeedback::pushDebugInfo
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
Definition: qgsprocessingfeedback.cpp:95
QgsProcessingFeedback::htmlLog
virtual QString htmlLog() const
Returns the HTML formatted contents of the log, which contains all messages pushed to the feedback ob...
Definition: qgsprocessingfeedback.cpp:142
QgsFeedback::progress
double progress() const SIP_HOLDGIL
Returns the current progress reported by the feedback object.
Definition: qgsfeedback.h:93
QgsProcessingMultiStepFeedback::textLog
QString textLog() const override
Returns the plain text contents of the log, which contains all messages pushed to the feedback object...
Definition: qgsprocessingfeedback.cpp:207
QgsProcessingMultiStepFeedback::pushCommandInfo
void pushCommandInfo(const QString &info) override
Pushes an informational message containing a command from the algorithm.
Definition: qgsprocessingfeedback.cpp:187
QgsProcessingMultiStepFeedback::pushInfo
void pushInfo(const QString &info) override
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:182
QgsProcessingMultiStepFeedback::setCurrentStep
void setCurrentStep(int step)
Sets the step which is being executed.
Definition: qgsprocessingfeedback.cpp:161
QgsProcessingMultiStepFeedback::setProgressText
void setProgressText(const QString &text) override
Sets a progress report text string.
Definition: qgsprocessingfeedback.cpp:167
qgsprocessingprovider.h
QgsProcessingFeedback::pushWarning
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:68
qgsgeos.h
Qgis::devVersion
static QString devVersion()
The development version.
Definition: qgis.cpp:294