Skip to content

Commit

Permalink
Use nicely formatted results in processing logs
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 9, 2024
1 parent 3e10cf9 commit 3c17921
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ report the output from executing an external command or subprocess.
Pushes a summary of the QGIS (and underlying library) version information to the log.

.. versionadded:: 3.4.7
%End

void pushFormattedResults( const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap &results );
%Docstring
Pushes a summary of the execution ``results`` to the log

.. versionadded:: 3.36
%End

virtual QString htmlLog() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ report the output from executing an external command or subprocess.
Pushes a summary of the QGIS (and underlying library) version information to the log.

.. versionadded:: 3.4.7
%End

void pushFormattedResults( const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap &results );
%Docstring
Pushes a summary of the execution ``results`` to the log

.. versionadded:: 3.36
%End

virtual QString htmlLog() const;
Expand Down
5 changes: 1 addition & 4 deletions python/plugins/processing/gui/AlgorithmDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'

from pprint import pformat
import datetime
import time

Expand Down Expand Up @@ -283,9 +282,7 @@ def on_complete(ok, results):
if ok:
self.feedback.pushInfo(
self.tr(elapsed_time(start_time, 'Execution completed in')))
self.feedback.pushInfo(self.tr('Results:'))
r = {k: v for k, v in results.items() if k not in ('CHILD_RESULTS', 'CHILD_INPUTS')}
self.feedback.pushCommandInfo(pformat(r))
self.feedback.pushFormattedResults(self.algorithm(), self.context, results)
else:
self.feedback.reportError(
self.tr(elapsed_time(start_time, 'Execution failed after')))
Expand Down
28 changes: 28 additions & 0 deletions src/core/processing/qgsprocessingfeedback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ void QgsProcessingFeedback::pushVersionInfo( const QgsProcessingProvider *provid
}
}

void QgsProcessingFeedback::pushFormattedResults( const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap &results )
{
if ( results.empty() )
return;

pushInfo( tr( "Results:" ) );

const QList< const QgsProcessingOutputDefinition * > outputs = algorithm->outputDefinitions();
for ( const QgsProcessingOutputDefinition *output : outputs )
{
const QString outputName = output->name();
if ( outputName == QLatin1String( "CHILD_RESULTS" ) || outputName == QLatin1String( "CHILD_INPUTS" ) )
continue;

if ( !results.contains( outputName ) )
continue;

bool ok = false;
const QString textValue = output->valueAsString( results.value( output->name() ), context, ok );
const QString formattedValue = output->valueAsFormattedString( results.value( output->name() ), context, ok );
if ( ok )
{
pushFormattedMessage( QStringLiteral( "<code>&nbsp;&nbsp;%1: %2</code>" ).arg( output->name(), formattedValue ),
QStringLiteral( " %1: %2" ).arg( output->name(), textValue ) );
}
}
}

QString QgsProcessingFeedback::htmlLog() const
{
return mHtmlLog;
Expand Down
9 changes: 9 additions & 0 deletions src/core/processing/qgsprocessingfeedback.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "qgsmessagelog.h"

class QgsProcessingProvider;
class QgsProcessingAlgorithm;
class QgsProcessingContext;

/**
* \class QgsProcessingFeedback
Expand Down Expand Up @@ -141,6 +143,13 @@ class CORE_EXPORT QgsProcessingFeedback : public QgsFeedback
*/
void pushVersionInfo( const QgsProcessingProvider *provider = nullptr );

/**
* Pushes a summary of the execution \a results to the log
*
* \since QGIS 3.36
*/
void pushFormattedResults( const QgsProcessingAlgorithm *algorithm, QgsProcessingContext &context, const QVariantMap &results );

/**
* Returns the HTML formatted contents of the log, which contains all messages pushed to the feedback object.
*
Expand Down

0 comments on commit 3c17921

Please sign in to comment.