QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 #if PROJ_VERSION_MAJOR > 4
24 #include <proj.h>
25 #else
26 #include <proj_api.h>
27 #endif
28 
30  : mLogFeedback( logFeedback )
31 {
32 
33 }
34 
36 {
37 }
38 
39 void QgsProcessingFeedback::reportError( const QString &error, bool )
40 {
41  if ( mLogFeedback )
42  QgsMessageLog::logMessage( error, tr( "Processing" ), Qgis::Critical );
43 
44  mHtmlLog.append( QStringLiteral( "<span style=\"color:red\">%1</span><br/>" ).arg( error.toHtmlEscaped() ).replace( '\n', QLatin1String( "<br>" ) ) );
45  mTextLog.append( error + '\n' );
46 }
47 
48 void QgsProcessingFeedback::pushInfo( const QString &info )
49 {
50  if ( mLogFeedback )
51  QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::Info );
52 
53  mHtmlLog.append( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) + QStringLiteral( "<br/>" ) );
54  mTextLog.append( info + '\n' );
55 }
56 
57 void QgsProcessingFeedback::pushCommandInfo( const QString &info )
58 {
59  if ( mLogFeedback )
60  QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::Info );
61 
62  mHtmlLog.append( QStringLiteral( "<code>%1</code><br/>" ).arg( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) ) );
63  mTextLog.append( info + '\n' );
64 }
65 
66 void QgsProcessingFeedback::pushDebugInfo( const QString &info )
67 {
68  if ( mLogFeedback )
69  QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::Info );
70 
71  mHtmlLog.append( QStringLiteral( "<span style=\"color:#777\">%1</span><br/>" ).arg( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) ) );
72  mTextLog.append( info + '\n' );
73 }
74 
75 void QgsProcessingFeedback::pushConsoleInfo( const QString &info )
76 {
77  if ( mLogFeedback )
78  QgsMessageLog::logMessage( info, tr( "Processing" ), Qgis::Info );
79 
80  mHtmlLog.append( QStringLiteral( "<code style=\"color:#777\">%1</code><br/>" ).arg( info.toHtmlEscaped().replace( '\n', QLatin1String( "<br>" ) ) ) );
81  mTextLog.append( info + '\n' );
82 }
83 
85 {
86  pushDebugInfo( tr( "QGIS version: %1" ).arg( Qgis::version() ) );
87  if ( QString( Qgis::devVersion() ) != QLatin1String( "exported" ) )
88  {
89  pushDebugInfo( tr( "QGIS code revision: %1" ).arg( Qgis::devVersion() ) );
90  }
91  pushDebugInfo( tr( "Qt version: %1" ).arg( qVersion() ) );
92  pushDebugInfo( tr( "GDAL version: %1" ).arg( GDALVersionInfo( "RELEASE_NAME" ) ) );
93  pushDebugInfo( tr( "GEOS version: %1" ).arg( GEOSversion() ) );
94 
95 #if PROJ_VERSION_MAJOR > 4
96  PJ_INFO info = proj_info();
97  pushDebugInfo( tr( "PROJ version: %1" ).arg( info.release ) );
98 #else
99  pushDebugInfo( tr( "PROJ version: %1" ).arg( PJ_VERSION ) );
100 #endif
101  if ( provider && !provider->versionInfo().isEmpty() )
102  {
103  pushDebugInfo( tr( "%1 version: %2" ).arg( provider->name(), provider->versionInfo() ) );
104  }
105 }
106 
108 {
109  return mHtmlLog;
110 }
111 
113 {
114  return mTextLog;
115 }
116 
117 
119  : mChildSteps( childAlgorithmCount )
120  , mFeedback( feedback )
121 {
122  connect( mFeedback, &QgsFeedback::canceled, this, &QgsFeedback::cancel, Qt::DirectConnection );
123  connect( this, &QgsFeedback::progressChanged, this, &QgsProcessingMultiStepFeedback::updateOverallProgress );
124 }
125 
127 {
128  mCurrentStep = step;
129  mFeedback->setProgress( 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps );
130 }
131 
133 {
134  mFeedback->setProgressText( text );
135 }
136 
137 void QgsProcessingMultiStepFeedback::reportError( const QString &error, bool fatalError )
138 {
139  mFeedback->reportError( error, fatalError );
140 }
141 
142 void QgsProcessingMultiStepFeedback::pushInfo( const QString &info )
143 {
144  mFeedback->pushInfo( info );
145 }
146 
148 {
149  mFeedback->pushCommandInfo( info );
150 }
151 
153 {
154  mFeedback->pushDebugInfo( info );
155 }
156 
158 {
159  mFeedback->pushConsoleInfo( info );
160 }
161 
163 {
164  return mFeedback->htmlLog();
165 }
166 
168 {
169  return mFeedback->textLog();
170 }
171 
172 void QgsProcessingMultiStepFeedback::updateOverallProgress( double progress )
173 {
174  double baseProgress = 100.0 * static_cast< double >( mCurrentStep ) / mChildSteps;
175  double currentAlgorithmProgress = progress / mChildSteps;
176  mFeedback->setProgress( baseProgress + currentAlgorithmProgress );
177 }
178 
QgsFeedback::setProgress
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition: qgsfeedback.h:62
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:276
QgsProcessingFeedback::pushCommandInfo
virtual void pushCommandInfo(const QString &info)
Pushes an informational message containing a command from the algorithm.
Definition: qgsprocessingfeedback.cpp:57
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:38
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:48
QgsProcessingFeedback::reportError
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Definition: qgsprocessingfeedback.cpp:39
QgsFeedback::progress
double progress() const
Returns the current progress reported by the feedback object.
Definition: qgsfeedback.h:79
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:137
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:112
QgsFeedback::cancel
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
Definition: qgsfeedback.h:84
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:157
QgsProcessingFeedback::pushConsoleInfo
virtual void pushConsoleInfo(const QString &info)
Pushes a console feedback message from the algorithm.
Definition: qgsprocessingfeedback.cpp:75
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:162
QgsProcessingProvider::name
virtual QString name() const =0
Returns the provider name, which is used to describe the provider within the GUI.
Qgis::Info
@ Info
Definition: qgis.h:90
QgsProcessingMultiStepFeedback::QgsProcessingMultiStepFeedback
QgsProcessingMultiStepFeedback(int steps, QgsProcessingFeedback *feedback)
Constructor for QgsProcessingMultiStepFeedback, for a process with the specified number of steps.
Definition: qgsprocessingfeedback.cpp:118
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:84
qgsprocessingfeedback.h
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:152
QgsProcessingFeedback::pushDebugInfo
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
Definition: qgsprocessingfeedback.cpp:66
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
Qgis::Critical
@ Critical
Definition: qgis.h:92
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:107
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:167
QgsProcessingMultiStepFeedback::pushCommandInfo
void pushCommandInfo(const QString &info) override
Pushes an informational message containing a command from the algorithm.
Definition: qgsprocessingfeedback.cpp:147
QgsProcessingMultiStepFeedback::pushInfo
void pushInfo(const QString &info) override
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:142
QgsProcessingMultiStepFeedback::setCurrentStep
void setCurrentStep(int step)
Sets the step which is being executed.
Definition: qgsprocessingfeedback.cpp:126
QgsProcessingMultiStepFeedback::setProgressText
void setProgressText(const QString &text) override
Sets a progress report text string.
Definition: qgsprocessingfeedback.cpp:132
qgsprocessingprovider.h
qgsgeos.h
Qgis::devVersion
static QString devVersion()
The development version.
Definition: qgis.cpp:293